pcp-37 / add endpoint, redirect insufficient funds to paypal to choose different payment method

This commit is contained in:
David Remer 2020-08-27 10:58:08 +03:00
parent 80b492e083
commit 16501ee765
4 changed files with 90 additions and 11 deletions

View file

@ -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
);
},
];

View file

@ -0,0 +1,54 @@
<?php
declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\WcGateway\Endpoint;
use Inpsyde\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use Inpsyde\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use Inpsyde\PayPalCommerce\Webhooks\Handler\PrefixTrait;
class ReturnUrlEndpoint
{
use PrefixTrait;
public const ENDPOINT = 'ppc-return-url';
private $gateway;
private $orderEndpoint;
public function __construct(PayPalGateway $gateway, OrderEndpoint $orderEndpoint, string $prefix)
{
$this->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;
}
}

View file

@ -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;
}

View file

@ -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)