|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Gbowo\Adapter\Paystack\Plugin; |
|
4
|
|
|
|
|
5
|
|
|
use Gbowo\Adapter\Paystack\Exception\TransactionVerficationFailedException; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
use Gbowo\Contract\Customer\BillInterface; |
|
8
|
|
|
use function GuzzleHttp\json_decode; |
|
9
|
|
|
use Gbowo\Plugin\AbstractChargeWithToken; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Charge a customer with the token returned from the first transaction initiated with the Paystack |
|
13
|
|
|
* @author Lanre Adelowo <[email protected]> |
|
14
|
|
|
* Class ChargeWithToken |
|
15
|
|
|
* @package Gbowo\Adapter\Paystack\Plugin |
|
16
|
|
|
*/ |
|
17
|
|
|
class ChargeWithToken extends AbstractChargeWithToken implements BillInterface |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The relative link for charging users |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
const TOKEN_CHARGE_RELATIVE_LINK = "/charge_token"; |
|
25
|
|
|
|
|
26
|
|
|
const SUCCESS_MESSAGE = "Charge successful"; |
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $baseUrl; |
|
31
|
|
|
|
|
32
|
13 |
|
public function __construct(string $baseUrl) |
|
33
|
|
|
{ |
|
34
|
13 |
|
$this->baseUrl = $baseUrl; |
|
35
|
13 |
|
} |
|
36
|
|
|
|
|
37
|
3 |
|
public function handle(array $data) |
|
38
|
|
|
{ |
|
39
|
3 |
|
if (!array_key_exists("token", $data)) { |
|
40
|
1 |
|
throw new InvalidArgumentException( |
|
41
|
1 |
|
"A token must be specified" |
|
42
|
|
|
); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
2 |
|
$response = $this->chargeByToken($data); |
|
46
|
|
|
|
|
47
|
2 |
|
$res = json_decode($response->getBody(), true); |
|
48
|
|
|
|
|
49
|
2 |
|
if (strcmp($res['message'], self::SUCCESS_MESSAGE) !== 0) { |
|
50
|
1 |
|
throw new TransactionVerficationFailedException( |
|
51
|
1 |
|
"The transaction was not successful" |
|
52
|
|
|
); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
1 |
|
return $res['data']; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
2 |
|
public function chargeByToken($token) |
|
59
|
|
|
{ |
|
60
|
2 |
|
return $this->adapter->getHttpClient() |
|
61
|
2 |
|
->post($this->baseUrl . self::TOKEN_CHARGE_RELATIVE_LINK); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|