From 98f6d0de4513c01c124d31159eee5dfcded827d1 Mon Sep 17 00:00:00 2001 From: Alex P Date: Fri, 21 Apr 2023 11:12:32 +0300 Subject: [PATCH] Save funding source for block orders --- modules/ppcp-blocks/resources/js/checkout-block.js | 10 +++++++--- modules/ppcp-blocks/src/PayPalPaymentMethod.php | 11 ++++++----- modules/ppcp-wc-gateway/src/Gateway/PayPalGateway.php | 8 +++++++- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/modules/ppcp-blocks/resources/js/checkout-block.js b/modules/ppcp-blocks/resources/js/checkout-block.js index e1b4b8f52..def53bd7a 100644 --- a/modules/ppcp-blocks/resources/js/checkout-block.js +++ b/modules/ppcp-blocks/resources/js/checkout-block.js @@ -5,6 +5,8 @@ import {loadPaypalScript} from '../../../ppcp-button/resources/js/modules/Helper const config = wc.wcSettings.getSetting('ppcp-gateway_data'); +window.ppcpFundingSource = config.fundingSource; + const PayPalComponent = ({ onClick, onClose, @@ -39,7 +41,6 @@ const PayPalComponent = ({ context: 'express', order_id: config.scriptData.order_id, payment_method: 'ppcp-gateway', - funding_source: 'paypal', createaccount: false }), }); @@ -75,7 +76,7 @@ const PayPalComponent = ({ body: JSON.stringify({ nonce: config.scriptData.ajax.approve_order.nonce, order_id: data.orderID, - //funding_source: , + funding_source: window.ppcpFundingSource ?? 'paypal', }) }); @@ -125,7 +126,9 @@ const PayPalComponent = ({ } }; - const handleClick = () => { + const handleClick = (data, actions) => { + window.ppcpFundingSource = data.fundingSource; + onClick(); }; @@ -141,6 +144,7 @@ const PayPalComponent = ({ meta: { paymentMethodData: { 'paypal_order_id': config.scriptData.continuation.order_id, + 'funding_source': window.ppcpFundingSource ?? 'paypal', }, }, }; diff --git a/modules/ppcp-blocks/src/PayPalPaymentMethod.php b/modules/ppcp-blocks/src/PayPalPaymentMethod.php index 0aeff9fc5..3c792868e 100644 --- a/modules/ppcp-blocks/src/PayPalPaymentMethod.php +++ b/modules/ppcp-blocks/src/PayPalPaymentMethod.php @@ -158,11 +158,12 @@ class PayPalPaymentMethod extends AbstractPaymentMethodType { } return array( - 'id' => $this->gateway->id, - 'title' => $this->gateway->title, - 'description' => $this->gateway->description, - 'enabled' => $this->settings_status->is_smart_button_enabled_for_location( $script_data['context'] ), - 'scriptData' => $script_data, + 'id' => $this->gateway->id, + 'title' => $this->gateway->title, + 'description' => $this->gateway->description, + 'enabled' => $this->settings_status->is_smart_button_enabled_for_location( $script_data['context'] ), + 'fundingSource' => $this->session_handler->funding_source(), + 'scriptData' => $script_data, ); } } diff --git a/modules/ppcp-wc-gateway/src/Gateway/PayPalGateway.php b/modules/ppcp-wc-gateway/src/Gateway/PayPalGateway.php index 944d7b67b..58037e60e 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/PayPalGateway.php +++ b/modules/ppcp-wc-gateway/src/Gateway/PayPalGateway.php @@ -424,7 +424,13 @@ class PayPalGateway extends \WC_Payment_Gateway { } // phpcs:ignore WordPress.Security.NonceVerification.Missing - $funding_source = wc_clean( wp_unslash( $_POST['ppcp-funding-source'] ?? '' ) ); + $funding_source = wc_clean( wp_unslash( $_POST['ppcp-funding-source'] ?? ( $_POST['funding_source'] ?? '' ) ) ); + + if ( $funding_source ) { + $wc_order->set_payment_method_title( $this->funding_source_renderer->render_name( $funding_source ) ); + $wc_order->save(); + } + if ( 'card' !== $funding_source && $this->is_free_trial_order( $wc_order ) ) { $user_id = (int) $wc_order->get_customer_id(); $tokens = $this->payment_token_repository->all_for_user_id( $user_id );