diff --git a/modules.local/ppcp-wc-gateway/services.php b/modules.local/ppcp-wc-gateway/services.php index 11b5711df..105417e63 100644 --- a/modules.local/ppcp-wc-gateway/services.php +++ b/modules.local/ppcp-wc-gateway/services.php @@ -11,6 +11,7 @@ use Inpsyde\PayPalCommerce\WcGateway\Admin\OrderTablePaymentStatusColumn; use Inpsyde\PayPalCommerce\WcGateway\Admin\PaymentStatusOrderDetail; use Inpsyde\PayPalCommerce\WcGateway\Checkout\CheckoutPayPalAddressPreset; use Inpsyde\PayPalCommerce\WcGateway\Checkout\DisableGateways; +use Inpsyde\PayPalCommerce\WcGateway\Endpoint\ReturnUrlEndpoint; use Inpsyde\PayPalCommerce\WcGateway\Gateway\CreditCardGateway; use Inpsyde\PayPalCommerce\WcGateway\Gateway\PayPalGateway; use Inpsyde\PayPalCommerce\WcGateway\Notice\AuthorizeOrderActionNotice; @@ -1551,4 +1552,14 @@ return [ dirname(__FILE__, 3) . '/woocommerce-paypal-commerce-gateway.php' ); }, + 'wcgateway.endpoint.return-url' => static function (ContainerInterface $container) : ReturnUrlEndpoint { + $gateway = $container->get('wcgateway.paypal-gateway'); + $endpoint = $container->get('api.endpoint.order'); + $prefix = $container->get('api.prefix'); + return new ReturnUrlEndpoint( + $gateway, + $endpoint, + $prefix + ); + }, ]; diff --git a/modules.local/ppcp-wc-gateway/src/Endpoint/ReturnUrlEndpoint.php b/modules.local/ppcp-wc-gateway/src/Endpoint/ReturnUrlEndpoint.php new file mode 100644 index 000000000..d5f740897 --- /dev/null +++ b/modules.local/ppcp-wc-gateway/src/Endpoint/ReturnUrlEndpoint.php @@ -0,0 +1,54 @@ +gateway = $gateway; + $this->orderEndpoint = $orderEndpoint; + $this->prefix = $prefix; + } + + public function handleRequest() { + + if (! isset($_GET['token'])) { + exit; + } + $token = sanitize_text_field(wp_unslash($_GET['token'])); + $order = $this->orderEndpoint->order($token); + if (! $order) { + exit; + } + + $wcOrderId = $this->sanitizeCustomId($order->purchaseUnits()[0]->customId()); + if (! $wcOrderId) { + exit; + } + + $wcOrder = wc_get_order($wcOrderId); + if (! $wcOrder) { + exit; + } + + $success = $this->gateway->process_payment($wcOrderId); + if (isset($success['result']) && $success['result'] === 'success') { + wp_redirect($success['redirect']); + exit; + } + wp_redirect(wc_get_checkout_url()); + exit; + } +} diff --git a/modules.local/ppcp-wc-gateway/src/Gateway/PayPalGateway.php b/modules.local/ppcp-wc-gateway/src/Gateway/PayPalGateway.php index 624207251..8ab781c7c 100644 --- a/modules.local/ppcp-wc-gateway/src/Gateway/PayPalGateway.php +++ b/modules.local/ppcp-wc-gateway/src/Gateway/PayPalGateway.php @@ -147,24 +147,26 @@ class PayPalGateway extends \WC_Payment_Gateway } } catch (PayPalApiException $error) { if ($error->hasDetail('INSTRUMENT_DECLINED')) { - $this->sessionHandler->destroySessionData(); - wc_add_notice( - __( - 'The payment provider refused the payout. Please choose a different payment method.', - 'woocommerce-paypal-commerce-gateway' - ), - 'error' - ); + $host = $this->config->has('sandbox_on') && $this->config->get('sandbox_on') ? + 'https://www.sandbox.paypal.com/' : 'https://www.paypal.com/'; + $url = $host . 'checkoutnow?token=' . $this->sessionHandler->order()->id(); + return [ 'result' => 'success', - 'redirect' => wc_get_checkout_url(), + 'redirect' => $url, ]; } - throw $error; + $this->sessionHandler->destroySessionData(); + wc_add_notice( + __( + 'Something went wrong. Please try again.', + 'woocommerce-paypal-commerce-gateway' + ), + 'error' + ); } - wc_add_notice($this->orderProcessor->lastError()); return null; } diff --git a/modules.local/ppcp-wc-gateway/src/WcGatewayModule.php b/modules.local/ppcp-wc-gateway/src/WcGatewayModule.php index 4b3e45bed..9296a453d 100644 --- a/modules.local/ppcp-wc-gateway/src/WcGatewayModule.php +++ b/modules.local/ppcp-wc-gateway/src/WcGatewayModule.php @@ -14,6 +14,7 @@ use Inpsyde\PayPalCommerce\WcGateway\Admin\OrderTablePaymentStatusColumn; use Inpsyde\PayPalCommerce\WcGateway\Admin\PaymentStatusOrderDetail; use Inpsyde\PayPalCommerce\WcGateway\Checkout\CheckoutPayPalAddressPreset; use Inpsyde\PayPalCommerce\WcGateway\Checkout\DisableGateways; +use Inpsyde\PayPalCommerce\WcGateway\Endpoint\ReturnUrlEndpoint; use Inpsyde\PayPalCommerce\WcGateway\Gateway\CreditCardGateway; use Inpsyde\PayPalCommerce\WcGateway\Gateway\PayPalGateway; use Inpsyde\PayPalCommerce\WcGateway\Notice\ConnectAdminNotice; @@ -69,6 +70,17 @@ class WcGatewayModule implements ModuleInterface delete_option('woocommerce_' . CreditCardGateway::ID . '_settings'); } ); + + add_action( + 'wc_ajax_' . ReturnUrlEndpoint::ENDPOINT, + static function () use ($container) { + $endpoint = $container->get('wcgateway.endpoint.return-url'); + /** + * @var ReturnUrlEndpoint $endpoint + */ + $endpoint->handleRequest(); + } + ); } private function ajaxGatewayEnabler(ContainerInterface $container)