/
Api.php
210 lines (177 loc) · 5.74 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
<?php
namespace Webburza\Payum\Setefi;
use Http\Message\MessageFactory;
use Payum\Core\Exception\Http\HttpException;
use Payum\Core\Exception\InvalidArgumentException;
use Payum\Core\HttpClientInterface;
use Webburza\Payum\Setefi\Response\Payment;
use Webburza\Payum\Setefi\Response\PaymentAuthorization;
/**
* Class Api.
*/
class Api
{
const CURRENCY_GBP = 'GBP';
const CURRENCY_EUR = 'EUR';
const CURRENCY_USD = 'USD';
const LANGUAGE_ITA = 'ITA';
const OPERATION_INITIALIZE = 'initialize';
// Setefi response properties
const PROPERTY_AUTHORIZATION = 'authorizationcode';
const PROPERTY_HOSTED_URL = 'hostedpageurl';
const PROPERTY_PAYMENT_ID = 'paymentid';
const PROPERTY_RESULT = 'result';
const PROPERTY_RESPONSE_CODE = 'responsecode';
const PROPERTY_SECURITY_TOKEN = 'securitytoken';
const PROPERTY_CARD_COUNTRY = 'cardcountry';
const PROPERTY_CARD_EXPIRY = 'cardexpirydate';
const PROPERTY_CARD_TYPE = 'cardtype';
const PROPERTY_CARD_MASKED_PAN = 'maskedpan';
const PROPERTY_CARD_3D_SECURE = 'threedsecure';
const PROPERTY_RRN = 'rrn';
const PROPERTY_CUSTOM_FIELD = 'customfield';
const STATUS_SUCCESS = '000';
const STATUS_CANCELED = '-1';
private static $currencies = [
self::CURRENCY_GBP => '826',
self::CURRENCY_EUR => '978',
self::CURRENCY_USD => '840',
];
/**
* @var HttpClientInterface
*/
protected $client;
/**
* @var MessageFactory
*/
protected $messageFactory;
/**
* @var array
*/
protected $options = [];
/**
* {@inheritdoc}
*/
public function __construct(array $options, HttpClientInterface $client, MessageFactory $messageFactory)
{
$this->options = $options;
$this->client = $client;
$this->messageFactory = $messageFactory;
}
/**
* @param array $params
*
* @return array
*
* @throws \Payum\Core\Exception\Http\HttpException
* @throws \Payum\Core\Exception\InvalidArgumentException
*/
public function createTransaction(array $params)
{
$params['operationType'] = static::OPERATION_INITIALIZE;
$params = $this->preparePayment($params);
// we get SimpleXML back, convert to a DVO
$response = (array) $this->doRequest('POST', $params);
$payment = Payment::fromSetefiResponse($response);
$params['payment'] = $payment->toArray();
return $params;
}
/**
* @param array $model
* @param array $authorizationRequest
*
* @return array
*
* @throws \Payum\Core\Exception\InvalidArgumentException
*/
public function authorizeTransaction(array $model, array $authorizationRequest)
{
$payment = Payment::fromArray($model['payment']);
$authorization = PaymentAuthorization::fromSetefiResponse($authorizationRequest);
if (true === $authorization->isCanceled()) {
$model['cancellation'] = $authorization->toArray();
return $model;
}
if (false === $payment->isMatchingAuthorization($authorization)) {
throw new InvalidArgumentException('Invalid payment authorization');
}
$model['authorization'] = $authorization->toArray();
// append the payment ID here, as per Setefi instructions
$model['redirectToMerchantUrl'] .= (false === strpos($model['redirectToMerchantUrl'], '?') ? '?' : '&').http_build_query([
'paymentid' => $payment->getId(),
]);
return $model;
}
/**
* @param array $params
*
* @return array
*
* @throws \Payum\Core\Exception\InvalidArgumentException
*/
protected function preparePayment(array $params)
{
$params['currencyCode'] = $this->getCurrency($params['currencyCode']);
return $this->addGlobalParams($params);
}
/**
* @param array $params
*
* @return array
*/
protected function addGlobalParams(array $params)
{
$params['id'] = $this->options['terminal_id'];
$params['password'] = $this->options['terminal_password'];
return $params;
}
/**
* {@inheritdoc}
*
* @throws \Payum\Core\Exception\Http\HttpException
*/
protected function doRequest($method, array $fields)
{
$headers = [
'Content-Type' => 'application/x-www-form-urlencoded',
];
$request = $this->messageFactory->createRequest(
$method,
$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);
}
$xmlResponse = simplexml_load_string($response->getBody());
if ($xmlResponse->errorcode) {
throw new HttpException(
sprintf('Setefi error (%1$s): %2$s', $xmlResponse->errorcode, $xmlResponse->errormessage)
);
}
return $xmlResponse;
}
/**
* {@inheritdoc}
*/
protected function getApiEndpoint()
{
return $this->options['sandbox'] ? 'https://test.monetaonline.it/monetaweb/payment/2/xml' : 'https://www.monetaonline.it/monetaweb/payment/2/xml';
}
/**
* @param $currencyCode
*
* @return string
*
* @throws InvalidArgumentException
*/
protected function getCurrency($currencyCode)
{
if (false === array_key_exists($currencyCode, self::$currencies)) {
throw new InvalidArgumentException(sprintf('Setefi: unknown currency "%1$s"', $currencyCode));
}
return self::$currencies[$currencyCode];
}
}