Merge pull request #2069 from woocommerce/PCP-2713-payments-with-saved-card-token-uses-capture-intent-when-authorize-is-configured

Payments with saved card tokens use Capture intent when Authorize is configured (2713)
This commit is contained in:
Emili Castells 2024-03-06 14:50:19 +01:00 committed by GitHub
commit 8d00b04d2e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 94 additions and 20 deletions

View file

@ -829,6 +829,7 @@ return array(
$container->get( 'api.endpoint.order' ), $container->get( 'api.endpoint.order' ),
$container->get( 'session.handler' ), $container->get( 'session.handler' ),
$container->get( 'wc-subscriptions.helpers.real-time-account-updater' ), $container->get( 'wc-subscriptions.helpers.real-time-account-updater' ),
$container->get( 'wcgateway.settings' ),
$container->get( 'woocommerce.logger.woocommerce' ) $container->get( 'woocommerce.logger.woocommerce' )
); );
}, },

View file

@ -22,6 +22,7 @@ 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\Session\SessionHandler; use WooCommerce\PayPalCommerce\Session\SessionHandler;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\RealTimeAccountUpdaterHelper; use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\RealTimeAccountUpdaterHelper;
use WP_Error; use WP_Error;
@ -90,6 +91,13 @@ class CaptureCardPayment implements EndpointInterface {
*/ */
private $real_time_account_updater_helper; private $real_time_account_updater_helper;
/**
* The settings.
*
* @var Settings
*/
private $settings;
/** /**
* The logger. * The logger.
* *
@ -108,6 +116,7 @@ class CaptureCardPayment implements EndpointInterface {
* @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 RealTimeAccountUpdaterHelper $real_time_account_updater_helper Real Time Account Updater helper. * @param RealTimeAccountUpdaterHelper $real_time_account_updater_helper Real Time Account Updater helper.
* @param Settings $settings The settings.
* @param LoggerInterface $logger The logger. * @param LoggerInterface $logger The logger.
*/ */
public function __construct( public function __construct(
@ -119,6 +128,7 @@ class CaptureCardPayment implements EndpointInterface {
OrderEndpoint $order_endpoint, OrderEndpoint $order_endpoint,
SessionHandler $session_handler, SessionHandler $session_handler,
RealTimeAccountUpdaterHelper $real_time_account_updater_helper, RealTimeAccountUpdaterHelper $real_time_account_updater_helper,
Settings $settings,
LoggerInterface $logger LoggerInterface $logger
) { ) {
$this->request_data = $request_data; $this->request_data = $request_data;
@ -130,6 +140,7 @@ class CaptureCardPayment implements EndpointInterface {
$this->session_handler = $session_handler; $this->session_handler = $session_handler;
$this->real_time_account_updater_helper = $real_time_account_updater_helper; $this->real_time_account_updater_helper = $real_time_account_updater_helper;
$this->logger = $logger; $this->logger = $logger;
$this->settings = $settings;
} }
/** /**
@ -198,10 +209,11 @@ class CaptureCardPayment implements EndpointInterface {
* @throws RuntimeException When request fails. * @throws RuntimeException When request fails.
*/ */
private function create_order( string $vault_id ): stdClass { private function create_order( string $vault_id ): stdClass {
$items = array( $this->purchase_unit_factory->from_wc_cart() ); $intent = $this->settings->has( 'intent' ) && strtoupper( (string) $this->settings->get( 'intent' ) ) === 'AUTHORIZE' ? 'AUTHORIZE' : 'CAPTURE';
$items = array( $this->purchase_unit_factory->from_wc_cart() );
$data = array( $data = array(
'intent' => 'CAPTURE', 'intent' => $intent,
'purchase_units' => array_map( 'purchase_units' => array_map(
static function ( PurchaseUnit $item ): array { static function ( PurchaseUnit $item ): array {
return $item->to_array( true, false ); return $item->to_array( true, false );

View file

@ -127,9 +127,11 @@ return array(
$state, $state,
$transaction_url_provider, $transaction_url_provider,
$subscription_helper, $subscription_helper,
$logger,
$payments_endpoint, $payments_endpoint,
$vaulted_credit_card_handler $vaulted_credit_card_handler,
$container->get( 'onboarding.environment' ),
$container->get( 'api.endpoint.order' ),
$logger
); );
}, },
'wcgateway.card-button-gateway' => static function ( ContainerInterface $container ): CardButtonGateway { 'wcgateway.card-button-gateway' => static function ( ContainerInterface $container ): CardButtonGateway {

View file

@ -13,11 +13,15 @@ use Exception;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use WC_Order; use WC_Order;
use WC_Payment_Tokens; use WC_Payment_Tokens;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentsEndpoint; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentsEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException; use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException; use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\Onboarding\State; use WooCommerce\PayPalCommerce\Onboarding\State;
use WooCommerce\PayPalCommerce\Session\SessionHandler; use WooCommerce\PayPalCommerce\Session\SessionHandler;
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
use WooCommerce\PayPalCommerce\WcGateway\Processor\PaymentsStatusHandlingTrait;
use WooCommerce\PayPalCommerce\WcGateway\Processor\TransactionIdHandlingTrait; use WooCommerce\PayPalCommerce\WcGateway\Processor\TransactionIdHandlingTrait;
use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper; use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper;
use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenRepository; use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenRepository;
@ -33,7 +37,7 @@ use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
*/ */
class CreditCardGateway extends \WC_Payment_Gateway_CC { class CreditCardGateway extends \WC_Payment_Gateway_CC {
use ProcessPaymentTrait, GatewaySettingsRendererTrait, TransactionIdHandlingTrait; use ProcessPaymentTrait, GatewaySettingsRendererTrait, TransactionIdHandlingTrait, PaymentsStatusHandlingTrait;
const ID = 'ppcp-credit-card-gateway'; const ID = 'ppcp-credit-card-gateway';
@ -114,13 +118,6 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
*/ */
protected $subscription_helper; protected $subscription_helper;
/**
* The logger.
*
* @var LoggerInterface
*/
protected $logger;
/** /**
* The payments endpoint * The payments endpoint
* *
@ -128,6 +125,27 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
*/ */
protected $payments_endpoint; protected $payments_endpoint;
/**
* The environment.
*
* @var Environment
*/
private $environment;
/**
* The order endpoint.
*
* @var OrderEndpoint
*/
private $order_endpoint;
/**
* The logger.
*
* @var LoggerInterface
*/
protected $logger;
/** /**
* CreditCardGateway constructor. * CreditCardGateway constructor.
* *
@ -140,9 +158,11 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
* @param State $state The state. * @param State $state The state.
* @param TransactionUrlProvider $transaction_url_provider Service able to provide view transaction url base. * @param TransactionUrlProvider $transaction_url_provider Service able to provide view transaction url base.
* @param SubscriptionHelper $subscription_helper The subscription helper. * @param SubscriptionHelper $subscription_helper The subscription helper.
* @param LoggerInterface $logger The logger.
* @param PaymentsEndpoint $payments_endpoint The payments endpoint. * @param PaymentsEndpoint $payments_endpoint The payments endpoint.
* @param VaultedCreditCardHandler $vaulted_credit_card_handler The vaulted credit card handler. * @param VaultedCreditCardHandler $vaulted_credit_card_handler The vaulted credit card handler.
* @param Environment $environment The environment.
* @param OrderEndpoint $order_endpoint The order endpoint.
* @param LoggerInterface $logger The logger.
*/ */
public function __construct( public function __construct(
SettingsRenderer $settings_renderer, SettingsRenderer $settings_renderer,
@ -154,9 +174,11 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
State $state, State $state,
TransactionUrlProvider $transaction_url_provider, TransactionUrlProvider $transaction_url_provider,
SubscriptionHelper $subscription_helper, SubscriptionHelper $subscription_helper,
LoggerInterface $logger,
PaymentsEndpoint $payments_endpoint, PaymentsEndpoint $payments_endpoint,
VaultedCreditCardHandler $vaulted_credit_card_handler VaultedCreditCardHandler $vaulted_credit_card_handler,
Environment $environment,
OrderEndpoint $order_endpoint,
LoggerInterface $logger
) { ) {
$this->id = self::ID; $this->id = self::ID;
$this->settings_renderer = $settings_renderer; $this->settings_renderer = $settings_renderer;
@ -168,9 +190,11 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
$this->state = $state; $this->state = $state;
$this->transaction_url_provider = $transaction_url_provider; $this->transaction_url_provider = $transaction_url_provider;
$this->subscription_helper = $subscription_helper; $this->subscription_helper = $subscription_helper;
$this->logger = $logger;
$this->payments_endpoint = $payments_endpoint; $this->payments_endpoint = $payments_endpoint;
$this->vaulted_credit_card_handler = $vaulted_credit_card_handler; $this->vaulted_credit_card_handler = $vaulted_credit_card_handler;
$this->environment = $environment;
$this->order_endpoint = $order_endpoint;
$this->logger = $logger;
if ( $state->current_state() === State::STATE_ONBOARDED ) { if ( $state->current_state() === State::STATE_ONBOARDED ) {
$this->supports = array( 'refunds' ); $this->supports = array( 'refunds' );
@ -370,14 +394,41 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
if ( $saved_payment_card ) { if ( $saved_payment_card ) {
if ( $saved_payment_card['payment_source'] === 'card' && $saved_payment_card['status'] === 'COMPLETED' ) { if ( $saved_payment_card['payment_source'] === 'card' && $saved_payment_card['status'] === 'COMPLETED' ) {
$wc_order->update_meta_data( PayPalGateway::ORDER_ID_META_KEY, $saved_payment_card['order_id'] ); $wc_order->update_meta_data( PayPalGateway::ORDER_ID_META_KEY, $saved_payment_card['order_id'] );
$wc_order->update_meta_data(
PayPalGateway::ORDER_PAYMENT_MODE_META_KEY,
$this->environment->current_environment_is( Environment::SANDBOX ) ? 'sandbox' : 'live'
);
$wc_order->save_meta_data(); $wc_order->save_meta_data();
$this->update_transaction_id( $saved_payment_card['order_id'], $wc_order ); $order_id = $saved_payment_card['order_id'] ?? '';
$wc_order->payment_complete(); if ( $order_id ) {
$order = $this->order_endpoint->order( $order_id );
$wc_order->update_meta_data( PayPalGateway::INTENT_META_KEY, $order->intent() );
if ( $order->intent() === 'AUTHORIZE' ) {
$order = $this->order_endpoint->authorize( $order );
$wc_order->update_meta_data( AuthorizedPaymentsProcessor::CAPTURED_META_KEY, 'false' );
if ( $this->subscription_helper->has_subscription( $wc_order->get_id() ) ) {
$wc_order->update_meta_data( '_ppcp_captured_vault_webhook', 'false' );
}
}
$transaction_id = $this->get_paypal_order_transaction_id( $order );
if ( $transaction_id ) {
$this->update_transaction_id( $transaction_id, $wc_order );
}
$this->handle_new_order_status( $order, $wc_order );
}
WC()->session->set( 'ppcp_saved_payment_card', null ); WC()->session->set( 'ppcp_saved_payment_card', null );
return $this->handle_payment_success( $wc_order ); return $this->handle_payment_success( $wc_order );
} }
WC()->session->set( 'ppcp_saved_payment_card', null );
} }
/** /**

View file

@ -4,6 +4,8 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\WcGateway\Gateway; namespace WooCommerce\PayPalCommerce\WcGateway\Gateway;
use Mockery; use Mockery;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface; use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use WC_Order; use WC_Order;
@ -32,6 +34,8 @@ class CreditCardGatewayTest extends TestCase
private $logger; private $logger;
private $paymentsEndpoint; private $paymentsEndpoint;
private $vaultedCreditCardHandler; private $vaultedCreditCardHandler;
private $environment;
private $orderEndpoint;
private $testee; private $testee;
public function setUp(): void public function setUp(): void
@ -50,6 +54,8 @@ class CreditCardGatewayTest extends TestCase
$this->logger = Mockery::mock(LoggerInterface::class); $this->logger = Mockery::mock(LoggerInterface::class);
$this->paymentsEndpoint = Mockery::mock(PaymentsEndpoint::class); $this->paymentsEndpoint = Mockery::mock(PaymentsEndpoint::class);
$this->vaultedCreditCardHandler = Mockery::mock(VaultedCreditCardHandler::class); $this->vaultedCreditCardHandler = Mockery::mock(VaultedCreditCardHandler::class);
$this->environment = Mockery::mock(Environment::class);
$this->orderEndpoint = Mockery::mock(OrderEndpoint::class);
$this->state->shouldReceive('current_state')->andReturn(State::STATE_ONBOARDED); $this->state->shouldReceive('current_state')->andReturn(State::STATE_ONBOARDED);
$this->config->shouldReceive('has')->andReturn(true); $this->config->shouldReceive('has')->andReturn(true);
@ -67,9 +73,11 @@ class CreditCardGatewayTest extends TestCase
$this->state, $this->state,
$this->transactionUrlProvider, $this->transactionUrlProvider,
$this->subscriptionHelper, $this->subscriptionHelper,
$this->logger,
$this->paymentsEndpoint, $this->paymentsEndpoint,
$this->vaultedCreditCardHandler $this->vaultedCreditCardHandler,
$this->environment,
$this->orderEndpoint,
$this->logger
); );
} }