get_id() > 0 ) { $subscription_id = $membership->get_gateway_customer_id(); if ( false !== strpos( $subscription_id, 'bt_' ) ) { $is_braintree = true; } } /** * Filters whether or not the membership is a Braintree subscription. * * @param bool $is_braintree * @param RCP_Membership $membership * * @since 3.0 */ return (bool) apply_filters( 'rcp_is_braintree_membership', $is_braintree, $membership ); } /** * Determines if all necessary Braintree API credentials are available. * * @since 2.7 * @return bool */ function rcp_has_braintree_api_access() { global $rcp_options; if ( rcp_is_sandbox() ) { $merchant_id = ! empty( $rcp_options['braintree_sandbox_merchantId'] ) ? sanitize_text_field( $rcp_options['braintree_sandbox_merchantId'] ) : ''; $public_key = ! empty( $rcp_options['braintree_sandbox_publicKey'] ) ? sanitize_text_field( $rcp_options['braintree_sandbox_publicKey'] ) : ''; $private_key = ! empty( $rcp_options['braintree_sandbox_privateKey'] ) ? sanitize_text_field( $rcp_options['braintree_sandbox_privateKey'] ) : ''; $encryption_key = ! empty( $rcp_options['braintree_sandbox_encryptionKey'] ) ? sanitize_text_field( $rcp_options['braintree_sandbox_encryptionKey'] ) : ''; } else { $merchant_id = ! empty( $rcp_options['braintree_live_merchantId'] ) ? sanitize_text_field( $rcp_options['braintree_live_merchantId'] ) : ''; $public_key = ! empty( $rcp_options['braintree_live_publicKey'] ) ? sanitize_text_field( $rcp_options['braintree_live_publicKey'] ) : ''; $private_key = ! empty( $rcp_options['braintree_live_privateKey'] ) ? sanitize_text_field( $rcp_options['braintree_live_privateKey'] ) : ''; $encryption_key = ! empty( $rcp_options['braintree_live_encryptionKey'] ) ? sanitize_text_field( $rcp_options['braintree_live_encryptionKey'] ) : ''; } if ( ! empty( $merchant_id ) && ! empty( $public_key ) && ! empty( $private_key ) && ! empty( $encryption_key ) ) { return true; } return false; } /** * Cancel a Braintree membership by subscription ID. * * @param string $subscription_id Braintree subscription ID. * * @since 3.0 * @return true|WP_Error True on success, WP_Error on failure. */ function rcp_braintree_cancel_membership( $subscription_id ) { global $rcp_options; $ret = true; if ( rcp_is_sandbox() ) { $merchant_id = ! empty( $rcp_options['braintree_sandbox_merchantId'] ) ? sanitize_text_field( $rcp_options['braintree_sandbox_merchantId'] ) : ''; $public_key = ! empty( $rcp_options['braintree_sandbox_publicKey'] ) ? sanitize_text_field( $rcp_options['braintree_sandbox_publicKey'] ) : ''; $private_key = ! empty( $rcp_options['braintree_sandbox_privateKey'] ) ? sanitize_text_field( $rcp_options['braintree_sandbox_privateKey'] ) : ''; $encryption_key = ! empty( $rcp_options['braintree_sandbox_encryptionKey'] ) ? sanitize_text_field( $rcp_options['braintree_sandbox_encryptionKey'] ) : ''; $environment = 'sandbox'; } else { $merchant_id = ! empty( $rcp_options['braintree_live_merchantId'] ) ? sanitize_text_field( $rcp_options['braintree_live_merchantId'] ) : ''; $public_key = ! empty( $rcp_options['braintree_live_publicKey'] ) ? sanitize_text_field( $rcp_options['braintree_live_publicKey'] ) : ''; $private_key = ! empty( $rcp_options['braintree_live_privateKey'] ) ? sanitize_text_field( $rcp_options['braintree_live_privateKey'] ) : ''; $encryption_key = ! empty( $rcp_options['braintree_live_encryptionKey'] ) ? sanitize_text_field( $rcp_options['braintree_live_encryptionKey'] ) : ''; $environment = 'production'; } if ( ! class_exists( 'Braintree\\Gateway' ) ) { require_once RCP_PLUGIN_DIR . 'pro/includes/libraries/braintree/lib/Braintree.php'; } $gateway = new Braintree\Gateway( array( 'environment' => $environment, 'merchantId' => $merchant_id, 'publicKey' => $public_key, 'privateKey' => $private_key ) ); try { $result = $gateway->subscription()->cancel( $subscription_id ); if ( ! $result->success ) { $status = $result->errors->forKey( 'subscription' )->onAttribute( 'status' ); /** * Don't throw an exception if the subscription is already cancelled. */ if ( '81905' != $status[0]->code ) { $ret = new WP_Error( 'rcp_braintree_error', $result->message ); } } } catch ( Exception $e ) { $ret = new WP_Error( 'rcp_braintree_error', $e->getMessage() ); } return $ret; } /** * Checks for the legacy Braintree gateway * and deactivates it and shows a notice. * * @since 2.8 * @return void */ function rcp_braintree_detect_legacy_plugin() { if ( ! is_admin() || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { return; } if ( is_plugin_active( 'rcp-braintree/rcp-braintree.php' ) ) { deactivate_plugins( 'rcp-braintree/rcp-braintree.php', true ); } } add_action( 'admin_init', 'rcp_braintree_detect_legacy_plugin' ); /** * Checks for legacy Braintree webhook endpoints * and fires off the webhook processing for those requests. * * @since 2.8 * @return void */ add_action( 'init', function() { if ( ! empty( $_GET['bt_challenge'] ) || ( ! empty( $_POST['bt_signature'] ) && ! empty( $_POST['bt_payload'] ) ) ) { add_filter( 'rcp_process_gateway_webhooks', '__return_true' ); } }, -100000 ); // Must run before rcp_process_gateway_webooks which is hooked on -99999 /** * Add JS to the update card form * * @since 3.3 * @return void */ function rcp_braintree_update_card_form_js() { global $rcp_membership; if ( ! rcp_is_braintree_membership( $rcp_membership ) || ! rcp_has_braintree_api_access() ) { return; } $gateway = new RCP_Payment_Gateway_Braintree(); $gateway->scripts(); } add_action( 'rcp_before_update_billing_card_form', 'rcp_braintree_update_card_form_js' ); /** * Update the billing card for a given membership * * @param RCP_Membership $membership * * @since 3.3 * @return void */ function rcp_braintree_update_membership_billing_card( $membership ) { if ( ! $membership instanceof RCP_Membership ) { return; } if ( ! rcp_is_braintree_membership( $membership ) ) { return; } if ( empty( $_POST['payment_method_nonce'] ) ) { wp_die( __( 'Missing payment method nonce.', 'rcp' ) ); } $subscription_id = $membership->get_gateway_subscription_id(); if ( empty( $subscription_id ) ) { wp_die( __( 'Invalid subscription.', 'rcp' ) ); } global $rcp_options; if ( rcp_is_sandbox() ) { $merchant_id = ! empty( $rcp_options['braintree_sandbox_merchantId'] ) ? sanitize_text_field( $rcp_options['braintree_sandbox_merchantId'] ) : ''; $public_key = ! empty( $rcp_options['braintree_sandbox_publicKey'] ) ? sanitize_text_field( $rcp_options['braintree_sandbox_publicKey'] ) : ''; $private_key = ! empty( $rcp_options['braintree_sandbox_privateKey'] ) ? sanitize_text_field( $rcp_options['braintree_sandbox_privateKey'] ) : ''; $environment = 'sandbox'; } else { $merchant_id = ! empty( $rcp_options['braintree_live_merchantId'] ) ? sanitize_text_field( $rcp_options['braintree_live_merchantId'] ) : ''; $public_key = ! empty( $rcp_options['braintree_live_publicKey'] ) ? sanitize_text_field( $rcp_options['braintree_live_publicKey'] ) : ''; $private_key = ! empty( $rcp_options['braintree_live_privateKey'] ) ? sanitize_text_field( $rcp_options['braintree_live_privateKey'] ) : ''; $environment = 'production'; } if ( ! class_exists( 'Braintree\\Gateway' ) ) { require_once RCP_PLUGIN_DIR . 'pro/includes/libraries/braintree/lib/Braintree.php'; } $gateway = new Braintree\Gateway( array( 'environment' => $environment, 'merchantId' => $merchant_id, 'publicKey' => $public_key, 'privateKey' => $private_key ) ); try { $gateway->subscription()->update( $subscription_id, array( 'paymentMethodNonce' => sanitize_text_field( $_POST['payment_method_nonce'] ) ) ); wp_redirect( add_query_arg( 'card', 'updated' ) ); exit; } catch ( \Exception $e ) { wp_die( sprintf( __( 'An error occurred: %s', 'rcp' ), $e->getMessage() ) ); } } add_action( 'rcp_update_membership_billing_card', 'rcp_braintree_update_membership_billing_card' ); /** * The origin of this function was the migration of 3DS V1 to 3DS V2. * * Output the additional fields needed by Braintree to fulfill the 3DS2 such as address fields. * * @return void */ function rcp_braintree_additional_fields() { ?>
'failed', 'message' => 'Invalid Nonce. Consider reloading the page.', ], 401); } $billing_address = array_key_exists( 'billingAddress', $post) ? array_map( 'rcp_sanitize_fields', $post['billingAddress'] ) : $post; if(array_key_exists( 'additionalInformation', $post) ) { $additional_information = array_map( 'rcp_sanitize_fields', $post['additionalInformation'] ); $additional_information['shippingAddress'] = array_key_exists( 'shippingAddress', $additional_information ) ? array_map( 'rcp_sanitize_fields', $additional_information['shippingAddress'] ) : $additional_information; } $result = [ 'billingAddress' => $billing_address, 'additionalInformation' => $billing_address, ]; wp_send_json_success( $result, 200 ); } /** * Function that will check if the current value if a string and sanitize it, otherwise it will just return the * Array|Object. * * @param array|string $_field The field to check. * @since 3.5.23.1 * @return array|string The Sanitized String or the Array|Object. */ function rcp_sanitize_fields( $_field ) { if( is_object( $_field ) || is_array( $_field ) ) { return $_field; } return sanitize_text_field( $_field ); }