Pass save payment method checkbox value

This commit is contained in:
Emili Castells Guasch 2023-11-27 16:00:12 +01:00
parent 7a8650dbfa
commit cd8564d81e
4 changed files with 30 additions and 20 deletions

View file

@ -181,6 +181,7 @@ class OrderEndpoint {
* @param string $paypal_request_id The PayPal request id. * @param string $paypal_request_id The PayPal request id.
* @param string $user_action The user action. * @param string $user_action The user action.
* @param string $payment_method WC payment method. * @param string $payment_method WC payment method.
* @param array $request_data Request data.
* *
* @return Order * @return Order
* @throws RuntimeException If the request fails. * @throws RuntimeException If the request fails.
@ -192,7 +193,8 @@ class OrderEndpoint {
PaymentToken $payment_token = null, PaymentToken $payment_token = null,
string $paypal_request_id = '', string $paypal_request_id = '',
string $user_action = ApplicationContext::USER_ACTION_CONTINUE, string $user_action = ApplicationContext::USER_ACTION_CONTINUE,
string $payment_method = '' string $payment_method = '',
array $request_data = array()
): Order { ): Order {
$bearer = $this->bearer->bearer(); $bearer = $this->bearer->bearer();
$data = array( $data = array(
@ -223,7 +225,7 @@ class OrderEndpoint {
/** /**
* The filter can be used to modify the order creation request body data. * 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'; $url = trailingslashit( $this->host ) . 'v2/checkout/orders';
$args = array( $args = array(
'method' => 'POST', 'method' => 'POST',

View file

@ -56,6 +56,8 @@ class CheckoutActionHandler {
const paymentMethod = getCurrentPaymentMethod(); const paymentMethod = getCurrentPaymentMethod();
const fundingSource = window.ppcpFundingSource; 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, { return fetch(this.config.ajax.create_order.endpoint, {
method: 'POST', method: 'POST',
headers: { headers: {
@ -72,7 +74,8 @@ class CheckoutActionHandler {
funding_source: fundingSource, funding_source: fundingSource,
// send as urlencoded string to handle complex fields via PHP functions the same as normal form submit // send as urlencoded string to handle complex fields via PHP functions the same as normal form submit
form_encoded: new URLSearchParams(formData).toString(), form_encoded: new URLSearchParams(formData).toString(),
createaccount: createaccount createaccount: createaccount,
save_payment_method: savePaymentMethod
}) })
}).then(function (res) { }).then(function (res) {
return res.json(); return res.json();

View file

@ -304,7 +304,7 @@ class CreateOrderEndpoint implements EndpointInterface {
} }
try { try {
$order = $this->create_paypal_order( $wc_order, $payment_method ); $order = $this->create_paypal_order( $wc_order, $payment_method, $data );
} catch ( Exception $exception ) { } catch ( Exception $exception ) {
$this->logger->error( 'Order creation failed: ' . $exception->getMessage() ); $this->logger->error( 'Order creation failed: ' . $exception->getMessage() );
throw $exception; throw $exception;
@ -416,6 +416,7 @@ class CreateOrderEndpoint implements EndpointInterface {
* *
* @param \WC_Order|null $wc_order WC order to get data from. * @param \WC_Order|null $wc_order WC order to get data from.
* @param string $payment_method WC payment method. * @param string $payment_method WC payment method.
* @param array $data Request data.
* *
* @return Order Created PayPal order. * @return Order Created PayPal order.
* *
@ -423,7 +424,7 @@ class CreateOrderEndpoint implements EndpointInterface {
* @throws PayPalApiException If create order request fails. * @throws PayPalApiException If create order request fails.
* phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.WrongNumber * 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 ); assert( $this->purchase_unit instanceof PurchaseUnit );
$funding_source = $this->parsed_request_data['funding_source'] ?? ''; $funding_source = $this->parsed_request_data['funding_source'] ?? '';
@ -466,7 +467,8 @@ class CreateOrderEndpoint implements EndpointInterface {
null, null,
'', '',
$action, $action,
$payment_method $payment_method,
$data
); );
} catch ( PayPalApiException $exception ) { } catch ( PayPalApiException $exception ) {
// Looks like currently there is no proper way to validate the shipping address for PayPal, // Looks like currently there is no proper way to validate the shipping address for PayPal,

View file

@ -89,7 +89,7 @@ class SavePaymentMethodsModule implements ModuleInterface {
// Adds attributes needed to save payment method. // Adds attributes needed to save payment method.
add_filter( add_filter(
'ppcp_create_order_request_body_data', '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 // phpcs:ignore WordPress.Security.NonceVerification.Missing
$wc_order_action = wc_clean( wp_unslash( $_POST['wc_order_action'] ?? '' ) ); $wc_order_action = wc_clean( wp_unslash( $_POST['wc_order_action'] ?? '' ) );
if ( $wc_order_action === 'wcs_process_renewal' ) { if ( $wc_order_action === 'wcs_process_renewal' ) {
@ -112,21 +112,24 @@ class SavePaymentMethodsModule implements ModuleInterface {
} }
if ( $payment_method === CreditCardGateway::ID ) { if ( $payment_method === CreditCardGateway::ID ) {
$data['payment_source'] = array( $save_payment_method = $request_data['save_payment_method'] ?? false;
'card' => array( if ( $save_payment_method ) {
'attributes' => array( $data['payment_source'] = array(
'vault' => array( 'card' => array(
'store_in_vault' => 'ON_SUCCESS', '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; return $data;
}, },
10, 10,
2 3
); );
add_action( add_action(