/** * Cancel a member's payment profile * * @access public * @since 2.1 */ function rcp_cancel_member_payment_profile( $member_id = 0 ) { global $rcp_options; $success = false; $member = new RCP_Member( $member_id ); if( ! rcp_can_member_cancel( $member_id ) ) { return $success; } if( rcp_is_stripe_subscriber( $member_id ) ) { if( ! class_exists( 'Stripe\Stripe' ) ) { require_once RCP_PLUGIN_DIR . 'includes/libraries/stripe/init.php'; } if ( isset( $rcp_options['sandbox'] ) ) { $secret_key = trim( $rcp_options['stripe_test_secret'] ); } else { $secret_key = trim( $rcp_options['stripe_live_secret'] ); } \Stripe\Stripe::setApiKey( $secret_key ); try { $cu = \Stripe\Customer::retrieve( $member->get_payment_profile_id() ); $cu->cancelSubscription( array( 'at_period_end' => false ) ); $success = true; } 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>" . __( 'Error code:', '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>" . __( 'Error code:', '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>" . __( 'Error code:', '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>" . __( 'Error code:', '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 = "<h4>" . __( 'An error occurred', 'rcp' ) . "</h4>"; $error .= print_r( $e, true ); wp_die( $error, __( 'Error', 'rcp' ), array( 'response' => 401 ) ); } } elseif( rcp_is_paypal_subscriber( $member_id ) ) { if( rcp_has_paypal_api_access() && $member->get_payment_profile_id() ) { // Set PayPal API key credentials. $api_username = isset( $rcp_options['sandbox'] ) ? 'test_paypal_api_username' : 'live_paypal_api_username'; $api_password = isset( $rcp_options['sandbox'] ) ? 'test_paypal_api_password' : 'live_paypal_api_password'; $api_signature = isset( $rcp_options['sandbox'] ) ? 'test_paypal_api_signature' : 'live_paypal_api_signature'; $api_endpoint = isset( $rcp_options['sandbox'] ) ? 'https://api-3t.sandbox.paypal.com/nvp' : 'https://api-3t.paypal.com/nvp'; $args = array( 'USER' => $rcp_options[ $api_username ], 'PWD' => $rcp_options[ $api_password ], 'SIGNATURE' => $rcp_options[ $api_signature ], 'VERSION' => '76.0', 'METHOD' => 'ManageRecurringPaymentsProfileStatus', 'PROFILEID' => $member->get_payment_profile_id(), 'ACTION' => 'Cancel' ); $error_msg = ''; $request = wp_remote_post( $api_endpoint, array( 'body' => $args, 'timeout' => 30 ) ); if ( is_wp_error( $request ) ) { $success = false; $error_msg = $request->get_error_message(); } else { $body = wp_remote_retrieve_body( $request ); if( is_string( $body ) ) { wp_parse_str( $body, $body ); } if( empty( $request['response'] ) ) { $success = false; } if( empty( $request['response']['code'] ) || 200 !== (int) $request['response']['code'] ) { $success = false; } if( empty( $request['response']['message'] ) || 'OK' !== $request['response']['message'] ) { $success = false; } if( isset( $body['ACK'] ) && 'success' === strtolower( $body['ACK'] ) ) { $success = true; } else { $success = false; if( isset( $body['L_LONGMESSAGE0'] ) ) { $error_msg = $body['L_LONGMESSAGE0']; } } } if( ! $success ) { wp_die( sprintf( __( 'There was a problem cancelling the subscription, please contact customer support. Error: %s', 'rcp' ), $error_msg ), array( 'response' => 400 ) ); } } } if( $success ) { $member->cancel(); } return $success; }
/** * 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'); } }