Create a webhook programmatically in wordpress/woocommerce - php

i'm making a Plugin to make a integration of one ERP to Woocommerce and i'm thinking about using the web hooks for that integration, the problem create the web hooks when the plugin is activated for the first time, and not oblige people to create the woocommerce webhooks manually and configure them. i googled and tried several things but looks like no one works, also tried make a sql query, but don't worked.
i tried this:
function createWebhook($userID,$topic,$secret,$deliveryURL,$status)
{
$webhook = new WC_Webhook();
$webhook->set_user_id($userID); // User ID used while generating the webhook payload.
$webhook->set_topic( $topic ); // Event used to trigger a webhook.
$webhook->set_secret( $secret ); // Secret to validate webhook when received.
$webhook->set_delivery_url( $deliveryURL ); // URL where webhook should be sent.
$webhook->set_status( $status ); // Webhook status.
$save = $webhook->save();
return $save;
}
from this thread
without sucess, getting a lot of errors.
Anyway, thanks for the help. and i hope you'r ale in safety and with good health!

You are missing the webhook name
Add $webhook->set_name('Webhook Name');

Related

Stripe Webhook Testing Issue - Test payments don't call the webhook

I'm trying to integrate Stripe to accept payments on a website. I've come to the point where pretty much everything works from the client's perspective - test payments go through, everything is integrated and I can do stuff on payment intent succeeded (on the client). However, I can only detect that on the front-end.
Now, I want to show customer some sensitive data when payment is completed. I can't do it on front end - because then client does not even have to pay for that. I need to use webhooks.
So, I've created a sample webhook like this:
require_once 'stripe-php-7.97.0/init.php';
$endpoint_secret = 'endpoint_secret_code';
$payload = #file_get_contents('php://input');
$sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];
$event = null;
try {
$event = \Stripe\Webhook::constructEvent(
$payload, $sig_header, $endpoint_secret
);
} catch(\UnexpectedValueException $e) {
// Invalid payload
http_response_code(400);
exit();
} catch(\Stripe\Exception\SignatureVerificationException $e) {
// Invalid signature
http_response_code(400);
exit();
}
ob_flush();
ob_start();
var_dump($event);
file_put_contents("dump.txt", ob_get_flush());
// Handle the event
switch ($event->type) {
case 'payment_intent.succeeded':
$paymentIntent = $event->data->object;
var_dump($event->type);
// ... handle other event types
default:
echo 'Received unknown event type ' . $event->type;
}
http_response_code(200);
I've also created a webhook in the Stripe dashboard, like this:
However, based on the payment amount, I need to do something on this web hook - create a session, store data into the database and so on and so on. I've made it possible with Stripe CLI to test this webhook. However, how to test it using the test payments on the actual page? I have no idea how to do that - it does not work at the moment and without it, I can't be sure that it will work properly once everything goes live until I properly test it like this.
Does anyone have any tips, tutorials, or any kind of help to solve this? I'm pretty much stuck on it so any help would be greatly appreciated.

I need to add a custom function to post data to woocommerce's checkout process

