PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.18.0
GiveWP – Donation Plugin and Fundraising Platform v4.18.0
4.18.0 4.17.0 4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 All 257 releases
give / src / Framework / PaymentGateways / Traits / HandleHttpResponses.php

HandleHttpResponses.php in GiveWP – Donation Plugin and Fundraising Platform 4.18.0, at src/Framework/PaymentGateways/Traits/HandleHttpResponses.php

100 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 4.17.0 Declare the nullable parameter explicitly.
50 * @since 3.0.0 Update response with type and WP_Error errors.
51 * @since 2.21.0 Handle PHP exception.
52 * @since 2.19.0
53 */
54 public function handleExceptionResponse(
55 Exception $exception,
56 string $message,
57 string $type = DonationFormErrorTypes::UNKNOWN,
58 ?WP_Error $errors = null
59 ) {
60 if ($exception instanceof PaymentGatewayException) {
61 $message = $exception->getMessage();
62 $type = DonationFormErrorTypes::GATEWAY;
63 }
64
65 if (wp_doing_ajax() || $this->wantsJson()) {
66 $response = new JsonResponse($message);
67 if (!$errors) {
68 $errors = new WP_Error($type, $exception->getMessage());
69 }
70
71 // This structure works for v2 and v3 forms.
72 wp_send_json_error([
73 'type' => $type,
74 'errors' => $errors,
75 'data' => $response->getData()
76 ]);
77 }
78
79 // This is for backwards compatibility with v2 forms.
80 give_set_error('PaymentGatewayException', $message);
81 give_send_back_to_checkout();
82 }
83
84 /**
85 * This checks the server headers for 'application/json' to determine if it should respond with json.
86 *
87 * @since 2.32.0
88 *
89 * @return bool
90 */
91 protected function wantsJson(): bool
92 {
93 if (isset($_SERVER['HTTP_ACCEPT']) && str_contains($_SERVER['HTTP_ACCEPT'], 'application/json')) {
94 return true;
95 }
96
97 return isset($_SERVER['CONTENT_TYPE']) && str_contains($_SERVER['CONTENT_TYPE'], 'application/json');
98 }
99 }
100