-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathApi.php
executable file
·306 lines (264 loc) · 9.83 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
namespace Valiton\Payum\Payone;
use GuzzleHttp\Psr7\Request;
use Payum\Core\Bridge\Guzzle\HttpClientFactory;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Exception\Http\HttpException;
use Payum\Core\Exception\InvalidArgumentException;
use Payum\Core\HttpClientInterface;
class Api
{
public const SOLUTION_NAME = 'valiton';
public const SOLUTION_VERSION = '0.6';
public const INTEGRATOR_NAME = 'payum';
public const INTEGRATOR_VERSION = '1.2';
public const FIELD_AMOUNT = 'amount';
public const FIELD_BIC = 'bic';
public const FIELD_CITY = 'city';
public const FIELD_COMPANY = 'company';
public const FIELD_COUNTRY = 'country';
public const FIELD_CREDITOR_IDENTIFIER = 'creditor_identifier';
public const FIELD_CURRENCY = 'currency';
public const FIELD_CURRENCY_CODE = 'currency';
public const FIELD_CUSTOMER_MESSAGE = 'customermessage';
public const FIELD_EMAIL = 'email';
public const FIELD_ERROR_CODE = 'errorcode';
public const FIELD_ERROR_MESSAGE = 'errormessage';
public const FIELD_FILE_CONTENTS = 'file_contents';
public const FIELD_FILE_FORMAT = 'file_format';
public const FIELD_FILE_REFERENCE = 'file_reference';
public const FIELD_FILE_TYPE = 'file_type';
public const FIELD_FIRST_NAME = 'firstname';
public const FIELD_IBAN = 'iban';
public const FIELD_LANGUAGE = 'language';
public const FIELD_LAST_NAME = 'lastname';
public const FIELD_MANDATE_DATE = 'mandate_confirmed';
public const FIELD_MANDATE_IDENTIFICATION = 'mandate_identification';
public const FIELD_MANDATE_STATUS = 'mandate_status';
public const FIELD_MANDATE_TEXT = 'mandate_text';
public const FIELD_NARRATIVE_TEXT = 'narrative_text';
public const FIELD_PAYMENT_METHOD = 'payment_method';
public const FIELD_PSEUDO_CARD_PAN = 'pseudocardpan';
public const FIELD_REFERENCE = 'reference';
public const FIELD_SHIPPING_CITY = 'shipping_city';
public const FIELD_SHIPPING_COMPANY = 'shipping_company';
public const FIELD_SHIPPING_COUNTRY = 'shipping_country';
public const FIELD_SHIPPING_FIRST_NAME = 'shipping_firstname';
public const FIELD_SHIPPING_LAST_NAME = 'shipping_lastname';
public const FIELD_SHIPPING_STREET = 'shipping_street';
public const FIELD_SHIPPING_ZIP = 'shipping_zip';
public const FIELD_STATUS = 'status';
public const FIELD_TRANSACTION_STATUS = 'transaction_status';
public const FIELD_TRUNCATED_CARD_PAN = 'truncatedcardpan';
public const FIELD_TX_ACTION = 'txaction';
public const FIELD_ZIP = 'zip';
/**
* @deprecated Use FIELD_FIRST_NAME instead
*/
public const FIELD_FIRSTNAME = 'firstname';
/**
* @deprecated Use FIELD_LAST_NAME instead
*/
public const FIELD_LASTNAME = 'lastname';
public const MANDATE_STATUS_ACTIVE = 'active';
public const MANDATE_STATUS_PENDING = 'pending';
public const PAYMENT_METHOD_CREDIT_CARD_PPAN = 'credit_card_ppan';
public const PAYMENT_METHOD_DIRECT_DEBIT_SEPA = 'direct_debit_sepa';
public const PAYMENT_METHOD_GIROPAY = 'giropay';
public const PAYMENT_METHOD_PAYDIREKT = 'paydirekt';
public const STATUS_APPROVED = 'APPROVED';
public const STATUS_ERROR = 'ERROR';
public const STATUS_REDIRECT = 'REDIRECT';
public const TRANSACTION_STATUS_COMPLETED = 'completed';
/**
* @var array
*/
protected $options = [];
/**
* @var HttpClientInterface
*/
protected $client;
/**
* @param array $options
* @param HttpClientInterface $client
*
* @throws \Payum\Core\Exception\InvalidArgumentException if an option is invalid
*/
public function __construct(array $options, HttpClientInterface $client = null)
{
$options = ArrayObject::ensureArrayObject($options);
$options->defaults($this->options);
$options->validateNotEmpty(array(
'merchant_id',
'portal_id',
'key',
'sub_account_id',
));
if (false === is_bool($options['sandbox'])) {
throw new InvalidArgumentException('The boolean sandbox option must be set.');
}
$this->options = $options;
$this->client = $client ?: HttpClientFactory::create();
}
/**
* @param array $fields
* @return array
*/
public function preauthorize(array $fields)
{
$fields['request'] = 'preauthorization';
return $this->decodeBody($this->doRequest($fields));
}
/**
* @param array $fields
* @return array
*/
public function authorize(array $fields)
{
$fields['request'] = 'authorization';
return $this->decodeBody($this->doRequest($fields));
}
/**
* @param array $fields
* @return array
*/
public function capture(array $fields)
{
$fields['request'] = 'capture';
return $this->decodeBody($this->doRequest($fields));
}
/**
* @param array $fields
* @return array
*/
public function manageMandate(array $fields)
{
$fields['clearingtype'] = 'elv';
$fields['request'] = 'managemandate';
return $this->decodeBody($this->doRequest($fields));
}
/**
* @param array $fields
* @return string
*/
public function getFile(array $fields)
{
$fields = array_intersect_key(
$fields,
array_flip([
Api::FIELD_FILE_FORMAT,
Api::FIELD_FILE_REFERENCE,
Api::FIELD_FILE_TYPE,
])
);
$fields['request'] = 'getfile';
return $this->doRequest($fields);
}
public function refund(array $fields)
{
$fields['request'] = 'refund';
$fields['sequencenumber'] = 2;
return $this->decodeBody($this->doRequest($fields));
}
/**
* @param array $fields
*
* @return string
*/
protected function doRequest(array $fields)
{
$headers = [
'Content-Type' => 'application/x-www-form-urlencoded',
];
$fields['mid'] = $this->options['merchant_id'];
$fields['portalid'] = $this->options['portal_id'];
$fields['key'] = hash('md5', $this->options['key']);
if ('getfile' !== $fields['request']) {
// "aid" may not be passed for getfile requests
$fields['aid'] = $this->options['sub_account_id'];
}
$fields['api_version'] = '3.9';
$fields['mode'] = $this->options['sandbox'] ? 'test' : 'live';
$fields['encoding'] = 'UTF-8';
$fields['solution_name'] = static::SOLUTION_NAME;
$fields['solution_version'] = static::SOLUTION_VERSION;
$fields['integrator_name'] = static::INTEGRATOR_NAME;
$fields['integrator_version'] = static::INTEGRATOR_VERSION;
$narrativeTextMaxLength = 81;
if (array_key_exists(static::FIELD_PAYMENT_METHOD, $fields)) {
switch ($fields[static::FIELD_PAYMENT_METHOD]) {
case Api::PAYMENT_METHOD_CREDIT_CARD_PPAN:
unset(
$fields[static::FIELD_BIC],
$fields[static::FIELD_IBAN]
);
$fields['clearingtype'] = 'cc';
break;
case Api::PAYMENT_METHOD_DIRECT_DEBIT_SEPA:
$fields['clearingtype'] = 'elv';
break;
case Api::PAYMENT_METHOD_GIROPAY:
$fields['clearingtype'] = 'sb';
$fields['onlinebanktransfertype'] = 'GPY';
break;
case Api::PAYMENT_METHOD_PAYDIREKT:
unset(
$fields[static::FIELD_BIC],
$fields[static::FIELD_IBAN]
);
$fields['clearingtype'] = 'wlt';
$fields['wallettype'] = 'PDT';
$narrativeTextMaxLength = 37;
break;
default:
throw new InvalidArgumentException('Invalid payment method "' . $fields[static::FIELD_PAYMENT_METHOD] . '"');
}
unset($fields[Api::FIELD_PAYMENT_METHOD]);
}
// remove some fields from request
$fields = array_diff_key(
$fields,
array_flip([
Api::FIELD_CUSTOMER_MESSAGE,
Api::FIELD_ERROR_CODE,
Api::FIELD_ERROR_MESSAGE,
'completed_status',
])
);
// validate fields
if (array_key_exists(static::FIELD_NARRATIVE_TEXT, $fields) && $narrativeTextMaxLength < strlen($fields[static::FIELD_NARRATIVE_TEXT])) {
throw new InvalidArgumentException('Field "narrative_text" must not be longer than ' . $narrativeTextMaxLength . ' characters.');
}
$request = new Request('POST', $this->getApiEndpoint(), $headers, http_build_query($fields));
$response = $this->client->send($request);
if (false === ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300)) {
throw HttpException::factory($request, $response);
}
return (string)$response->getBody();
}
/**
* @param string $bodyContents
*
* @return array
*/
private function decodeBody($bodyContents)
{
$result = array();
foreach (explode("\n", $bodyContents) as $responseLine) {
$responseLine = urldecode($responseLine);
$delimiterPosition = strpos($responseLine, '=');
if (false === $delimiterPosition) {
continue;
}
$result[substr($responseLine, 0, $delimiterPosition)] = trim(substr($responseLine, $delimiterPosition + 1));
}
return $result;
}
/**
* @return string
*/
protected function getApiEndpoint()
{
return 'https://api.pay1.de/post-gateway/';
}
}