-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApi.php
229 lines (217 loc) · 7.09 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
<?php
namespace Alexrowesoap\Payumsagepay;
use GuzzleHttp\Psr7\Request;
use Http\Message\MessageFactory;
use Payum\Core\Exception\InvalidArgumentException;
use Payum\Core\HttpClientInterface;
use Alexrowesoap\Payumsagepay\Api\Signature\Validator;
class Api
{
const VERSION = '3.00';
const OPERATION_PAYMENT = 'PAYMENT';
const OPERATION_DEFFERED = 'DEFFERED';
const OPERATION_AUTHENTICATE = 'AUTHENTICATE';
const OPERATION_REPEAT = 'REPEAT';
const STATUS_OK = 'OK';
const STATUS_PENDING = 'PENDING';
const STATUS_OK_REPEATED = 'OK REPEATED';
const STATUS_MALFORMED = 'MALFORMED';
const STATUS_INVALID = 'INVALID';
const STATUS_ERROR = 'ERROR';
/**
* @var HttpClientInterface
*/
protected $client;
/**
* @var MessageFactory
*/
protected $messageFactory;
protected $options = array(
'vendor' => null,
'sandbox' => null,
);
/**
* @param array $options
* @param HttpClientInterface $client
* @param MessageFactory $messageFactory
*
* @throws \Payum\Core\Exception\InvalidArgumentException if an option is invalid
*/
public function __construct(array $options, HttpClientInterface $client, MessageFactory $messageFactory)
{
$this->client = $client;
$this->options = array_replace($this->options, $options);
if (true == empty($this->options['vendor'])) {
throw new InvalidArgumentException('The vendor option must be set.');
}
if (false == is_bool($this->options['sandbox'])) {
throw new InvalidArgumentException('The boolean sandbox option must be set.');
}
$this->messageFactory = $messageFactory;
}
/**
* @return string
*/
protected function getApiEndpoint()
{
return $this->options['sandbox'] ? 'http://sandbox.example.com' : 'http://example.com';
}
/**
* @param array $paymentDetails
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function createOffsitePurchase(array $paymentDetails)
{
$paymentDetails['TxType'] = static::OPERATION_PAYMENT;
$query = http_build_query($paymentDetails);
$request = new Request(
'post',
$this->getOffsiteResource() . $this->getGatewayHost(),
null,
$query
);
return $this->doRequest($request);
}
public function getMissingDetails(array $details)
{
$required = array(
'VPSProtocol' => null,
'TxType' => null,
'Vendor' => null,
'VendorTxCode' => null,
'Amount' => null,
'Currency' => null,
'Description' => null,
'NotificationURL' => null,
'BillingSurname' => null,
'BillingFirstnames' => null,
'BillingAddress1' => null,
'BillingCity' => null,
'BillingPostCode' => null,
'BillingCountry' => null,
'DeliverySurname' => null,
'DeliveryFirstnames' => null,
'DeliveryAddress1' => null,
'DeliveryCity' => null,
'DeliveryPostCode' => null,
'DeliveryCountry' => null,
);
return $this->removeNullValues(array_diff_key($required, $details));
}
/**
*
* @param Request $request
* @return \Psr\Http\Message\ResponseInterface
*/
protected function doRequest(Request $request)
{
$response = $this->client->send($request);
return $response;
}
/**
* @param array $paymentDetails
* @return array
*/
public function prepareOffsiteDetails(array $paymentDetails)
{
$supportedParams = array(
'VendorTxCode' => null,
'Amount' => null,
'Currency' => null,
'Description' => null,
'NotificationURL' => null,
'Token' => null,
'BillingSurname' => null,
'BillingFirstnames' => null,
'BillingAddress1' => null,
'BillingAddress2' => null,
'BillingCity' => null,
'BillingPostCode' => null,
'BillingCountry' => null,
'BillingState' => null,
'BillingPhone' => null,
'DeliverySurname' => null,
'DeliveryFirstnames' => null,
'DeliveryAddress1' => null,
'DeliveryAddress2' => null,
'DeliveryCity' => null,
'DeliveryPostCode' => null,
'DeliveryCountry' => null,
'DeliveryState' => null,
'DeliveryPhone' => null,
'CustomerEMail' => null,
'Basket' => null,
'AllowGiftAid' => null,
'ApplyAVSCV2' => null,
'Apply3DSecure' => null,
'Profile' => null,
'BillingAgreement' => null,
'AccountType' => null,
'CreateToken' => null,
'StoreToken' => null,
'BasketXML' => null,
'CustomerXML' => null,
'SurchargeXML' => null,
'VendorData' => null,
'ReferrerID' => null,
'Language' => null,
'Website' => null,
'FIRecipientAcctNumber' => null,
'FIRecipientSurname' => null,
'FIRecipientPostcode' => null,
'FIRecipientDoB' => null,
);
$paymentDetails = array_filter(
array_replace(
$supportedParams,
array_intersect_key($paymentDetails, $supportedParams)
)
);
$paymentDetails['TxType'] = static::OPERATION_PAYMENT;
$paymentDetails = $this->appendGlobalParams($paymentDetails);
return $paymentDetails;
}
public function tamperingDetected(array $notification, array $model)
{
$validator = new Validator();
$available = $validator->getAvailableParams();
foreach ($available as $key => $value) {
if (array_key_exists($key, $notification)) {
$available[$key] = $notification[$key];
}
}
$available['SecurityKey'] = $model['SecurityKey'];
$available['VendorName'] = $this->options['vendor'];
$reciviedHash = $notification['VPSSignature'];
$validator->setParams($available);
return $validator->tamperingDetected($reciviedHash);
}
protected function appendGlobalParams(array $paymentDetails = array())
{
$paymentDetails['VPSProtocol'] = self::VERSION;
$paymentDetails['Vendor'] = $this->options['vendor'];
return $paymentDetails;
}
protected function getGatewayHost()
{
return $this->options['sandbox'] ?
'https://test.sagepay.com' :
'https://live.sagepay.com'
;
}
protected function getOffsiteResource()
{
return '/gateway/service/vspserver-register.vsp';
}
protected function removeNullValues($params)
{
$cleared = array();
foreach ($params as $key => $value) {
if ($value != null) {
$cleared[$key] = $value;
}
}
return $cleared;
}
}