-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAmplifypayAdapter.php
125 lines (104 loc) · 3.24 KB
/
AmplifypayAdapter.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
namespace Gbowo\Adapter\Amplifypay;
use GuzzleHttp\Client;
use function Gbowo\env;
use Gbowo\Traits\Pluggable;
use function GuzzleHttp\json_decode;
use Psr\Http\Message\ResponseInterface;
use Gbowo\Contract\Adapter\AdapterInterface;
use Gbowo\Adapter\Amplifypay\Traits\KeyVerifier;
use Gbowo\Exception\InvalidHttpResponseException;
use Gbowo\Adapter\Amplifypay\Plugin\GetPaymentData;
use Gbowo\Adapter\Amplifypay\Plugin\ChargeWithToken;
use Gbowo\Adapter\Amplifypay\Plugin\UnsubscribeCustomer;
/**
* @method chargeWithToken(array $data)
* @method unsubcribeCustomerFromPlan(array $data)
* @method getPaymentData(string $transactionReference)
* @author Lanre Adelowo <[email protected]>
* Class AmplifypayAdapter
* @package Gbowo\Adapter\Amplifypay
*/
class AmplifypayAdapter implements AdapterInterface
{
use Pluggable, KeyVerifier;
/**
* @var \GuzzleHttp\Client
*/
protected $httpClient;
/**
* Keys to initiate a successful request
* @var array
*/
protected $apiKeys;
/**
* @var string
*/
const BASE_URL = 'https://api.amplifypay.com/merchant';
/**
* Only pass a client constructor in if it (client object) has been properly bootstrapped
* AmplifypayAdapter constructor.
* @param \GuzzleHttp\Client|null $client
*/
public function __construct(Client $client = null)
{
$this->httpClient = $client ?? $this->setHttpClient();
$this->apiKeys = [
"merchantId" => env("AMPLIFYPAY_MERCHANT_ID"),
"apiKey" => env("AMPLIFYPAY_API_KEY")
];
$this->registerPlugins();
}
protected function registerPlugins()
{
$this->addPlugin(new GetPaymentData(self::BASE_URL, $this->apiKeys))
->addPlugin(new ChargeWithToken(self::BASE_URL, $this->apiKeys))
->addPlugin(new UnsubscribeCustomer(self::BASE_URL, $this->apiKeys));
}
public function charge(array $data)
{
$response = $this->authorizeTransaction('/transact', array_merge($this->apiKeys, $data));
$response = json_decode($response->getBody(), true);
$this->verifyKeys($response['ApiKey'], $this->apiKeys['apiKey']);
return $response['PaymentUrl'];
}
protected function authorizeTransaction(string $relative, array $data = null)
{
/**
* @var ResponseInterface $response
*/
$response = $this->httpClient->post(
self::BASE_URL . $relative, [
'body' => json_encode($data)
]
);
if ($response->getStatusCode() === 200) {
return $response;
}
throw InvalidHttpResponseException::createFromResponse($response);
}
/**
* @return \GuzzleHttp\Client
*/
public function getHttpClient(): Client
{
return $this->httpClient;
}
/**
* @codeCoverageIgnore
* @return \GuzzleHttp\Client
*/
protected function setHttpClient() : Client
{
return new Client(
[
'base_uri' => self::BASE_URL,
'headers' => [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'Cache-Control' => 'no-cache'
]
]
);
}
}