pmpro_ipnSaveOrder

Save a new order from IPN info.

Description

pmpro_ipnSaveOrder( $txn_id, $last_order ); 

$last_order passed in is the previous order for this subscription.

Parameters (2)

0. $txn_id
The txn id.
1. $last_order
The last order.

Usage

  1. if ( !function_exists( 'pmpro_ipnSaveOrder' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'paid-memberships-pro/services/ipnhandler.php'; 
  3.  
  4. // The txn id. 
  5. $txn_id = null; 
  6.  
  7. // The last order. 
  8. $last_order = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = pmpro_ipnSaveOrder($txn_id, $last_order); 
  12.  

Defined (1)

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

/services/ipnhandler.php  
  1. function pmpro_ipnSaveOrder( $txn_id, $last_order ) { 
  2. global $wpdb; 
  3.  
  4. //check that txn_id has not been previously processed 
  5. $old_txn = $wpdb->get_var( "SELECT payment_transaction_id FROM $wpdb->pmpro_membership_orders WHERE payment_transaction_id = '" . $txn_id . "' LIMIT 1" ); 
  6.  
  7. if ( empty( $old_txn ) ) { 
  8. //save order 
  9. $morder = new MemberOrder(); 
  10. $morder->user_id = $last_order->user_id; 
  11. $morder->membership_id = $last_order->membership_id; 
  12. $morder->payment_transaction_id = $txn_id; 
  13. $morder->subscription_transaction_id = $last_order->subscription_transaction_id; 
  14. $morder->gateway = $last_order->gateway; 
  15. $morder->gateway_environment = $last_order->gateway_environment; 
  16.  
  17. // Payment Status 
  18. $morder->status = 'success'; // We have confirmed that and thats the reason we are here. 
  19. // Payment Type. 
  20. $morder->payment_type = $last_order->payment_type; 
  21.  
  22. //set amount based on which PayPal type 
  23. if ( false !== stripos( $last_order->gateway, "paypal" ) ) { 
  24.  
  25. if ( isset( $_POST['amount'] ) && ! empty( $_POST['amount'] ) ) { 
  26. $morder->InitialPayment = $_POST['amount']; //not the initial payment, but the class is expecting that 
  27. $morder->PaymentAmount = $_POST['amount']; 
  28. } elseif ( isset( $_POST['mc_gross'] ) && ! empty( $_POST['mc_gross'] ) ) { 
  29. $morder->InitialPayment = $_POST['mc_gross']; //not the initial payment, but the class is expecting that 
  30. $morder->PaymentAmount = $_POST['mc_gross']; 
  31. } elseif ( isset( $_POST['payment_gross'] ) && ! empty( $_POST['payment_gross' ] ) ) { 
  32. $morder->InitialPayment = $_POST['payment_gross']; //not the initial payment, but the class is expecting that 
  33. $morder->PaymentAmount = $_POST['payment_gross']; 
  34.  
  35. $morder->FirstName = $_POST['first_name']; 
  36. $morder->LastName = $_POST['last_name']; 
  37. $morder->Email = $_POST['payer_email']; 
  38.  
  39. //getaddressinfo if appropriate 
  40. if ( $last_order->gateway == "paypal" ) //website payments pro 
  41. $morder->Address1 = get_user_meta( $last_order->user_id, "pmpro_baddress1", true ); 
  42. $morder->City = get_user_meta( $last_order->user_id, "pmpro_bcity", true ); 
  43. $morder->State = get_user_meta( $last_order->user_id, "pmpro_bstate", true ); 
  44. $morder->CountryCode = "US"; 
  45. $morder->Zip = get_user_meta( $last_order->user_id, "pmpro_bzip", true ); 
  46. $morder->PhoneNumber = get_user_meta( $last_order->user_id, "pmpro_bphone", true ); 
  47.  
  48. $morder->billing->name = $_POST['first_name'] . " " . $_POST['last_name']; 
  49. $morder->billing->street = get_user_meta( $last_order->user_id, "pmpro_baddress1", true ); 
  50. $morder->billing->city = get_user_meta( $last_order->user_id, "pmpro_bcity", true ); 
  51. $morder->billing->state = get_user_meta( $last_order->user_id, "pmpro_bstate", true ); 
  52. $morder->billing->zip = get_user_meta( $last_order->user_id, "pmpro_bzip", true ); 
  53. $morder->billing->country = get_user_meta( $last_order->user_id, "pmpro_bcountry", true ); 
  54. $morder->billing->phone = get_user_meta( $last_order->user_id, "pmpro_bphone", true ); 
  55.  
  56. //get CC info that is on file 
  57. $morder->cardtype = get_user_meta( $last_order->user_id, "pmpro_CardType", true ); 
  58. $morder->accountnumber = hideCardNumber( get_user_meta( $last_order->user_id, "pmpro_AccountNumber", true ), false ); 
  59. $morder->expirationmonth = get_user_meta( $last_order->user_id, "pmpro_ExpirationMonth", true ); 
  60. $morder->expirationyear = get_user_meta( $last_order->user_id, "pmpro_ExpirationYear", true ); 
  61. $morder->ExpirationDate = $morder->expirationmonth . $morder->expirationyear; 
  62. $morder->ExpirationDate_YdashM = $morder->expirationyear . "-" . $morder->expirationmonth; 
  63.  
  64. //figure out timestamp or default to none (today) 
  65. if ( ! empty( $_POST['payment_date'] ) ) { 
  66. $morder->timestamp = strtotime( $_POST['payment_date'] ); 
  67.  
  68. // Save theeventID for the last processed user/IPN (in case we want to be able to replay IPN requests) 
  69. $ipn_id = isset($_POST['ipn_track_id']) ? sanitize_text_field( $_POST['ipn_track_id'] ) : null; 
  70.  
  71. // Allow extraction of the IPN Track ID from theordernotes (if needed) 
  72. $morder->notes = "{$morder->notes} [IPN_ID]{$ipn_id}[/IPN_ID]"; 
  73.  
  74. /** 
  75. * Post processing for a specificsubscriptionrelated IPNeventID 
  76. * @param string $ipn_id - The ipn_track_id from the PayPal IPN request 
  77. * @param MemberOrder $morder - The completed Member Order object for the IPN request 
  78. */ 
  79. do_action('pmpro_subscription_ipn_event_processed', $ipn_id, $morder ); 
  80.  
  81. if ( ! is_null( $ipn_id ) ) { 
  82. if ( false === update_user_meta( $morder->user_id, "pmpro_last_{$morder->gateway}_ipn_id", $ipn_id )) { 
  83. ipnlog( "Unable to save the IPNeventID ({$ipn_id}) to usermeta for {$morder->user_id} " ); 
  84.  
  85. //save 
  86. $morder->saveOrder(); 
  87. $morder->getMemberOrderByID( $morder->id ); 
  88.  
  89. //email the user their invoice 
  90. $pmproemail = new PMProEmail(); 
  91. $pmproemail->sendInvoiceEmail( get_userdata( $last_order->user_id ), $morder ); 
  92.  
  93. //hook forsuccessfulsubscription payments 
  94. do_action( "pmpro_subscription_payment_completed", $morder ); 
  95.  
  96. ipnlog( "Neworder(" . $morder->code . ") created." ); 
  97.  
  98. return true; 
  99. } else { 
  100. ipnlog( "Duplicate Transaction ID: " . $txn_id ); 
  101.  
  102. return false;