/
extension.driver.php
executable file
·390 lines (323 loc) · 12.4 KB
/
extension.driver.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<?php
class Extension_SecurePay extends Extension {
const GATEWAY_ERROR = 100;
const DATA_ERROR = 200;
public function about() {
return array(
'name' => 'SecurePay',
'version' => '1.0',
'release-date' => '2009-11-03',
'author' => array(
array(
'name' => 'Brendan Abbott',
'email' => 'brendan@bloodbone.ws'
),
array(
'name' => 'Henry Singleton'
)
),
'description' => 'Manage and track payments using SecurePay.'
);
}
private static $defaults = array(
'messageID' => '',
'messageTimestamp' => '',
'timeoutValue' => '60',
'apiVersion' => 'xml-4.2',
'merchantID' => '',
'password' => '',
'RequestType' => 'Payment',
'txnType' => '0',
'txnSource' => '23',
'amount' => '',
'currency' => 'AUD',
'purchaseOrderNo' => '',
'cardNumber' => '',
'expiryDate' => '',
'cvv' => ''
);
private static $required_fields = array(
'messageID',
'messageTimestamp',
'timeoutValue',
'apiVersion',
'merchantID',
'password',
'RequestType',
'txnType',
'txnSource',
'amount',
'purchaseOrderNo',
'cardNumber',
'expiryDate',
);
private static $approved_codes = array(
'00', //Approved
'08', //Honour with ID
'10', //Partial Amount Approved
'11', //Approved VIP
'16', //Approved, Update Track 3
'77' //Approved (ANZ only)
);
/*-------------------------------------------------------------------------
Definition:
-------------------------------------------------------------------------*/
public function install() {
Symphony::Configuration()->set('merchant-id', '', 'securepay');
Symphony::Configuration()->set('merchant-password', '', 'securepay');
Symphony::Configuration()->set('production-gateway-uri', 'https://www.securepay.com.au/xmlapi/payment', 'securepay');
Symphony::Configuration()->set('development-gateway-uri', 'https://www.securepay.com.au/test/payment', 'securepay');
Symphony::Configuration()->set('gateway-mode', 'development', 'securepay');
Administration::instance()->saveConfig();
}
public function uninstall() {
Symphony::Configuration()->remove('securepay');
Administration::instance()->saveConfig();
}
public function getSubscribedDelegates() {
return array(
array(
'page' => '/system/preferences/',
'delegate' => 'AddCustomPreferenceFieldsets',
'callback' => 'appendPreferences'
),
array(
'page' => '/system/preferences/',
'delegate' => 'Save',
'callback' => 'savePreferences'
)
);
}
/*-------------------------------------------------------------------------
Utilities:
-------------------------------------------------------------------------*/
public static function isTesting() {
return Symphony::Configuration()->get('gateway-mode', 'securepay') == 'development';
}
public static function getGatewayURI() {
$mode = self::isTesting() ? 'development' : 'production';
return (string)Symphony::Configuration()->get("{$mode}-gateway-uri", 'securepay');
}
public static function getMerchantId() {
return (string)Symphony::Configuration()->get("merchant-id", 'securepay');
}
public static function getMerchantPassword() {
return (string)Symphony::Configuration()->get("merchant-password", 'securepay');
}
public static function getFields($section_id) {
$sectionManager = new SectionManager(Frontend::instance());
$section = $sectionManager->fetch($section_id);
if (!($section instanceof Section)) return array();
return $section->fetchFields();
}
public static function fetchEntries($entries) {
$result = array();
if (!is_array($entries)) $entries = array($entries);
foreach ($entries as $entry) {
$result[$entry] = self::fetchEntry($entry);
}
return $result;
}
public static function fetchEntry($entries) {
$entryManager = new EntryManager(Frontend::instance());
$entries = $entryManager->fetch($entries);
foreach ($entries as $order => $entry) {
$section_id = $entry->get('section_id');
$fields = self::getFields($section_id);
$entry_data = array(
'id'=> $entry->get('id')
);
foreach ($fields as $field) {
$entry_data[$field->get('element_name')] = $entry->getData($field->get('id'));
}
return $entry_data;
}
}
/*-------------------------------------------------------------------------
Delegate Callbacks:
-------------------------------------------------------------------------*/
/**
* Allows a user to enter their eWay details to be saved into the Configuration
*
* @uses AddCustomPreferenceFieldsets
*/
public static function appendPreferences($context) {
// If the Payment Gateway Interface extension is installed, don't
// double display the preference, unless this function is called from
// the `pgi-loader` context.
if(
Symphony::ExtensionManager()->fetchStatus('pgi_loader') == EXTENSION_ENABLED
xor isset($context['pgi-loader'])
) return;
$fieldset = new XMLElement('fieldset');
$fieldset->setAttribute('class', 'settings');
$fieldset->appendChild(new XMLElement('legend', __('SecurePay')));
$div = new XMLElement('div', null, array('class' => 'group'));
// Merchant ID
$label = new XMLElement('label', __('Merchant ID'));
$label->appendChild(
Widget::Input('settings[securepay][merchant-id]', self::getMerchantId())
);
$div->appendChild($label);
// Merchant ID
$label = new XMLElement('label', __('Merchant Password'));
$label->appendChild(
Widget::Input('settings[securepay][merchant-password]', self::getMerchantPassword())
);
$div->appendChild($label);
$fieldset->appendChild($div);
$div = new XMLElement('div');
// Build the Gateway Mode
$label = new XMLElement('label', __('Gateway Mode'));
$options = array(
array('development', self::isTesting(), __('Development')),
array('production', !self::isTesting(), __('Production'))
);
$label->appendChild(Widget::Select('settings[securepay][gateway-mode]', $options));
$div->appendChild($label);
$fieldset->appendChild($div);
$context['wrapper']->appendChild($fieldset);
}
/**
* Saves the Customer ID and the gateway mode to the configuration
*
* @uses savePreferences
*/
public function savePreferences(array &$context){
$settings = $context['settings'];
// Active Section
Symphony::Configuration()->set('merchant-id', $settings['securepay']['merchant-id'], 'securepay');
Symphony::Configuration()->set('merchant-password', $settings['securepay']['merchant-password'], 'securepay');
Symphony::Configuration()->set('gateway-mode', $settings['securepay']['gateway-mode'], 'securepay');
Administration::instance()->saveConfig();
}
/*-------------------------------------------------------------------------
Process Transaction:
-------------------------------------------------------------------------*/
public static function processTransaction(array $values = array()) {
// Merge Defaults and passed values
$request_array = array_merge(Extension_SecurePay::$defaults, $values);
// Authentication
$request_array['merchantID'] = self::getMerchantId();
$request_array['password'] = self::getMerchantPassword();
// Message Info
$request_array['messageID'] = md5(time());
$request_array['messageTimestamp'] = time();
$request_array = array_intersect_key($request_array, Extension_SecurePay::$defaults);
// Check for missing fields
$valid_data = true;
$missing_fields = array();
$error = null;
foreach (Extension_SecurePay::$required_fields as $field_name) {
// Don't use empty as sometimes '0' is a valid value
if (!array_key_exists($field_name, $request_array) || $request_array[$field_name] == '') {
$missing_fields[] = $field_name;
$valid_data = false;
}
}
// The data is invalid, return a `DATA_ERROR`
if(!$valid_data) {
return(array(
'status' => __('Data error'),
'response-code' => Extension_SecurePay::DATA_ERROR,
'response-message' => __('Missing Fields: %s', array(implode(', ', $missing_fields))),
'missing-fields' => $missing_fields
));
}
//Generate the XML
$SecurePayMessage = new XMLElement('SecurePayMessage');
$MessageInfo = new XMLElement('MessageInfo');
$MessageInfo->appendChild(new XMLElement('messageID',$request_array['messageID']));
$MessageInfo->appendChild(new XMLElement('messageTimestamp',$request_array['messageTimestamp']));
$MessageInfo->appendChild(new XMLElement('timeoutValue',$request_array['timeoutValue']));
$MessageInfo->appendChild(new XMLElement('apiVersion',$request_array['apiVersion']));
$SecurePayMessage->appendChild($MessageInfo);
$MerchantInfo = new XMLElement('MerchantInfo');
$MerchantInfo->appendChild(new XMLElement('merchantID',$request_array['merchantID']));
$MerchantInfo->appendChild(new XMLElement('password',$request_array['password']));
$SecurePayMessage->appendChild($MerchantInfo);
$RequestType = new XMLElement('RequestType',$request_array['RequestType']);
$SecurePayMessage->appendChild($RequestType);
$Txn = new XMLElement('Txn');
$Txn->setAttribute('ID', '1');
$Txn->appendChild(new XMLElement('txnType',$request_array['txnType']));
$Txn->appendChild(new XMLElement('txnSource',$request_array['txnSource']));
$Txn->appendChild(new XMLElement('amount',$request_array['amount']));
$Txn->appendChild(new XMLElement('purchaseOrderNo',$request_array['purchaseOrderNo']));
$CreditCardInfo = new XMLElement('CreditCardInfo');
$CreditCardInfo->appendChild(new XMLElement('cardNumber',$request_array['cardNumber']));
$CreditCardInfo->appendChild(new XMLElement('expiryDate',$request_array['expiryDate']));
$CreditCardInfo->appendChild(new XMLElement('cvv', $request_array['cvv']));
$Txn->appendChild($CreditCardInfo);
$TxnList = new XMLElement('TxnList');
$TxnList->setAttribute('count','1');
$TxnList->appendChild($Txn);
$Payment = new XMLElement('Payment');
$Payment->appendChild($TxnList);
$SecurePayMessage->appendChild($Payment);
// Curl
require_once(TOOLKIT . '/class.gateway.php');
$curl = new Gateway;
$curl->init(self::getGatewayURI());
$curl->setopt('POST', true);
$curl->setopt('POSTFIELDS', $SecurePayMessage->generate(true));
$curl_result = $curl->exec();
$info = $curl->getInfoLast();
// The Gateway did not connect to eWay successfully
if(!in_array($info["http_code"], array('200', '201'))) {
Symphony::$Log->pushToLog($error, E_USER_ERROR, true);
// Return a `GATEWAY_ERROR`
return(array(
'status' => __('Gateway error'),
'response-code' => Extension_SecurePay::GATEWAY_ERROR,
'response-message' => __('There was an error connecting to SecurePay.')
));
}
else {
// Create a document for the result and load the resul
$securepay_result = new DOMDocument('1.0', 'utf-8');
$securepay_result->formatOutput = true;
$securepay_result->loadXML($curl_result);
$securepay_result_xpath = new DOMXPath($securepay_result);
/*
header('text/xml');
echo $curl_result;
exit;
*/
// So even thought SecurePay replied, we've got to check if
// they actually understood what we asked them.
$securepay_code = $securepay_result_xpath->evaluate('string(/SecurePayMessage/Status/statusCode)');
if($securepay_code != '000') {
// Return a `GATEWAY_ERROR`
return(array(
'status' => __('SecurePay error'),
'response-code' => $securepay_code,
'response-message' => $securepay_result_xpath->evaluate('string(/SecurePayMessage/Status/statusDescription)')
));
}
// A transaction actually occured, so the status context has
// shifted to the Bank response
else {
// Generate status result:
$txn_code = $securepay_result_xpath->evaluate('string(/SecurePayMessage/Payment/TxnList/Txn/responseCode)');
$txn_message = $securepay_result_xpath->evaluate('string(/SecurePayMessage/Payment/TxnList/Txn/responseText)');
$return = array();
if(!in_array($txn_code, Extension_SecurePay::$approved_codes)) {
$return['status'] = __('Declined');
}
else {
$return['status'] = __('Approved');
$return['pgi-transaction-id'] = $securepay_result_xpath->evaluate('string(/SecurePayMessage/Payment/TxnList/Txn/txnID)');
}
$return = array_merge(
$return,
array(
'response-code' => $txn_code,
'response-message' => $txn_message
)
);
return $return;
}
}
}
}