1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Omnipay\PaypalRest\Message; |
4
|
|
|
|
5
|
|
|
use Omnipay\Common\Exception\InvalidRequestException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @author Ivan Kerin <[email protected]> |
9
|
|
|
* @copyright 2014, Clippings Ltd. |
10
|
|
|
* @license http://spdx.org/licenses/BSD-3-Clause |
11
|
|
|
*/ |
12
|
|
|
class PaymentRequest extends AbstractPaypalRequest |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @return string |
16
|
|
|
*/ |
17
|
|
|
public function getEndpoint() |
18
|
|
|
{ |
19
|
|
|
return '/payments/payment'; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @return string |
24
|
|
|
*/ |
25
|
|
|
public function getIntent() |
26
|
|
|
{ |
27
|
|
|
return $this->getParameter('intent'); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param string $value |
32
|
|
|
*/ |
33
|
|
|
public function setIntent($value) |
34
|
|
|
{ |
35
|
|
|
return $this->setParameter('intent', $value); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
|
|
public function getPayerId() |
42
|
|
|
{ |
43
|
|
|
return $this->getParameter('payerId'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $value |
48
|
|
|
*/ |
49
|
|
|
public function setPayerId($value) |
50
|
|
|
{ |
51
|
|
|
return $this->setParameter('payerId', $value); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param mixed $data |
56
|
|
|
* @return Omnipay\PaypalRest\Message\PaymentResponse |
57
|
|
|
*/ |
58
|
|
|
public function sendData($data) |
59
|
|
|
{ |
60
|
|
|
$httpResponse = $this->sendHttpRequest($data); |
61
|
|
|
|
62
|
|
|
if ($this->getRequiredRedirect()) { |
63
|
|
|
return $this->response = new PaymentApproveResponse( |
64
|
|
|
$this, |
65
|
|
|
$httpResponse->json(), |
66
|
|
|
$httpResponse->getStatusCode() |
67
|
|
|
); |
68
|
|
|
} else { |
69
|
|
|
return $this->response = new PaymentResponse( |
70
|
|
|
$this, |
71
|
|
|
$httpResponse->json(), |
72
|
|
|
$httpResponse->getStatusCode() |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* array|null |
79
|
|
|
*/ |
80
|
|
|
public function getItemsData() |
81
|
|
|
{ |
82
|
|
|
$item_list = array(); |
83
|
|
|
|
84
|
|
|
if ($this->getItems()) { |
85
|
|
|
foreach ($this->getItems() as $item) { |
86
|
|
|
$item_list['items'][] = array( |
87
|
|
|
'name' => mb_strimwidth($item->getName(), 0, 126, '…'), |
88
|
|
|
'quantity' => $item->getQuantity(), |
89
|
|
|
'price' => $item->getPrice(), |
90
|
|
|
'description' => mb_strimwidth($item->getDescription(), 0, 126, '…'), |
91
|
|
|
'currency' => $this->getCurrency(), |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $item_list; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
public function getTransactionData() |
103
|
|
|
{ |
104
|
|
|
$this->validate('currency', 'amount'); |
105
|
|
|
|
106
|
|
|
return array( |
107
|
|
|
'transactions' => array( |
108
|
|
|
array_filter(array( |
109
|
|
|
'description' => $this->getDescription(), |
110
|
|
|
'amount' => array( |
111
|
|
|
'total' => $this->getAmount(), |
112
|
|
|
'currency' => $this->getCurrency(), |
113
|
|
|
), |
114
|
|
|
'item_list' => $this->getItemsData() |
115
|
|
|
)) |
116
|
|
|
), |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
|
|
public function getPayerPaypalData() |
124
|
|
|
{ |
125
|
|
|
$this->validate('returnUrl', 'cancelUrl'); |
126
|
|
|
|
127
|
|
|
return array( |
128
|
|
|
'payer' => array( |
129
|
|
|
'payment_method' => 'paypal', |
130
|
|
|
), |
131
|
|
|
'redirect_urls' => array( |
132
|
|
|
'return_url' => $this->getReturnUrl(), |
133
|
|
|
'cancel_url' => $this->getCancelUrl(), |
134
|
|
|
), |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return array |
140
|
|
|
*/ |
141
|
|
|
public function getPayerCardReferenceData() |
142
|
|
|
{ |
143
|
|
|
$this->validate('cardReference'); |
144
|
|
|
|
145
|
|
|
return array( |
146
|
|
|
'payer' => array( |
147
|
|
|
'payment_method' => 'credit_card', |
148
|
|
|
'funding_instruments' => array( |
149
|
|
|
array( |
150
|
|
|
'credit_card_token' => array_filter(array( |
151
|
|
|
'credit_card_id' => $this->getCardReference(), |
152
|
|
|
'payer_id' => $this->getPayerId() |
153
|
|
|
)), |
154
|
|
|
), |
155
|
|
|
), |
156
|
|
|
), |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return array |
162
|
|
|
*/ |
163
|
|
|
public function getPayerCardData() |
164
|
|
|
{ |
165
|
|
|
return array( |
166
|
|
|
'payer' => array( |
167
|
|
|
'payment_method' => 'credit_card', |
168
|
|
|
'funding_instruments' => array( |
169
|
|
|
array( |
170
|
|
|
'credit_card' => $this->getPaypalCard() |
171
|
|
|
), |
172
|
|
|
), |
173
|
|
|
), |
174
|
|
|
); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return boolean |
179
|
|
|
*/ |
180
|
|
|
public function getRequiredRedirect() |
181
|
|
|
{ |
182
|
|
|
return false === ($this->getCardReference() or $this->getCard()); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return array |
187
|
|
|
*/ |
188
|
|
|
public function getPayerData() |
189
|
|
|
{ |
190
|
|
|
if ($this->getCardReference()) { |
191
|
|
|
return $this->getPayerCardReferenceData(); |
192
|
|
|
} elseif ($this->getCard()) { |
193
|
|
|
return $this->getPayerCardData(); |
194
|
|
|
} else { |
195
|
|
|
return $this->getPayerPaypalData(); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @return array |
201
|
|
|
*/ |
202
|
|
|
public function getData() |
203
|
|
|
{ |
204
|
|
|
$this->validate('intent'); |
205
|
|
|
|
206
|
|
|
if (false === in_array($this->getIntent(), array('sale', 'authorize'))) { |
207
|
|
|
throw new InvalidRequestException('Intent can only be "sale" or "authorize"'); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
return array_merge_recursive( |
211
|
|
|
array('intent' => $this->getIntent()), |
212
|
|
|
$this->getTransactionData(), |
213
|
|
|
$this->getPayerData() |
214
|
|
|
); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|