-
Notifications
You must be signed in to change notification settings - Fork 0
/
groupBuyingPaymentSense.class.php
628 lines (541 loc) · 28 KB
/
groupBuyingPaymentSense.class.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
<?php
class Group_Buying_PaymentSense extends Group_Buying_Credit_Card_Processors {
const API_ENDPOINT = 'paymentsensegateway.com';
const API_ENDPOINT_PORT = '4430';
const MODE_TEST = 'sandbox';
const MODE_LIVE = 'live';
const API_MERCHANTID_OPTION = 'gb_paymentsense_username';
const API_PASSWORD_OPTION = 'gb_paymentsense_password';
const API_SECRETKEY_OPTION = 'gb_paymentsense_secretkey';
const API_MODE_OPTION = 'gb_paymentsense_mode';
const API_CC_OPTION = 'gb_paymentsense_currency_code';
const PAYMENT_METHOD = 'Credit (PaymentSense)';
protected static $instance;
private $api_mode = self::MODE_TEST;
private $api_merchantid = '';
private $api_password = '';
private $api_secretkey = '';
private $currency_code = '';
public static function get_instance() {
if ( !( isset( self::$instance ) && is_a( self::$instance, __CLASS__ ) ) ) {
self::$instance = new self();
}
return self::$instance;
}
private function get_api_url() {
if ( self::API_ENDPOINT_PORT == '443' ) {
return trailingslashit( self::API_ENDPOINT );
} else {
return trailingslashit( self::API_ENDPOINT . ':' . self::API_ENDPOINT_PORT );
}
}
public function get_payment_method() {
return self::PAYMENT_METHOD;
}
protected function __construct() {
parent::__construct();
$this->api_merchantid = get_option( self::API_MERCHANTID_OPTION, '' );
$this->api_password = get_option( self::API_PASSWORD_OPTION, '' );
$this->api_secretkey = get_option( self::API_SECRETKEY_OPTION, time() );
$this->currency_code = get_option( self::API_CC_OPTION, '826' );
$this->api_mode = get_option( self::API_MODE_OPTION, self::MODE_TEST );
add_action( 'admin_init', array( $this, 'register_settings' ), 10, 0 );
add_action( 'purchase_completed', array( $this, 'complete_purchase' ), 10, 1 );
add_action(self::CRON_HOOK, array($this, 'capture_pending_payments'));
// Handle the return of user from 3dsecure
add_action('gb_load_cart', array($this,'back_from_3d_secure'), 10, 0);
// pop the products array so that only a single item can be added to the cart at a time.
add_action('gb_cart_load_products_get', array( get_class(), 'pop_cart_products'), 100, 2 );
}
public static function register() {
self::add_payment_processor( __CLASS__, self::__( 'PaymentSense' ) );
}
public static function pop_cart_products( $products, Group_Buying_Cart $cart ) {
if ( count($products) > 1 ) {
return array(array_pop($products));
}
return $products;
}
/**
* Process a payment
*
* @param Group_Buying_Checkouts $checkout
* @param Group_Buying_Purchase $purchase
* @return Group_Buying_Payment|bool FALSE if the payment failed, otherwise a Payment object
*/
public function process_payment( Group_Buying_Checkouts $checkout, Group_Buying_Purchase $purchase, $threeD_pass = FALSE ) {
if ( $purchase->get_total( $this->get_payment_method() ) < 0.01 ) {
// Nothing to do here, another payment handler intercepted and took care of everything
// See if we can get that payment and just return it
$payments = Group_Buying_Payment::get_payments_for_purchase( $purchase->get_id() );
foreach ( $payments as $payment_id ) {
$payment = Group_Buying_Payment::get_instance( $payment_id );
return $payment;
}
}
if ( $_REQUEST['gb_checkout_action'] == 'back_from_3dsecure' ) {
$transaction_response = $this->process_3d_secure();
} else {
$transaction_response = $this->process_sale( $checkout, $purchase );
}
if ( self::DEBUG ) error_log( "transaction_response: " . print_r( $transaction_response, true ) );
if ( $transaction_response === FALSE ) {
return;
}
// Purchase since payment was successful above.
// Create deals array.
$deal_info = array(); // creating purchased products array for payment below
foreach ( $purchase->get_products() as $item ) {
if ( isset( $item['payment_method'][$this->get_payment_method()] ) ) {
if ( !isset( $deal_info[$item['deal_id']] ) ) {
$deal_info[$item['deal_id']] = array();
}
$deal_info[$item['deal_id']][] = $item;
}
}
// Include shipping in payment object
if ( isset( $checkout->cache['shipping'] ) ) {
$shipping_address = array();
$shipping_address['first_name'] = $checkout->cache['shipping']['first_name'];
$shipping_address['last_name'] = $checkout->cache['shipping']['last_name'];
$shipping_address['street'] = $checkout->cache['shipping']['street'];
$shipping_address['city'] = $checkout->cache['shipping']['city'];
$shipping_address['zone'] = $checkout->cache['shipping']['zone'];
$shipping_address['postal_code'] = $checkout->cache['shipping']['postal_code'];
$shipping_address['country'] = $checkout->cache['shipping']['country'];
}
// New payment
$payment_id = Group_Buying_Payment::new_payment( array(
'payment_method' => $this->get_payment_method(),
'purchase' => $purchase->get_id(),
'amount' => gb_get_number_format( $purchase->get_total( $this->get_payment_method() ) ), // TODO CHANGE to NVP_DATA Match
'data' => array(
'api_response' => $transaction_response, // TODO Pass the transaction object along and use it.
'masked_cc_number' => $this->mask_card_number( $this->cc_cache['cc_number'] ), // save for possible credits later
'uncaptured_deals' => $deal_info,
),
'deals' => $deal_info,
'shipping_address' => $shipping_address,
), Group_Buying_Payment::STATUS_AUTHORIZED );
if ( !$payment_id ) {
return FALSE;
}
$payment = Group_Buying_Payment::get_instance( $payment_id );
do_action( 'payment_authorized', $payment );
return $payment;
}
/**
* Complete the purchase after the process_payment action, otherwise vouchers will not be activated.
*
* @param Group_Buying_Purchase $purchase
* @return void
*/
public function complete_purchase( Group_Buying_Purchase $purchase ) {
$items_captured = array(); // Creating simple array of items that are captured
foreach ( $purchase->get_products() as $item ) {
$items_captured[] = $item['deal_id'];
}
$payments = Group_Buying_Payment::get_payments_for_purchase( $purchase->get_id() );
foreach ( $payments as $payment_id ) {
$payment = Group_Buying_Payment::get_instance( $payment_id );
$this->capture_payment( $payment );
}
}
/**
* Try to capture all pending payments
*
* @return void
*/
public function capture_pending_payments() {
$payments = Group_Buying_Payment::get_pending_payments();
foreach ( $payments as $payment_id ) {
$payment = Group_Buying_Payment::get_instance($payment_id);
$this->capture_payment($payment);
}
}
/**
* Grabs error messages from a Authorize response and displays them to the user
*
* @param array $response
* @param bool $display
* @return void
*/
private function set_error_messages( $response, $display = TRUE ) {
if ( $display ) {
self::set_message( $response, self::MESSAGE_STATUS_ERROR );
error_log( $response );
} else {
error_log( $response );
}
}
/**
* Build the NVP data array for submitting the current checkout to Authorize as an Authorization request
*
* @param Group_Buying_Checkouts $checkout
* @param Group_Buying_Purchase $purchase
* @return array
*/
private function process_sale( Group_Buying_Checkouts $checkout, Group_Buying_Purchase $purchase ) {
$user = get_userdata( $purchase->get_user() );
$amount = gb_get_number_format( $purchase->get_total( $this->get_payment_method() ), '' ); // Remove the decimal
$description = '';
foreach ( $purchase->get_products() as $item ) {
if ( isset( $item['payment_method'][$this->get_payment_method()] ) ) {
$deal = Group_Buying_Deal::get_instance( $item['deal_id'] );
$description .= $deal->get_slug().', ';
}
}
require_once 'lib/PaymentSystem.php';
$rgeplRequestGatewayEntryPointList = new RequestGatewayEntryPointList();
// you need to put the correct gateway entry point urls in here
// contact support to get the correct urls
// The actual values to use for the entry points can be established in a number of ways
// 1) By periodically issuing a call to GetGatewayEntryPoints
// 2) By storing the values for the entry points returned with each transaction
// 3) Speculatively firing transactions at https://gw1.xxx followed by gw2, gw3, gw4....
// The lower the metric (2nd parameter) means that entry point will be attempted first,
// EXCEPT if it is -1 - in this case that entry point will be skipped
// NOTE: You do NOT have to add the entry points in any particular order - the list is sorted
// by metric value before the transaction submitting process begins
// The 3rd parameter is a retry attempt, so it is possible to try that entry point that number of times
// before failing over onto the next entry point in the list
$rgeplRequestGatewayEntryPointList->add( "https://gw1.".self::get_api_url(), 100, 1 );
$rgeplRequestGatewayEntryPointList->add( "https://gw2.".self::get_api_url(), 200, 1 );
$rgeplRequestGatewayEntryPointList->add( "https://gw3.".self::get_api_url(), 300, 1 );
$cdtCardDetailsTransaction = new CardDetailsTransaction( $rgeplRequestGatewayEntryPointList );
$cdtCardDetailsTransaction->getMerchantAuthentication()->setMerchantID( (string) $this->api_merchantid );
$cdtCardDetailsTransaction->getMerchantAuthentication()->setPassword( (string) $this->api_password );
$cdtCardDetailsTransaction->getTransactionDetails()->getMessageDetails()->setTransactionType( "PREAUTH" );
$cdtCardDetailsTransaction->getTransactionDetails()->getAmount()->setValue( $amount );
$cdtCardDetailsTransaction->getTransactionDetails()->getCurrencyCode()->setValue( $this->currency_code );
$cdtCardDetailsTransaction->getTransactionDetails()->setOrderID( (string) $purchase->get_id() );
$cdtCardDetailsTransaction->getTransactionDetails()->setOrderDescription( $description );
$cdtCardDetailsTransaction->getTransactionDetails()->getTransactionControl()->getEchoCardType()->setValue( true );
$cdtCardDetailsTransaction->getTransactionDetails()->getTransactionControl()->getEchoAmountReceived()->setValue( true );
$cdtCardDetailsTransaction->getTransactionDetails()->getTransactionControl()->getEchoAVSCheckResult()->setValue( true );
$cdtCardDetailsTransaction->getTransactionDetails()->getTransactionControl()->getEchoCV2CheckResult()->setValue( true );
$cdtCardDetailsTransaction->getTransactionDetails()->getTransactionControl()->getThreeDSecureOverridePolicy()->setValue( true );
$cdtCardDetailsTransaction->getTransactionDetails()->getTransactionControl()->getDuplicateDelay()->setValue( 60 );
$cdtCardDetailsTransaction->getTransactionDetails()->getThreeDSecureBrowserDetails()->getDeviceCategory()->setValue( 0 );
$cdtCardDetailsTransaction->getTransactionDetails()->getThreeDSecureBrowserDetails()->setAcceptHeaders( "*/*" );
$cdtCardDetailsTransaction->getTransactionDetails()->getThreeDSecureBrowserDetails()->setUserAgent( $_SERVER["HTTP_USER_AGENT"] );
$cdtCardDetailsTransaction->getCardDetails()->setCardName( $checkout->cache['billing']['first_name'] . ' ' . $checkout->cache['billing']['last_name'] );
$cdtCardDetailsTransaction->getCardDetails()->setCardNumber( $this->cc_cache['cc_number'] );
if ( $this->cc_cache['cc_expiration_month'] != '' ) {
$cdtCardDetailsTransaction->getCardDetails()->getExpiryDate()->getMonth()->setValue( substr( '0' . $this->cc_cache['cc_expiration_month'], -2 ) );
}
if ( $this->cc_cache['cc_expiration_year'] != '' ) {
$cdtCardDetailsTransaction->getCardDetails()->getExpiryDate()->getYear()->setValue( substr( $this->cc_cache['cc_expiration_year'], -2 ) );
}
if ( $this->cc_cache['cc_start_expiration_month'] != '' ) {
$cdtCardDetailsTransaction->getCardDetails()->getStartDate()->getMonth()->setValue( substr( '0' . $this->cc_cache['cc_start_expiration_month'], -2 ) );
}
if ( $this->cc_cache['cc_start_expiration_year'] != '' ) {
$cdtCardDetailsTransaction->getCardDetails()->getStartDate()->getYear()->setValue( substr( $this->cc_cache['cc_start_expiration_year'], -2 ) );
}
// $cdtCardDetailsTransaction->getCardDetails()->setIssueNumber( $IssueNumber );
$cdtCardDetailsTransaction->getCardDetails()->setCV2( $this->cc_cache['cc_cvv'] );
$cdtCardDetailsTransaction->getCustomerDetails()->getBillingAddress()->setAddress1( $checkout->cache['billing']['first_name'] . ' ' . $checkout->cache['billing']['last_name'] );
$cdtCardDetailsTransaction->getCustomerDetails()->getBillingAddress()->setAddress2( $checkout->cache['billing']['street'] );
$cdtCardDetailsTransaction->getCustomerDetails()->getBillingAddress()->setCity( $checkout->cache['billing']['city'] );
$cdtCardDetailsTransaction->getCustomerDetails()->getBillingAddress()->setState( $checkout->cache['billing']['zone'] );
$cdtCardDetailsTransaction->getCustomerDetails()->getBillingAddress()->setPostCode( $checkout->cache['billing']['phone'] );
/*/
if ( $checkout->cache['billing']['country'] != '' &&
$checkout->cache['billing']['country'] != '-1' &&
$iclISOCountryList->getISOCountry( $checkout->cache['billing']['country'], $icISOCountry ) ) {
$cdtCardDetailsTransaction->getCustomerDetails()->getBillingAddress()->getCountryCode()->setValue( $icISOCountry->getISOCode() );
}
/**/
$cdtCardDetailsTransaction->getCustomerDetails()->setEmailAddress( $user->user_email );
$cdtCardDetailsTransaction->getCustomerDetails()->setPhoneNumber( $checkout->cache['billing']['phone'] );
$cdtCardDetailsTransaction->getCustomerDetails()->setCustomerIPAddress( $_SERVER['REMOTE_ADDR'] );
if ( self::DEBUG ) error_log( "TRANSACTION +++++++++++++++++++: " . print_r( $cdtCardDetailsTransaction, true ) );
$boTransactionProcessed = $cdtCardDetailsTransaction->processTransaction( $cdtrCardDetailsTransactionResult, $todTransactionOutputData );
if ( self::DEBUG ) error_log( "PROCESSED TRANSACTION +++++++++++++++++++: " . print_r( $boTransactionProcessed, true ) );
if ( !$boTransactionProcessed ) {
// could not communicate with the payment gateway
$message = "Couldn't communicate with payment gateway". $cdtCardDetailsTransaction->getLastException()->getMessage();
self::set_error_messages( $message );
$TransactionSuccessful = FALSE;
} else {
if ( self::DEBUG ) error_log( "status code ++++++++++: " . print_r( $cdtrCardDetailsTransactionResult->getStatusCode(), true ) );
switch ( $cdtrCardDetailsTransactionResult->getStatusCode() ) {
case 0:
// status code of 0 - means transaction successful
$Message = $cdtrCardDetailsTransactionResult->getMessage();
self::set_error_messages( $Message );
$TransactionSuccessful = $cdtrCardDetailsTransactionResult;
$TransactionSuccessful->CrossReference = $todTransactionOutputData->getCrossReference();
break;
case 3:
// status code of 3 - means 3D Secure authentication required
// Redirect before proceeding
$PaREQ = $todTransactionOutputData->getThreeDSecureOutputData()->getPaREQ();
$CrossReference = $todTransactionOutputData->getCrossReference();
$acs_url = $todTransactionOutputData->getThreeDSecureOutputData()->getACSURL();
self::redirect( $PaREQ, $CrossReference, $acs_url, add_query_arg( array( 'back_from_3dsecure' => 1, 'order_id' => $purchase->get_id() ), Group_Buying_Checkouts::get_url() ) );
exit();
break;
case 5:
// status code of 5 - means transaction declined
$Message = $cdtrCardDetailsTransactionResult->getMessage();
self::set_error_messages( $Message );
$TransactionSuccessful = FALSE;
break;
case 20:
// status code of 20 - means duplicate transaction
if ( $cdtrCardDetailsTransactionResult->getPreviousTransactionResult()->getStatusCode()->getValue() == 0 ) {
$TransactionSuccessful = $cdtrCardDetailsTransactionResult;
}
else {
$PreviousTransactionMessage = $cdtrCardDetailsTransactionResult->getPreviousTransactionResult()->getMessage();
self::set_error_messages( $PreviousTransactionMessage );
$TransactionSuccessful = FALSE;
}
break;
case 30:
// status code of 30 - means an error occurred
$Message = $cdtrCardDetailsTransactionResult->getMessage();
if ( $cdtrCardDetailsTransactionResult->getErrorMessages()->getCount() > 0 ) {
$Message = $Message."<br /><ul>";
for ( $LoopIndex = 0; $LoopIndex < $cdtrCardDetailsTransactionResult->getErrorMessages()->getCount(); $LoopIndex++ ) {
$Message = $Message."<li>".$cdtrCardDetailsTransactionResult->getErrorMessages()->getAt( $LoopIndex )."</li>";
}
$Message = $Message."</ul>";
$TransactionSuccessful = FALSE;
}
if ( $todTransactionOutputData == null ) {
$szResponseCrossReference = "";
}
else {
$szResponseCrossReference = $todTransactionOutputData->getCrossReference();
}
self::set_error_messages( $Message );
break;
default:
// unhandled status code
$Message = $cdtrCardDetailsTransactionResult->getMessage();
if ( $todTransactionOutputData == null ) {
$szResponseCrossReference = "";
}
else {
$szResponseCrossReference = $todTransactionOutputData->getCrossReference();
}
$TransactionSuccessful = FALSE;
self::set_error_messages( $Message );
break;
}
}
return $TransactionSuccessful;
}
public function capture_payment( Group_Buying_Payment $payment ) {
// is this the right payment processor? does the payment still need processing?
if ( $payment->get_payment_method() == $this->get_payment_method() && $payment->get_status() != Group_Buying_Payment::STATUS_COMPLETE ) {
//$payment->set_status(Group_Buying_Payment::STATUS_COMPLETE);
$data = $payment->get_data();
// Do we have a transaction ID to use for the capture?
if ( isset( $data['api_response'] ) ) {
$items_to_capture = $this->items_to_capture($payment);
if ( $items_to_capture ) {
// Get amount
$total = 0;
foreach ( $items_to_capture as $price ) {
$total += $price;
}
// Do some juggling of the object to get the cross reference id
$json = json_encode( $data['api_response'] );
$api_response = json_decode( $json, true );
$CrossReference = $api_response['CrossReference'];
// Payment
require_once 'lib/PaymentSystem.php';
$rgeplRequestGatewayEntryPointList = new RequestGatewayEntryPointList();
$rgeplRequestGatewayEntryPointList->add( "https://gw1.".self::get_api_url(), 100, 1 );
$rgeplRequestGatewayEntryPointList->add( "https://gw2.".self::get_api_url(), 200, 1 );
$rgeplRequestGatewayEntryPointList->add( "https://gw3.".self::get_api_url(), 300, 1 );
$CrossReferenceTransaction = new CrossReferenceTransaction( $rgeplRequestGatewayEntryPointList );
$CrossReferenceTransaction->getMerchantAuthentication()->setMerchantID( (string) $this->api_merchantid );
$CrossReferenceTransaction->getMerchantAuthentication()->setPassword( (string) $this->api_password );
$CrossReferenceTransaction->getTransactionDetails()->getMessageDetails()->setTransactionType( "COLLECTION" );
$CrossReferenceTransaction->getTransactionDetails()->getMessageDetails()->setCrossReference( $CrossReference );
$CrossReferenceTransaction->getTransactionDetails()->getAmount()->setValue( $total );
$CrossReferenceTransaction->getTransactionDetails()->getCurrencyCode()->setValue( $this->currency_code );
$CrossReferenceTransaction->getTransactionDetails()->setOrderID( (string) $payment->get_purchase() );
$boTransactionProcessed = $CrossReferenceTransaction->processTransaction( $cdtrCardDetailsTransactionResult, $todTransactionOutputData );
if ( $cdtrCardDetailsTransactionResult->getStatusCode() == ( 0 || 20 ) ) { // success or duplicate
foreach ( $items_to_capture as $deal_id => $amount ) {
unset($data['uncaptured_deals'][$deal_id]);
}
// Reset data
if ( !isset($data['capture_response']) ) {
$data['capture_response'] = array();
}
$data['capture_response'] = $cdtrCardDetailsTransactionResult;
$payment->set_data( $data );
do_action('payment_captured', $payment, array_keys($items_to_capture));
$payment->set_status(Group_Buying_Payment::STATUS_COMPLETE);
do_action('payment_complete', $payment);
}
}
}
}
}
public static function redirect( $PaREQ, $CrossReference, $acs_url, $return_url ) {
$_input = ' <input type="hidden" name="%s" value="%s" />';
$_html = array();
$_html[] = "<html>";
$_html[] = "<head><title>Processing Payment...</title></head>";
$_html[] = "<body onLoad=\"document.forms['3d_secure'].submit();\">";
//$_html[] = "<body>";
$_html[] = '<center><img src="'. gb_get_header_logo() .'"></center>';
$_html[] = "<center><h2>";
$_html[] = self::__("Please wait, your order is being processed and you will be redirected to your bank website.");
$_html[] = "</h2></center>";
$_html[] = '<form name="3d_secure" action="'. $acs_url .'" method="post">';
$_html[] = '<input type="hidden" value="'.$PaREQ.'" name="PaREQ" />';
$_html[] = '<input type="hidden" value="'.$CrossReference.'" name="MD" />';
$_html[] = '<input type="hidden" value="'. $return_url .'" name="TermUrl" />';
$_html[] = "<center><br/><br/>";
$_html[] = self::__("If you are not automatically redirected to your bank to complete your purchase within 5 seconds...");
$_html[] = "<br/><br/>\n";
$_html[] = '<input type="submit" value="'.self::__('Click Here').'"></center>';
$_html[] = '</form>';
$_html[] = '</body>';
$return = implode("\n", $_html);
print $return;
exit();
}
public function back_from_3d_secure() {
if ( isset( $_POST['MD'] ) && isset( $_POST['PaRes'] ) ) {
$_REQUEST['gb_checkout_action'] = 'back_from_3dsecure';
}
}
public static function process_3d_secure() {
if ( isset( $_POST['MD'] ) == false || isset( $_POST['PaRes'] ) == false ) {
$message = "There were errors collecting the responses back from the ACS";
self::set_error_messages( $message );
return FALSE;
}
$api_merchantid = get_option( self::API_MERCHANTID_OPTION, '' );
$api_password = get_option( self::API_PASSWORD_OPTION, '' );
$MD = $_POST['MD'];
$PaRES = $_POST['PaRes'];
require_once 'lib/PaymentSystem.php';
$rgeplRequestGatewayEntryPointList = new RequestGatewayEntryPointList();
$rgeplRequestGatewayEntryPointList->add( "https://gw1.".self::get_api_url(), 100, 1 );
$rgeplRequestGatewayEntryPointList->add( "https://gw2.".self::get_api_url(), 200, 1 );
$rgeplRequestGatewayEntryPointList->add( "https://gw3.".self::get_api_url(), 300, 1 );
$tdsaThreeDSecureAuthentication = new ThreeDSecureAuthentication( $rgeplRequestGatewayEntryPointList );
$tdsaThreeDSecureAuthentication->getMerchantAuthentication()->setMerchantID( (string) $api_merchantid );
$tdsaThreeDSecureAuthentication->getMerchantAuthentication()->setPassword( (string) $api_password );
$tdsaThreeDSecureAuthentication->getThreeDSecureInputData()->setCrossReference( $MD );
$tdsaThreeDSecureAuthentication->getThreeDSecureInputData()->setPaRES( $PaRES );
$boTransactionProcessed = $tdsaThreeDSecureAuthentication->processTransaction( $tdsarThreeDSecureAuthenticationResult, $todTransactionOutputData );
if ( self::DEBUG ) error_log( "3D secure authentication: " . print_r( $tdsarThreeDSecureAuthenticationResult->getStatusCode(), true ) );
if ( $boTransactionProcessed == false ) {
// could not communicate with the payment gateway
$Message = "Couldn't communicate with payment gateway";
self::set_error_messages( $Message );
$TransactionSuccessful = FALSE;
}
else {
if ( self::DEBUG ) error_log( "3D secure authentication code: " . print_r( $tdsarThreeDSecureAuthenticationResult->getStatusCode(), true ) );
switch ( $tdsarThreeDSecureAuthenticationResult->getStatusCode() ) {
case 0:
// status code of 0 - means transaction successful
$TransactionSuccessful = $tdsarThreeDSecureAuthenticationResult;
break;
case 5:
// status code of 5 - means transaction declined
$Message = $tdsarThreeDSecureAuthenticationResult->getMessage();
self::set_error_messages( $Message );
$TransactionSuccessful = FALSE;
break;
case 20:
// status code of 20 - means duplicate transaction
if ( $tdsarThreeDSecureAuthenticationResult->getPreviousTransactionResult()->getStatusCode()->getValue() == 0 ) {
$TransactionSuccessful = $cdtrCardDetailsTransactionResult;
}
else {
$PreviousTransactionMessage = $tdsarThreeDSecureAuthenticationResult->getPreviousTransactionResult()->getMessage();
self::set_error_messages( $PreviousTransactionMessage );
$TransactionSuccessful = FALSE;
}
break;
case 30:
// status code of 30 - means an error occurred
$Message = $tdsarThreeDSecureAuthenticationResult->getMessage();
if ( $tdsarThreeDSecureAuthenticationResult->getErrorMessages()->getCount() > 0 ) {
$Message = $Message."<br /><ul>";
for ( $LoopIndex = 0; $LoopIndex < $tdsarThreeDSecureAuthenticationResult->getErrorMessages()->getCount(); $LoopIndex++ ) {
$Message = $Message."<li>".$tdsarThreeDSecureAuthenticationResult->getErrorMessages()->getAt( $LoopIndex )."</li>";
}
$Message = $Message."</ul>";
$TransactionSuccessful = false;
}
if ( $todTransactionOutputData == null ) {
$szResponseCrossReference = "";
}
else {
$szResponseCrossReference = $todTransactionOutputData->getCrossReference();
}
$TransactionSuccessful = FALSE;
self::set_error_messages( $Message );
break;
default:
// unhandled status code
$Message = $tdsarThreeDSecureAuthenticationResult->getMessage();
if ( $todTransactionOutputData == null ) {
$szResponseCrossReference = "";
}
else {
$szResponseCrossReference = $todTransactionOutputData->getCrossReference();
}
$TransactionSuccessful = FALSE;
self::set_error_messages( $Message );
break;
}
}
return $TransactionSuccessful;
}
public function register_settings() {
$page = Group_Buying_Payment_Processors::get_settings_page();
$section = 'gb_authorizenet_settings';
add_settings_section( $section, self::__( 'PaymentSense' ), array( $this, 'display_settings_section' ), $page );
register_setting( $page, self::API_MERCHANTID_OPTION );
register_setting( $page, self::API_PASSWORD_OPTION );
//register_setting( $page, self::API_SECRETKEY_OPTION );
register_setting( $page, self::API_CC_OPTION );
add_settings_field( self::API_MERCHANTID_OPTION, self::__( 'Merchant ID (Username)' ), array( $this, 'display_api_username_field' ), $page, $section );
add_settings_field( self::API_PASSWORD_OPTION, self::__( 'Merchant Password' ), array( $this, 'display_api_password_field' ), $page, $section );
//add_settings_field( self::API_SECRETKEY_OPTION, self::__( 'Secret Key' ), array( $this, 'display_api_key_field' ), $page, $section );
add_settings_field( self::API_CC_OPTION, self::__( 'Currency' ), array( $this, 'display_currency_code_field' ), $page, $section );
}
public function display_api_username_field() {
echo '<input type="text" name="'.self::API_MERCHANTID_OPTION.'" value="'.$this->api_merchantid.'" size="80" />';
}
public function display_api_password_field() {
echo '<input type="text" name="'.self::API_PASSWORD_OPTION.'" value="'.$this->api_password.'" size="80" />';
}
public function display_api_key_field() {
echo '<input type="text" name="'.self::API_SECRETKEY_OPTION.'" value="'.$this->api_secretkey.'" size="80" />';
}
public function display_api_mode_field() {
echo '<label><input type="radio" name="'.self::API_MODE_OPTION.'" value="'.self::MODE_LIVE.'" '.checked( self::MODE_LIVE, $this->api_mode, FALSE ).'/> '.self::__( 'Live' ).'</label><br />';
echo '<label><input type="radio" name="'.self::API_MODE_OPTION.'" value="'.self::MODE_TEST.'" '.checked( self::MODE_TEST, $this->api_mode, FALSE ).'/> '.self::__( 'Sandbox' ).'</label>';
}
public function display_currency_code_field() {
echo '<input type="text" name="'.self::API_CC_OPTION.'" value="'.$this->currency_code.'" size="80" />';
echo '<br/>'.gb__( 'ISO 4217 e.g. 826' );
}
public function display_exp_meta_box() {
return GB_PATH . '/controllers/payment_processors/meta-boxes/exp-only.php';
}
public function display_price_meta_box() {
return GB_PATH . '/controllers/payment_processors/meta-boxes/no-dyn-price.php';
}
public function display_limits_meta_box() {
return GB_PATH . '/controllers/payment_processors/meta-boxes/no-tipping.php';
}
}
Group_Buying_PaymentSense::register();