diff --git a/modules/ppcp-api-client/src/Endpoint/class-orderendpoint.php b/modules/ppcp-api-client/src/Endpoint/class-orderendpoint.php index adb41a59b..b70ddaa75 100644 --- a/modules/ppcp-api-client/src/Endpoint/class-orderendpoint.php +++ b/modules/ppcp-api-client/src/Endpoint/class-orderendpoint.php @@ -390,7 +390,7 @@ class OrderEndpoint { } $json = json_decode( $response['body'] ); $status_code = (int) wp_remote_retrieve_response_code( $response ); - if ( 201 !== $status_code ) { + if ( ! in_array( $status_code, array( 200, 201 ), true ) ) { if ( false !== strpos( $response['body'], ErrorResponse::ORDER_ALREADY_AUTHORIZED ) ) { return $this->order( $order->id() ); } diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index d26879653..93c04b827 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -83,6 +83,7 @@ return array( $payer_factory = $container->get( 'api.factory.payer' ); $order_endpoint = $container->get( 'api.endpoint.order' ); $subscription_helper = $container->get( 'subscription.helper' ); + $logger = $container->get( 'woocommerce.logger.woocommerce' ); return new CreditCardGateway( $settings_renderer, $order_processor, @@ -98,7 +99,8 @@ return array( $purchase_unit_factory, $payer_factory, $order_endpoint, - $subscription_helper + $subscription_helper, + $logger ); }, 'wcgateway.disabler' => static function ( $container ): DisableGateways { diff --git a/modules/ppcp-wc-gateway/src/Gateway/class-creditcardgateway.php b/modules/ppcp-wc-gateway/src/Gateway/class-creditcardgateway.php index f4b47099a..e23c90600 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/class-creditcardgateway.php +++ b/modules/ppcp-wc-gateway/src/Gateway/class-creditcardgateway.php @@ -9,6 +9,7 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\WcGateway\Gateway; +use Psr\Log\LoggerInterface; use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint; use WooCommerce\PayPalCommerce\ApiClient\Factory\PayerFactory; use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory; @@ -46,6 +47,13 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC { */ protected $subscription_helper; + /** + * The logger. + * + * @var LoggerInterface + */ + protected $logger; + /** * The URL to the module. * @@ -104,8 +112,9 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC { * @param PaymentTokenRepository $payment_token_repository The payment token repository. * @param PurchaseUnitFactory $purchase_unit_factory The purchase unit factory. * @param PayerFactory $payer_factory The payer factory. - * @param OrderEndpoint $order_endpoint The order endpoint. + * @param OrderEndpoint $order_endpoint The order endpoint. * @param SubscriptionHelper $subscription_helper The subscription helper. + * @param LoggerInterface $logger The logger. */ public function __construct( SettingsRenderer $settings_renderer, @@ -122,7 +131,8 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC { PurchaseUnitFactory $purchase_unit_factory, PayerFactory $payer_factory, OrderEndpoint $order_endpoint, - SubscriptionHelper $subscription_helper + SubscriptionHelper $subscription_helper, + LoggerInterface $logger ) { $this->id = self::ID; @@ -190,6 +200,7 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC { $this->order_endpoint = $order_endpoint; $this->transaction_url_provider = $transaction_url_provider; $this->subscription_helper = $subscription_helper; + $this->logger = $logger; } /** diff --git a/modules/ppcp-wc-gateway/src/Gateway/class-processpaymenttrait.php b/modules/ppcp-wc-gateway/src/Gateway/class-processpaymenttrait.php index 7c62e2387..4ce4c29d1 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/class-processpaymenttrait.php +++ b/modules/ppcp-wc-gateway/src/Gateway/class-processpaymenttrait.php @@ -85,7 +85,27 @@ trait ProcessPaymentTrait { 'redirect' => $this->get_return_url( $wc_order ), ); } + + if ( $order->status()->is( OrderStatus::COMPLETED ) && $order->intent() === 'AUTHORIZE' ) { + $this->order_endpoint->authorize( $order ); + $wc_order->update_meta_data( PayPalGateway::CAPTURED_META_KEY, 'false' ); + $wc_order->update_meta_data( PayPalGateway::ORDER_ID_META_KEY, $order->id() ); + $wc_order->update_status( + 'on-hold', + __( 'Awaiting payment.', 'woocommerce-paypal-payments' ) + ); + + $this->session_handler->destroy_session_data(); + return array( + 'result' => 'success', + 'redirect' => $this->get_return_url( $wc_order ), + ); + } + + $this->logger->warning( "Could neither capture nor authorize order {$order->id()} using a saved credit card:" . 'Status: ' . $order->status()->name() . ' Intent: ' . $order->intent() ); + } catch ( RuntimeException $error ) { + $this->logger->error( $error->getMessage() ); $this->session_handler->destroy_session_data(); wc_add_notice( $error->getMessage(), 'error' ); return null;