Home / Admin / WP Simple Pay: Custom Webhook Handling Example
Duplicate Snippet

Embed Snippet on Your Site

WP Simple Pay: Custom Webhook Handling Example

An example of handling the `coupon.updated` webhook event. For a full list of available events see: https://stripe.com/docs/api/events/list

Code Preview
php
<?php
/**
 * @link https://library.wpcode.com/snippet/qorp835k/
 * 
 * Wait until WP Simple Pay is fully loaded.
 */
add_action( 'init', function() {
	require_once( SIMPLE_PAY_INC . 'pro/webhooks/class-webhook-base.php' );
	require_once( SIMPLE_PAY_INC . 'pro/webhooks/class-webhook-interface.php' );
	/**
	 * Adds handling for `coupon.updated` webhook.
	 *
	 * @param array $webhooks Registered webhooks.
	 * @return array
	 */
	function simpay_custom_webhooks_get_event_whitelist( $webhooks ) {
		$webhooks['coupon.updated'] = '\Custom_Coupon_Updated_Webhook';
		return $webhooks;
	}
	add_filter( 'simpay_webhooks_get_event_whitelist', 'simpay_custom_webhooks_get_event_whitelist' );
	/**
	 * Callback for `coupon.updated` webook.
	 */
	class Custom_Coupon_Updated_Webhook extends SimplePay\Pro\Webhooks\Webhook_Base implements SimplePay\Pro\Webhooks\Webhook_Interface {
		/**
		 * Handle the Webhook's data.
		 */
		public function handle() {
			$object = $this->event->data->object;
			// Deal with the webhook main object data.
			// Find the available object data by viewing Events in
			//
			// https://dashboard.stripe.com/test/events
			//   or
			// https://dashboard.stripe.com/events
			// $charge_id = $object->id;
			// $customer_id = $object->customer
			// $amount = $object->amount
			// $metadata = $object->metadata
			// etc...
		}
	}
} );

Comments

Add a Comment