I have been asked by the owner of a woocommerce/wordpress site to add a function into the checkout process that's kicked off when a user clicks "place order", but I'm not entirely sure how to go about getting this function to go off once the order is placed. The reason for their request is that this particular customer wants their supplier to be automatically notified of any orders, as well as their staff.
What the function does (or is supposed to do) is take the information from the checkout form, and pass it to a SOAP API used by the supplier's system.
Here's the code:
<?php
$request = new HttpRequest();
$request->setUrl($PRIVATE);
$request->setMethod(HTTP_METH_POST);
$request->setHeaders(array(
'Postman-Token' => $private_key,
'cache-control' => 'no-cache',
'SOAPAction' => $private_url,
'Content-Type' => 'text/xml'
));
$request->setBody('<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
//This part doesn't actually matter - is specific to the supplier's system and I've removed it for privacy.
</soapenv:Body>
</soapenv:Envelope>');
try {
$response = $request->send();
echo $response->getBody();
} catch (HttpException $ex) {
echo $ex;
}
?>
The major issue here is that there is no staging site (and I don't have the ability to create one), so I need to implement this on a live site, and I'm not confident that this will not break the checkout process, or the site at large if I try to use this. Unfortunately, I haven't found a lot of material detailing how others have handled similar implementations in my searching - most of what I see is "use a plug-in that won't solve your issue", or something to that effect.
That said, I really need to understand what's going on so that I can get this project back into the client's hands. Thank you in advance for your time and assistance!
I've done similiar implementations in the past. I add it to the woocommerce_thankyou hook.
add_action('woocommerce_thankyou', 'send_order');
function send_order($order_id){
$order = wc_get_order( $order_id );
/*** Your code ***/
}

Pay 3rd Party With PayPal API

I'm developing an application which pays out payments to a 3rd party. The source code for what I'm using is found here:
http://paypal.github.io/PayPal-PHP-SDK/sample/doc/payments/CreateThirdPartyPayment.html
I've added the following as well for my ClientID and Client Secret:
$apiContext = new \PayPal\Rest\ApiContext(
new \PayPal\Auth\OAuthTokenCredential(
'xxxxxxxxxxxxxxxxxxxxxxxxx', // ClientID
'xxxxxxxxxxxxxxxxxxxxxxxxx' // ClientSecret
)
);
When using my sandbox credentials, the code succeeds as it should. When swapping to the live credentials, however, I get an invalid client, authentication failed error. I've ensure the live ClientID and Client Secret are exactly what I have on my developer portal.
I've been looking all over the developer portal looking for a possible setting I'm missing, or something not configured correctly but have come to a loss. Any advice?
Update: I also don't see any issues/errors on the developer portal when logged into developer.paypal.com
Update 2:
I found this just now:
public function setConfig(array $config)
{
PayPalConfigManager::getInstance()->addConfigs($config);
}
Do I need to set the config by $apiContext->setConfig('LIVE') (or something to this effect?)
With the assistance from #JashParakh I was able to solve the issue. I added the following:
$config['mode'] = 'Live';
$apiContext->setConfig($config);
The payment went through in live and is should have.

Pushwoosh: Set Badge with PHP API

I'm using a paid Pushwoosh Account in order to send programmatically push-notifications to my users.
I've implemented the Pushwoosh PHP SDK and everything works great, but I'm not able to set a Notification-Badge on iOS devices as seen on the following image:
My code so far is:
$devicesArr[] = ...
$pushwoosh = Pushwoosh::create()
->setApplication('79XXX-9CXXX')
->setAuth('Wkf...2C8');
//Create the Message
$request = CreateMessageRequest::create()
->addNotification(Notification::create()
->setContent('A new workout is available!')
->setDevices($devicesArr));
//Call the REST Web Service
$response = $pushwoosh->createMessage($request);
I tried to use the ->setBadge(5) method according to this page but this also does not work:
$request = SetBadgeRequest::create()
->setBadge(1)
->setHwid('18D...1AF');
Do you know how I can achieve my goal?
I can't reproduce this on Cordova Sample (https://github.com/Pushwoosh/pushwoosh-phonegap-cordova-sample/tree/master/Phonegap-iOS-Android-WP), can you share a simple sample?
Are you sure the app is closed when you receive push with badge?
Per the docs, this is not the method you use to set the badge number on the device.
Important
This method IS NOT used to update the badge value on the device.
Instead please use /createMessage request with the "ios_badges" parameter.
http://docs.pushwoosh.com/docs/setbadge

Braintree webhooks, GET & POST configuration

I am trying to use webhooks to send emails about whether subscriptions were successfully charged or not. I use bt_challenge code to successfully register the webhook on Braintree's dashboard:
if(isset($_GET["bt_challenge"])) {
echo(Braintree_WebhookNotification::verify($_GET["bt_challenge"]));
}
However, when I use the subscription information from $webhook notification:
if(isset($_POST["bt_signature"]) && isset($_POST["bt_payload"])) {
$webhookNotification = Braintree_WebhookNotification::parse(
$_POST["bt_signature"], $_POST["bt_payload"]
);
}
I don't get an email sent. Furthermore, when I try to re-register the webhook from the Braintree dashboard, it won't recognize it with this second snippet of code in the file. Are both of these snippets meant to be in the same file? This part is unclear to me.
Following these two snippets, I have my phpmailer code, which is properly configured and sends a email when the script is called not via webhook. The phpmailer code is not contained within the "if" statement of either of these first two code blocks.

Categories