| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Framework\PaymentGateways\Traits; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use Give\DonationForms\ValueObjects\DonationFormErrorTypes; |
| 7 |
use Give\Framework\Http\Response\Types\JsonResponse; |
| 8 |
use Give\Framework\Http\Response\Types\RedirectResponse; |
| 9 |
use Give\Framework\PaymentGateways\Exceptions\PaymentGatewayException; |
| 10 |
use WP_Error; |
| 11 |
|
| 12 |
trait HandleHttpResponses |
| 13 |
{ |
| 14 |
/** |
| 15 |
* Handle Response |
| 16 |
* |
| 17 |
* @since 2.32.0 added check for responding with json |
| 18 |
* @since 2.27.0 add support for json content-type |
| 19 |
* @since 2.18.0 |
| 20 |
* |
| 21 |
* @param RedirectResponse|JsonResponse $type |
| 22 |
*/ |
| 23 |
public function handleResponse($type) |
| 24 |
{ |
| 25 |
if ($type instanceof RedirectResponse) { |
| 26 |
if ($this->wantsJson()) { |
| 27 |
wp_send_json([ |
| 28 |
'type' => 'redirect', |
| 29 |
'data' => [ |
| 30 |
'redirectUrl' => $type->getTargetUrl() |
| 31 |
] |
| 32 |
]); |
| 33 |
} |
| 34 |
|
| 35 |
wp_redirect($type->getTargetUrl()); |
| 36 |
exit; |
| 37 |
} |
| 38 |
|
| 39 |
if ($type instanceof JsonResponse) { |
| 40 |
wp_send_json(['data' => $type->getData()]); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Handle response on basis of request mode when exception occurs: |
| 46 |
* 1. Redirect to donation form if donation form submit. |
| 47 |
* 2. Return json response if processing payment on ajax. |
| 48 |
* |
| 49 |
* @since 3.0.0 Update response with type and WP_Error errors. |
| 50 |
* @since 2.21.0 Handle PHP exception. |
| 51 |
* @since 2.19.0 |
| 52 |
*/ |
| 53 |
public function handleExceptionResponse( |
| 54 |
Exception $exception, |
| 55 |
string $message, |
| 56 |
string $type = DonationFormErrorTypes::UNKNOWN, |
| 57 |
WP_Error $errors = null |
| 58 |
) { |
| 59 |
if ($exception instanceof PaymentGatewayException) { |
| 60 |
$message = $exception->getMessage(); |
| 61 |
$type = DonationFormErrorTypes::GATEWAY; |
| 62 |
} |
| 63 |
|
| 64 |
if (wp_doing_ajax() || $this->wantsJson()) { |
| 65 |
$response = new JsonResponse($message); |
| 66 |
if (!$errors) { |
| 67 |
$errors = new WP_Error($type, $exception->getMessage()); |
| 68 |
} |
| 69 |
|
| 70 |
// This structure works for v2 and v3 forms. |
| 71 |
wp_send_json_error([ |
| 72 |
'type' => $type, |
| 73 |
'errors' => $errors, |
| 74 |
'data' => $response->getData() |
| 75 |
]); |
| 76 |
} |
| 77 |
|
| 78 |
// This is for backwards compatibility with v2 forms. |
| 79 |
give_set_error('PaymentGatewayException', $message); |
| 80 |
give_send_back_to_checkout(); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* This checks the server headers for 'application/json' to determine if it should respond with json. |
| 85 |
* |
| 86 |
* @since 2.32.0 |
| 87 |
* |
| 88 |
* @return bool |
| 89 |
*/ |
| 90 |
protected function wantsJson(): bool |
| 91 |
{ |
| 92 |
if (isset($_SERVER['HTTP_ACCEPT']) && str_contains($_SERVER['HTTP_ACCEPT'], 'application/json')) { |
| 93 |
return true; |
| 94 |
} |
| 95 |
|
| 96 |
return isset($_SERVER['CONTENT_TYPE']) && str_contains($_SERVER['CONTENT_TYPE'], 'application/json'); |
| 97 |
} |
| 98 |
} |
| 99 |
|