From 2527f45a1ea220a9bd17c42e8ab2fe987d244cfa Mon Sep 17 00:00:00 2001 From: Emili Castells Guasch Date: Fri, 8 Mar 2024 17:38:43 +0100 Subject: [PATCH] Add wc invoice id when creating PayPal order --- .../js/modules/Renderer/CardFieldsRenderer.js | 14 +--- .../ppcp-save-payment-methods/services.php | 1 - .../src/Endpoint/CaptureCardPayment.php | 81 ++----------------- modules/ppcp-wc-gateway/services.php | 2 + .../src/Gateway/CreditCardGateway.php | 50 +++++++----- 5 files changed, 40 insertions(+), 108 deletions(-) diff --git a/modules/ppcp-button/resources/js/modules/Renderer/CardFieldsRenderer.js b/modules/ppcp-button/resources/js/modules/Renderer/CardFieldsRenderer.js index 3287c7492..73532a727 100644 --- a/modules/ppcp-button/resources/js/modules/Renderer/CardFieldsRenderer.js +++ b/modules/ppcp-button/resources/js/modules/Renderer/CardFieldsRenderer.js @@ -127,19 +127,7 @@ class CardFieldsRenderer { const paymentToken = document.querySelector('input[name="wc-ppcp-credit-card-gateway-payment-token"]:checked')?.value if(paymentToken && paymentToken !== 'new') { - fetch(this.defaultConfig.ajax.capture_card_payment.endpoint, { - method: 'POST', - credentials: 'same-origin', - body: JSON.stringify({ - nonce: this.defaultConfig.ajax.capture_card_payment.nonce, - payment_token: paymentToken - }) - }).then((res) => { - return res.json(); - }).then((data) => { - document.querySelector('#place_order').click(); - }); - + document.querySelector('#place_order').click(); return; } diff --git a/modules/ppcp-save-payment-methods/services.php b/modules/ppcp-save-payment-methods/services.php index f60b1e6fd..5d04b16ab 100644 --- a/modules/ppcp-save-payment-methods/services.php +++ b/modules/ppcp-save-payment-methods/services.php @@ -821,7 +821,6 @@ return array( }, 'save-payment-methods.endpoint.capture-card-payment' => static function( ContainerInterface $container ): CaptureCardPayment { return new CaptureCardPayment( - $container->get( 'button.request-data' ), $container->get( 'api.host' ), $container->get( 'api.bearer' ), $container->get( 'api.factory.order' ), diff --git a/modules/ppcp-save-payment-methods/src/Endpoint/CaptureCardPayment.php b/modules/ppcp-save-payment-methods/src/Endpoint/CaptureCardPayment.php index 6d18a82a6..554c6edcb 100644 --- a/modules/ppcp-save-payment-methods/src/Endpoint/CaptureCardPayment.php +++ b/modules/ppcp-save-payment-methods/src/Endpoint/CaptureCardPayment.php @@ -12,15 +12,12 @@ namespace WooCommerce\PayPalCommerce\SavePaymentMethods\Endpoint; use Psr\Log\LoggerInterface; use RuntimeException; use stdClass; -use WC_Payment_Tokens; use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\RequestTrait; use WooCommerce\PayPalCommerce\ApiClient\Entity\PurchaseUnit; use WooCommerce\PayPalCommerce\ApiClient\Factory\OrderFactory; use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory; -use WooCommerce\PayPalCommerce\Button\Endpoint\EndpointInterface; -use WooCommerce\PayPalCommerce\Button\Endpoint\RequestData; use WooCommerce\PayPalCommerce\Session\SessionHandler; use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings; use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\RealTimeAccountUpdaterHelper; @@ -29,19 +26,10 @@ use WP_Error; /** * Class CaptureCardPayment */ -class CaptureCardPayment implements EndpointInterface { +class CaptureCardPayment { use RequestTrait; - const ENDPOINT = 'ppc-capture-card-payment'; - - /** - * The request data. - * - * @var RequestData - */ - private $request_data; - /** * The host. * @@ -108,7 +96,6 @@ class CaptureCardPayment implements EndpointInterface { /** * CaptureCardPayment constructor. * - * @param RequestData $request_data The request data. * @param string $host The host. * @param Bearer $bearer The bearer. * @param OrderFactory $order_factory The order factory. @@ -120,7 +107,6 @@ class CaptureCardPayment implements EndpointInterface { * @param LoggerInterface $logger The logger. */ public function __construct( - RequestData $request_data, string $host, Bearer $bearer, OrderFactory $order_factory, @@ -131,7 +117,6 @@ class CaptureCardPayment implements EndpointInterface { Settings $settings, LoggerInterface $logger ) { - $this->request_data = $request_data; $this->host = $host; $this->bearer = $bearer; $this->order_factory = $order_factory; @@ -139,66 +124,8 @@ class CaptureCardPayment implements EndpointInterface { $this->order_endpoint = $order_endpoint; $this->session_handler = $session_handler; $this->real_time_account_updater_helper = $real_time_account_updater_helper; - $this->logger = $logger; $this->settings = $settings; - } - - /** - * Returns the nonce. - * - * @return string - */ - public static function nonce(): string { - return self::ENDPOINT; - } - - /** - * Handles the request. - * - * @return bool - */ - public function handle_request(): bool { - $data = $this->request_data->read_request( $this->nonce() ); - - $tokens = WC_Payment_Tokens::get_customer_tokens( get_current_user_id() ); - foreach ( $tokens as $token ) { - if ( $token->get_id() === (int) $data['payment_token'] ) { - try { - $order = $this->create_order( $token->get_token() ); - - $id = $order->id ?? ''; - $status = $order->status ?? ''; - $payment_source = isset( $order->payment_source->card ) ? 'card' : ''; - $expiry = $order->payment_source->card->expiry ?? ''; - $last_digits = $order->payment_source->card->last_digits ?? ''; - if ( $id && $status && $payment_source && $expiry && $last_digits ) { - $this->real_time_account_updater_helper->update_wc_card_token( - $expiry, - $last_digits, - $token - ); - - WC()->session->set( - 'ppcp_saved_payment_card', - array( - 'order_id' => $id, - 'status' => $status, - 'payment_source' => $payment_source, - ) - ); - - wp_send_json_success(); - return true; - } - } catch ( RuntimeException $exception ) { - wp_send_json_error(); - return false; - } - } - } - - wp_send_json_error(); - return false; + $this->logger = $logger; } /** @@ -208,7 +135,7 @@ class CaptureCardPayment implements EndpointInterface { * @return stdClass * @throws RuntimeException When request fails. */ - private function create_order( string $vault_id ): stdClass { + public function create_order( string $vault_id, string $custom_id, string $invoice_id ): stdClass { $intent = $this->settings->has( 'intent' ) && strtoupper( (string) $this->settings->get( 'intent' ) ) === 'AUTHORIZE' ? 'AUTHORIZE' : 'CAPTURE'; $items = array( $this->purchase_unit_factory->from_wc_cart() ); @@ -230,6 +157,8 @@ class CaptureCardPayment implements EndpointInterface { ), ), ), + 'custom_id' => $custom_id, + 'invoice_id' => $invoice_id, ); $bearer = $this->bearer->bearer(); diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index f7521ad61..57197065f 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -131,6 +131,8 @@ return array( $vaulted_credit_card_handler, $container->get( 'onboarding.environment' ), $container->get( 'api.endpoint.order' ), + $container->get('save-payment-methods.endpoint.capture-card-payment'), + $container->get( 'api.prefix' ), $logger ); }, diff --git a/modules/ppcp-wc-gateway/src/Gateway/CreditCardGateway.php b/modules/ppcp-wc-gateway/src/Gateway/CreditCardGateway.php index 421c2675e..a15d80975 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/CreditCardGateway.php +++ b/modules/ppcp-wc-gateway/src/Gateway/CreditCardGateway.php @@ -19,6 +19,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException; use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException; use WooCommerce\PayPalCommerce\Onboarding\Environment; use WooCommerce\PayPalCommerce\Onboarding\State; +use WooCommerce\PayPalCommerce\SavePaymentMethods\Endpoint\CaptureCardPayment; use WooCommerce\PayPalCommerce\Session\SessionHandler; use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor; use WooCommerce\PayPalCommerce\WcGateway\Processor\PaymentsStatusHandlingTrait; @@ -139,6 +140,20 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC { */ private $order_endpoint; + /** + * Capture card payment. + * + * @var CaptureCardPayment + */ + private $capture_card_payment; + + /** + * The prefix. + * + * @var string + */ + private $prefix; + /** * The logger. * @@ -162,6 +177,8 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC { * @param VaultedCreditCardHandler $vaulted_credit_card_handler The vaulted credit card handler. * @param Environment $environment The environment. * @param OrderEndpoint $order_endpoint The order endpoint. + * @param CaptureCardPayment $capture_card_payment Capture card payment. + * @param string $prefix The prefix. * @param LoggerInterface $logger The logger. */ public function __construct( @@ -178,6 +195,8 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC { VaultedCreditCardHandler $vaulted_credit_card_handler, Environment $environment, OrderEndpoint $order_endpoint, + CaptureCardPayment $capture_card_payment, + string $prefix, LoggerInterface $logger ) { $this->id = self::ID; @@ -194,6 +213,8 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC { $this->vaulted_credit_card_handler = $vaulted_credit_card_handler; $this->environment = $environment; $this->order_endpoint = $order_endpoint; + $this->capture_card_payment = $capture_card_payment; + $this->prefix = $prefix; $this->logger = $logger; if ( $state->current_state() === State::STATE_ONBOARDED ) { @@ -390,19 +411,16 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC { ); } - $saved_payment_card = WC()->session->get( 'ppcp_saved_payment_card' ); - if ( $saved_payment_card ) { - if ( $saved_payment_card['payment_source'] === 'card' && $saved_payment_card['status'] === 'COMPLETED' ) { - $wc_order->update_meta_data( PayPalGateway::ORDER_ID_META_KEY, $saved_payment_card['order_id'] ); - $wc_order->update_meta_data( - PayPalGateway::ORDER_PAYMENT_MODE_META_KEY, - $this->environment->current_environment_is( Environment::SANDBOX ) ? 'sandbox' : 'live' - ); - $wc_order->save_meta_data(); + $card_payment_token_id = wc_clean( wp_unslash( $_POST['wc-ppcp-credit-card-gateway-payment-token'] ?? '' ) ); + if($card_payment_token_id) { + $tokens = WC_Payment_Tokens::get_customer_tokens( get_current_user_id() ); + foreach ( $tokens as $token ) { + if ( $token->get_id() === (int) $card_payment_token_id ) { + $custom_id = $wc_order->get_order_number(); + $invoice_id = $this->prefix . $wc_order->get_order_number(); + $create_order = $this->capture_card_payment->create_order($token->get_token(), $custom_id, $invoice_id); - $order_id = $saved_payment_card['order_id'] ?? ''; - if ( $order_id ) { - $order = $this->order_endpoint->order( $order_id ); + $order = $this->order_endpoint->order( $create_order->id ); $wc_order->update_meta_data( PayPalGateway::INTENT_META_KEY, $order->intent() ); if ( $order->intent() === 'AUTHORIZE' ) { @@ -421,14 +439,10 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC { } $this->handle_new_order_status( $order, $wc_order ); + + return $this->handle_payment_success( $wc_order ); } - - WC()->session->set( 'ppcp_saved_payment_card', null ); - - return $this->handle_payment_success( $wc_order ); } - - WC()->session->set( 'ppcp_saved_payment_card', null ); } /**