|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace TPG\PayFast; |
| 6 | + |
| 7 | +use GuzzleHttp\Client; |
| 8 | +use GuzzleHttp\Exception\ClientException; |
| 9 | +use http\Encoding\Stream; |
| 10 | +use TPG\PayFast\Exceptions\PayFastException; |
| 11 | + |
| 12 | +class Request |
| 13 | +{ |
| 14 | + protected const ENDPOINT = 'https://api.payfast.co.za'; |
| 15 | + |
| 16 | + protected Client $client; |
| 17 | + protected Merchant $merchant; |
| 18 | + protected bool $testing = false; |
| 19 | + |
| 20 | + public function __construct(Merchant $merchant, Client $client = null) |
| 21 | + { |
| 22 | + $this->merchant = $merchant; |
| 23 | + $this->client = $client ?? new Client(); |
| 24 | + } |
| 25 | + |
| 26 | + public function testing(bool $testing = true): self |
| 27 | + { |
| 28 | + $this->testing = $testing; |
| 29 | + |
| 30 | + return $this; |
| 31 | + } |
| 32 | + |
| 33 | + public function make(string $method, string $uri, array $formParams = []): array |
| 34 | + { |
| 35 | + try { |
| 36 | + $response = $this->client->request($method, $this->endpoint($uri), [ |
| 37 | + 'headers' => $this->headers(), |
| 38 | + 'form_params' => $formParams, |
| 39 | + 'query' => [ |
| 40 | + 'testing' => $this->testing ? 'true' : 'false', |
| 41 | + ] |
| 42 | + ]); |
| 43 | + |
| 44 | + if ($response->getStatusCode() !== 200) { |
| 45 | + throw new PayFastException('Bad response from PayFast API'); |
| 46 | + } |
| 47 | + |
| 48 | + return $this->extractJson($response->getBody()->getContents()); |
| 49 | + |
| 50 | + } catch (ClientException $exception) { |
| 51 | + |
| 52 | + throw new PayFastException($this->getErrorMessage($exception->getCode()), $exception->getCode()); |
| 53 | + |
| 54 | + } catch (\JsonException $exception) { |
| 55 | + |
| 56 | + throw new PayFastException('Invalid JSON response'); |
| 57 | + |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + protected function extractJson(string $contents): array |
| 62 | + { |
| 63 | + $json = substr( |
| 64 | + $contents, |
| 65 | + $startPos = strpos($contents, '{'), |
| 66 | + strrpos($contents, '}') - ($startPos - 1) |
| 67 | + ); |
| 68 | + |
| 69 | + return json_decode($json, true, 512, JSON_THROW_ON_ERROR); |
| 70 | + } |
| 71 | + |
| 72 | + protected function headers(array $body = []): array |
| 73 | + { |
| 74 | + $headers = [ |
| 75 | + 'merchant-id' => $this->merchant->merchantId(), |
| 76 | + 'version' => 'v1', |
| 77 | + 'timestamp' => (new \DateTime())->format(DATE_ATOM), |
| 78 | + ]; |
| 79 | + |
| 80 | + $headers['signature'] = (new Signature(array_merge($headers, $body), $this->merchant->passphrase()))->generate(true); |
| 81 | + |
| 82 | + return $headers; |
| 83 | + } |
| 84 | + |
| 85 | + protected function endpoint(string $uri = null): string |
| 86 | + { |
| 87 | + if ($uri[0] !== '/') { |
| 88 | + $uri = '/'.$uri; |
| 89 | + } |
| 90 | + |
| 91 | + return self::ENDPOINT.$uri; |
| 92 | + } |
| 93 | + |
| 94 | + protected function getErrorMessage(int $code): string |
| 95 | + { |
| 96 | + switch ($code) { |
| 97 | + case 400: |
| 98 | + return 'Invalid request'; |
| 99 | + case 401: |
| 100 | + return 'Authorization failed'; |
| 101 | + case 404: |
| 102 | + return 'Service not found'; |
| 103 | + case 429: |
| 104 | + return 'Rate limited'; |
| 105 | + case 500: |
| 106 | + return 'PayFast Application/Communication error'; |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments