extend ajax error response data

This commit is contained in:
David Remer 2020-08-26 08:06:14 +03:00
parent 66c1f467b9
commit 74eacf81b3
5 changed files with 68 additions and 17 deletions

View file

@ -7,6 +7,7 @@ namespace Inpsyde\PayPalCommerce\Button\Endpoint;
use Inpsyde\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint; use Inpsyde\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use Inpsyde\PayPalCommerce\ApiClient\Entity\Order; use Inpsyde\PayPalCommerce\ApiClient\Entity\Order;
use Inpsyde\PayPalCommerce\ApiClient\Entity\OrderStatus; use Inpsyde\PayPalCommerce\ApiClient\Entity\OrderStatus;
use Inpsyde\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use Inpsyde\PayPalCommerce\Button\Exception\RuntimeException; use Inpsyde\PayPalCommerce\Button\Exception\RuntimeException;
use Inpsyde\PayPalCommerce\Button\Helper\ThreeDSecure; use Inpsyde\PayPalCommerce\Button\Helper\ThreeDSecure;
use Inpsyde\PayPalCommerce\Session\SessionHandler; use Inpsyde\PayPalCommerce\Session\SessionHandler;
@ -96,7 +97,14 @@ class ApproveOrderEndpoint implements EndpointInterface
wp_send_json_success($order); wp_send_json_success($order);
return true; return true;
} catch (\RuntimeException $error) { } 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; return false;
} }
} }

View file

@ -6,6 +6,7 @@ namespace Inpsyde\PayPalCommerce\Button\Endpoint;
use Inpsyde\PayPalCommerce\ApiClient\Entity\Item; use Inpsyde\PayPalCommerce\ApiClient\Entity\Item;
use Inpsyde\PayPalCommerce\ApiClient\Entity\PurchaseUnit; use Inpsyde\PayPalCommerce\ApiClient\Entity\PurchaseUnit;
use Inpsyde\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use Inpsyde\PayPalCommerce\ApiClient\Repository\CartRepository; use Inpsyde\PayPalCommerce\ApiClient\Repository\CartRepository;
use Inpsyde\PayPalCommerce\Button\Exception\RuntimeException; use Inpsyde\PayPalCommerce\Button\Exception\RuntimeException;
@ -44,7 +45,15 @@ class ChangeCartEndpoint implements EndpointInterface
try { try {
return $this->handleData(); return $this->handleData();
} catch (RuntimeException $error) { } 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; return false;
} }
} }
@ -55,10 +64,15 @@ class ChangeCartEndpoint implements EndpointInterface
$products = $this->productsFromData($data); $products = $this->productsFromData($data);
if (! $products) { if (! $products) {
wp_send_json_error( wp_send_json_error(
__( [
'name' => '',
'message' => __(
'Necessary fields not defined. Action aborted.', 'Necessary fields not defined. Action aborted.',
'woocommerce-paypal-commerce-gateway' 'woocommerce-paypal-commerce-gateway'
) ),
'code' => 0,
'details' => [],
]
); );
return false; return false;
} }
@ -102,7 +116,14 @@ class ChangeCartEndpoint implements EndpointInterface
); );
wc_clear_notices(); wc_clear_notices();
} }
wp_send_json_error($message);
wp_send_json_error(
[
'name' => '',
'message' => $message,
'code' => 0,
'details' => [],
]);
return true; return true;
} }

View file

@ -6,6 +6,7 @@ namespace Inpsyde\PayPalCommerce\Button\Endpoint;
use Inpsyde\PayPalCommerce\ApiClient\Entity\Order; use Inpsyde\PayPalCommerce\ApiClient\Entity\Order;
use Inpsyde\PayPalCommerce\ApiClient\Entity\PaymentMethod; use Inpsyde\PayPalCommerce\ApiClient\Entity\PaymentMethod;
use Inpsyde\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use Inpsyde\PayPalCommerce\ApiClient\Factory\PayerFactory; use Inpsyde\PayPalCommerce\ApiClient\Factory\PayerFactory;
use Inpsyde\PayPalCommerce\Button\Exception\RuntimeException; use Inpsyde\PayPalCommerce\Button\Exception\RuntimeException;
use Inpsyde\PayPalCommerce\ApiClient\Repository\CartRepository; use Inpsyde\PayPalCommerce\ApiClient\Repository\CartRepository;
@ -94,10 +95,12 @@ class CreateOrderEndpoint implements EndpointInterface
return true; return true;
} catch (\RuntimeException $error) { } catch (\RuntimeException $error) {
wp_send_json_error( wp_send_json_error(
__( [
'Something went wrong. Please try again or choose another payment source.', 'name' => is_a($error, PayPalApiException::class) ? $error->name() : '',
'woocommerce-paypal-commerce-gateway' 'message' => $error->getMessage(),
) 'code' => $error->getCode(),
'details' => is_a($error, PayPalApiException::class) ? $error->details() : [],
]
); );
return false; return false;
} }
@ -141,7 +144,15 @@ class CreateOrderEndpoint implements EndpointInterface
$this->earlyOrderHandler->registerForOrder($order); $this->earlyOrderHandler->registerForOrder($order);
return $data; 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; return $data;
} }
} }

View file

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Button\Endpoint; namespace Inpsyde\PayPalCommerce\Button\Endpoint;
use Inpsyde\PayPalCommerce\ApiClient\Endpoint\IdentityToken; use Inpsyde\PayPalCommerce\ApiClient\Endpoint\IdentityToken;
use Inpsyde\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use Inpsyde\PayPalCommerce\ApiClient\Exception\RuntimeException; use Inpsyde\PayPalCommerce\ApiClient\Exception\RuntimeException;
class DataClientIdEndpoint implements EndpointInterface class DataClientIdEndpoint implements EndpointInterface
@ -41,7 +42,13 @@ class DataClientIdEndpoint implements EndpointInterface
]); ]);
return true; return true;
} catch (RuntimeException $error) { } 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; return false;
} }
} }

View file

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Inpsyde\PayPalCommerce\Button\Helper; namespace Inpsyde\PayPalCommerce\Button\Helper;
use Inpsyde\PayPalCommerce\ApiClient\Entity\Order; use Inpsyde\PayPalCommerce\ApiClient\Entity\Order;
use Inpsyde\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use Inpsyde\PayPalCommerce\Onboarding\State; use Inpsyde\PayPalCommerce\Onboarding\State;
use Inpsyde\PayPalCommerce\Session\SessionHandler; use Inpsyde\PayPalCommerce\Session\SessionHandler;
use Inpsyde\PayPalCommerce\WcGateway\Gateway\PayPalGateway; use Inpsyde\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
@ -77,10 +78,13 @@ class EarlyOrderHandler
wp_send_json_success($order->toArray()); wp_send_json_success($order->toArray());
} catch (\RuntimeException $error) { } catch (\RuntimeException $error) {
wp_send_json_error( wp_send_json_error(
__( [
'Something went wrong. Please try again or choose another payment source.', 'name' => is_a($error, PayPalApiException::class) ? $error->name() : '',
'woocommerce-paypal-commerce-gateway' 'message' => $error->getMessage(),
) 'code' => $error->getCode(),
'details' => is_a($error, PayPalApiException::class) ? $error->details() : [],
]
); );
} }
} }