diff --git a/modules.local/ppcp-button/src/Endpoint/ApproveOrderEndpoint.php b/modules.local/ppcp-button/src/Endpoint/ApproveOrderEndpoint.php index 641c5ea02..ae73571f3 100644 --- a/modules.local/ppcp-button/src/Endpoint/ApproveOrderEndpoint.php +++ b/modules.local/ppcp-button/src/Endpoint/ApproveOrderEndpoint.php @@ -7,6 +7,7 @@ namespace Inpsyde\PayPalCommerce\Button\Endpoint; use Inpsyde\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint; use Inpsyde\PayPalCommerce\ApiClient\Entity\Order; use Inpsyde\PayPalCommerce\ApiClient\Entity\OrderStatus; +use Inpsyde\PayPalCommerce\ApiClient\Exception\PayPalApiException; use Inpsyde\PayPalCommerce\Button\Exception\RuntimeException; use Inpsyde\PayPalCommerce\Button\Helper\ThreeDSecure; use Inpsyde\PayPalCommerce\Session\SessionHandler; @@ -96,7 +97,14 @@ class ApproveOrderEndpoint implements EndpointInterface wp_send_json_success($order); return true; } catch (\RuntimeException $error) { - wp_send_json_error($error->getMessage()); + wp_send_json_error( + [ + 'name' => is_a($error, PayPalApiException::class) ? $error->name() : '', + 'message' => $error->getMessage(), + 'code' => $error->getCode(), + 'details' => is_a($error, PayPalApiException::class) ? $error->details() : [], + ] + ); return false; } } diff --git a/modules.local/ppcp-button/src/Endpoint/ChangeCartEndpoint.php b/modules.local/ppcp-button/src/Endpoint/ChangeCartEndpoint.php index 6366edb76..0b20e1636 100644 --- a/modules.local/ppcp-button/src/Endpoint/ChangeCartEndpoint.php +++ b/modules.local/ppcp-button/src/Endpoint/ChangeCartEndpoint.php @@ -6,6 +6,7 @@ namespace Inpsyde\PayPalCommerce\Button\Endpoint; use Inpsyde\PayPalCommerce\ApiClient\Entity\Item; use Inpsyde\PayPalCommerce\ApiClient\Entity\PurchaseUnit; +use Inpsyde\PayPalCommerce\ApiClient\Exception\PayPalApiException; use Inpsyde\PayPalCommerce\ApiClient\Repository\CartRepository; use Inpsyde\PayPalCommerce\Button\Exception\RuntimeException; @@ -44,7 +45,15 @@ class ChangeCartEndpoint implements EndpointInterface try { return $this->handleData(); } catch (RuntimeException $error) { - wp_send_json_error($error->getMessage()); + + wp_send_json_error( + [ + 'name' => is_a($error, PayPalApiException::class) ? $error->name() : '', + 'message' => $error->getMessage(), + 'code' => $error->getCode(), + 'details' => is_a($error, PayPalApiException::class) ? $error->details() : [], + ] + ); return false; } } @@ -55,10 +64,15 @@ class ChangeCartEndpoint implements EndpointInterface $products = $this->productsFromData($data); if (! $products) { wp_send_json_error( - __( - 'Necessary fields not defined. Action aborted.', - 'woocommerce-paypal-commerce-gateway' - ) + [ + 'name' => '', + 'message' => __( + 'Necessary fields not defined. Action aborted.', + 'woocommerce-paypal-commerce-gateway' + ), + 'code' => 0, + 'details' => [], + ] ); return false; } @@ -102,7 +116,14 @@ class ChangeCartEndpoint implements EndpointInterface ); wc_clear_notices(); } - wp_send_json_error($message); + + wp_send_json_error( + [ + 'name' => '', + 'message' => $message, + 'code' => 0, + 'details' => [], + ]); return true; } diff --git a/modules.local/ppcp-button/src/Endpoint/CreateOrderEndpoint.php b/modules.local/ppcp-button/src/Endpoint/CreateOrderEndpoint.php index f51e31ec9..235591a67 100644 --- a/modules.local/ppcp-button/src/Endpoint/CreateOrderEndpoint.php +++ b/modules.local/ppcp-button/src/Endpoint/CreateOrderEndpoint.php @@ -6,6 +6,7 @@ namespace Inpsyde\PayPalCommerce\Button\Endpoint; use Inpsyde\PayPalCommerce\ApiClient\Entity\Order; use Inpsyde\PayPalCommerce\ApiClient\Entity\PaymentMethod; +use Inpsyde\PayPalCommerce\ApiClient\Exception\PayPalApiException; use Inpsyde\PayPalCommerce\ApiClient\Factory\PayerFactory; use Inpsyde\PayPalCommerce\Button\Exception\RuntimeException; use Inpsyde\PayPalCommerce\ApiClient\Repository\CartRepository; @@ -94,10 +95,12 @@ class CreateOrderEndpoint implements EndpointInterface return true; } catch (\RuntimeException $error) { wp_send_json_error( - __( - 'Something went wrong. Please try again or choose another payment source.', - 'woocommerce-paypal-commerce-gateway' - ) + [ + 'name' => is_a($error, PayPalApiException::class) ? $error->name() : '', + 'message' => $error->getMessage(), + 'code' => $error->getCode(), + 'details' => is_a($error, PayPalApiException::class) ? $error->details() : [], + ] ); return false; } @@ -141,7 +144,15 @@ class CreateOrderEndpoint implements EndpointInterface $this->earlyOrderHandler->registerForOrder($order); return $data; } - wp_send_json_error($errors->get_error_message()); + + wp_send_json_error( + [ + 'name' => '', + 'message' => $errors->get_error_message(), + 'code' => (int) $errors->get_error_code(), + 'details' => [], + ] + ); return $data; } } diff --git a/modules.local/ppcp-button/src/Endpoint/DataClientIdEndpoint.php b/modules.local/ppcp-button/src/Endpoint/DataClientIdEndpoint.php index 904553a05..f6e403bc9 100644 --- a/modules.local/ppcp-button/src/Endpoint/DataClientIdEndpoint.php +++ b/modules.local/ppcp-button/src/Endpoint/DataClientIdEndpoint.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Inpsyde\PayPalCommerce\Button\Endpoint; use Inpsyde\PayPalCommerce\ApiClient\Endpoint\IdentityToken; +use Inpsyde\PayPalCommerce\ApiClient\Exception\PayPalApiException; use Inpsyde\PayPalCommerce\ApiClient\Exception\RuntimeException; class DataClientIdEndpoint implements EndpointInterface @@ -41,7 +42,13 @@ class DataClientIdEndpoint implements EndpointInterface ]); return true; } catch (RuntimeException $error) { - wp_send_json_error($error->getMessage()); + wp_send_json_error( + [ + 'name' => is_a($error, PayPalApiException::class) ? $error->name() : '', + 'message' => $error->getMessage(), + 'code' => $error->getCode(), + 'details' => is_a($error, PayPalApiException::class) ? $error->details() : [], + ]); return false; } } diff --git a/modules.local/ppcp-button/src/Helper/EarlyOrderHandler.php b/modules.local/ppcp-button/src/Helper/EarlyOrderHandler.php index ad9760ecc..0f804a3be 100644 --- a/modules.local/ppcp-button/src/Helper/EarlyOrderHandler.php +++ b/modules.local/ppcp-button/src/Helper/EarlyOrderHandler.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Inpsyde\PayPalCommerce\Button\Helper; use Inpsyde\PayPalCommerce\ApiClient\Entity\Order; +use Inpsyde\PayPalCommerce\ApiClient\Exception\PayPalApiException; use Inpsyde\PayPalCommerce\Onboarding\State; use Inpsyde\PayPalCommerce\Session\SessionHandler; use Inpsyde\PayPalCommerce\WcGateway\Gateway\PayPalGateway; @@ -77,10 +78,13 @@ class EarlyOrderHandler wp_send_json_success($order->toArray()); } catch (\RuntimeException $error) { wp_send_json_error( - __( - 'Something went wrong. Please try again or choose another payment source.', - 'woocommerce-paypal-commerce-gateway' - ) + [ + 'name' => is_a($error, PayPalApiException::class) ? $error->name() : '', + 'message' => $error->getMessage(), + 'code' => $error->getCode(), + 'details' => is_a($error, PayPalApiException::class) ? $error->details() : [], + + ] ); } }