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