From 018b497611f1e8bb28d30702ebac186a5a1179cb Mon Sep 17 00:00:00 2001 From: Emili Castells Guasch Date: Mon, 8 Jan 2024 14:37:51 +0100 Subject: [PATCH] Move create wc card payment method to helper class --- .../src/Endpoint/CreatePaymentToken.php | 22 +++--------- .../src/WooCommercePaymentTokens.php | 35 +++++++++++++++++++ 2 files changed, 39 insertions(+), 18 deletions(-) diff --git a/modules/ppcp-save-payment-methods/src/Endpoint/CreatePaymentToken.php b/modules/ppcp-save-payment-methods/src/Endpoint/CreatePaymentToken.php index 6698c4340..61fcae9da 100644 --- a/modules/ppcp-save-payment-methods/src/Endpoint/CreatePaymentToken.php +++ b/modules/ppcp-save-payment-methods/src/Endpoint/CreatePaymentToken.php @@ -98,7 +98,8 @@ class CreatePaymentToken implements EndpointInterface { $result = $this->payment_method_tokens_endpoint->payment_tokens( $payment_source ); if ( is_user_logged_in() && isset( $result->customer->id ) ) { - update_user_meta( get_current_user_id(), '_ppcp_target_customer_id', $result->customer->id ); + $current_user_id = get_current_user_id(); + update_user_meta( $current_user_id, '_ppcp_target_customer_id', $result->customer->id ); if ( isset( $result->payment_source->paypal ) ) { $email = ''; @@ -107,29 +108,14 @@ class CreatePaymentToken implements EndpointInterface { } $this->wc_payment_tokens->create_payment_token_paypal( - get_current_user_id(), + $current_user_id, $result->id, $email ); } if ( isset( $result->payment_source->card ) ) { - $token = new \WC_Payment_Token_CC(); - $token->set_token( $result->id ); - $token->set_user_id( get_current_user_id() ); - $token->set_gateway_id( CreditCardGateway::ID ); - - $token->set_last4( $result->payment_source->card->last_digits ?? '' ); - $expiry = explode( '-', $result->payment_source->card->expiry ?? '' ); - $token->set_expiry_year( $expiry[0] ?? '' ); - $token->set_expiry_month( $expiry[1] ?? '' ); - - $brand = $result->payment_source->card->brand ?? __( 'N/A', 'woocommerce-paypal-payments' ); - if ( $brand ) { - $token->set_card_type( $brand ); - } - - $token->save(); + $this->wc_payment_tokens->create_payment_token_card( $current_user_id, $result ); } } diff --git a/modules/ppcp-save-payment-methods/src/WooCommercePaymentTokens.php b/modules/ppcp-save-payment-methods/src/WooCommercePaymentTokens.php index 4fb066b6e..d9e13ce33 100644 --- a/modules/ppcp-save-payment-methods/src/WooCommercePaymentTokens.php +++ b/modules/ppcp-save-payment-methods/src/WooCommercePaymentTokens.php @@ -11,10 +11,13 @@ namespace WooCommerce\PayPalCommerce\SavePaymentMethods; use Exception; use Psr\Log\LoggerInterface; +use stdClass; +use WC_Payment_Token_CC; use WC_Payment_Tokens; use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenFactory; use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenHelper; use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenPayPal; +use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway; use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway; /** @@ -99,4 +102,36 @@ class WooCommercePaymentTokens { ); } } + + /** + * Creates a WC Payment Token for Credit Card payment. + * + * @param int $customer_id The WC customer ID. + * @param stdClass $payment_token The Credit Card payment token. + * + * @return void + */ + public function create_payment_token_card( int $customer_id, stdClass $payment_token ): void { + $wc_tokens = WC_Payment_Tokens::get_customer_tokens( $customer_id, CreditCardGateway::ID ); + if ( $this->payment_token_helper->token_exist( $wc_tokens, $payment_token->id ) ) { + return; + } + + $token = new WC_Payment_Token_CC(); + $token->set_token( $payment_token->id ); + $token->set_user_id( get_current_user_id() ); + $token->set_gateway_id( CreditCardGateway::ID ); + + $token->set_last4( $payment_token->payment_source->card->last_digits ?? '' ); + $expiry = explode( '-', $payment_token->payment_source->card->expiry ?? '' ); + $token->set_expiry_year( $expiry[0] ?? '' ); + $token->set_expiry_month( $expiry[1] ?? '' ); + + $brand = $payment_token->payment_source->card->brand ?? __( 'N/A', 'woocommerce-paypal-payments' ); + if ( $brand ) { + $token->set_card_type( $brand ); + } + + $token->save(); + } }