Add helper for updating card

This commit is contained in:
Emili Castells Guasch 2024-02-12 14:32:30 +01:00
parent 3994637220
commit 8f7c7b2eb2
4 changed files with 146 additions and 19 deletions

View file

@ -12,6 +12,7 @@ namespace WooCommerce\PayPalCommerce\SavePaymentMethods;
use WooCommerce\PayPalCommerce\SavePaymentMethods\Endpoint\CaptureCardPayment; use WooCommerce\PayPalCommerce\SavePaymentMethods\Endpoint\CaptureCardPayment;
use WooCommerce\PayPalCommerce\SavePaymentMethods\Endpoint\CreatePaymentToken; use WooCommerce\PayPalCommerce\SavePaymentMethods\Endpoint\CreatePaymentToken;
use WooCommerce\PayPalCommerce\SavePaymentMethods\Endpoint\CreateSetupToken; use WooCommerce\PayPalCommerce\SavePaymentMethods\Endpoint\CreateSetupToken;
use WooCommerce\PayPalCommerce\SavePaymentMethods\Helper\RealTimeAccountUpdaterHelper;
use WooCommerce\PayPalCommerce\SavePaymentMethods\Helper\SavePaymentMethodsApplies; use WooCommerce\PayPalCommerce\SavePaymentMethods\Helper\SavePaymentMethodsApplies;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface; use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
@ -723,6 +724,9 @@ return array(
$container->get( 'save-payment-methods.wc-payment-tokens' ) $container->get( 'save-payment-methods.wc-payment-tokens' )
); );
}, },
'save-payment-methods.helpers.real-time-account-updater' => static function ( ContainerInterface $container ) : RealTimeAccountUpdaterHelper {
return new RealTimeAccountUpdaterHelper();
},
'save-payment-methods.endpoint.capture-card-payment' => static function( ContainerInterface $container ): CaptureCardPayment { 'save-payment-methods.endpoint.capture-card-payment' => static function( ContainerInterface $container ): CaptureCardPayment {
return new CaptureCardPayment( return new CaptureCardPayment(
$container->get( 'button.request-data' ), $container->get( 'button.request-data' ),
@ -732,6 +736,7 @@ return array(
$container->get( 'api.factory.purchase-unit' ), $container->get( 'api.factory.purchase-unit' ),
$container->get( 'api.endpoint.order' ), $container->get( 'api.endpoint.order' ),
$container->get( 'session.handler' ), $container->get( 'session.handler' ),
$container->get( 'save-payment-methods.helpers.real-time-account-updater' ),
$container->get( 'woocommerce.logger.woocommerce' ) $container->get( 'woocommerce.logger.woocommerce' )
); );
}, },

View file

@ -21,6 +21,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Factory\OrderFactory;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory; use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;
use WooCommerce\PayPalCommerce\Button\Endpoint\EndpointInterface; use WooCommerce\PayPalCommerce\Button\Endpoint\EndpointInterface;
use WooCommerce\PayPalCommerce\Button\Endpoint\RequestData; use WooCommerce\PayPalCommerce\Button\Endpoint\RequestData;
use WooCommerce\PayPalCommerce\SavePaymentMethods\Helper\RealTimeAccountUpdaterHelper;
use WooCommerce\PayPalCommerce\Session\SessionHandler; use WooCommerce\PayPalCommerce\Session\SessionHandler;
use WP_Error; use WP_Error;
@ -82,6 +83,13 @@ class CaptureCardPayment implements EndpointInterface {
*/ */
private $session_handler; private $session_handler;
/**
* Real Time Account Updater helper.
*
* @var RealTimeAccountUpdaterHelper
*/
private $real_time_account_updater_helper;
/** /**
* The logger. * The logger.
* *
@ -92,14 +100,15 @@ class CaptureCardPayment implements EndpointInterface {
/** /**
* CaptureCardPayment constructor. * CaptureCardPayment constructor.
* *
* @param RequestData $request_data The request data. * @param RequestData $request_data The request data.
* @param string $host The host. * @param string $host The host.
* @param Bearer $bearer The bearer. * @param Bearer $bearer The bearer.
* @param OrderFactory $order_factory The order factory. * @param OrderFactory $order_factory The order factory.
* @param PurchaseUnitFactory $purchase_unit_factory The purchase unit factory. * @param PurchaseUnitFactory $purchase_unit_factory The purchase unit factory.
* @param OrderEndpoint $order_endpoint The order endpoint. * @param OrderEndpoint $order_endpoint The order endpoint.
* @param SessionHandler $session_handler The session handler. * @param SessionHandler $session_handler The session handler.
* @param LoggerInterface $logger The logger. * @param RealTimeAccountUpdaterHelper $real_time_account_updater_helper Real Time Account Updater helper.
* @param LoggerInterface $logger The logger.
*/ */
public function __construct( public function __construct(
RequestData $request_data, RequestData $request_data,
@ -109,16 +118,18 @@ class CaptureCardPayment implements EndpointInterface {
PurchaseUnitFactory $purchase_unit_factory, PurchaseUnitFactory $purchase_unit_factory,
OrderEndpoint $order_endpoint, OrderEndpoint $order_endpoint,
SessionHandler $session_handler, SessionHandler $session_handler,
RealTimeAccountUpdaterHelper $real_time_account_updater_helper,
LoggerInterface $logger LoggerInterface $logger
) { ) {
$this->request_data = $request_data; $this->request_data = $request_data;
$this->host = $host; $this->host = $host;
$this->bearer = $bearer; $this->bearer = $bearer;
$this->order_factory = $order_factory; $this->order_factory = $order_factory;
$this->purchase_unit_factory = $purchase_unit_factory; $this->purchase_unit_factory = $purchase_unit_factory;
$this->order_endpoint = $order_endpoint; $this->order_endpoint = $order_endpoint;
$this->logger = $logger; $this->session_handler = $session_handler;
$this->session_handler = $session_handler; $this->real_time_account_updater_helper = $real_time_account_updater_helper;
$this->logger = $logger;
} }
/** /**
@ -148,6 +159,8 @@ class CaptureCardPayment implements EndpointInterface {
$status = $order->status ?? ''; $status = $order->status ?? '';
$payment_source = isset( $order->payment_source->card ) ? 'card' : ''; $payment_source = isset( $order->payment_source->card ) ? 'card' : '';
if ( $id && $status && $payment_source ) { if ( $id && $status && $payment_source ) {
$this->real_time_account_updater_helper->update_wc_token_from_paypal_response( $order, $token );
WC()->session->set( WC()->session->set(
'ppcp_saved_payment_card', 'ppcp_saved_payment_card',
array( array(
@ -191,11 +204,11 @@ class CaptureCardPayment implements EndpointInterface {
), ),
'payment_source' => array( 'payment_source' => array(
'card' => array( 'card' => array(
'vault_id' => $vault_id, 'vault_id' => $vault_id,
'stored_credential' => array( 'stored_credential' => array(
'payment_initiator' => 'CUSTOMER', 'payment_initiator' => 'CUSTOMER',
'payment_type' => 'UNSCHEDULED', 'payment_type' => 'UNSCHEDULED',
'usage' => 'SUBSEQUENT' 'usage' => 'SUBSEQUENT',
), ),
), ),
), ),

View file

@ -0,0 +1,45 @@
<?php
/**
* Real Time Account Updater helper class.
*
* @package WooCommerce\PayPalCommerce\SavePaymentMethods\Helper
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\SavePaymentMethods\Helper;
use stdClass;
use WC_Payment_Token;
/**
* Class RealTimeAccountUpdaterHelper
*/
class RealTimeAccountUpdaterHelper {
/**
* Updates WC Payment Token from PayPal response.
*
* @param stdClass $order PayPal order response data.
* @param WC_Payment_Token $token WC Payment Token.
* @return void
*/
public function update_wc_token_from_paypal_response( stdClass $order, WC_Payment_Token $token ): void {
$expiry = $order->payment_source->card->expiry ?? '';
$wc_expiry = $token->get_expiry_month() . '-' . $token->get_expiry_year();
if ( $expiry !== $wc_expiry ) {
$expiry_split = explode( '-', $expiry );
$token->set_expiry_year( $expiry_split[0] );
$token->set_expiry_month( $expiry_split[1] );
$token->save();
}
$last_digits = $order->payment_source->card->last_digits ?? '';
$wc_last_digits = $token->get_last4();
if ( $last_digits !== $wc_last_digits ) {
$token->set_last4( $last_digits );
$token->save();
}
}
}

View file

@ -0,0 +1,64 @@
<?php
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Tests\E2e;
use WC_Payment_Token_CC;
use WC_Payment_Tokens;
use WooCommerce\PayPalCommerce\SavePaymentMethods\Helper\RealTimeAccountUpdaterHelper;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
class RealTimeAccountUpdaterTest extends TestCase
{
public function tearDown(): void
{
$tokens = WC_Payment_Tokens::get_customer_tokens(1, CreditCardGateway::ID);
foreach ($tokens as $token) {
$token->delete();
}
parent::tearDown();
}
public function testUpdateCard()
{
$response = (object)[
'payment_source' => (object)[
'card' => (object)[
'last_digits' => '0004',
'expiry' => '2042-02',
'brand' => 'VISA',
]
]
];
$token = $this->createToken();
(new RealTimeAccountUpdaterHelper())->update_wc_token_from_paypal_response($response, $token);
$this->assertTrue($token->get_expiry_year() === '2042');
$this->assertTrue($token->get_expiry_month() === '02');
$this->assertTrue($token->get_last4() === '0004');
}
/**
* @return WC_Payment_Token_CC
*/
private function createToken(): \WC_Payment_Token_CC
{
$token = new WC_Payment_Token_CC();
$token->set_token('abc123');
$token->set_user_id(1);
$token->set_gateway_id(CreditCardGateway::ID);
$token->set_last4('1234');
$token->set_expiry_month('01');
$token->set_expiry_year('2025');
$token->set_card_type('VISA');
$token->save();
return $token;
}
}