Pass the errors to the ErrorResponse handler

This commit is contained in:
Mészáros Róbert 2020-04-15 11:14:26 +03:00
parent df9a74d551
commit d989617f69

View file

@ -41,8 +41,10 @@ class PaymentsEndpoint
]; ];
$response = wp_remote_get($url, $args); $response = wp_remote_get($url, $args);
$json = json_decode($response['body']);
if (is_wp_error($response)) { if (is_wp_error($response)) {
$this->handleResponseWpError($url, $args);
throw new RuntimeException( throw new RuntimeException(
__( __(
'Could not get authorized payment info.', 'Could not get authorized payment info.',
@ -52,6 +54,13 @@ class PaymentsEndpoint
} }
if (wp_remote_retrieve_response_code($response) !== 200) { if (wp_remote_retrieve_response_code($response) !== 200) {
$errors = $this->errorResponseFactory->fromPayPalResponse(
$json,
(int)wp_remote_retrieve_response_code($response),
$url,
$args
);
add_action('woocommerce-paypal-commerce-gateway.error', $errors);
throw new RuntimeException( throw new RuntimeException(
__( __(
'Could not get authorized payment info.', 'Could not get authorized payment info.',
@ -60,7 +69,6 @@ class PaymentsEndpoint
); );
} }
$json = json_decode($response['body']);
$authorization = $this->authorizationFactory->fromPayPalRequest($json); $authorization = $this->authorizationFactory->fromPayPalRequest($json);
return $authorization; return $authorization;
} }
@ -78,8 +86,10 @@ class PaymentsEndpoint
]; ];
$response = wp_remote_post($url, $args); $response = wp_remote_post($url, $args);
$json = json_decode($response['body']);
if (is_wp_error($response)) { if (is_wp_error($response)) {
$this->handleResponseWpError($url, $args);
throw new RuntimeException( throw new RuntimeException(
__( __(
'Could not capture authorized payment.', 'Could not capture authorized payment.',
@ -89,6 +99,13 @@ class PaymentsEndpoint
} }
if (wp_remote_retrieve_response_code($response) !== 201) { if (wp_remote_retrieve_response_code($response) !== 201) {
$errors = $this->errorResponseFactory->fromPayPalResponse(
$json,
(int)wp_remote_retrieve_response_code($response),
$url,
$args
);
add_action('woocommerce-paypal-commerce-gateway.error', $errors);
throw new RuntimeException( throw new RuntimeException(
__( __(
'Could not capture authorized payment.', 'Could not capture authorized payment.',
@ -97,8 +114,16 @@ class PaymentsEndpoint
); );
} }
$json = json_decode($response['body']);
$authorization = $this->authorizationFactory->fromPayPalRequest($json); $authorization = $this->authorizationFactory->fromPayPalRequest($json);
return $authorization; return $authorization;
} }
private function handleResponseWpError(string $url, array $args)
{
$errors = $this->errorResponseFactory->unknownError(
$url,
$args
);
add_action('woocommerce-paypal-commerce-gateway.error', $errors);
}
} }