-
Notifications
You must be signed in to change notification settings - Fork 1
/
Api.php
263 lines (210 loc) · 7.25 KB
/
Api.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
namespace Payum\Paypal\ProHosted\Nvp;
use Http\Message\MessageFactory;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Exception\Http\HttpException;
use Payum\Core\Exception\InvalidArgumentException;
use Payum\Core\Exception\RuntimeException;
use Payum\Core\HttpClientInterface;
/**
* @link https://developer.paypal.com/webapps/developer/docs/classic/products/website-payments-pro-hosted-solution
* @link https://www.paypalobjects.com/webstatic/en_GB/developer/docs/pdf/hostedsolution_uk.pdf
* @link https://developer.paypal.com/docs/classic/api/button-manager/BMCreateButton_API_Operation_NVP/
* L_ERRORCODE @link https://developer.paypal.com/webapps/developer/docs/classic/api/errorcodes/#id09C3GA00GR1
*/
class Api
{
public const VERSION = '65.2';
public const ACK_SUCCESS = 'Success';
public const ACK_SUCCESS_WITH_WARNING = 'SuccessWithWarning';
public const ACK_FAILURE = 'Failure';
public const ACK_FAILUREWITHWARNING = 'FailureWithWarning';
public const ACK_WARNING = 'Warning';
/**
* No status
*/
public const PAYMENTSTATUS_NONE = 'None';
/**
* A reversal has been canceled; for example, when you win a dispute and the funds for the reversal have been returned to you.
*/
public const PAYMENTSTATUS_CANCELED_REVERSAL = 'Canceled-Reversal';
/**
* The payment has been completed, and the funds have been added successfully to your account balance.
*/
public const PAYMENTSTATUS_COMPLETED = 'Completed';
/**
* You denied the payment. This happens only if the payment was previously pending because of possible reasons described for the PendingReason element.
*/
public const PAYMENTSTATUS_DENIED = 'Denied';
/**
* The authorization period for this payment has been reached.
*/
public const PAYMENTSTATUS_EXPIRED = 'Expired';
/**
* The payment has failed. This happens only if the payment was made from your buyer's bank account.
*/
public const PAYMENTSTATUS_FAILED = 'Failed';
/**
* The transaction has not terminated, e.g. an authorization may be awaiting completion.
*/
public const PAYMENTSTATUS_IN_PROGRESS = 'In-Progress';
/**
* The payment has been partially refunded.
*/
public const PAYMENTSTATUS_PARTIALLY_REFUNDED = 'Partially-Refunded';
/**
* The payment is pending. See the PendingReason field for more information.
*/
public const PAYMENTSTATUS_PENDING = 'Pending';
/**
* A payment was reversed due to a chargeback or other type of reversal.
* The funds have been removed from your account balance and returned to the buyer.
* The reason for the reversal is specified in the ReasonCode element.
*/
public const PAYMENTSTATUS_REVERSED = 'Reversed';
/**
* You refunded the payment.
*/
public const PAYMENTSTATUS_REFUNDED = 'Refunded';
/**
* A payment has been accepted.
*/
public const PAYMENTSTATUS_PROCESSED = 'Processed';
public const PAYERSTATUS_VERIFIED = 'verified';
public const PAYERSTATUS_UNVERIFIED = 'unverified';
public const PENDINGREASON_AUTHORIZATION = 'authorization';
public const PAYMENTACTION_SALE = 'sale';
public const FORM_CMD = '_hosted-payment';
/**
* @var HttpClientInterface
*/
protected $client;
/**
* @var MessageFactory
*/
protected $messageFactory;
/**
* @var array
*/
protected $options = [
'username' => null,
'password' => null,
'signature' => null,
'business' => null,
'return' => null,
'sandbox' => null,
'cmd' => self::FORM_CMD,
];
/**
* @throws InvalidArgumentException if an option is invalid
*/
public function __construct(array $options, HttpClientInterface $client, MessageFactory $messageFactory)
{
$options = ArrayObject::ensureArrayObject($options);
$options->defaults($this->options);
$options->validateNotEmpty([
'username',
'password',
'signature',
]);
if (! is_bool($options['sandbox'])) {
throw new InvalidArgumentException('The boolean sandbox option must be set.');
}
$this->options = $options;
$this->client = $client;
$this->messageFactory = $messageFactory;
}
/**
* Solution BMCreateButton
*
* @throws RuntimeException
*
* @return array
*/
public function doCreateButton(array $fields)
{
if (! isset($fields['return'])) {
if (! $this->options['return']) {
throw new RuntimeException('The return must be set either to FormRequest or to options.');
}
$fields['return'] = $this->options['return'];
}
$fields['paymentaction'] = self::PAYMENTACTION_SALE;
$fields['cmd'] = self::FORM_CMD;
$newFields = [];
$i = 0;
foreach ($fields as $key => $val) {
$newFields['L_BUTTONVAR' . $i] = $key . '=' . $val;
$i++;
}
$newFields['METHOD'] = 'BMCreateButton';
$newFields['BUTTONTYPE'] = 'PAYMENT';
$newFields['BUTTONCODE'] = 'TOKEN';
$this->addVersionField($newFields);
$this->addAuthorizeFields($newFields);
return $this->doRequest($newFields);
}
/**
* Require: TRANSACTIONID
*
* @param array $fields
*
* @return array
*/
public function getTransactionDetails($fields)
{
$fields['METHOD'] = 'GetTransactionDetails';
$this->addAuthorizeFields($fields);
$this->addVersionField($fields);
return $this->doRequest($fields);
}
/**
* @return bool
*/
public function isEnvironnementTest()
{
return $this->options['sandbox'];
}
/**
* @throws HttpException
*
* @return array
*/
protected function doRequest(array $fields)
{
$headers = [
'Content-Type' => 'application/x-www-form-urlencoded',
];
$request = $this->messageFactory->createRequest('POST', $this->getApiEndpoint(), $headers, http_build_query($fields));
$response = $this->client->send($request);
if (! ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300)) {
throw HttpException::factory($request, $response);
}
parse_str($response->getBody()->getContents(), $result);
foreach ($result as &$value) {
$value = urldecode($value);
}
return $result;
}
/**
* @return string
*/
protected function getApiEndpoint()
{
return $this->options['sandbox'] ? 'https://api-3t.sandbox.paypal.com/nvp' : 'https://api-3t.paypal.com/nvp';
}
protected function addAuthorizeFields(array &$fields): void
{
$fields['USER'] = $this->options['username'];
$fields['PWD'] = $this->options['password'];
$fields['SIGNATURE'] = $this->options['signature'];
if ($this->options['business']) {
$fields['BUSINESS'] = $this->options['business'];
$fields['SUBJECT'] = $this->options['business'];
}
}
protected function addVersionField(array &$fields): void
{
$fields['VERSION'] = self::VERSION;
}
}