Move create wc card payment method to helper class

This commit is contained in:
Emili Castells Guasch 2024-01-08 14:37:51 +01:00
parent dc9ad87b3e
commit 018b497611
2 changed files with 39 additions and 18 deletions

View file

@ -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 );
}
}

View file

@ -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();
}
}