-
Notifications
You must be signed in to change notification settings - Fork 3
/
paychoice.php
711 lines (608 loc) · 25.5 KB
/
paychoice.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
<?php
/**
* @plugin VMPayment - Paychoice
* @Website : http://www.paychoice.com.au
* @package VirtueMart
* @subpackage Plugins - payment
* @author Justin Caruana
* @copyright 2013 Paychoice
*/
if (!defined('_JEXEC')) die('Direct Access to ' . basename(__FILE__) . ' is not allowed.');
if (!class_exists('Creditcard')) require_once(JPATH_VM_ADMINISTRATOR . DS . 'helpers' . DS . 'creditcard.php');
if (!class_exists('vmPSPlugin')) require(JPATH_VM_PLUGINS . DS . 'vmpsplugin.php');
class plgVmpaymentPaychoice extends vmPSPlugin
{
//instance of class
public static $_this = false;
private $_cc_name = '';
private $_cc_type = '';
private $_cc_name_on_card = '';
private $_cc_number = '';
private $_cc_cvv = '';
private $_cc_expire_month = '';
private $_cc_expire_year = '';
private $_cc_valid = false;
private $_errormessage = array();
public $approved;
public $declined;
public $error;
const APPROVED = 1;
const DECLINED = 2;
const ERROR = 3;
const HELD = 4;
function __construct(& $subject, $config)
{
parent::__construct($subject, $config);
$this->_loggable = true;
$this->_tablepkey = 'id';
$this->_tableId = 'id';
$this->tableFields = array_keys($this->getTableSQLFields());
$varsToPush = array(
'paychoice_username' => array('', 'char'),
'paychoice_password' => array('', 'char'),
'paychoice_verified_status' => array('C', 'char'),
'paychoice_invalid_status' => array('P', 'char'),
'paychoice_test_request' => array(1, 'int'),
'paychoice_process_type' => array('', 'char'),
'countries' => array(0, 'char'),
'tax_id' => array(0, 'int')
);
$this->setConfigParameterable($this->_configTableFieldName, $varsToPush);
}
protected function getVmPluginCreateTableSQL() { return $this->createTableSQL('Payment Paychoice Table'); }
function getTableSQLFields()
{
$SQLfields = array(
'id' => ' int(1) unsigned NOT NULL AUTO_INCREMENT',
'virtuemart_order_id' => ' int(1) UNSIGNED DEFAULT NULL',
'order_number' => ' char(32) DEFAULT NULL',
'virtuemart_paymentmethod_id' => ' mediumint(1) UNSIGNED DEFAULT NULL',
'payment_name' => 'varchar(5000)',
'return_context' => ' char(255) NOT NULL DEFAULT \'\' ',
'tax_id' => ' smallint(1) DEFAULT NULL',
'paychoice_response_response_code' => ' varchar(50) DEFAULT NULL',
'paychoice_response_transaction_id' => ' varchar(50) DEFAULT NULL',
'paychoice_response_error_message' => ' text DEFAULT NULL',
'paychoice_response_raw' => ' text DEFAULT NULL',
);
return $SQLfields;
}
function plgVmDisplayListFEPayment(VirtueMartCart $cart, $selected=0, &$htmlIn)
{
JHTML::_('behavior.tooltip');
if ($this->getPluginMethods($cart->vendorId) === 0) {
if (empty($this->_name)) {
$app = JFactory::getApplication();
$app->enqueueMessage(JText::_('COM_VIRTUEMART_CART_NO_' . strtoupper($this->_psType)));
return false;
}
else
{
return false;
}
}
$html = array();
$method_name = $this->_psType . '_name';
JHTML::script('vmcreditcard.js', 'components/com_virtuemart/assets/js/', false);
JFactory::getLanguage()->load('com_virtuemart');
vmJsApi::jCreditCard();
$htmla = '';
$html = array();
foreach ($this->methods as $method)
{
if ($this->checkConditions($cart, $method, $cart->pricesUnformatted))
{
$methodSalesPrice = $this->calculateSalesPrice($cart, $method, $cart->pricesUnformatted);
$method->$method_name = $this->renderPluginName($method);
$html = $this->getPluginHtml($method, $selected, $methodSalesPrice);
if ($selected == $method->virtuemart_paymentmethod_id)
{
$this->_getPaychoiceIntoSession();
}
else
{
$this->_cc_type = '';
$this->_cc_name_on_card = '';
$this->_cc_number = '';
$this->_cc_cvv = '';
$this->_cc_expire_month = '';
$this->_cc_expire_year = '';
}
//$creditCards = $method->creditcards;
$creditCards = array('Visa','Mastercard','AmericanExpress','Diners');
$creditCardList = '';
if ($creditCards)
{
$creditCardList = ($this->_renderCreditCardList($creditCards, $this->_cc_type, $method->virtuemart_paymentmethod_id, false));
}
$sandbox_msg = "";
if ($method->paychoice_test_request)
{
$sandbox_msg .= '<br />' . JText::_('VMPAYMENT_PAYCHOICE_SANDBOX_TEST_NUMBERS');
}
//credit card list was in HTML but is not really needed
// <tr valign="top">
// <td nowrap width="10%" align="right"><label for="creditcardtype">' . JText::_('VMPAYMENT_PAYCHOICE_CCTYPE') . ':</label> </td>
// <td>' . $creditCardList .'</td>
// </tr>
$html .= '<br /><span class="vmpayment_cardinfo">' . JText::_('VMPAYMENT_PAYCHOICE_COMPLETE_FORM') . $sandbox_msg . '
<table border="0" cellspacing="0" cellpadding="2" width="100%">
<tr valign="top">
<td nowrap width="10%" align="right"><label for="cc_cvv">' . JText::_('VMPAYMENT_PAYCHOICE_NAMEONCARD') . ':</label> </td>
<td><input type="text" class="inputbox" id="cc_name_on_card_' . $method->virtuemart_paymentmethod_id . '" name="cc_name_on_card_' . $method->virtuemart_paymentmethod_id . '" value="' . $this->_cc_name_on_card . '" autocomplete="off" /></td>
</tr>
<tr valign="top">
<td nowrap width="10%" align="right"><label for="cc_type">' . JText::_('VMPAYMENT_PAYCHOICE_CCNUM') . ':</label> </td>
<td><input type="text" class="inputbox" id="cc_number_' . $method->virtuemart_paymentmethod_id . '" name="cc_number_' . $method->virtuemart_paymentmethod_id . '" value="' . $this->_cc_number . '" autocomplete="off" onchange="ccError=razCCerror(' . $method->virtuemart_paymentmethod_id . ');
CheckCreditCardNumber(this.value.replace(new RegExp(\'(-|\s)\', \'g\'),\'\'), ' . $method->virtuemart_paymentmethod_id . ');
if (!ccError) {this.value=\'\';}" />
<div id="cc_cardnumber_errormsg_' . $method->virtuemart_paymentmethod_id . '"></div>
</td>
</tr>
<tr valign="top">
<td nowrap width="10%" align="right"><label for="cc_cvv">' . JText::_('VMPAYMENT_PAYCHOICE_CVV2') . ':</label> </td>
<td><input type="text" class="inputbox" id="cc_cvv_' . $method->virtuemart_paymentmethod_id . '" name="cc_cvv_' . $method->virtuemart_paymentmethod_id . '" maxlength="4" size="5" value="' . $this->_cc_cvv . '" autocomplete="off" />
<span class="hasTip" title="' . JText::_('VMPAYMENT_PAYCHOICE_WHATISCVV') . '::' . JText::sprintf("VMPAYMENT_PAYCHOICE_WHATISCVV_TOOLTIP", '') . ' ">'.JText::_('VMPAYMENT_PAYCHOICE_WHATISCVV') . '</span>
</td>
</tr>
<tr><td nowrap width="10%" align="right">' . JText::_('VMPAYMENT_PAYCHOICE_EXDATE') . ': </td>
<td> ';
$html .= shopfunctions::listMonths('cc_expire_month_' . $method->virtuemart_paymentmethod_id, $this->_cc_expire_month);
$html .= " / ";
$html .= shopfunctions::listYears('cc_expire_year_' . $method->virtuemart_paymentmethod_id, $this->_cc_expire_year, null, null, "onchange=\"var month = document.getElementById('cc_expire_month_'.$method->virtuemart_paymentmethod_id); if(!CreditCardisExpiryDate(month.value,this.value, '.$method->virtuemart_paymentmethod_id.')){this.value='';month.value='';}\" ");
$html .='<div id="cc_expiredate_errormsg_' . $method->virtuemart_paymentmethod_id . '"></div>';
$html .= '</td> </tr> </table></span>';
$htmla[] = $html;
}
}
$htmlIn[] = $htmla;
return true;
}
protected function checkConditions($cart, $method, $cart_prices)
{
$address = (($cart->ST == 0) ? $cart->BT : $cart->ST);
$amount = $cart_prices['salesPrice'];
$amount_cond = ( ($amount >= $method->min_amount AND $amount <= $method->max_amount) || ($amount >= $method->min_amount AND ($method->max_amount == 0) ));
if (!$amount_cond) return false;
$countries = array();
if (!empty($method->countries))
{
if (!is_array($method->countries)) $countries[0] = $method->countries;
else $countries = $method->countries;
}
//probably did not gave his BT:ST address
if (!is_array($address))
{
$address = array();
$address['virtuemart_country_id'] = 0;
}
if (!isset($address['virtuemart_country_id'])) $address['virtuemart_country_id'] = 0;
if (count($countries) == 0 || in_array($address['virtuemart_country_id'], $countries) || count($countries) == 0)
{
return true;
}
return false;
}
function _setPaychoiceIntoSession()
{
$session = JFactory::getSession();
$data = new stdClass();
//card information
$data->cc_type = $this->_cc_type;
$data->cc_name_on_card = $this->_cc_name_on_card;
$data->cc_number = $this->_cc_number;
$data->cc_cvv = $this->_cc_cvv;
$data->cc_expire_month = $this->_cc_expire_month;
$data->cc_expire_year = $this->_cc_expire_year;
$data->cc_valid = $this->_cc_valid;
$session->set('paychoice', serialize($data), 'vm');
}
function _getPaychoiceIntoSession()
{
$session = JFactory::getSession();
$paychoiceSession = $session->get('paychoice', 0, 'vm');
if (!empty($paychoiceSession))
{
$data = unserialize($paychoiceSession);
$this->_cc_type = $data->cc_type;
$this->_cc_name_on_card = $data->cc_name_on_card;
$this->_cc_number = $data->cc_number;
$this->_cc_cvv = $data->cc_cvv;
$this->_cc_expire_month = $data->cc_expire_month;
$this->_cc_expire_year = $data->cc_expire_year;
$this->_cc_valid = $data->cc_valid;
}
}
function plgVmOnCheckoutCheckDataPayment(VirtueMartCart $cart)
{
if (!$this->selectedThisByMethodId( $cart->virtuemart_paymentmethod_id))
{
return null; //Another method was selected, do nothing
}
$this->_getPaychoiceIntoSession();
return $this->_validate_creditcard_data(true);
}
function plgVmOnStoreInstallPaymentPluginTable($jplugin_id)
{
return parent::onStoreInstallPluginTable( $jplugin_id);
}
function plgVmOnSelectCheckPayment(VirtueMartCart $cart)
{
if (!$this->selectedThisByMethodId( $cart->virtuemart_paymentmethod_id))
{
return null; //Another method was selected, do nothing
}
$this->_cc_type = JRequest::getVar('cc_type_' . $cart->virtuemart_paymentmethod_id, '');
$this->_cc_name_on_card = JRequest::getVar('cc_name_on_card_' . $cart->virtuemart_paymentmethod_id, '');
$this->_cc_number = str_replace(array(" ","-"),"",JRequest::getVar('cc_number_' . $cart->virtuemart_paymentmethod_id, ''));
$this->_cc_cvv = JRequest::getVar('cc_cvv_' . $cart->virtuemart_paymentmethod_id, '');
$this->_cc_expire_month = JRequest::getVar('cc_expire_month_' . $cart->virtuemart_paymentmethod_id, '');
$this->_cc_expire_year = JRequest::getVar('cc_expire_year_' . $cart->virtuemart_paymentmethod_id, '');
if (!$this->_validate_creditcard_data(true))
{
return false; //returns string containing errors
}
$this->_setPaychoiceIntoSession();
return true;
}
public function plgVmOnSelectedCalculatePricePayment(VirtueMartCart $cart, array &$cart_prices, $payment_name)
{
if (!($method = $this->getVmPluginMethod($cart->virtuemart_paymentmethod_id)))
{
return null; //Another method was selected, do nothing
}
if (!$this->selectedThisElement($method->payment_element))
{
return false;
}
$this->_getPaychoiceIntoSession();
$cart_prices['payment_tax_id'] = 0;
$cart_prices['payment_value'] = 0;
if (!$this->checkConditions($cart, $method, $cart_prices))
{
return false;
}
$payment_name = $this->renderPluginName($method);
$this->setCartPrices($cart, $cart_prices, $method);
return true;
}
/*
* @param $plugin plugin
*/
protected function renderPluginName($plugin)
{
$return = '';
$plugin_name = $this->_psType . '_name';
$plugin_desc = $this->_psType . '_desc';
$description = '';
$logosFieldName = $this->_psType . '_logos';
$logos = $plugin->$logosFieldName;
if (!empty($logos)) $return = $this->displayLogos($logos) . ' ';
if (!empty($plugin->$plugin_desc)) $description = '<span class="' . $this->_type . '_description">' . $plugin->$plugin_desc . '</span>';
$this->_getPaychoiceIntoSession();
$extrainfo=$this->getExtraPluginNameInfo();
$pluginName = $return . '<span class="' . $this->_type . '_name">' . $plugin->$plugin_name . '</span>' . $description. $extrainfo ;
return $pluginName;
}
/**
* Display stored payment data for an order
* @see components/com_virtuemart/helpers/vmPaymentPlugin::plgVmOnShowOrderPaymentBE()
*/
function plgVmOnShowOrderBEPayment($virtuemart_order_id, $virtuemart_payment_id)
{
if (!$this->selectedThisByMethodId( $virtuemart_payment_id))
{
return null; //Another method was selected, do nothing
}
$db = JFactory::getDBO();
$q = 'SELECT * FROM `' . $this->_tablename . '` WHERE `virtuemart_order_id` = ' . $virtuemart_order_id;
$db->setQuery($q);
if (!($paymentTable = $db->loadObject()))
{
JError::raiseWarning(500, $db->getErrorMsg());
return '';
}
$html = '<table class="adminlist">' . "\n";
$html .= $this->getHtmlHeaderBE();
$html .= $this->getHtmlRowBE('PAYCHOICE_PAYMENT_NAME', $paymentTable->payment_name);
$code = "paychoice_response_";
foreach ($paymentTable as $key => $value)
{
if($key=='paychoice_response_raw') continue;
if (substr($key, 0, strlen($code)) == $code)
{
$html .= $this->getHtmlRowBE($key, $value);
}
}
$html .= '</table>' . "\n";
return $html;
}
function plgVmConfirmedOrder($cart, $order)
{
if (!($method = $this->getVmPluginMethod($order['details']['BT']->virtuemart_paymentmethod_id)))
{
return null; //Another method was selected, do nothing
}
if (!$this->selectedThisElement($method->payment_element))
{
return false;
}
$usrBT = $order['details']['BT'];
$usrST = ((isset($order['details']['ST'])) ? $order['details']['ST'] : $order['details']['BT']);
$session = JFactory::getSession();
$return_context = $session->getId();
$transaction_key = $this->get_passkey();
if ($transaction_key === false)
{
return false;
}
//Prepare data that should be stored in the database
$dbValues = array();
$dbValues['order_number'] = $order['details']['BT']->order_number;
$dbValues['virtuemart_order_id'] = $order['details']['BT']->virtuemart_order_id;
$dbValues['payment_method_id'] = $order['details']['BT']->virtuemart_paymentmethod_id;
$dbValues['return_context'] = $return_context;
$dbValues['payment_name'] = parent::renderPluginName($method);
$this->storePSPluginInternalData($dbValues);
//send a request
$vendorId = 1;
$vendorModel = VmModel::getModel('vendor');
$vendorName = $vendorModel->getVendorName($vendorId);
$params = (object) array(
'invoice_ref'=>$order['details']['BT']->order_number,
'nameoncard'=>$this->_cc_name_on_card,
'cc_number'=>$this->_cc_number,
'expirymonth'=>$this->_cc_expire_month,
'expiryyear'=>substr($this->_cc_expire_year,2,2), //We need to show the year with two digits only
'cvv'=>$this->_cc_cvv,
'trans_number'=>$order['details']['BT']->order_number,
'order_number'=>$order['details']['BT']->order_number,
'order_total'=>$order['details']['BT']->order_total,
);
$response = $this->_doPayment($method,$params);
$html = $this->_handleResponse($response, $order, $dbValues['payment_name']);
if ($this->error)
{
JRequest::setVar('html', $html);
return; //will not process the order
}
else if ($this->approved)
{
$new_status = $method->paychoice_verified_status;
}
else if ($this->declined)
{
$new_status = $method->paychoice_invalid_status;
}
$this->_clearPaychoiceSession();
return $this->processConfirmedOrderPaymentResponse(1, $cart, $order, $html,$dbValues['payment_name'], $new_status);
}
function _handleResponse($response, $order, $payment_name)
{
$virtuemart_order_id = VirtueMartModelOrders::getOrderIdByOrderNumber($response->charge->reference);
if (!$virtuemart_order_id)
{
$this->approved = false;
$this->error = true;
$this->logInfo(JText::sprintf('VMPAYMENT_PAYCHOICE_ERROR_NO_ORDER_NUMBER', $response->order_number), 'ERROR');
$this->sendEmailToVendorAndAdmins(JText::sprintf('VMPAYMENT_PAYCHOICE_ERROR_NO_ORDER_NUMBER', $response->order_number), JText::sprintf('VMPAYMENT_PAYCHOICE_ERROR_WHILE_PROCESSING_PAYMENT', $response->order_number));
$html = Jtext::sprintf('VMPAYMENT_PAYCHOICE_ERROR', $response->charge->error, $response->charge->error_code) . "<br />";
$this->logInfo($html, 'PAYMENT DECLINED');
return $html; //the transaction has been submitted, we don't want to delete the order }
}
$response_fields = array();
$response_fields['virtuemart_order_id'] = $virtuemart_order_id;
$response_fields['invoice_number'] = $response->order_number;
$response_fields['paychoice_response_response_code'] = "{$response->charge->status_code} {$response->charge->status}";
$response_fields['paychoice_response_transaction_id'] = $response->charge->id;
$response_fields['paychoice_response_error_message'] = "{$response->charge->error_code} {$response->charge->error}";
$response_fields['paychoice_response_raw'] = print_r($response, true);
if($response->charge->status_code == '0' )
{
$this->approved = true;
$this->error = false;
$this->storePSPluginInternalData($response_fields, 'virtuemart_order_id', true);
}
elseif($response->charge->status_code == '5')
{
$this->approved = false;
$this->declined = true;
$html = Jtext::sprintf('VMPAYMENT_PAYCHOICE_ERROR', $response->charge->error, $response->charge->error_code) . "<br />";
$this->logInfo($html, 'PAYMENT DECLINED');
$this->storePSPluginInternalData($response_fields, 'virtuemart_order_id', true);
return $html; //the transaction has been submitted, we don't want to delete the order
}
else
{
$this->approved = false;
$this->error = true;
$this->logInfo(JText::_('VMPAYMENT_PAYCHOICE_ERROR_CONNECTING'), 'ERROR');
$this->sendEmailToVendorAndAdmins(JText::_('VMPAYMENT_PAYCHOICE_ERROR_EMAIL_SUBJECT'), JText::_('VMPAYMENT_PAYCHOICE_ERROR_CONNECTING'));
$this->storePSPluginInternalData($response_fields, 'virtuemart_order_id', true);
return JText::_('VMPAYMENT_PAYCHOICE_ERROR_CONNECTING');
}
$this->logInfo('Order Number' . $response->order_number . ' payment approved', 'message');
return $html;
}
function plgVmGetPaymentCurrency($virtuemart_paymentmethod_id, &$paymentCurrencyId)
{
if (!($method = $this->getVmPluginMethod($virtuemart_paymentmethod_id)))
{
return null; //Another method was selected, do nothing
}
if (!$this->selectedThisElement($method->payment_element))
{
return false;
}
$db = JFactory::getDBO();
$q = 'SELECT `virtuemart_currency_id` FROM `#__virtuemart_currencies` WHERE `currency_code_3`= "AUD"' ;
$db->setQuery($q);
$paymentCurrencyId = $db->loadResult();
}
function _clearPaychoiceSession()
{
$session = JFactory::getSession();
$session->clear('paychoice', 'vm');
}
function getExtraPluginNameInfo() {
$creditCardInfos = '';
if ($this->_validate_creditcard_data(false)) {
$cc_number = "**** **** **** " . substr($this->_cc_number, -4);
$creditCardInfos .= '<br /><span class="vmpayment_cardinfo">' . JText::_('VMPAYMENT_PAYCHOICE_CCTYPE').': ' . $this->_cc_type . '<br />';
$creditCardInfos .=JText::_('VMPAYMENT_PAYCHOICE_CCNUM').': ' . $cc_number . '<br />';
$creditCardInfos .= JText::_('VMPAYMENT_PAYCHOICE_CVV2').': ' . '****' . '<br />';
$creditCardInfos .= JText::_('VMPAYMENT_PAYCHOICE_EXDATE').': ' . $this->_cc_expire_month . '/' . $this->_cc_expire_year;
$creditCardInfos .="</span>";
}
return $creditCardInfos;
}
function _renderCreditCardList($creditCards, $selected_cc_type, $paymentmethod_id, $multiple = false, $attrs='') {
$idA = $id = 'cc_type_' . $paymentmethod_id;
if (!is_array($creditCards)) $creditCards = (array) $creditCards;
foreach ($creditCards as $creditCard) $options[] = JHTML::_('select.option', $creditCard, JText::_('VMPAYMENT_PAYCHOICE_' . strtoupper($creditCard)));
if ($multiple) {
$attrs = 'multiple="multiple"';
$idA .= '[]';
}
return JHTML::_('select.genericlist', $options, $idA, $attrs, 'value', 'text', $selected_cc_type);
}
function _validate_creditcard_data($enqueueMessage=true) {
$html = '';
$this->_cc_valid = true;
if (!Creditcard::validate_credit_card_number($this->_cc_type, $this->_cc_number)) {
$this->_errormessage[] = 'VMPAYMENT_PAYCHOICE_CARD_NUMBER_INVALID';
$this->_cc_valid = false;
}
if (!Creditcard::validate_credit_card_cvv($this->_cc_type, $this->_cc_cvv)) {
$this->_errormessage[] = 'VMPAYMENT_PAYCHOICE_CARD_CVV_INVALID';
$this->_cc_valid = false;
}
if (!Creditcard::validate_credit_card_date($this->_cc_type, $this->_cc_expire_month, $this->_cc_expire_year)) {
$this->_errormessage[] = 'VMPAYMENT_PAYCHOICE_CARD_CVV_INVALID';
$this->_cc_valid = false;
}
if (!$this->_cc_valid) {
foreach ($this->_errormessage as $msg) $html .= Jtext::_($msg) . "<br/>";
}
if (!$this->_cc_valid && $enqueueMessage) {
$app = & JFactory::getApplication();
$app->enqueueMessage($html);
}
return $this->_cc_valid;
}
function _getfield($string, $length) { return substr($string, 0, $length); }
function plgVmOnCheckAutomaticSelectedPayment(VirtueMartCart $cart, array $cart_prices = array()) { return parent::onCheckAutomaticSelected($cart, $cart_prices); }
protected function plgVmOnShowOrderFEPayment($virtuemart_order_id, $virtuemart_paymentmethod_id, &$payment_name) { $this->onShowOrderFE($virtuemart_order_id, $virtuemart_paymentmethod_id, $payment_name); }
function plgVmOnShowOrderPrintPayment($order_number, $method_id) { return parent::onShowOrderPrint($order_number, $method_id); }
function plgVmDeclarePluginParamsPayment($name, $id, &$data) { return $this->declarePluginParams('payment', $name, $id, $data); }
function plgVmSetOnTablePluginParamsPayment($name, $id, &$table) { return $this->setOnTablePluginParams($name, $id, $table); }
function _doPayment($method,$params)
{
//Instantiate the paychoice http client
$paychoiceClient = new PaychoiceProxy();
//Use the sandbox endpoint if undefined
$useSandbox = $method->paychoice_test_request;
$requestData["currency"] = 'AUD'; //Hardcoded in AUD until support for multiple currencies is completed.
$requestData["amount"] = $params->order_total;
$requestData["reference"] = $params->trans_number;
$requestData["card[name]"] = $params->nameoncard;
$requestData["card[number]"] = $params->cc_number;
$requestData["card[expiry_month]"] = $params->expirymonth;
$requestData["card[expiry_year]"] = $params->expiryyear;
$requestData["card[cvv]"] = $params->cvv;
try
{
$credentials = $method->paychoice_username . ":" . $method->paychoice_password;
$response = $paychoiceClient->sendChargeRequest($credentials, $useSandbox, $requestData);
//Make sure the API returned something
if (!isset($response))
{
$errorMessage = "Transaction Error: Payment processor did not return a valid response.";
}
//Set an error message if the transaction failed
if ($response->charge->status_code != '0')
{
$errorMessage = "Transaction Error. Payment processor declined transaction: {$response->charge->error_code} {$response->charge->error}";
}
}
catch (PaychoiceException $e)
{
$errorMessage = $e->getMessage();
}
//Set an error and redirect if something went wrong
if (isset($errorMessage) && strlen($errorMessage))
{
}
return $response;
}
}
class PaychoiceProxy
{
public function sendChargeRequest($credentials, $useSandbox, $requestData)
{
$headers = array();
if (strlen($credentials) < 3)
{
throw new PayChoiceException("Paychoice merchant details not set");
}
if (strlen($useSandbox) < 1)
{
throw new PayChoiceException("Paychoice sandbox/live environment not set");
}
$environment = $useSandbox == true ? "sandbox" : "secure";
$endPoint = "https://{$environment}.paychoice.com.au/api/v3/charge";
//Initialise CURL and set base options
$curl = curl_init();
curl_setopt($curl, CURLOPT_TIMEOUT, 60);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl, CURLOPT_FRESH_CONNECT, true);
curl_setopt($curl, CURLOPT_FORBID_REUSE, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded; charset=utf-8'));
//Setup CURL request method
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $this->encodeData($requestData));
//Setup CURL params for this request
curl_setopt($curl, CURLOPT_URL, $endPoint);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, $credentials);
//Run CURL
$response = curl_exec($curl);
$error = curl_error($curl);
$responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$responseObject = json_decode($response);
if (is_object($responseObject) && $responseObject->object_type == "error")
{
$errorParam = strlen($responseObject->error->param) > 0 ? ". Parameter: " . $responseObject->error->param : "";
throw new PaychoiceException("Paychoice returned an error. Error: " . $responseObject->error->message . $errorParam);
}
//Check for CURL errors
if (isset($error) && strlen($error))
{
throw new PaychoiceException("Could not successfully communicate with payment processor. Error: {$error}.");
}
else if (isset($responseCode) && strlen($responseCode) && $responseCode == '500')
{
throw new PaychoiceException("Could not successfully communicate with payment processor. HTTP response code {$responseCode}.");
}
return $responseObject;
}
private function encodeData($requestData)
{
if (!is_array($requestData))
{
throw new PaychoiceException("Request data is not in an array");
}
$formValues = "";
foreach($requestData as $key=>$value)
{
$formValues .= $key.'='.urlencode($value).'&';
}
rtrim($formValues, '&');
return $formValues;
}
}
class PaychoiceException extends Exception {}
//No closing tag