Skip to content

Instantly share code, notes, and snippets.

@girafffee
Created April 14, 2022 07:52
Show Gist options
  • Save girafffee/040ff0ec2f148cd429d4a91cc7006c50 to your computer and use it in GitHub Desktop.
Save girafffee/040ff0ec2f148cd429d4a91cc7006c50 to your computer and use it in GitHub Desktop.
Example of adding a PayPal listener to subscribe to the cancel event
<?php
function jfb_add_paypal_webhooks( array $webhooks ) {
require_once 'jfb-paypal-cancel-subscription-webhook.php';
$webhooks[] = new JFB_Paypal_Cancel_Subscription_Webhook();
return $webhooks;
}
add_filter(
'jet-form-builder/gateways/paypal/events',
'jfb_add_paypal_webhooks'
);
<?php
class JFB_Paypal_Cancel_Subscription_Webhook extends \Jet_FB_Paypal\EventsHandlers\BillingSubscriptionCancelled {
public function on_catch_event( $webhook_event ) {
// Subscription status changes here
parent::on_catch_event( $webhook_event );
// here you can write you own code
// $webhook_event (array) - this is the decoded json object in the webhook-details-sample.json file
/**
* You can throw a \Jet_Form_Builder\Exceptions\Gateway_Exception if necessary.
* This is useful for debugging
* @link https://developer.paypal.com/developer/dashboard/webhooks/sandbox
* or for live app:
* @link https://developer.paypal.com/developer/dashboard/webhooks/live
*/
}
}
{
"id": "WH-6K860688AJ126540G-XXXXXXXXXXXXX",
"create_time": "2022-04-08T11:55:41.871Z",
"resource_type": "subscription",
"event_type": "BILLING.SUBSCRIPTION.CANCELLED",
"summary": "Subscription cancelled",
"resource": {
"status_change_note": "Not satisfied with the service",
"quantity": "1",
"subscriber": {
"email_address": "[email protected]",
"payer_id": "VFG6LG948GRRL",
"name": {
"given_name": "Олександр",
"surname": "Іваненко"
},
"shipping_address": {
"name": {
"full_name": "Oleg Mongol"
},
"address": {
"address_line_1": "Pushkinskaya street",
"address_line_2": "2 dodjedf",
"admin_area_2": "Kiev",
"admin_area_1": "IVANO-FRANKIVSKA OBL",
"postal_code": "54029",
"country_code": "UA"
}
}
},
"create_time": "2022-04-08T06:44:05Z",
"plan_overridden": false,
"shipping_amount": {
"currency_code": "USD",
"value": "0.0"
},
"start_time": "2022-04-08T06:43:30Z",
"update_time": "2022-04-08T11:55:24Z",
"billing_info": {
"outstanding_balance": {
"currency_code": "USD",
"value": "0.0"
},
"cycle_executions": [
{
"tenure_type": "REGULAR",
"sequence": 1,
"cycles_completed": 1,
"cycles_remaining": 2,
"current_pricing_scheme_version": 1,
"total_cycles": 3
}
],
"last_payment": {
"amount": {
"currency_code": "USD",
"value": "100.0"
},
"time": "2022-04-08T06:44:06Z"
},
"failed_payments_count": 0
},
"links": [
{
"href": "https://api.sandbox.paypal.com/v1/billing/subscriptions/I-AKD1KVE7KSNA",
"rel": "self",
"method": "GET",
"encType": "application/json"
}
],
"id": "I-AKDDKVE74SNA",
"plan_id": "P-2RW14005CO679391XWFPMEWI",
"status": "CANCELLED",
"status_update_time": "2022-04-08T11:55:24Z"
},
"status": "SUCCESS",
"transmissions": [
{
"webhook_url": "https://XXXX.net/wp-json/jet-form-builder/v1/paypal/event-subscription",
"http_status": 200,
"reason_phrase": "HTTP/1.1 200 Connection established",
"response_headers": {
"Keep-Alive": "timeout=5, max=100",
"allow": "POST",
"date": "Fri, 08 Apr 2022 11:56:16 GMT",
"content-length": "9",
"server": "LiteSpeed",
"access-control-allow-headers": "Authorization, X-WP-Nonce, Content-Disposition, Content-MD5, Content-Type",
"Connection": "Keep-Alive",
"link": "<https://XXXX.net/wp-json/>; rel=\"https://api.w.org/\"",
"x-litespeed-tag": "d45_HTTP.200",
"x-litespeed-cache-control": "no-cache",
"access-control-expose-headers": "X-WP-Total, X-WP-TotalPages, Link",
"x-content-type-options": "nosniff",
"x-robots-tag": "noindex",
"x-powered-by": "PHP/7.4.28",
"content-type": "application/json; charset=UTF-8",
"alt-svc": "h3=\":443\"; ma=2592000, h3-29=\":443\"; ma=2592000, h3-Q050=\":443\"; ma=2592000, h3-Q046=\":443\"; ma=2592000, h3-Q043=\":443\"; ma=2592000, quic=\":443\"; ma=2592000; v=\"43,46\""
},
"transmission_id": "ddb88cf0-b672-11ec-aece-0d658cb51104",
"status": "SUCCESS",
"timestamp": "2022-04-08T11:56:03Z"
}
],
"links": [
{
"href": "https://api.sandbox.paypal.com/v1/notifications/webhooks-events/WH-6K860688AJ126540G-XXXXXXXXXXXXX",
"rel": "self",
"method": "GET",
"encType": "application/json"
},
{
"href": "https://api.sandbox.paypal.com/v1/notifications/webhooks-events/WH-6K860688AJ126540G-XXXXXXXXXXXXX/resend",
"rel": "resend",
"method": "POST",
"encType": "application/json"
}
],
"event_version": "1.0",
"resource_version": "2.0"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment