|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PagaMasTarde\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Httpful\Http; |
|
6
|
|
|
use Httpful\Mime; |
|
7
|
|
|
use PagaMasTarde\Model\Charge; |
|
8
|
|
|
use PagaMasTarde\Model\Refund; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class ChargeService |
|
12
|
|
|
* @package PagaMasTarde\Service |
|
13
|
|
|
*/ |
|
14
|
|
|
class ChargeService extends AbstractService |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Charges endpoint |
|
18
|
|
|
*/ |
|
19
|
|
|
const CHARGES_ENDPOINT = '/api/1/charges'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* URI Separator |
|
23
|
|
|
*/ |
|
24
|
|
|
const SLASH = '/'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Refunds Endpoint |
|
28
|
|
|
*/ |
|
29
|
|
|
const REFUNDS_ENDPOINT = '/refunds'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param int $page |
|
33
|
|
|
* |
|
34
|
|
|
* @return Charge[] |
|
35
|
|
|
*/ |
|
36
|
|
|
public function getCharges($page = null) |
|
37
|
|
|
{ |
|
38
|
|
|
$response = $this->getRequest() |
|
39
|
|
|
->method(Http::GET) |
|
40
|
|
|
->uri($this->baseUri . self::CHARGES_ENDPOINT . $this->addGetParameters(array( |
|
41
|
|
|
'page' => $page, |
|
42
|
|
|
))) |
|
43
|
|
|
->send() |
|
44
|
|
|
; |
|
45
|
|
|
|
|
46
|
|
|
if ($response->hasErrors()) { |
|
47
|
|
|
return array(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
foreach ($response->body->response as $charge) { |
|
51
|
|
|
$charges[] = new Charge($charge); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return isset($charges) ? $charges : array(); |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param string $orderId |
|
59
|
|
|
* |
|
60
|
|
|
* @return Charge[] |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getChargesByOrderId($orderId) |
|
63
|
|
|
{ |
|
64
|
|
|
$response = $this->getRequest() |
|
65
|
|
|
->method(Http::GET) |
|
66
|
|
|
->uri($this->baseUri . self::CHARGES_ENDPOINT . $this->addGetParameters(array( |
|
67
|
|
|
'order_id' => $orderId, |
|
68
|
|
|
))) |
|
69
|
|
|
->send() |
|
70
|
|
|
; |
|
71
|
|
|
|
|
72
|
|
|
if ($response->hasErrors()) { |
|
73
|
|
|
return array(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
foreach ($response->body->response as $charge) { |
|
77
|
|
|
$charges[] = new Charge($charge); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return isset($charges) ? $charges : array(); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param string $orderId |
|
85
|
|
|
* |
|
86
|
|
|
* @return bool |
|
87
|
|
|
*/ |
|
88
|
|
|
public function validatePaymentForOrderId($orderId) |
|
89
|
|
|
{ |
|
90
|
|
|
$charges = $this->getChargesByOrderId($orderId); |
|
91
|
|
|
|
|
92
|
|
|
if (!empty($charges)) { |
|
93
|
|
|
foreach ($charges as $charge) { |
|
94
|
|
|
if ($charge->getPaid() === true && $charge->getStatus() == 'paid') { |
|
95
|
|
|
return true; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return false; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param string $id |
|
105
|
|
|
* |
|
106
|
|
|
* @return Charge | null |
|
107
|
|
|
*/ |
|
108
|
|
|
public function getChargeById($id) |
|
109
|
|
|
{ |
|
110
|
|
|
$response = $this->getRequest() |
|
111
|
|
|
->method(Http::GET) |
|
112
|
|
|
->uri($this->baseUri . self::CHARGES_ENDPOINT . '/' . $id) |
|
113
|
|
|
->send() |
|
114
|
|
|
; |
|
115
|
|
|
|
|
116
|
|
|
if ($response->hasErrors() || !$response->hasBody() || empty($response->body->response)) { |
|
117
|
|
|
return null; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return new Charge($response->body->response); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param Charge $charge |
|
125
|
|
|
* |
|
126
|
|
|
* @return null|Refund |
|
127
|
|
|
*/ |
|
128
|
|
|
public function refundCharge(Charge $charge) |
|
129
|
|
|
{ |
|
130
|
|
|
$response = $this->getRequest() |
|
131
|
|
|
->method(Http::POST) |
|
132
|
|
|
->uri( |
|
133
|
|
|
$this->baseUri . |
|
134
|
|
|
self::CHARGES_ENDPOINT . |
|
135
|
|
|
self::SLASH . |
|
136
|
|
|
$charge->getId() . |
|
137
|
|
|
self::REFUNDS_ENDPOINT |
|
138
|
|
|
) |
|
139
|
|
|
->body(array( |
|
140
|
|
|
'refund[amount]' => $charge->getAmount() |
|
141
|
|
|
), Mime::FORM) |
|
142
|
|
|
->expects(Mime::JSON) |
|
143
|
|
|
->send() |
|
144
|
|
|
; |
|
145
|
|
|
|
|
146
|
|
|
if ($response->hasErrors() || !$response->hasBody() || empty($response->body->response)) { |
|
147
|
|
|
return null; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
return new Refund($response->body->response); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @param Charge $charge |
|
155
|
|
|
* @param int $amount |
|
156
|
|
|
* |
|
157
|
|
|
* @return null|Refund |
|
158
|
|
|
*/ |
|
159
|
|
|
public function refundChargePartial(Charge $charge, $amount) |
|
160
|
|
|
{ |
|
161
|
|
|
$response = $this->getRequest() |
|
162
|
|
|
->method(Http::POST) |
|
163
|
|
|
->uri( |
|
164
|
|
|
$this->baseUri . |
|
165
|
|
|
self::CHARGES_ENDPOINT . |
|
166
|
|
|
self::SLASH . |
|
167
|
|
|
$charge->getId() . |
|
168
|
|
|
self::REFUNDS_ENDPOINT |
|
169
|
|
|
) |
|
170
|
|
|
->body(array( |
|
171
|
|
|
'refund[amount]' => $amount |
|
172
|
|
|
), Mime::FORM) |
|
173
|
|
|
->expects(Mime::JSON) |
|
174
|
|
|
->send() |
|
175
|
|
|
; |
|
176
|
|
|
|
|
177
|
|
|
if ($response->hasErrors() || !$response->hasBody() || empty($response->body->response)) { |
|
178
|
|
|
return null; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
return new Refund($response->body->response); |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|