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 |