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( 'session.handler' ),
$container->get( 'wc-subscriptions.helpers.real-time-account-updater' ),
$container->get( 'wcgateway.settings' ),
$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\RequestData;
use WooCommerce\PayPalCommerce\Session\SessionHandler;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\RealTimeAccountUpdaterHelper;
use WP_Error;
@ -90,6 +91,13 @@ class CaptureCardPayment implements EndpointInterface {
*/
private $real_time_account_updater_helper;
/**
* The settings.
*
* @var Settings
*/
private $settings;
/**
* The logger.
*
@ -108,6 +116,7 @@ class CaptureCardPayment implements EndpointInterface {
* @param OrderEndpoint $order_endpoint The order endpoint.
* @param SessionHandler $session_handler The session handler.
* @param RealTimeAccountUpdaterHelper $real_time_account_updater_helper Real Time Account Updater helper.
* @param Settings $settings The settings.
* @param LoggerInterface $logger The logger.
*/
public function __construct(
@ -119,6 +128,7 @@ class CaptureCardPayment implements EndpointInterface {
OrderEndpoint $order_endpoint,
SessionHandler $session_handler,
RealTimeAccountUpdaterHelper $real_time_account_updater_helper,
Settings $settings,
LoggerInterface $logger
) {
$this->request_data = $request_data;
@ -130,6 +140,7 @@ class CaptureCardPayment implements EndpointInterface {
$this->session_handler = $session_handler;
$this->real_time_account_updater_helper = $real_time_account_updater_helper;
$this->logger = $logger;
$this->settings = $settings;
}
/**
@ -198,10 +209,11 @@ class CaptureCardPayment implements EndpointInterface {
* @throws RuntimeException When request fails.
*/
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(
'intent' => 'CAPTURE',
'intent' => $intent,
'purchase_units' => array_map(
static function ( PurchaseUnit $item ): array {
return $item->to_array( true, false );

View file

@ -127,9 +127,11 @@ return array(
$state,
$transaction_url_provider,
$subscription_helper,
$logger,
$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 {

View file

@ -13,11 +13,15 @@ use Exception;
use Psr\Log\LoggerInterface;
use WC_Order;
use WC_Payment_Tokens;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentsEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\Onboarding\State;
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\WcSubscriptions\Helper\SubscriptionHelper;
use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenRepository;
@ -33,7 +37,7 @@ use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
*/
class CreditCardGateway extends \WC_Payment_Gateway_CC {
use ProcessPaymentTrait, GatewaySettingsRendererTrait, TransactionIdHandlingTrait;
use ProcessPaymentTrait, GatewaySettingsRendererTrait, TransactionIdHandlingTrait, PaymentsStatusHandlingTrait;
const ID = 'ppcp-credit-card-gateway';
@ -114,13 +118,6 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
*/
protected $subscription_helper;
/**
* The logger.
*
* @var LoggerInterface
*/
protected $logger;
/**
* The payments endpoint
*
@ -128,6 +125,27 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
*/
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.
*
@ -140,9 +158,11 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
* @param State $state The state.
* @param TransactionUrlProvider $transaction_url_provider Service able to provide view transaction url base.
* @param SubscriptionHelper $subscription_helper The subscription helper.
* @param LoggerInterface $logger The logger.
* @param PaymentsEndpoint $payments_endpoint The payments endpoint.
* @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(
SettingsRenderer $settings_renderer,
@ -154,9 +174,11 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
State $state,
TransactionUrlProvider $transaction_url_provider,
SubscriptionHelper $subscription_helper,
LoggerInterface $logger,
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->settings_renderer = $settings_renderer;
@ -168,9 +190,11 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
$this->state = $state;
$this->transaction_url_provider = $transaction_url_provider;
$this->subscription_helper = $subscription_helper;
$this->logger = $logger;
$this->payments_endpoint = $payments_endpoint;
$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 ) {
$this->supports = array( 'refunds' );
@ -370,14 +394,41 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
if ( $saved_payment_card ) {
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_PAYMENT_MODE_META_KEY,
$this->environment->current_environment_is( Environment::SANDBOX ) ? 'sandbox' : 'live'
);
$wc_order->save_meta_data();
$this->update_transaction_id( $saved_payment_card['order_id'], $wc_order );
$wc_order->payment_complete();
$order_id = $saved_payment_card['order_id'] ?? '';
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 );
return $this->handle_payment_success( $wc_order );
}
WC()->session->set( 'ppcp_saved_payment_card', null );
}
/**