These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Gbowo\Adapter\Amplifypay; |
||
4 | |||
5 | use GuzzleHttp\Client; |
||
6 | use function Gbowo\env; |
||
7 | use Gbowo\Traits\Pluggable; |
||
8 | use function GuzzleHttp\json_decode; |
||
9 | use Psr\Http\Message\ResponseInterface; |
||
10 | use Gbowo\Contract\Adapter\AdapterInterface; |
||
11 | use Gbowo\Adapter\Amplifypay\Traits\KeyVerifier; |
||
12 | use Gbowo\Exception\InvalidHttpResponseException; |
||
13 | use Gbowo\Adapter\Amplifypay\Plugin\GetPaymentData; |
||
14 | use Gbowo\Adapter\Amplifypay\Plugin\ChargeWithToken; |
||
15 | use Gbowo\Adapter\Amplifypay\Plugin\UnsubscribeCustomer; |
||
16 | |||
17 | /** |
||
18 | * @method chargeWithToken(array $data) |
||
19 | * @method unsubcribeCustomerFromPlan(array $data) |
||
20 | * @method getPaymentData(string $transactionReference) |
||
21 | * @author Lanre Adelowo <[email protected]> |
||
22 | * Class AmplifypayAdapter |
||
23 | * @package Gbowo\Adapter\Amplifypay |
||
24 | */ |
||
25 | class AmplifypayAdapter implements AdapterInterface |
||
26 | { |
||
27 | |||
28 | use Pluggable, KeyVerifier; |
||
29 | |||
30 | /** |
||
31 | * @var \GuzzleHttp\Client |
||
32 | */ |
||
33 | protected $httpClient; |
||
34 | |||
35 | /** |
||
36 | * Keys to initiate a successful request |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $apiKeys; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | const BASE_URL = 'https://api.amplifypay.com/merchant'; |
||
45 | |||
46 | /** |
||
47 | * Only pass a client constructor in if it (client object) has been properly bootstrapped |
||
48 | * AmplifypayAdapter constructor. |
||
49 | * @param \GuzzleHttp\Client|null $client |
||
50 | */ |
||
51 | 25 | public function __construct(Client $client = null) |
|
52 | { |
||
53 | 25 | $this->httpClient = $client ?? $this->setHttpClient(); |
|
54 | |||
55 | 25 | $this->apiKeys = [ |
|
56 | 25 | "merchantId" => env("AMPLIFYPAY_MERCHANT_ID"), |
|
57 | 25 | "apiKey" => env("AMPLIFYPAY_API_KEY") |
|
58 | ]; |
||
59 | |||
60 | 25 | $this->registerPlugins(); |
|
61 | 25 | } |
|
62 | |||
63 | 25 | protected function registerPlugins() |
|
64 | { |
||
65 | 25 | $this->addPlugin(new GetPaymentData(self::BASE_URL, $this->apiKeys)) |
|
66 | 25 | ->addPlugin(new ChargeWithToken(self::BASE_URL, $this->apiKeys)) |
|
67 | 25 | ->addPlugin(new UnsubscribeCustomer(self::BASE_URL, $this->apiKeys)); |
|
68 | 25 | } |
|
69 | |||
70 | 3 | public function charge(array $data) |
|
71 | { |
||
72 | |||
73 | 3 | $response = $this->authorizeTransaction('/transact', array_merge($this->apiKeys, $data)); |
|
74 | |||
75 | 2 | $response = json_decode($response->getBody(), true); |
|
76 | |||
77 | 2 | $this->verifyKeys($response['ApiKey'], $this->apiKeys['apiKey']); |
|
78 | |||
79 | 1 | return $response['PaymentUrl']; |
|
80 | } |
||
81 | |||
82 | 3 | protected function authorizeTransaction(string $relative, array $data = null) |
|
83 | { |
||
84 | /** |
||
85 | * @var ResponseInterface $response |
||
86 | */ |
||
87 | 3 | $response = $this->httpClient->post( |
|
88 | 3 | self::BASE_URL . $relative, [ |
|
89 | 3 | 'body' => json_encode($data) |
|
90 | ] |
||
91 | ); |
||
92 | |||
93 | 3 | if ($response->getStatusCode() === 200) { |
|
94 | 2 | return $response; |
|
95 | } |
||
96 | |||
97 | 1 | throw InvalidHttpResponseException::createFromResponse($response); |
|
98 | } |
||
99 | |||
100 | /** |
||
101 | * @return \GuzzleHttp\Client |
||
102 | */ |
||
103 | 15 | public function getHttpClient(): Client |
|
104 | { |
||
105 | 15 | return $this->httpClient; |
|
106 | } |
||
107 | |||
108 | /** |
||
109 | * @codeCoverageIgnore |
||
110 | * @return \GuzzleHttp\Client |
||
111 | */ |
||
112 | View Code Duplication | protected function setHttpClient() : Client |
|
0 ignored issues
–
show
|
|||
113 | { |
||
114 | return new Client( |
||
115 | [ |
||
116 | 'base_uri' => self::BASE_URL, |
||
117 | 'headers' => [ |
||
118 | 'Content-Type' => 'application/json', |
||
119 | 'Accept' => 'application/json', |
||
120 | 'Cache-Control' => 'no-cache' |
||
121 | ] |
||
122 | ] |
||
123 | ); |
||
124 | } |
||
125 | } |
||
126 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.