getOldOrderFromInvoiceEvent

The Paid Memberships Pro getOldOrderFromInvoiceEvent function.

Description

getOldOrderFromInvoiceEvent( $pmpro_stripe_event ); 

Parameters (1)

0. $pmpro_stripe_event
The pmpro stripe event.

Usage

  1. if ( !function_exists( 'getOldOrderFromInvoiceEvent' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'paid-memberships-pro/services/stripe-webhook.php'; 
  3.  
  4. // The pmpro stripe event. 
  5. $pmpro_stripe_event = null; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = getOldOrderFromInvoiceEvent($pmpro_stripe_event); 
  9.  

Defined (1)

The function is defined in the following location(s).

/services/stripe-webhook.php  
  1. function getOldOrderFromInvoiceEvent($pmpro_stripe_event) 
  2. //pause here to give PMPro a chance to finish checkout 
  3.  
  4. global $wpdb; 
  5.  
  6. $customer_id = $pmpro_stripe_event->data->object->customer; 
  7. $subscription_id = $pmpro_stripe_event->data->object->id; 
  8.  
  9. // nocustomerpassed? we can't cross reference 
  10. if(empty($customer_id)) 
  11. return false; 
  12.  
  13. // okay, add an invoice. first lookup the user_id from thesubscriptionid passed 
  14. $old_order_id = $wpdb->get_var("SELECT id FROM $wpdb->pmpro_membership_orders WHERE (subscription_transaction_id = '" . $customer_id . "' OR subscription_transaction_id = '" . esc_sql($subscription_id) . "') ANDgateway= stripeORDERBY timestamp DESC LIMIT 1"); 
  15.  
  16. // since v1.8, PMPro may store the Stripe subscription_id (sub_XXXX) instead of the Stripe customer_id (cus_XXXX) 
  17. // so that last query may turn up an empty result 
  18. if(empty($old_order_id)) 
  19. // let's look up the Stripe subscription_id instead 
  20. // unfortunately, the subscription_id is not included in the JSON data from the Stripe event 
  21. // so, we must look up the subscription_id from the invoice_id, which IS included in the JSON data from the Stripe event 
  22. $invoice_id = $pmpro_stripe_event->data->object->invoice; 
  23.  
  24. try { 
  25.  
  26. $invoice = Stripe_Invoice::retrieve( $invoice_id ); 
  27.  
  28. } catch (Exception $e) { 
  29. error_log("Unable to fetch Stripe Invoice object: " . $e->getMessage()); 
  30. $invoice = null; 
  31.  
  32. if (isset( $invoice->subscription )) { 
  33. $subscription_id = $invoice->subscription; 
  34. $old_order_id = $wpdb->get_var( "SELECT id FROM $wpdb->pmpro_membership_orders WHERE (subscription_transaction_id = '" . $subscription_id . "' OR subscription_transaction_id = '" . esc_sql($subscription_id) . "') ANDgateway= stripeORDERBY timestamp DESC LIMIT 1" ); 
  35.  
  36. if (!empty($old_order_id)) { 
  37.  
  38. $old_order = new MemberOrder( $old_order_id ); 
  39.  
  40. if(isset($old_order->id) && ! empty($old_order->id)) 
  41. return $old_order; 
  42.  
  43. return false;