Skip to content

Instantly share code, notes, and snippets.

@messica
Last active September 27, 2019 01:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save messica/29a04f686181e33a350d43f8b76536f1 to your computer and use it in GitHub Desktop.
Save messica/29a04f686181e33a350d43f8b76536f1 to your computer and use it in GitHub Desktop.
Redirect PMPro related webhooks to PMPro webhook handler.
<?php
/**
* Redirect PMPro related webhooks to PMPro webhook handler.
*/
function my_woocommerce_api_request_redirect_pmpro( $request ) {
global $wpdb;
// Only continue if PMPro is loaded.
if ( ! defined( 'PMPRO_DIR' ) ) {
return true;
}
// Load Stripe library if necessary.
if(!class_exists("Stripe\Stripe")) {
require_once( PMPRO_DIR . "/includes/lib/Stripe/init.php" );
}
try {
Stripe\Stripe::setApiKey( pmpro_getOption( "stripe_secretkey" ) );
} catch ( Exception $e ) {
pmpro_stripeWebhookExit();
}
// retrieve the request's body and parse it as JSON
if(empty($_REQUEST['event_id']))
{
$body = @file_get_contents('php://input');
$post_event = json_decode($body);
//get the id
if(!empty($post_event))
$event_id = sanitize_text_field($post_event->id);
}
else
{
$event_id = sanitize_text_field($_REQUEST['event_id']);
}
//get the event through the API now
if(!empty($event_id))
{
try
{
global $pmpro_stripe_event;
$pmpro_stripe_event = Stripe\Event::retrieve($event_id);
}
catch(Exception $e)
{
// pmpro_stripeWebhookExit();
$pmpro_stripe_event = $post_event; //for testing you may want to assume that the passed in event is legit
}
}
// Check for a PMPro related Customer
if ( ! empty( $pmpro_stripe_event->data->object->customer ) ) {
$sql = "SELECT umeta_id FROM {$wpdb->usermeta} WHERE meta_key = 'pmpro_stripe_customerid' AND meta_value = '{$pmpro_stripe_event->data->object->customer}'";
$pmpro_customer_id = $wpdb->get_var( $sql );
}
// If there is a related order in PMPro, load the PMPro webhook handler and exit.
if ( ! empty( $pmpro_customer_id ) ) {
require_once( PMPRO_DIR . '/services/stripe-webhook.php' );
exit();
}
}
add_action( 'woocommerce_api_request', 'my_woocommerce_api_request_redirect_pmpro', 1, 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment