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

View file

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

View file

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

View file

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

View file

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