Handle errors from PayPal response

This commit is contained in:
Emili Castells Guasch 2024-08-13 16:14:15 +02:00
parent 86acc00f85
commit 10172907df
2 changed files with 57 additions and 5 deletions

View file

@ -12,6 +12,7 @@ namespace WooCommerce\PayPalCommerce\LocalAlternativePaymentMethods;
use WC_Payment_Gateway;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\Orders;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;
use WooCommerce\PayPalCommerce\Button\Exception\RuntimeException;
/**
* Class BancontactGateway
@ -111,7 +112,7 @@ class BancontactGateway extends WC_Payment_Gateway {
'payment_source' => array(
'bancontact' => array(
'country_code' => 'BE',
'name' => 'John Doe',
'name' => $wc_order->get_billing_first_name() . ' ' . $wc_order->get_billing_last_name(),
),
),
'processing_instruction' => 'ORDER_COMPLETE_ON_PAYMENT_APPROVAL',
@ -129,12 +130,25 @@ class BancontactGateway extends WC_Payment_Gateway {
'application_context' => array(
'locale' => 'en-BE',
'return_url' => $this->get_return_url( $wc_order ),
'cancel_url' => wc_get_checkout_url(),
'cancel_url' => add_query_arg( 'cancelled', 'true', $this->get_return_url( $wc_order ) ),
),
);
$response = $this->orders_endpoint->create( $request_body );
$body = json_decode( $response['body'] );
try {
$response = $this->orders_endpoint->create( $request_body );
} catch ( RuntimeException $exception ) {
$wc_order->update_status(
'failed',
$exception->getMessage()
);
return array(
'result' => 'failure',
'redirect' => wc_get_checkout_url(),
);
}
$body = json_decode( $response['body'] );
$payer_action = '';
foreach ( $body->links as $link ) {
@ -143,6 +157,8 @@ class BancontactGateway extends WC_Payment_Gateway {
}
}
WC()->cart->empty_cart();
return array(
'result' => 'success',
'redirect' => esc_url( $payer_action ),

View file

@ -10,6 +10,7 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\LocalAlternativePaymentMethods;
use Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry;
use WC_Order;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface;
use WooCommerce\PayPalCommerce\Vendor\Interop\Container\ServiceProviderInterface;
@ -37,10 +38,14 @@ class LocalAlternativePaymentMethodsModule implements ModuleInterface {
add_filter(
'woocommerce_available_payment_gateways',
function ( $methods ) use ( $c ) {
if ( is_admin() ) {
return $methods;
}
$customer_country = WC()->customer->get_billing_country() ?: WC()->customer->get_shipping_country();
$site_currency = get_woocommerce_currency();
if ( $customer_country === 'BE' && $site_currency === 'EUR' ) {
$methods[] = $c->get( 'ppcp-local-apms.bancontact.wc-gateway' );
$methods[ BancontactGateway::ID ] = $c->get( 'ppcp-local-apms.bancontact.wc-gateway' );
}
return $methods;
@ -64,5 +69,36 @@ class LocalAlternativePaymentMethodsModule implements ModuleInterface {
return $data;
}
);
add_action(
'woocommerce_before_thankyou',
function( $order_id ) {
$order = wc_get_order( $order_id );
if ( ! $order instanceof WC_Order ) {
return;
}
// phpcs:disable WordPress.Security.NonceVerification.Recommended
$cancelled = wc_clean( wp_unslash( $_GET['cancelled'] ?? '' ) );
$order_key = wc_clean( wp_unslash( $_GET['key'] ?? '' ) );
// phpcs:enable
if (
$order->get_payment_method() !== BancontactGateway::ID
|| ! $cancelled
|| $order->get_order_key() !== $order_key
) {
return;
}
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$error_code = wc_clean( wp_unslash( $_GET['errorcode'] ?? '' ) );
if ( $error_code === 'processing_error' || $error_code === 'payment_error' ) {
$order->update_status( 'failed', __( "The payment can't be processed because of an error.", 'woocommerce-paypal-payments' ) );
add_filter( 'woocommerce_order_has_status', '__return_true' );
}
}
);
}
}