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'); }