Merge branch 'trunk' into PCP-1968-subscriptions-api-renewals

This commit is contained in:
Emili Castells Guasch 2023-09-12 09:36:51 +02:00
commit 250172a2ad
25 changed files with 301 additions and 68 deletions

View file

@ -80,6 +80,7 @@ return array(
$order_endpoint = $container->get( 'api.endpoint.order' );
$authorized_payments_processor = $container->get( 'wcgateway.processor.authorized-payments' );
$payment_token_factory = $container->get( 'vaulting.payment-token-factory' );
$payment_token_helper = $container->get( 'vaulting.payment-token-helper' );
$refund_fees_updater = $container->get( 'wcgateway.helper.refund-fees-updater' );
return array(
@ -95,7 +96,7 @@ return array(
new PaymentCaptureRefunded( $logger, $refund_fees_updater ),
new PaymentCaptureReversed( $logger ),
new PaymentCaptureCompleted( $logger, $order_endpoint ),
new VaultPaymentTokenCreated( $logger, $prefix, $authorized_payments_processor, $payment_token_factory ),
new VaultPaymentTokenCreated( $logger, $prefix, $authorized_payments_processor, $payment_token_factory, $payment_token_helper ),
new VaultPaymentTokenDeleted( $logger ),
new PaymentCapturePending( $logger ),
new PaymentSaleCompleted( $logger ),

View file

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