mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 18:16:38 +08:00
Ensure WC payment token does not exist before creating it
This commit is contained in:
parent
fa60c3c8f5
commit
e819dc68df
5 changed files with 93 additions and 45 deletions
|
@ -13,6 +13,7 @@ use Psr\Log\LoggerInterface;
|
|||
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;
|
||||
|
@ -54,6 +55,13 @@ class VaultPaymentTokenCreated implements RequestHandler {
|
|||
*/
|
||||
protected $payment_token_factory;
|
||||
|
||||
/**
|
||||
* The payment token helper.
|
||||
*
|
||||
* @var PaymentTokenHelper
|
||||
*/
|
||||
private $payment_token_helper;
|
||||
|
||||
/**
|
||||
* VaultPaymentTokenCreated constructor.
|
||||
*
|
||||
|
@ -61,17 +69,20 @@ class VaultPaymentTokenCreated implements RequestHandler {
|
|||
* @param string $prefix The prefix.
|
||||
* @param AuthorizedPaymentsProcessor $authorized_payments_processor The authorized payment processor.
|
||||
* @param PaymentTokenFactory $payment_token_factory The payment token factory.
|
||||
* @param PaymentTokenHelper $payment_token_helper The payment token helper.
|
||||
*/
|
||||
public function __construct(
|
||||
LoggerInterface $logger,
|
||||
string $prefix,
|
||||
AuthorizedPaymentsProcessor $authorized_payments_processor,
|
||||
PaymentTokenFactory $payment_token_factory
|
||||
PaymentTokenFactory $payment_token_factory,
|
||||
PaymentTokenHelper $payment_token_helper
|
||||
) {
|
||||
$this->logger = $logger;
|
||||
$this->prefix = $prefix;
|
||||
$this->authorized_payments_processor = $authorized_payments_processor;
|
||||
$this->payment_token_factory = $payment_token_factory;
|
||||
$this->payment_token_helper = $payment_token_helper;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -123,33 +134,39 @@ class VaultPaymentTokenCreated implements RequestHandler {
|
|||
|
||||
if ( ! is_null( $request['resource'] ) && isset( $request['resource']['id'] ) ) {
|
||||
if ( ! is_null( $request['resource']['source'] ) && isset( $request['resource']['source']['card'] ) ) {
|
||||
$token = new WC_Payment_Token_CC();
|
||||
$token->set_token( $request['resource']['id'] );
|
||||
$token->set_user_id( $wc_customer_id );
|
||||
$token->set_gateway_id( CreditCardGateway::ID );
|
||||
$wc_tokens = WC_Payment_Tokens::get_customer_tokens( $wc_customer_id, CreditCardGateway::ID );
|
||||
if ( ! $this->payment_token_helper->token_exist( $wc_tokens, $request['resource']['id'] ) ) {
|
||||
$token = new WC_Payment_Token_CC();
|
||||
$token->set_token( $request['resource']['id'] );
|
||||
$token->set_user_id( $wc_customer_id );
|
||||
$token->set_gateway_id( CreditCardGateway::ID );
|
||||
|
||||
$token->set_last4( $request['resource']['source']['card']['last_digits'] ?? '' );
|
||||
$expiry = explode( '-', $request['resource']['source']['card']['expiry'] ?? '' );
|
||||
$token->set_expiry_year( $expiry[0] ?? '' );
|
||||
$token->set_expiry_month( $expiry[1] ?? '' );
|
||||
$token->set_card_type( $request['resource']['source']['card']['brand'] ?? '' );
|
||||
$token->save();
|
||||
WC_Payment_Tokens::set_users_default( $wc_customer_id, $token->get_id() );
|
||||
} elseif ( isset( $request['resource']['source']['paypal'] ) ) {
|
||||
$payment_token_paypal = $this->payment_token_factory->create( 'paypal' );
|
||||
assert( $payment_token_paypal instanceof PaymentTokenPayPal );
|
||||
|
||||
$payment_token_paypal->set_token( $request['resource']['id'] );
|
||||
$payment_token_paypal->set_user_id( $wc_customer_id );
|
||||
$payment_token_paypal->set_gateway_id( PayPalGateway::ID );
|
||||
|
||||
$email = $request['resource']['source']['paypal']['payer']['email_address'] ?? '';
|
||||
if ( $email && is_email( $email ) ) {
|
||||
$payment_token_paypal->set_email( $email );
|
||||
$token->set_last4( $request['resource']['source']['card']['last_digits'] ?? '' );
|
||||
$expiry = explode( '-', $request['resource']['source']['card']['expiry'] ?? '' );
|
||||
$token->set_expiry_year( $expiry[0] ?? '' );
|
||||
$token->set_expiry_month( $expiry[1] ?? '' );
|
||||
$token->set_card_type( $request['resource']['source']['card']['brand'] ?? '' );
|
||||
$token->save();
|
||||
WC_Payment_Tokens::set_users_default( $wc_customer_id, $token->get_id() );
|
||||
}
|
||||
} elseif ( isset( $request['resource']['source']['paypal'] ) ) {
|
||||
$wc_tokens = WC_Payment_Tokens::get_customer_tokens( $wc_customer_id, PayPalGateway::ID );
|
||||
if ( ! $this->payment_token_helper->token_exist( $wc_tokens, $request['resource']['id'] ) ) {
|
||||
$payment_token_paypal = $this->payment_token_factory->create( 'paypal' );
|
||||
assert( $payment_token_paypal instanceof PaymentTokenPayPal );
|
||||
|
||||
$payment_token_paypal->save();
|
||||
WC_Payment_Tokens::set_users_default( $wc_customer_id, $payment_token_paypal->get_id() );
|
||||
$payment_token_paypal->set_token( $request['resource']['id'] );
|
||||
$payment_token_paypal->set_user_id( $wc_customer_id );
|
||||
$payment_token_paypal->set_gateway_id( PayPalGateway::ID );
|
||||
|
||||
$email = $request['resource']['source']['paypal']['payer']['email_address'] ?? '';
|
||||
if ( $email && is_email( $email ) ) {
|
||||
$payment_token_paypal->set_email( $email );
|
||||
}
|
||||
|
||||
$payment_token_paypal->save();
|
||||
WC_Payment_Tokens::set_users_default( $wc_customer_id, $payment_token_paypal->get_id() );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue