diff --git a/modules/ppcp-api-client/src/Endpoint/OrderEndpoint.php b/modules/ppcp-api-client/src/Endpoint/OrderEndpoint.php index d150b5bd9..8e03dc5fb 100644 --- a/modules/ppcp-api-client/src/Endpoint/OrderEndpoint.php +++ b/modules/ppcp-api-client/src/Endpoint/OrderEndpoint.php @@ -181,6 +181,7 @@ class OrderEndpoint { * @param string $paypal_request_id The PayPal request id. * @param string $user_action The user action. * @param string $payment_method WC payment method. + * @param array $request_data Request data. * * @return Order * @throws RuntimeException If the request fails. @@ -192,7 +193,8 @@ class OrderEndpoint { PaymentToken $payment_token = null, string $paypal_request_id = '', string $user_action = ApplicationContext::USER_ACTION_CONTINUE, - string $payment_method = '' + string $payment_method = '', + array $request_data = array() ): Order { $bearer = $this->bearer->bearer(); $data = array( @@ -223,7 +225,7 @@ class OrderEndpoint { /** * The filter can be used to modify the order creation request body data. */ - $data = apply_filters( 'ppcp_create_order_request_body_data', $data, $payment_method ); + $data = apply_filters( 'ppcp_create_order_request_body_data', $data, $payment_method, $request_data ); $url = trailingslashit( $this->host ) . 'v2/checkout/orders'; $args = array( 'method' => 'POST', diff --git a/modules/ppcp-button/resources/js/modules/ActionHandler/CheckoutActionHandler.js b/modules/ppcp-button/resources/js/modules/ActionHandler/CheckoutActionHandler.js index b6882ff3f..3316c26bf 100644 --- a/modules/ppcp-button/resources/js/modules/ActionHandler/CheckoutActionHandler.js +++ b/modules/ppcp-button/resources/js/modules/ActionHandler/CheckoutActionHandler.js @@ -56,6 +56,8 @@ class CheckoutActionHandler { const paymentMethod = getCurrentPaymentMethod(); const fundingSource = window.ppcpFundingSource; + const savePaymentMethod = !!document.getElementById('wc-ppcp-credit-card-gateway-new-payment-method')?.checked; + return fetch(this.config.ajax.create_order.endpoint, { method: 'POST', headers: { @@ -72,7 +74,8 @@ class CheckoutActionHandler { funding_source: fundingSource, // send as urlencoded string to handle complex fields via PHP functions the same as normal form submit form_encoded: new URLSearchParams(formData).toString(), - createaccount: createaccount + createaccount: createaccount, + save_payment_method: savePaymentMethod }) }).then(function (res) { return res.json(); diff --git a/modules/ppcp-button/src/Endpoint/CreateOrderEndpoint.php b/modules/ppcp-button/src/Endpoint/CreateOrderEndpoint.php index 1e24c741d..2150b931f 100644 --- a/modules/ppcp-button/src/Endpoint/CreateOrderEndpoint.php +++ b/modules/ppcp-button/src/Endpoint/CreateOrderEndpoint.php @@ -304,7 +304,7 @@ class CreateOrderEndpoint implements EndpointInterface { } try { - $order = $this->create_paypal_order( $wc_order, $payment_method ); + $order = $this->create_paypal_order( $wc_order, $payment_method, $data ); } catch ( Exception $exception ) { $this->logger->error( 'Order creation failed: ' . $exception->getMessage() ); throw $exception; @@ -416,6 +416,7 @@ class CreateOrderEndpoint implements EndpointInterface { * * @param \WC_Order|null $wc_order WC order to get data from. * @param string $payment_method WC payment method. + * @param array $data Request data. * * @return Order Created PayPal order. * @@ -423,7 +424,7 @@ class CreateOrderEndpoint implements EndpointInterface { * @throws PayPalApiException If create order request fails. * phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.WrongNumber */ - private function create_paypal_order( \WC_Order $wc_order = null, string $payment_method = '' ): Order { + private function create_paypal_order( \WC_Order $wc_order = null, string $payment_method = '', array $data = array() ): Order { assert( $this->purchase_unit instanceof PurchaseUnit ); $funding_source = $this->parsed_request_data['funding_source'] ?? ''; @@ -466,7 +467,8 @@ class CreateOrderEndpoint implements EndpointInterface { null, '', $action, - $payment_method + $payment_method, + $data ); } catch ( PayPalApiException $exception ) { // Looks like currently there is no proper way to validate the shipping address for PayPal, diff --git a/modules/ppcp-save-payment-methods/src/SavePaymentMethodsModule.php b/modules/ppcp-save-payment-methods/src/SavePaymentMethodsModule.php index f128a770b..b00d9b1e2 100644 --- a/modules/ppcp-save-payment-methods/src/SavePaymentMethodsModule.php +++ b/modules/ppcp-save-payment-methods/src/SavePaymentMethodsModule.php @@ -89,7 +89,7 @@ class SavePaymentMethodsModule implements ModuleInterface { // Adds attributes needed to save payment method. add_filter( 'ppcp_create_order_request_body_data', - function( array $data, string $payment_method ): array { + function( array $data, string $payment_method, array $request_data ): array { // phpcs:ignore WordPress.Security.NonceVerification.Missing $wc_order_action = wc_clean( wp_unslash( $_POST['wc_order_action'] ?? '' ) ); if ( $wc_order_action === 'wcs_process_renewal' ) { @@ -112,21 +112,24 @@ class SavePaymentMethodsModule implements ModuleInterface { } if ( $payment_method === CreditCardGateway::ID ) { - $data['payment_source'] = array( - 'card' => array( - 'attributes' => array( - 'vault' => array( - 'store_in_vault' => 'ON_SUCCESS', + $save_payment_method = $request_data['save_payment_method'] ?? false; + if ( $save_payment_method ) { + $data['payment_source'] = array( + 'card' => array( + 'attributes' => array( + 'vault' => array( + 'store_in_vault' => 'ON_SUCCESS', + ), ), ), - ), - ); - - $target_customer_id = get_user_meta( get_current_user_id(), '_ppcp_target_customer_id', true ); - if ( $target_customer_id ) { - $data['payment_source']['card']['attributes']['customer'] = array( - 'id' => $target_customer_id, ); + + $target_customer_id = get_user_meta( get_current_user_id(), '_ppcp_target_customer_id', true ); + if ( $target_customer_id ) { + $data['payment_source']['card']['attributes']['customer'] = array( + 'id' => $target_customer_id, + ); + } } } @@ -146,7 +149,7 @@ class SavePaymentMethodsModule implements ModuleInterface { return $data; }, 10, - 2 + 3 ); add_action(