Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save JarrydLong/704f8ffd5edd6fe55861a66e7bd29623 to your computer and use it in GitHub Desktop.

Select an option

Save JarrydLong/704f8ffd5edd6fe55861a66e7bd29623 to your computer and use it in GitHub Desktop.
<?php //do not copy
/* This recipe will forward all requests from a temporary webhook URL
* to the current PMPro Stripe Webhook URL
*
* 1. Navigate to Memberships > Settings > Payment Gateway and Edit Settings on the Stripe Gateway
* 2. Disable your webhook - do not create a new one
* 3. Login to your Stripe account, navigate to Webhooks
* 4. Create a new webhook with the URL of https://MYSITE.com/?pmpro-api=stripe_webhook
* 5. For testing purposes, enable all Stripe events
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_pmpro_forward_stripe_webhook() {
if ( empty( $_REQUEST['pmpro-api'] ) ) {
return;
}
$raw_body = file_get_contents( 'php://input' );
// Get Stripe signature header
$stripe_signature = '';
if ( isset( $_SERVER['HTTP_STRIPE_SIGNATURE'] ) ) {
$stripe_signature = $_SERVER['HTTP_STRIPE_SIGNATURE'];
}
$args = array(
'body' => $raw_body,
'headers' => array(
'Content-Type' => 'application/json',
'Stripe-Signature' => $stripe_signature,
),
'timeout' => 60,
'httpversion' => '1.1',
);
$response = wp_safe_remote_post(
'https://YOUR_SITE_URL/wp-admin/admin-ajax.php?action=stripe_webhook',
$args
);
if ( is_wp_error( $response ) ) {
status_header( 500 );
exit;
}
// Return same response Stripe expects
echo wp_remote_retrieve_body( $response );
exit;
}
add_action( 'init', 'my_pmpro_forward_stripe_webhook', 99 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment