/** * Get the data being exported * * @access public * @since 1.5 * @return array */ public function get_data() { global $wpdb; $data = array(); $subscription = isset($_POST['rcp-subscription']) ? absint($_POST['rcp-subscription']) : null; $status = isset($_POST['rcp-status']) ? sanitize_text_field($_POST['rcp-status']) : 'active'; $offset = isset($_POST['rcp-offset']) ? absint($_POST['rcp-offset']) : null; $number = isset($_POST['rcp-number']) ? absint($_POST['rcp-number']) : null; $members = rcp_get_members($status, $subscription, $offset, $number); if ($members) { foreach ($members as $member) { $member = new RCP_Member($member->ID); $discounts = get_user_meta($member->ID, 'rcp_user_discounts', true); if (!empty($discounts) && is_array($discounts) && !$discounts instanceof stdClass) { foreach ($discounts as $key => $code) { if (!is_string($code)) { unset($discounts[$key]); } } $discounts = implode(' ', $discounts); } $data[] = array('user_id' => $member->ID, 'user_login' => $member->user_login, 'user_email' => $member->user_email, 'first_name' => $member->first_name, 'last_name' => $member->last_name, 'subscription' => $member->get_subscription_id(), 'subscription_key' => $member->get_subscription_key(), 'expiration' => $member->get_expiration_date(), 'status' => $member->get_status(), 'discount_codes' => $discounts, 'profile_id' => $member->get_payment_profile_id(), 'is_recurring' => $member->is_recurring()); } } $data = apply_filters('rcp_export_get_data', $data); $data = apply_filters('rcp_export_get_data_' . $this->export_type, $data); return $data; }
/** * Determine if a member is a Stripe subscriber * * @since v2.1 * @access public * @param $user_id INT the ID of the user to check * @return bool */ function rcp_is_stripe_subscriber($user_id = 0) { if (empty($user_id)) { $user_id = get_current_user_id(); } $ret = false; $member = new RCP_Member($user_id); $profile_id = $member->get_payment_profile_id(); // Check if the member is a Stripe customer if (false !== strpos($profile_id, 'cus_')) { $ret = true; } return (bool) apply_filters('rcp_is_stripe_subscriber', $ret, $user_id); }
/** * Determine if a member is a PayPal subscriber * * @since v2.0 * @access public * @param $user_id INT the ID of the user to check * @return bool */ function rcp_is_paypal_subscriber($user_id = 0) { if (empty($user_id)) { $user_id = get_current_user_id(); } $ret = false; $member = new RCP_Member($user_id); $profile_id = $member->get_payment_profile_id(); // Check if the member is a PayPal customer if (false !== strpos($profile_id, 'I-')) { $ret = true; } else { // The old way of identifying PayPal subscribers $ret = (bool) get_user_meta($user_id, 'rcp_paypal_subscriber', true); } return (bool) apply_filters('rcp_is_paypal_subscriber', $ret, $user_id); }
/** * Determines if a member can update the credit / debit card attached to their account * * @access public * @since 2.1 */ function rcp_member_can_update_billing_card( $user_id = 0 ) { if( empty( $user_id ) ) { $user_id = get_current_user_id(); } $ret = false; $member = new RCP_Member( $user_id ); if( $member->is_recurring() ) { $profile_id = $member->get_payment_profile_id(); // Check if the member is a Stripe customer if( false !== strpos( $profile_id, 'cus_' ) ) { $ret = true; } } return apply_filters( 'rcp_member_can_update_billing_card', $ret, $user_id ); }
/** * Determines if a member can cancel their subscription on site * * @access public * @since 2.1 */ function rcp_can_member_cancel($user_id = 0) { if (empty($user_id)) { $user_id = get_current_user_id(); } $ret = false; $member = new RCP_Member($user_id); if ($member->is_recurring() && $member->is_active() && 'cancelled' !== $member->get_status()) { $profile_id = $member->get_payment_profile_id(); // Check if the member is a Stripe customer if (rcp_is_stripe_subscriber($user_id)) { $ret = true; } elseif (rcp_is_paypal_subscriber($user_id) && rcp_has_paypal_api_access()) { $ret = true; } elseif (rcp_is_2checkout_subscriber($user_id) && defined('TWOCHECKOUT_ADMIN_USER') && defined('TWOCHECKOUT_ADMIN_PASSWORD')) { $ret = true; } } return apply_filters('rcp_member_can_cancel', $ret, $user_id); }
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 registration * * @since 2.1 */ public function process_signup() { \Stripe\Stripe::setApiKey($this->secret_key); $paid = false; $member = new RCP_Member($this->user_id); $customer_exists = false; if (empty($_POST['stripeToken'])) { wp_die(__('Missing Stripe token, please try again or contact support if the issue persists.', 'rcp'), __('Error', 'rcp'), array('response' => 400)); } if ($this->auto_renew) { // process a subscription sign up $plan_id = strtolower(str_replace(' ', '', $this->subscription_name)); if (!$this->plan_exists($plan_id)) { // create the plan if it doesn't exist $this->create_plan($this->subscription_name); } try { $customer_id = $member->get_payment_profile_id(); if ($customer_id) { $customer_exists = true; try { // Update the customer to ensure their card data is up to date $customer = \Stripe\Customer::retrieve($customer_id); if (isset($customer->deleted) && $customer->deleted) { // This customer was deleted $customer_exists = false; } // No customer found } catch (Exception $e) { $customer_exists = false; } } if (!$customer_exists) { $customer_args = array('card' => $_POST['stripeToken'], 'email' => $this->email, 'description' => 'User ID: ' . $this->user_id . ' - User Email: ' . $this->email . ' Subscription: ' . $this->subscription_name); if (!empty($this->discount_code)) { $customer_args['coupon'] = $this->discount_code; } $customer = \Stripe\Customer::create(apply_filters('rcp_stripe_customer_create_args', $customer_args, $this)); } else { $customer->card = $_POST['stripeToken']; } // Add fees before the plan is updated and charged if (!empty($this->signup_fee)) { if ($this->signup_fee > 0) { $description = sprintf(__('Signup Fee for %s', 'rcp'), $this->subscription_name); } else { $description = sprintf(__('Signup Discount for %s', 'rcp'), $this->subscription_name); } \Stripe\InvoiceItem::create(apply_filters('rcp_stripe_invoice_item_create_args', array('customer' => $customer->id, 'amount' => $this->signup_fee * 100, 'currency' => strtolower($this->currency), 'description' => $description), $this, $customer)); // Create the invoice containing taxes / discounts / fees $invoice = \Stripe\Invoice::create(apply_filters('rcp_stripe_invoice_create_args', array('customer' => $customer->id), $this, $customer)); } if (!empty($this->discount_code)) { $customer->coupon = $this->discount_code; } // Save the card and any coupon $customer->save(); // Process the invoice if there is one if (!empty($invoice)) { $invoice->pay(); } // Update the customer's subscription in Stripe $customer->updateSubscription(array('plan' => $plan_id)); $member->set_payment_profile_id($customer->id); // subscription payments are recorded via webhook $paid = true; } catch (\Stripe\Error\Card $e) { $body = $e->getJsonBody(); $err = $body['error']; $error = '<h4>' . __('An error occurred', 'rcp') . '</h4>'; if (isset($err['code'])) { $error .= '<p>' . sprintf(__('Error code: %s', 'rcp'), $err['code']) . '</p>'; } $error .= "<p>Status: " . $e->getHttpStatus() . "</p>"; $error .= "<p>Message: " . $err['message'] . "</p>"; wp_die($error, __('Error', 'rcp'), array('response' => '401')); exit; } catch (\Stripe\Error\InvalidRequest $e) { // Invalid parameters were supplied to Stripe's API $body = $e->getJsonBody(); $err = $body['error']; $error = '<h4>' . __('An error occurred', 'rcp') . '</h4>'; if (isset($err['code'])) { $error .= '<p>' . sprintf(__('Error code: %s', 'rcp'), $err['code']) . '</p>'; } $error .= "<p>Status: " . $e->getHttpStatus() . "</p>"; $error .= "<p>Message: " . $err['message'] . "</p>"; wp_die($error, __('Error', 'rcp'), array('response' => '401')); } catch (\Stripe\Error\Authentication $e) { // Authentication with Stripe's API failed // (maybe you changed API keys recently) $body = $e->getJsonBody(); $err = $body['error']; $error = '<h4>' . __('An error occurred', 'rcp') . '</h4>'; if (isset($err['code'])) { $error .= '<p>' . sprintf(__('Error code: %s', 'rcp'), $err['code']) . '</p>'; } $error .= "<p>Status: " . $e->getHttpStatus() . "</p>"; $error .= "<p>Message: " . $err['message'] . "</p>"; wp_die($error, __('Error', 'rcp'), array('response' => '401')); } catch (\Stripe\Error\ApiConnection $e) { // Network communication with Stripe failed $body = $e->getJsonBody(); $err = $body['error']; $error = '<h4>' . __('An error occurred', 'rcp') . '</h4>'; if (isset($err['code'])) { $error .= '<p>' . sprintf(__('Error code: %s', 'rcp'), $err['code']) . '</p>'; } $error .= "<p>Status: " . $e->getHttpStatus() . "</p>"; $error .= "<p>Message: " . $err['message'] . "</p>"; wp_die($error, __('Error', 'rcp'), array('response' => '401')); } catch (\Stripe\Error\Base $e) { // Display a very generic error to the user $body = $e->getJsonBody(); $err = $body['error']; $error = '<h4>' . __('An error occurred', 'rcp') . '</h4>'; if (isset($err['code'])) { $error .= '<p>' . sprintf(__('Error code: %s', 'rcp'), $err['code']) . '</p>'; } $error .= "<p>Status: " . $e->getHttpStatus() . "</p>"; $error .= "<p>Message: " . $err['message'] . "</p>"; wp_die($error, __('Error', 'rcp'), array('response' => '401')); } catch (Exception $e) { // Something else happened, completely unrelated to Stripe $error = '<p>' . __('An unidentified error occurred.', 'rcp') . '</p>'; $error .= print_r($e, true); wp_die($error, __('Error', 'rcp'), array('response' => '401')); } } else { // process a one time payment signup try { $charge = \Stripe\Charge::create(apply_filters('rcp_stripe_charge_create_args', array('amount' => $this->amount * 100, 'currency' => strtolower($this->currency), 'card' => $_POST['stripeToken'], 'description' => 'User ID: ' . $this->user_id . ' - User Email: ' . $this->email . ' Subscription: ' . $this->subscription_name, 'receipt_email' => $this->email, 'metadata' => array('email' => $this->email, 'user_id' => $this->user_id, 'level_id' => $this->subscription_id, 'level' => $this->subscription_name, 'key' => $this->subscription_key)), $this)); $payment_data = array('date' => date('Y-m-d g:i:s', current_time('timestamp')), 'subscription' => $this->subscription_name, 'payment_type' => 'Credit Card One Time', 'subscription_key' => $this->subscription_key, 'amount' => $this->amount, 'user_id' => $this->user_id, 'transaction_id' => $charge->id); $rcp_payments = new RCP_Payments(); $rcp_payments->insert($payment_data); $paid = true; } catch (\Stripe\Error\Card $e) { $body = $e->getJsonBody(); $err = $body['error']; $error = '<h4>' . __('An error occurred', 'rcp') . '</h4>'; if (isset($err['code'])) { $error .= '<p>' . sprintf(__('Error code: %s', 'rcp'), $err['code']) . '</p>'; } $error .= "<p>Status: " . $e->getHttpStatus() . "</p>"; $error .= "<p>Message: " . $err['message'] . "</p>"; wp_die($error, __('Error', 'rcp'), array('response' => '401')); exit; } catch (\Stripe\Error\InvalidRequest $e) { // Invalid parameters were supplied to Stripe's API $body = $e->getJsonBody(); $err = $body['error']; $error = '<h4>' . __('An error occurred', 'rcp') . '</h4>'; if (isset($err['code'])) { $error .= '<p>' . sprintf(__('Error code: %s', 'rcp'), $err['code']) . '</p>'; } $error .= "<p>Status: " . $e->getHttpStatus() . "</p>"; $error .= "<p>Message: " . $err['message'] . "</p>"; wp_die($error, __('Error', 'rcp'), array('response' => '401')); } catch (\Stripe\Error\Authentication $e) { // Authentication with Stripe's API failed // (maybe you changed API keys recently) $body = $e->getJsonBody(); $err = $body['error']; $error = '<h4>' . __('An error occurred', 'rcp') . '</h4>'; if (isset($err['code'])) { $error .= '<p>' . sprintf(__('Error code: %s', 'rcp'), $err['code']) . '</p>'; } $error .= "<p>Status: " . $e->getHttpStatus() . "</p>"; $error .= "<p>Message: " . $err['message'] . "</p>"; wp_die($error, __('Error', 'rcp'), array('response' => '401')); } catch (\Stripe\Error\ApiConnection $e) { // Network communication with Stripe failed $body = $e->getJsonBody(); $err = $body['error']; $error = '<h4>' . __('An error occurred', 'rcp') . '</h4>'; if (isset($err['code'])) { $error .= '<p>' . sprintf(__('Error code: %s', 'rcp'), $err['code']) . '</p>'; } $error .= "<p>Status: " . $e->getHttpStatus() . "</p>"; $error .= "<p>Message: " . $err['message'] . "</p>"; wp_die($error, __('Error', 'rcp'), array('response' => '401')); } catch (\Stripe\Error\Base $e) { // Display a very generic error to the user $body = $e->getJsonBody(); $err = $body['error']; $error = '<h4>' . __('An error occurred', 'rcp') . '</h4>'; if (isset($err['code'])) { $error .= '<p>' . sprintf(__('Error code: %s', 'rcp'), $err['code']) . '</p>'; } $error .= "<p>Status: " . $e->getHttpStatus() . "</p>"; $error .= "<p>Message: " . $err['message'] . "</p>"; wp_die($error, __('Error', 'rcp'), array('response' => '401')); } catch (Exception $e) { // Something else happened, completely unrelated to Stripe $error = '<p>' . __('An unidentified error occurred.', 'rcp') . '</p>'; $error .= print_r($e, true); wp_die($error, __('Error', 'rcp'), array('response' => '401')); } } if ($paid) { // set this user to active $member->set_status('active'); $member->set_recurring($this->auto_renew); if (!is_user_logged_in()) { // log the new user in rcp_login_user_in($this->user_id, $this->user_name, $_POST['rcp_user_pass']); } do_action('rcp_stripe_signup', $this->user_id, $this); } else { wp_die(__('An error occurred, please contact the site administrator: ', 'rcp') . get_bloginfo('admin_email'), __('Error', 'rcp'), array('response' => '401')); } // redirect to the success page, or error page if something went wrong wp_redirect($this->return_url); exit; }
/** * Determines if a member can cancel their subscription on site * * @access public * @since 2.1 */ function rcp_can_member_cancel( $user_id = 0 ) { if( empty( $user_id ) ) { $user_id = get_current_user_id(); } $ret = false; $member = new RCP_Member( $user_id ); if( $member->is_recurring() && $member->is_active() && 'cancelled' !== $member->get_status() ) { $profile_id = $member->get_payment_profile_id(); // Check if the member is a Stripe customer if( false !== strpos( $profile_id, 'cus_' ) ) { $ret = true; } elseif ( rcp_is_paypal_subscriber( $user_id ) && rcp_has_paypal_api_access() ) { $ret = true; } } return apply_filters( 'rcp_member_can_cancel', $ret, $user_id ); }
<p class="description"><?php _e('Enter the expiration date for this user in the format of yyyy-mm-dd', 'rcp'); ?> </p> </td> </tr> <tr valign="top"> <th scope="row" valign="top"> <label for="rcp-payment-profile-id"><?php _e('Payment Profile ID', 'rcp'); ?> </label> </th> <td> <input name="payment-profile-id" id="rcp-payment-profile-id" type="text" style="width: 200px;" value="<?php echo esc_attr($member->get_payment_profile_id()); ?> "/> <p class="description"><?php _e('This is the customer\'s payment profile ID in the payment processor', 'rcp'); ?> </p> </td> </tr> <tr valign="top"> <th scope="row" valign="top"> <?php _e('Recurring', 'rcp'); ?> </th> <td>