Skip to content

Commit

Permalink
Merge pull request woocommerce#2069 from woocommerce/PCP-2713-payment…
Browse files Browse the repository at this point in the history
…s-with-saved-card-token-uses-capture-intent-when-authorize-is-configured

Payments with saved card tokens use Capture intent when Authorize is configured (2713)
  • Loading branch information
Dinamiko authored Mar 6, 2024
2 parents a6df228 + 2d1e8d0 commit 8d00b04
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 19 deletions.
1 change: 1 addition & 0 deletions modules/ppcp-save-payment-methods/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,7 @@
$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' )
);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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;

Expand Down Expand Up @@ -90,6 +91,13 @@ class CaptureCardPayment implements EndpointInterface {
*/
private $real_time_account_updater_helper;

/**
* The settings.
*
* @var Settings
*/
private $settings;

/**
* The logger.
*
Expand All @@ -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(
Expand All @@ -119,6 +128,7 @@ public function __construct(
OrderEndpoint $order_endpoint,
SessionHandler $session_handler,
RealTimeAccountUpdaterHelper $real_time_account_updater_helper,
Settings $settings,
LoggerInterface $logger
) {
$this->request_data = $request_data;
Expand All @@ -130,6 +140,7 @@ public function __construct(
$this->session_handler = $session_handler;
$this->real_time_account_updater_helper = $real_time_account_updater_helper;
$this->logger = $logger;
$this->settings = $settings;
}

/**
Expand Down Expand Up @@ -198,10 +209,11 @@ public function handle_request(): bool {
* @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 );
Expand Down
6 changes: 4 additions & 2 deletions modules/ppcp-wc-gateway/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,11 @@
$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 {
Expand Down
77 changes: 64 additions & 13 deletions modules/ppcp-wc-gateway/src/Gateway/CreditCardGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@
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;
Expand All @@ -33,7 +37,7 @@
*/
class CreditCardGateway extends \WC_Payment_Gateway_CC {

use ProcessPaymentTrait, GatewaySettingsRendererTrait, TransactionIdHandlingTrait;
use ProcessPaymentTrait, GatewaySettingsRendererTrait, TransactionIdHandlingTrait, PaymentsStatusHandlingTrait;

const ID = 'ppcp-credit-card-gateway';

Expand Down Expand Up @@ -115,18 +119,32 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
protected $subscription_helper;

/**
* The logger.
* The payments endpoint
*
* @var LoggerInterface
* @var PaymentsEndpoint
*/
protected $logger;
protected $payments_endpoint;

/**
* The payments endpoint
* The environment.
*
* @var PaymentsEndpoint
* @var Environment
*/
protected $payments_endpoint;
private $environment;

/**
* The order endpoint.
*
* @var OrderEndpoint
*/
private $order_endpoint;

/**
* The logger.
*
* @var LoggerInterface
*/
protected $logger;

/**
* CreditCardGateway constructor.
Expand All @@ -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,
Expand All @@ -154,9 +174,11 @@ public function __construct(
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;
Expand All @@ -168,9 +190,11 @@ public function __construct(
$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' );
Expand Down Expand Up @@ -370,14 +394,41 @@ public function process_payment( $order_id ) {
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 );
}

/**
Expand Down
12 changes: 10 additions & 2 deletions tests/PHPUnit/WcGateway/Gateway/CreditCardGatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
namespace WooCommerce\PayPalCommerce\WcGateway\Gateway;

use Mockery;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use WC_Order;
Expand Down Expand Up @@ -32,6 +34,8 @@ class CreditCardGatewayTest extends TestCase
private $logger;
private $paymentsEndpoint;
private $vaultedCreditCardHandler;
private $environment;
private $orderEndpoint;
private $testee;

public function setUp(): void
Expand All @@ -50,6 +54,8 @@ public function setUp(): void
$this->logger = Mockery::mock(LoggerInterface::class);
$this->paymentsEndpoint = Mockery::mock(PaymentsEndpoint::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->config->shouldReceive('has')->andReturn(true);
Expand All @@ -67,9 +73,11 @@ public function setUp(): void
$this->state,
$this->transactionUrlProvider,
$this->subscriptionHelper,
$this->logger,
$this->paymentsEndpoint,
$this->vaultedCreditCardHandler
$this->vaultedCreditCardHandler,
$this->environment,
$this->orderEndpoint,
$this->logger
);
}

Expand Down

0 comments on commit 8d00b04

Please sign in to comment.