PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 2.4.4
Booking for Appointments and Events Calendar – Amelia v2.4.4
2.4.4 2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / src / Infrastructure / Services / PayPal / PayPalResponse.php
ameliabooking / src / Infrastructure / Services / PayPal Last commit date
PayPalClient.php 3 months ago PayPalResponse.php 3 months ago
PayPalResponse.php
113 lines
1 <?php
2
3 /**
4 * @copyright © Melograno Ventures. All rights reserved.
5 * @licence See LICENCE.md for license details.
6 */
7
8 namespace AmeliaBooking\Infrastructure\Services\PayPal;
9
10 /**
11 * Class PayPalResponse
12 *
13 */
14 class PayPalResponse
15 {
16 /**
17 * HTTP status codes that indicate a successful API call.
18 */
19 private const SUCCESS_CODES = [200, 201, 204];
20
21 /**
22 * @var array
23 */
24 private array $data;
25
26 /**
27 * @var int
28 */
29 private int $httpCode;
30
31 /**
32 * PayPalResponse constructor.
33 *
34 */
35 public function __construct(array $data)
36 {
37 $this->data = $data;
38 $this->httpCode = isset($data['_http_code']) ? (int)$data['_http_code'] : 0;
39 }
40
41 /**
42 * Whether the request was successful.
43 */
44 public function isSuccessful(): bool
45 {
46 return in_array($this->httpCode, self::SUCCESS_CODES, true);
47 }
48
49 /**
50 * Return the raw response data array.
51 */
52 public function getData(): array
53 {
54 return $this->data;
55 }
56
57 /**
58 * Return the HTTP status code.
59 */
60 public function getCode(): int
61 {
62 return $this->httpCode;
63 }
64
65 /**
66 * Return a human-readable error message from the response, if any.
67 *
68 * Checks PayPal v2 error fields in order of specificity.
69 */
70 public function getMessage(): string
71 {
72 if (!empty($this->data['message'])) {
73 return (string)$this->data['message'];
74 }
75
76 if (!empty($this->data['details'][0]['description'])) {
77 return (string)$this->data['details'][0]['description'];
78 }
79
80 if (!empty($this->data['details'][0]['issue'])) {
81 return (string)$this->data['details'][0]['issue'];
82 }
83
84 return '';
85 }
86
87 /**
88 * Return the PayPal order ID as the transaction reference.
89 */
90 public function getTransactionReference(): string
91 {
92 return !empty($this->data['id']) ? (string)$this->data['id'] : '';
93 }
94
95 /**
96 * Find and return the customer-facing approval URL from the links array.
97 */
98 public function getRedirectUrl(): ?string
99 {
100 if (empty($this->data['links']) || !is_array($this->data['links'])) {
101 return null;
102 }
103
104 foreach ($this->data['links'] as $link) {
105 if (!empty($link['rel']) && $link['rel'] === 'approve' && !empty($link['href'])) {
106 return (string)$link['href'];
107 }
108 }
109
110 return null;
111 }
112 }
113