Does NOT handle payment processing for the renewal. This should be called after receiving a renewal payment
public renew ( $recurring = false, $status = 'active', $expiration = '' ) |
/** * Process registration * * @since 2.1 */ public function process_signup() { global $rcp_options; $args = array('USER' => $this->username, 'PWD' => $this->password, 'SIGNATURE' => $this->signature, 'VERSION' => '121', 'METHOD' => 'CreateRecurringPaymentsProfile', 'AMT' => $this->amount, 'INITAMT' => 0, 'CURRENCYCODE' => strtoupper($this->currency), 'ITEMAMT' => round($this->amount + $this->signup_fee, 2), 'SHIPPINGAMT' => 0, 'TAXAMT' => 0, 'DESC' => $this->subscription_name, 'SOFTDESCRIPTOR' => get_bloginfo('name') . ': ' . $this->subscription_name, 'SOFTDESCRIPTORCITY' => get_bloginfo('admin_email'), 'CUSTOM' => $this->user_id, 'NOTIFYURL' => add_query_arg('listener', 'EIPN', home_url('index.php')), 'EMAIL' => $this->email, 'CREDITCARDTYPE' => '', 'ACCT' => sanitize_text_field($_POST['rcp_card_number']), 'EXPDATE' => sanitize_text_field($_POST['rcp_card_exp_month'] . $_POST['rcp_card_exp_year']), 'CVV2' => sanitize_text_field($_POST['rcp_card_cvc']), 'ZIP' => sanitize_text_field($_POST['rcp_card_zip']), 'BUTTONSOURCE' => 'EasyDigitalDownloads_SP', 'PROFILESTARTDATE' => date('Y-m-d\\Tg:i:s', strtotime('+' . $this->length . ' ' . $this->length_unit, time())), 'BILLINGPERIOD' => ucwords($this->length_unit), 'BILLINGFREQUENCY' => $this->length, 'FAILEDINITAMTACTION' => 'CancelOnFailure', 'TOTALBILLINGCYCLES' => $this->auto_renew ? 0 : 1); $request = wp_remote_post($this->api_endpoint, array('timeout' => 45, 'sslverify' => false, 'body' => $args)); if (is_wp_error($request)) { $error = '<p>' . __('An unidentified error occurred.', 'rcp') . '</p>'; $error .= '<p>' . $request->get_error_message() . '</p>'; wp_die($error, __('Error', 'rcp'), array('response' => '401')); } elseif (200 == $request['response']['code'] && 'OK' == $request['response']['message']) { parse_str($request['body'], $data); if ('failure' === strtolower($data['ACK'])) { $error = '<p>' . __('PayPal subscription creation failed.', 'rcp') . '</p>'; $error .= '<p>' . __('Error message:', 'rcp') . ' ' . $data['L_LONGMESSAGE0'] . '</p>'; $error .= '<p>' . __('Error code:', 'rcp') . ' ' . $data['L_ERRORCODE0'] . '</p>'; wp_die($error, __('Error', 'rcp'), array('response' => '401')); } else { // Successful signup if ('ActiveProfile' === $data['PROFILESTATUS']) { // Confirm a one-time payment $member = new RCP_Member($this->user_id); $member->renew($this->auto_renew); $member->set_payment_profile_id($data['PROFILEID']); } wp_redirect(esc_url_raw(rcp_get_return_url())); exit; exit; } } else { wp_die(__('Something has gone wrong, please try again', 'rcp'), __('Error', 'rcp'), array('back_link' => true, 'response' => '401')); } }
/** * Process registration * * @since 2.1 */ public function process_signup() { $member = new RCP_Member($this->user_id); $member->renew(false, ''); // setup the payment info in an array for storage $payment_data = array('subscription' => $this->subscription_name, 'payment_type' => 'manual', 'subscription_key' => $this->subscription_key, 'amount' => $this->amount + $this->signup_fee, 'user_id' => $this->user_id, 'transaction_id' => $this->generate_transaction_id()); $rcp_payments = new RCP_Payments(); $rcp_payments->insert($payment_data); wp_redirect($this->return_url); exit; }
/** * Process registration * * @since 2.1 */ public function process_signup() { $member = new RCP_Member($this->user_id); $old_level = get_user_meta($member->ID, '_rcp_old_subscription_id', true); if (!empty($old_level) && $old_level == $this->subscription_id) { $expiration = $member->calculate_expiration(); } else { delete_user_meta($member->ID, 'rcp_pending_expiration_date'); $expiration = $member->calculate_expiration(true); } $member->renew(false, 'pending', $expiration); // setup the payment info in an array for storage $payment_data = array('subscription' => $this->subscription_name, 'payment_type' => 'manual', 'subscription_key' => $this->subscription_key, 'amount' => $this->amount + $this->signup_fee, 'user_id' => $this->user_id, 'transaction_id' => $this->generate_transaction_id()); $rcp_payments = new RCP_Payments(); $rcp_payments->insert($payment_data); wp_redirect($this->return_url); exit; }
/** * Proccess webhooks * * @since 2.3 */ public function process_webhooks() { if (isset($_GET['listener']) && $_GET['listener'] == '2checkout') { global $wpdb; $hash = strtoupper(md5($_POST['sale_id'] . $this->seller_id . $_POST['invoice_id'] . $this->secret_word)); if (!hash_equals($hash, $_POST['md5_hash'])) { die('-1'); } if (empty($_POST['message_type'])) { die('-2'); } if (empty($_POST['vendor_id'])) { die('-3'); } $subscription_key = sanitize_text_field($_POST['vendor_order_id']); $member_id = $wpdb->get_var($wpdb->prepare("SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = 'rcp_subscription_key' AND meta_value = %s LIMIT 1", $subscription_key)); if (!$member_id) { die('-4'); } $member = new RCP_Member($member_id); if (!rcp_is_2checkout_subscriber($member->ID)) { return; } $payments = new RCP_Payments(); switch (strtoupper($_POST['message_type'])) { case 'ORDER_CREATED': break; case 'REFUND_ISSUED': $payment = $payments->get_payment_by('transaction_id', $_POST['invoice_id']); $payments->update($payment->id, array('status' => 'refunded')); if (!empty($_POST['recurring'])) { $member->cancel(); $member->add_note(__('Subscription cancelled via refund 2Checkout', 'rcp')); } break; case 'RECURRING_INSTALLMENT_SUCCESS': $payment_data = array('date' => date('Y-m-d H:i:s', strtotime($_POST['timestamp'], current_time('timestamp'))), 'subscription' => $member->get_subscription_name(), 'payment_type' => sanitize_text_field($_POST['payment_type']), 'subscription_key' => $subscription_key, 'amount' => sanitize_text_field($_POST['item_list_amount_1']), 'user_id' => $member->ID, 'transaction_id' => sanitize_text_field($_POST['invoice_id'])); $recurring = !empty($_POST['recurring']); $member->renew($recurring); $payments->insert($payment_data); $member->add_note(__('Subscription renewed in 2Checkout', 'rcp')); break; case 'RECURRING_INSTALLMENT_FAILED': break; case 'RECURRING_STOPPED': if (!$member->just_upgraded()) { $member->cancel(); $member->add_note(__('Subscription cancelled in 2Checkout', 'rcp')); } break; case 'RECURRING_COMPLETE': break; case 'RECURRING_RESTARTED': $member->set_status('active'); $member->add_note(__('Subscription restarted in 2Checkout', 'rcp')); break; case 'FRAUD_STATUS_CHANGED': switch ($_POST['fraud_status']) { case 'pass': break; case 'fail': $member->set_status('pending'); $member->add_note(__('Payment flagged as fraudulent in 2Checkout', 'rcp')); break; case 'wait': break; } break; } do_action('rcp_2co_' . strtolower($_POST['message_type']) . '_ins', $member); die('success'); } }
public function process_webhooks() { if (!isset($_GET['listener']) || strtolower($_GET['listener']) != 'stripe') { return; } // Ensure listener URL is not cached by W3TC if (!defined('DONOTCACHEPAGE')) { define('DONOTCACHEPAGE', true); } \Stripe\Stripe::setApiKey($this->secret_key); // retrieve the request's body and parse it as JSON $body = @file_get_contents('php://input'); $event_json_id = json_decode($body); $expiration = ''; // for extra security, retrieve from the Stripe API if (isset($event_json_id->id)) { $rcp_payments = new RCP_Payments(); $event_id = $event_json_id->id; try { $event = \Stripe\Event::retrieve($event_id); $payment_event = $event->data->object; if (empty($payment_event->customer)) { die('no customer attached'); } // retrieve the customer who made this payment (only for subscriptions) $user = rcp_get_member_id_from_profile_id($payment_event->customer); if (empty($user)) { // Grab the customer ID from the old meta keys global $wpdb; $user = $wpdb->get_var($wpdb->prepare("SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = '_rcp_stripe_user_id' AND meta_value = %s LIMIT 1", $payment_event->customer)); } if (empty($user)) { die('no user ID found'); } $member = new RCP_Member($user); // check to confirm this is a stripe subscriber if ($member) { if (!$member->get_subscription_id()) { die('no subscription ID for member'); } if ($event->type == 'charge.succeeded' || $event->type == 'invoice.payment_succeeded') { // setup payment data $payment_data = array('date' => date_i18n('Y-m-d g:i:s', $event->created), 'payment_type' => 'Credit Card', 'user_id' => $member->ID, 'amount' => '', 'transaction_id' => ''); if ($event->type == 'charge.succeeded') { // Successful one-time payment if (empty($payment_event->invoice)) { $payment_data['amount'] = $payment_event->amount / rcp_stripe_get_currency_multiplier(); $payment_data['transaction_id'] = $payment_event->id; // Successful subscription payment } else { $invoice = \Stripe\Invoice::retrieve($payment_event->invoice); $payment_data['amount'] = $invoice->amount_due / rcp_stripe_get_currency_multiplier(); $payment_data['transaction_id'] = $payment_event->id; } // Successful subscription paid made with account credit where no charge is created } elseif ($event->type == 'invoice.payment_succeeded' && empty($payment_event->charge)) { $payment_data['amount'] = $payment_event->amount_due / rcp_stripe_get_currency_multiplier(); $payment_data['transaction_id'] = $payment_event->id; $invoice = $payment_event; } if (!empty($payment_data['transaction_id']) && !$rcp_payments->payment_exists($payment_data['transaction_id'])) { if (!empty($invoice->subscription)) { $customer = \Stripe\Customer::retrieve($member->get_payment_profile_id()); $subscription = $customer->subscriptions->retrieve($invoice->subscription); if (!empty($subscription)) { $expiration = date('Y-m-d 23:59:59', $subscription->current_period_end); $member->set_recurring(); } $member->set_merchant_subscription_id($subscription->id); } $member->renew($member->is_recurring(), 'active', $expiration); // These must be retrieved after the status is set to active in order for upgrades to work properly $payment_data['subscription'] = $member->get_subscription_name(); $payment_data['subscription_key'] = $member->get_subscription_key(); // record this payment if it hasn't been recorded yet $rcp_payments->insert($payment_data); do_action('rcp_stripe_charge_succeeded', $user, $payment_data); die('rcp_stripe_charge_succeeded action fired successfully'); } else { die('duplicate payment found'); } } // failed payment if ($event->type == 'charge.failed') { do_action('rcp_stripe_charge_failed', $invoice); die('rcp_stripe_charge_failed action fired successfully'); } // Cancelled / failed subscription if ($event->type == 'customer.subscription.deleted') { if (!$member->just_upgraded()) { $member->set_status('cancelled'); die('member cancelled successfully'); } } do_action('rcp_stripe_' . $event->type, $payment_event); } } catch (Exception $e) { // something failed die('PHP exception: ' . $e->getMessage()); } die('1'); } die('no event ID found'); }
/** * Process PayPal IPN * * @since 2.1 */ public function process_webhooks() { if (!isset($_GET['listener']) || strtoupper($_GET['listener']) != 'IPN') { return; } global $rcp_options; nocache_headers(); if (!class_exists('IpnListener')) { // instantiate the IpnListener class include RCP_PLUGIN_DIR . 'includes/gateways/paypal/paypal-ipnlistener.php'; } $listener = new IpnListener(); $verified = false; if ($this->test_mode) { $listener->use_sandbox = true; } /* if( isset( $rcp_options['ssl'] ) ) { $listener->use_ssl = true; } else { $listener->use_ssl = false; } */ //To post using the fsockopen() function rather than cURL, use: if (isset($rcp_options['disable_curl'])) { $listener->use_curl = false; } try { $listener->requirePostMethod(); $verified = $listener->processIpn(); } catch (Exception $e) { status_header(402); //die( 'IPN exception: ' . $e->getMessage() ); } /* The processIpn() method returned true if the IPN was "VERIFIED" and false if it was "INVALID". */ if ($verified || isset($_POST['verification_override']) || ($this->test_mode || isset($rcp_options['disable_ipn_verify']))) { status_header(200); $user_id = 0; $posted = apply_filters('rcp_ipn_post', $_POST); // allow $_POST to be modified if (!empty($posted['custom']) && is_numeric($posted['custom'])) { $user_id = absint($posted['custom']); } else { if (!empty($posted['subscr_id'])) { $user_id = rcp_get_member_id_from_profile_id($posted['subscr_id']); } else { if (!empty($posted['payer_email'])) { $user = get_user_by('email', $posted['payer_email']); $user_id = $user ? $user->ID : false; } } } $member = new RCP_Member($user_id); if (!$member || !$member->get_subscription_id()) { die('no member found'); } if (!rcp_get_subscription_details($member->get_subscription_id())) { die('no subscription level found'); } $subscription_name = $posted['item_name']; $subscription_key = $posted['item_number']; $amount = number_format((double) $posted['mc_gross'], 2); $amount2 = number_format((double) $posted['mc_amount3'], 2); $payment_status = $posted['payment_status']; $currency_code = $posted['mc_currency']; $subscription_price = number_format((double) rcp_get_subscription_price($member->get_subscription_id()), 2); // setup the payment info in an array for storage $payment_data = array('date' => date('Y-m-d g:i:s', strtotime($posted['payment_date'], current_time('timestamp'))), 'subscription' => $posted['item_name'], 'payment_type' => $posted['txn_type'], 'subscription_key' => $subscription_key, 'amount' => $amount, 'user_id' => $user_id, 'transaction_id' => $posted['txn_id']); do_action('rcp_valid_ipn', $payment_data, $user_id, $posted); if ($posted['txn_type'] == 'web_accept' || $posted['txn_type'] == 'subscr_payment') { // only check for an existing payment if this is a payment IPD request if (rcp_check_for_existing_payment($posted['txn_type'], $posted['payment_date'], $subscription_key)) { $log_data = array('post_title' => __('Duplicate Payment', 'rcp'), 'post_content' => __('A duplicate payment was detected. The new payment was still recorded, so you may want to check into both payments.', 'rcp'), 'post_parent' => 0, 'log_type' => 'gateway_error'); $log_meta = array('user_subscription' => $posted['item_name'], 'user_id' => $user_id); $log_entry = WP_Logging::insert_log($log_data, $log_meta); die('duplicate IPN detected'); } if (strtolower($currency_code) != strtolower($rcp_options['currency'])) { // the currency code is invalid $log_data = array('post_title' => __('Invalid Currency Code', 'rcp'), 'post_content' => sprintf(__('The currency code in an IPN request did not match the site currency code. Payment data: %s', 'rcp'), json_encode($payment_data)), 'post_parent' => 0, 'log_type' => 'gateway_error'); $log_meta = array('user_subscription' => $posted['item_name'], 'user_id' => $user_id); $log_entry = WP_Logging::insert_log($log_data, $log_meta); die('invalid currency code'); } } if (isset($rcp_options['email_ipn_reports'])) { wp_mail(get_bloginfo('admin_email'), __('IPN report', 'rcp'), $listener->getTextReport()); } /* now process the kind of subscription/payment */ $rcp_payments = new RCP_Payments(); // Subscriptions switch ($posted['txn_type']) { case "subscr_signup": // when a new user signs up // store the recurring payment ID update_user_meta($user_id, 'rcp_paypal_subscriber', $posted['payer_id']); $member->set_payment_profile_id($posted['subscr_id']); do_action('rcp_ipn_subscr_signup', $user_id); die('successful subscr_signup'); break; case "subscr_payment": // when a user makes a recurring payment update_user_meta($user_id, 'rcp_paypal_subscriber', $posted['payer_id']); $member->set_payment_profile_id($posted['subscr_id']); $member->renew(true); // record this payment in the database $rcp_payments->insert($payment_data); do_action('rcp_ipn_subscr_payment', $user_id); die('successful subscr_payment'); break; case "subscr_cancel": // user is marked as cancelled but retains access until end of term $member->set_status('cancelled'); // set the use to no longer be recurring delete_user_meta($user_id, 'rcp_paypal_subscriber'); do_action('rcp_ipn_subscr_cancel', $user_id); die('successful subscr_cancel'); break; case "subscr_failed": do_action('rcp_ipn_subscr_failed'); die('successful subscr_failed'); break; case "subscr_eot": // user's subscription has reached the end of its term if ('cancelled' !== $member->get_status($user_id)) { $member->set_status('expired'); } do_action('rcp_ipn_subscr_eot', $user_id); die('successful subscr_eot'); break; case "web_accept": switch (strtolower($payment_status)) { case 'completed': // set this user to active $member->renew(); $rcp_payments->insert($payment_data); break; case 'denied': case 'expired': case 'failed': case 'voided': $member->set_status('cancelled'); break; } die('successful web_accept'); break; case "cart": case "express_checkout": default: break; } } else { if (isset($rcp_options['email_ipn_reports'])) { // an invalid IPN attempt was made. Send an email to the admin account to investigate wp_mail(get_bloginfo('admin_email'), __('Invalid IPN', 'rcp'), $listener->getTextReport()); } status_header(400); die('invalid IPN'); } }
/** * Process payment confirmation after returning from PayPal * * @since 2.1 */ public function process_confirmation() { if (isset($_POST['rcp_ppe_confirm_nonce']) && wp_verify_nonce($_POST['rcp_ppe_confirm_nonce'], 'rcp-ppe-confirm-nonce')) { $details = $this->get_checkout_details($_POST['token']); if (!empty($_GET['rcp-recurring'])) { // Successful payment, now create the recurring profile $args = array('USER' => $this->username, 'PWD' => $this->password, 'SIGNATURE' => $this->signature, 'VERSION' => '121', 'TOKEN' => $_POST['token'], 'METHOD' => 'CreateRecurringPaymentsProfile', 'PROFILESTARTDATE' => date('Y-m-d\\Tg:i:s', strtotime('+' . $details['subscription']['duration'] . ' ' . $details['subscription']['duration_unit'], time())), 'BILLINGPERIOD' => ucwords($details['subscription']['duration_unit']), 'BILLINGFREQUENCY' => $details['subscription']['duration'], 'AMT' => $details['AMT'], 'INITAMT' => round($details['AMT'] + $details['subscription']['fee'], 2), 'CURRENCYCODE' => $details['CURRENCYCODE'], 'FAILEDINITAMTACTION' => 'CancelOnFailure', 'L_BILLINGTYPE0' => 'RecurringPayments', 'DESC' => $details['subscription']['name'], 'BUTTONSOURCE' => 'EasyDigitalDownloads_SP'); $request = wp_remote_post($this->api_endpoint, array('timeout' => 45, 'sslverify' => false, 'body' => $args)); if (is_wp_error($request)) { $error = '<p>' . __('An unidentified error occurred.', 'rcp') . '</p>'; $error .= '<p>' . $request->get_error_message() . '</p>'; wp_die($error, __('Error', 'rcp'), array('response' => '401')); } elseif (200 == $request['response']['code'] && 'OK' == $request['response']['message']) { parse_str($request['body'], $data); if ('failure' === strtolower($data['ACK'])) { $error = '<p>' . __('PayPal payment processing failed.', 'rcp') . '</p>'; $error .= '<p>' . __('Error message:', 'rcp') . ' ' . $data['L_LONGMESSAGE0'] . '</p>'; $error .= '<p>' . __('Error code:', 'rcp') . ' ' . $data['L_ERRORCODE0'] . '</p>'; wp_die($error, __('Error', 'rcp'), array('response' => '401')); } else { $member = new RCP_Member($details['PAYMENTREQUEST_0_CUSTOM']); $member->renew(true); $member->set_payment_profile_id($data['PROFILEID']); $payment_data = array('date' => date('Y-m-d g:i:s', current_time('timestamp')), 'subscription' => $member->get_subscription_name(), 'payment_type' => 'PayPal Express', 'subscription_key' => $member->get_subscription_key(), 'amount' => round($details['AMT'] + $details['subscription']['fee'], 2), 'user_id' => $member->ID, 'transaction_id' => $data['PROFILEID']); $rcp_payments = new RCP_Payments(); $rcp_payments->insert($payment_data); wp_redirect(esc_url_raw(rcp_get_return_url())); exit; } } else { wp_die(__('Something has gone wrong, please try again', 'rcp'), __('Error', 'rcp'), array('back_link' => true, 'response' => '401')); } } else { // One time payment $args = array('USER' => $this->username, 'PWD' => $this->password, 'SIGNATURE' => $this->signature, 'VERSION' => '121', 'METHOD' => 'DoExpressCheckoutPayment', 'PAYMENTREQUEST_0_PAYMENTACTION' => 'Sale', 'TOKEN' => $_POST['token'], 'PAYERID' => $_POST['payer_id'], 'PAYMENTREQUEST_0_AMT' => $details['AMT'], 'PAYMENTREQUEST_0_ITEMAMT' => $details['AMT'], 'PAYMENTREQUEST_0_SHIPPINGAMT' => 0, 'PAYMENTREQUEST_0_TAXAMT' => 0, 'PAYMENTREQUEST_0_CURRENCYCODE' => $details['CURRENCYCODE'], 'BUTTONSOURCE' => 'EasyDigitalDownloads_SP'); $request = wp_remote_post($this->api_endpoint, array('timeout' => 45, 'sslverify' => false, 'body' => $args)); if (is_wp_error($request)) { $error = '<p>' . __('An unidentified error occurred.', 'rcp') . '</p>'; $error .= '<p>' . $request->get_error_message() . '</p>'; wp_die($error, __('Error', 'rcp'), array('response' => '401')); } elseif (200 == $request['response']['code'] && 'OK' == $request['response']['message']) { parse_str($request['body'], $data); if ('failure' === strtolower($data['ACK'])) { $error = '<p>' . __('PayPal payment processing failed.', 'rcp') . '</p>'; $error .= '<p>' . __('Error message:', 'rcp') . ' ' . $data['L_LONGMESSAGE0'] . '</p>'; $error .= '<p>' . __('Error code:', 'rcp') . ' ' . $data['L_ERRORCODE0'] . '</p>'; wp_die($error, __('Error', 'rcp'), array('response' => '401')); } else { // Confirm a one-time payment $member = new RCP_Member($details['CUSTOM']); $member->renew(false); $payment_data = array('date' => date('Y-m-d g:i:s', strtotime($data['PAYMENTINFO_0_ORDERTIME'])), 'subscription' => $member->get_subscription_name(), 'payment_type' => 'PayPal Express One Time', 'subscription_key' => $member->get_subscription_key(), 'amount' => $data['PAYMENTINFO_0_AMT'], 'user_id' => $member->ID, 'transaction_id' => $data['PAYMENTINFO_0_TRANSACTIONID']); $rcp_payments = new RCP_Payments(); $rcp_payments->insert($payment_data); wp_redirect(esc_url_raw(rcp_get_return_url())); exit; } } else { wp_die(__('Something has gone wrong, please try again', 'rcp'), __('Error', 'rcp'), array('back_link' => true, 'response' => '401')); } } } elseif (!empty($_GET['token']) && !empty($_GET['PayerID'])) { add_filter('the_content', array($this, 'confirmation_form'), 9999999); } }
public function renew_member($recurring = false, $status = 'active') { $member = new RCP_Member($this->user_id); $member->renew($recurring, $status); }
public function process_webhooks() { if (!isset($_GET['listener']) || strtolower($_GET['listener']) != 'stripe') { return; } // Ensure listener URL is not cached by W3TC define('DONOTCACHEPAGE', true); \Stripe\Stripe::setApiKey($this->secret_key); // retrieve the request's body and parse it as JSON $body = @file_get_contents('php://input'); $event_json_id = json_decode($body); // for extra security, retrieve from the Stripe API if (isset($event_json_id->id)) { $rcp_payments = new RCP_Payments(); $event_id = $event_json_id->id; try { $event = \Stripe\Event::retrieve($event_id); $invoice = $event->data->object; if (empty($invoice->customer)) { die('no customer attached'); } // retrieve the customer who made this payment (only for subscriptions) $user = rcp_get_member_id_from_profile_id($invoice->customer); if (empty($user)) { // Grab the customer ID from the old meta keys global $wpdb; $user = $wpdb->get_var($wpdb->prepare("SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = '_rcp_stripe_user_id' AND meta_value = %s LIMIT 1", $invoice->customer)); } if (empty($user)) { die('no user ID found'); } $member = new RCP_Member($user); // check to confirm this is a stripe subscriber if ($member) { // successful payment if ($event->type == 'charge.succeeded') { if (!$member->get_subscription_id()) { die('no subscription ID for member'); } $payment_data = array('date' => date('Y-m-d g:i:s', $event->created), 'subscription' => $member->get_subscription_name(), 'payment_type' => 'Credit Card', 'subscription_key' => $member->get_subscription_key(), 'amount' => $invoice->amount / 100, 'user_id' => $member->ID, 'transaction_id' => $invoice->id); if (!rcp_check_for_existing_payment($payment_data['payment_type'], $payment_data['date'], $payment_data['subscription_key'])) { // record this payment if it hasn't been recorded yet $rcp_payments->insert($payment_data); $member->renew($member->is_recurring()); do_action('rcp_stripe_charge_succeeded', $user, $payment_data); die('rcp_stripe_charge_succeeded action fired successfully'); } else { die('duplicate payment found'); } } // failed payment if ($event->type == 'charge.failed') { do_action('rcp_stripe_charge_failed', $invoice); die('rcp_stripe_charge_failed action fired successfully'); } // Cancelled / failed subscription if ($event->type == 'customer.subscription.deleted') { $member->set_status('cancelled'); die('member cancelled successfully'); } do_action('rcp_stripe_' . $event->type, $invoice); } } catch (Exception $e) { // something failed die('PHP exception: ' . $e->getMessage()); } die('1'); } die('no event ID found'); }
/** * Process PayPal IPN * * @since 2.1 */ public function process_webhooks() { if (!isset($_GET['listener']) || strtoupper($_GET['listener']) != 'EIPN') { return; } $user_id = 0; $posted = apply_filters('rcp_ipn_post', $_POST); // allow $_POST to be modified if (!empty($posted['custom']) && is_numeric($posted['custom'])) { $user_id = absint($posted['custom']); } else { if (!empty($posted['recurring_payment_id'])) { $user_id = rcp_get_member_id_from_profile_id($posted['recurring_payment_id']); } else { if (!empty($posted['payer_email'])) { $user = get_user_by('email', $posted['payer_email']); $user_id = $user ? $user->ID : false; } } } $member = new RCP_Member($user_id); if (!$member || !$member->get_subscription_id()) { die('no member found'); } if (!rcp_get_subscription_details($member->get_subscription_id())) { die('no subscription level found'); } $amount = number_format((double) $posted['mc_gross'], 2); // setup the payment info in an array for storage $payment_data = array('date' => date('Y-m-d g:i:s', strtotime($posted['payment_date'])), 'subscription' => $member->get_subscription_name(), 'payment_type' => $posted['txn_type'], 'subscription_key' => $member->get_subscription_key(), 'amount' => $amount, 'user_id' => $user_id, 'transaction_id' => $posted['txn_id']); do_action('rcp_valid_ipn', $payment_data, $user_id, $posted); if (isset($rcp_options['email_ipn_reports'])) { wp_mail(get_bloginfo('admin_email'), __('IPN report', 'rcp'), $listener->getTextReport()); } /* now process the kind of subscription/payment */ $rcp_payments = new RCP_Payments(); // Subscriptions switch ($posted['txn_type']) { case "recurring_payment": // when a user makes a recurring payment update_user_meta($user_id, 'rcp_paypal_subscriber', $posted['payer_id']); $member->set_payment_profile_id($posted['recurring_payment_id']); $member->renew(true); // record this payment in the database $rcp_payments->insert($payment_data); do_action('rcp_ipn_subscr_payment', $user_id); die('successful recurring_payment'); break; case "recurring_payment_profile_cancel": // user is marked as cancelled but retains access until end of term $member->set_status('cancelled'); // set the use to no longer be recurring delete_user_meta($user_id, 'rcp_paypal_subscriber'); do_action('rcp_ipn_subscr_cancel', $user_id); die('successful recurring_payment_profile_cancel'); break; case "recurring_payment_failed": case "recurring_payment_suspended_due_to_max_failed_payment": if ('cancelled' !== $member->get_status($user_id)) { $member->set_status('expired'); } do_action('rcp_ipn_subscr_failed'); die('successful recurring_payment_failed or recurring_payment_suspended_due_to_max_failed_payment'); break; } }