1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GloBee\PaymentApi; |
4
|
|
|
|
5
|
|
|
use GloBee\PaymentApi\Connectors\Connector; |
6
|
|
|
use GloBee\PaymentApi\Exceptions\PaymentRequestAlreadyExistsException; |
7
|
|
|
use GloBee\PaymentApi\Models\Account; |
8
|
|
|
use GloBee\PaymentApi\Models\Currency; |
9
|
|
|
use GloBee\PaymentApi\Models\PaymentRequest; |
10
|
|
|
|
11
|
|
|
class PaymentApi |
12
|
|
|
{ |
13
|
|
|
const VERSION = '0.5.0'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var Connector |
17
|
|
|
*/ |
18
|
|
|
private $connector; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* PaymentApi constructor. |
22
|
|
|
* |
23
|
|
|
* @param Connector $connector |
|
|
|
|
24
|
|
|
*/ |
25
|
5 |
|
public function __construct(Connector $connector) |
26
|
|
|
{ |
27
|
5 |
|
$this->connector = $connector; |
28
|
5 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return Account |
32
|
|
|
*/ |
33
|
1 |
|
public function getAccount() |
34
|
|
|
{ |
35
|
1 |
|
$data = $this->connector->getJson('v1/ping'); |
36
|
|
|
|
37
|
1 |
|
return Account::fromResponse($data['data']); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return Currency[] |
42
|
|
|
*/ |
43
|
1 |
|
public function getCurrencies() |
44
|
|
|
{ |
45
|
1 |
|
$data = $this->connector->getJson('v1/currencies'); |
46
|
|
|
|
47
|
1 |
|
return Currency::fromResponse($data['data']); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $paymentRequestId |
|
|
|
|
52
|
|
|
* |
53
|
|
|
* @return PaymentRequest |
54
|
|
|
*/ |
55
|
1 |
|
public function getPaymentRequest($paymentRequestId) |
|
|
|
|
56
|
|
|
{ |
57
|
1 |
|
$response = $this->connector->getJson('v1/payment-request/'.$paymentRequestId); |
58
|
|
|
|
59
|
1 |
|
return PaymentRequest::fromResponse($response['data']); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param PaymentRequest $paymentRequest |
|
|
|
|
64
|
|
|
* |
65
|
|
|
* @return PaymentRequest |
66
|
|
|
* @throws PaymentRequestAlreadyExistsException |
|
|
|
|
67
|
|
|
*/ |
68
|
2 |
|
public function createPaymentRequest(PaymentRequest $paymentRequest) |
69
|
|
|
{ |
70
|
2 |
|
if ($paymentRequest->id !== null) { |
|
|
|
|
71
|
1 |
|
throw new PaymentRequestAlreadyExistsException(); |
72
|
|
|
} |
73
|
1 |
|
$data = $this->filterData($paymentRequest->toArray()); |
74
|
1 |
|
$response = $this->connector->postJson('v1/payment-request', $data); |
75
|
|
|
|
76
|
1 |
|
return PaymentRequest::fromResponse($response['data']); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param array $data |
|
|
|
|
81
|
|
|
* |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
1 |
|
protected function filterData(array $data) |
85
|
|
|
{ |
86
|
|
|
$data = array_map(function ($item) { |
87
|
1 |
|
if (is_array($item)) { |
88
|
1 |
|
$item = $this->filterData($item); |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
return $item; |
92
|
1 |
|
}, $data); |
93
|
|
|
|
94
|
|
|
return array_filter($data, function ($item) { |
95
|
1 |
|
if (is_array($item)) { |
96
|
1 |
|
return !empty($item); |
97
|
|
|
} |
98
|
|
|
|
99
|
1 |
|
return $item !== null; |
100
|
1 |
|
}); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|