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\Paystack; |
||
4 | |||
5 | use GuzzleHttp\Client; |
||
6 | use function Gbowo\env; |
||
7 | use Gbowo\Traits\Pluggable; |
||
8 | use Gbowo\Contract\Adapter\AdapterInterface; |
||
9 | use function GuzzleHttp\json_decode; |
||
10 | use Psr\Http\Message\ResponseInterface; |
||
11 | use Gbowo\Adapter\Paystack\Traits\Payable; |
||
12 | use Gbowo\Adapter\Paystack\Plugin\GetPaymentData; |
||
13 | use Gbowo\Adapter\Paystack\Plugin\ChargeWithToken; |
||
14 | use Gbowo\Adapter\Paystack\Traits\VerifyHttpStatusResponseCode; |
||
15 | |||
16 | /** |
||
17 | * @method findCustomer(int $customerId) |
||
18 | * @method getPaymentData(string $transactionReference) |
||
19 | * @method chargeWithToken(array $data) |
||
20 | * @author Lanre Adelowo <[email protected]> |
||
21 | * Class PaystackAdapter |
||
22 | * @package Gbowo\Adapter\Paystack |
||
23 | */ |
||
24 | class PaystackAdapter implements AdapterInterface |
||
25 | { |
||
26 | use Pluggable, Payable, VerifyHttpStatusResponseCode; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | const API_LINK = 'https://api.paystack.co'; |
||
32 | |||
33 | /** |
||
34 | * @var \GuzzleHttp\Client |
||
35 | */ |
||
36 | protected $httpClient; |
||
37 | |||
38 | /** |
||
39 | * The Api link for paystack |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $baseUrl; |
||
43 | |||
44 | /** |
||
45 | * PaystackAdapter constructor. |
||
46 | * @param \GuzzleHttp\Client|null $client |
||
47 | */ |
||
48 | 25 | public function __construct(Client $client = null) |
|
49 | { |
||
50 | 25 | $this->baseUrl = self::API_LINK; |
|
51 | 25 | $this->httpClient = $client ?? $this->setHttpClient(env("PAYSTACK_SECRET_KEY")); |
|
52 | 25 | $this->registerPlugins(); |
|
53 | 25 | } |
|
54 | |||
55 | 25 | protected function registerPlugins() |
|
56 | { |
||
57 | 25 | $this->addPlugin(new GetPaymentData($this->baseUrl)) |
|
58 | 25 | ->addPlugin(new ChargeWithToken($this->baseUrl)); |
|
59 | 25 | } |
|
60 | |||
61 | /** |
||
62 | * @param array $data |
||
63 | * @return string The authorization url to render the secure payment gateway. |
||
64 | */ |
||
65 | 2 | public function charge(array $data) |
|
66 | { |
||
67 | 2 | $response = $this->authorizeTransaction("/transaction/initialize", $data); |
|
68 | |||
69 | 2 | $this->verifyResponse($response); |
|
70 | |||
71 | 1 | return $this->decodeResponse($response)['data']['authorization_url']; |
|
72 | } |
||
73 | |||
74 | /** |
||
75 | * @param \Psr\Http\Message\ResponseInterface $response |
||
76 | * @return mixed |
||
77 | */ |
||
78 | 1 | protected function decodeResponse(ResponseInterface $response) |
|
79 | { |
||
80 | 1 | return json_decode($response->getBody(), true); |
|
81 | } |
||
82 | |||
83 | /** |
||
84 | * @return \GuzzleHttp\Client |
||
85 | */ |
||
86 | 16 | public function getHttpClient(): Client |
|
87 | { |
||
88 | 16 | return $this->httpClient; |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * @codeCoverageIgnore |
||
93 | * @param string $token |
||
94 | * @return \GuzzleHttp\Client |
||
95 | */ |
||
96 | View Code Duplication | protected function setHttpClient(string $token) : Client |
|
0 ignored issues
–
show
|
|||
97 | { |
||
98 | return new Client( |
||
99 | [ |
||
100 | 'base_uri' => $this->baseUrl, |
||
101 | 'headers' => [ |
||
102 | 'Authorization' => 'Bearer ' . $token, |
||
103 | 'Content-Type' => 'application/json', |
||
104 | 'Accept' => 'application/json' |
||
105 | ] |
||
106 | ] |
||
107 | ); |
||
108 | } |
||
109 | } |
||
110 |
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.