@@ -371,15 +371,18 @@ public function getAllConfigChannels() {
371
371
public function getRedirectUrl () {
372
372
$ order = $ this ->getQuote ();
373
373
$ paymentMethod = get_class ($ order ->getPayment ()->getMethodInstance ());
374
- if ($ paymentMethod )
375
- $ modelPayment = Mage::getModel ($ paymentMethod );
376
- else
377
- $ modelPayment = $ this ;
378
374
379
- $ gate = Mage::helper ( ' bithypercharge/gateway ' ) ;
375
+ $ modelPayment = ( $ paymentMethod ) ? Mage::getModel ( $ paymentMethod ) : $ this ;
380
376
$ hypercharge_channels = $ this ->getConfigChannels ();
377
+
378
+ // set API call mode
381
379
$ mode = !$ modelPayment ->getConfigData ('mode ' );
382
- $ transactionTypes = explode (', ' , $ modelPayment ->getConfigData ('transaction_types ' ));
380
+ if ($ mode ) {
381
+ $ mode = Hypercharge \Config::ENV_LIVE ;
382
+ } else {
383
+ $ mode = Hypercharge \Config::ENV_SANDBOX ;
384
+ }
385
+
383
386
$ ttl = $ modelPayment ->getConfigData ('ttl ' );
384
387
385
388
if (!$ hypercharge_channels ) {
@@ -391,29 +394,39 @@ public function getRedirectUrl() {
391
394
Mage::throwException ('Could not retrieve order information ' );
392
395
return ;
393
396
}
397
+
394
398
$ currency = $ order ->getBaseCurrencyCode ();
395
399
if (!array_key_exists ($ currency , $ hypercharge_channels )) {
396
400
Mage::throwException (
397
- 'The merchant doesn \'t accept payments for this currency ' );
401
+ 'The merchant doesn \'t accept payments for this currency ' );
398
402
return ;
399
403
}
400
- $ gate ->setChannel ($ hypercharge_channels [$ currency ]['channel ' ]);
401
- $ gate ->setUsername ($ hypercharge_channels [$ currency ]['login ' ]);
402
- $ gate ->setPassword ($ hypercharge_channels [$ currency ]['pass ' ]);
403
404
404
- $ gate ->setMode ('live ' );
405
- if (!$ mode )
406
- $ gate ->setMode ('test ' );
407
405
408
- // is editable by user
409
- $ editableByUser = (int ) $ modelPayment ->getConfigData ('check_address ' );
406
+ Hypercharge \Config::set (
407
+ $ hypercharge_channels [$ currency ]['login ' ]
408
+ ,$ hypercharge_channels [$ currency ]['pass ' ]
409
+ ,$ mode
410
+ );
410
411
412
+
413
+ $ currency = $ order ->getBaseCurrencyCode ();
414
+ if (!array_key_exists ($ currency , $ hypercharge_channels )) {
415
+ Mage::throwException (
416
+ 'The merchant doesn \'t accept payments for this currency ' );
417
+ return ;
418
+ }
419
+
420
+ // is editable by user
421
+ $ editableByUser = (bool ) $ modelPayment ->getConfigData ('check_address ' );
422
+ $ amount = sprintf ("%.02f " , $ order ->getBaseGrandTotal ()) * 100 ;
411
423
$ billing = $ order ->getBillingAddress ();
424
+
412
425
$ paymentData = array (
413
426
'transaction_id ' => $ order ->getRealOrderId (),
414
427
'usage ' => 'Hypercharge Web Payment Form transaction ' ,
415
428
'description ' => 'Order number ' . $ order ->getRealOrderId (),
416
- 'amount ' => sprintf ( " %.02f " , $ order -> getBaseGrandTotal ()) * 100 ,
429
+ 'amount ' => ( int ) $ amount ,
417
430
'currency ' => $ currency ,
418
431
'editable_by_user ' => $ editableByUser ,
419
432
'customer_email ' => $ order ->getCustomerEmail (),
@@ -429,7 +442,6 @@ public function getRedirectUrl() {
429
442
'city ' => utf8_decode ($ billing ->getCity ()),
430
443
'country ' => utf8_decode ($ billing ->getCountryId ()),
431
444
'zip_code ' => utf8_decode ($ billing ->getData ('postcode ' )),
432
- 'state ' => utf8_decode ($ billing ->getData ('region ' ))
433
445
),
434
446
'shipping_address ' => array (
435
447
'first_name ' => utf8_decode ($ billing ->getFirstname ()),
@@ -438,7 +450,6 @@ public function getRedirectUrl() {
438
450
'city ' => utf8_decode ($ billing ->getCity ()),
439
451
'country ' => utf8_decode ($ billing ->getCountryId ()),
440
452
'zip_code ' => utf8_decode ($ billing ->getData ('postcode ' )),
441
- 'state ' => utf8_decode ($ billing ->getData ('region ' ))
442
453
),
443
454
);
444
455
@@ -455,48 +466,78 @@ public function getRedirectUrl() {
455
466
$ paymentData ['ttl ' ] = $ ttl ;
456
467
457
468
// Log some information
458
- Mage::helper ('bithypercharge ' )->logger ("WPF initiated. Gateway config: \n"
459
- . print_r ($ gate ->getGateway (), true )
460
- . "Mode: " . print_r ($ gate ->getMode (), true )
461
- . "\nRequest string: \n" . '<?xml version="1.0" encoding="utf-8"?> '
462
- . $ gate ->paramsXML ($ paymentData , 'wpf_payment ' ));
463
-
464
- if (!$ response = $ gate ->wpf_create ($ paymentData )) {
465
- $ order ->addStatusToHistory ($ order ->getStatus (), $ gate ->__ ('Could not initiate WPF ' ));
466
- $ order ->save ();
467
- Mage::getSingleton ('core/session ' )
468
- ->addError ($ gate ->__ ('Could not initiate WPF ' ));
469
- return ;
470
- }
469
+ Mage::helper ('bithypercharge ' )->logger ("WPF initiated: \n"
470
+ . "Mode: " . print_r ($ mode , true )
471
+ . "\nRequest string: \n"
472
+ . var_export ($ paymentData , true ));
473
+
471
474
472
- // Log the response
473
- Mage::helper ('bithypercharge ' )->logger ("WPF response received: \n" . print_r ($ response , true ));
474
475
475
476
// Instantiate payment method to log the response error
476
477
$ paymentInst = $ order ->getPayment ()->getMethodInstance ();
477
- if ($ response ['status ' ] == 'error ' ) {
478
- $ order ->addStatusToHistory ($ order ->getStatus (), $ response ['message ' ]);
478
+
479
+ try {
480
+ $ payment = Hypercharge \Payment::wpf ($ paymentData );
481
+
482
+ if ($ payment ->status == 'error ' ) {
483
+ $ order ->addStatusToHistory ($ order ->getStatus (), $ payment ->message );
484
+ $ order ->save ();
485
+ Mage::getSingleton ('core/session ' )->addError ($ mode ? $ payment ->message : $ payment ->technical_message );
486
+ $ paymentInst ->setTransactionId ($ paymentData ['transaction_id ' ]);
487
+ return ;
488
+ }
489
+
490
+ // And some information aggregation before we leave
491
+
492
+ if ( $ payment ->shouldRedirect ()) {
493
+ // ok, WPF session created.
494
+ Mage::helper ('bithypercharge ' )->logger ('Customer redirected to Hypercharge ' );
495
+ $ order ->addStatusToHistory ($ order ->getStatus (), Mage::helper ('bithypercharge ' )->__ ('Customer was redirected to Hypercharge. ' ));
496
+ $ order ->save ();
497
+ $ paymentInst ->setLastTransactionId ($ paymentData ['transaction_id ' ]);
498
+ Mage::getSingleton ('core/session ' )->setHyperRedirectUrl ($ payment ->redirect_url );
499
+ if ($ modelPayment ->getConfigData ('use_iframe ' ) == 1 ) {
500
+ return Mage::getUrl ('bit-hypercharge/wpfredirect/hypercharge ' );
501
+ } elseif ($ modelPayment ->getConfigData ('use_iframe ' ) == 2 ) {
502
+ Mage::getSingleton ('core/session ' )->setHyperReviewRedirect (1 );
503
+ return false ;
504
+ } else {
505
+ return $ payment ->redirect_url ;
506
+ }
507
+ } elseif ($ payment ->isPersistentInHypercharge ()) {
508
+ Mage::helper ('bithypercharge ' )->logger ($ payment ->message . " " . $ payment ->technical_message );
509
+ // payment has been created in hypercharge but something went wrong.
510
+ if ($ payment ->status == 'error ' ) {
511
+ $ order ->addStatusToHistory ($ order ->getStatus (), $ payment ->message );
512
+ $ order ->save ();
513
+ Mage::getSingleton ('core/session ' )->addError ($ mode ? $ payment ->message : $ payment ->technical_message );
514
+ $ paymentInst ->setTransactionId ($ paymentData ['transaction_id ' ]);
515
+ return ;
516
+ }
517
+
518
+ } else {
519
+ $ order ->addStatusToHistory ($ order ->getStatus (), $ this ->__ ('Could not initiate WPF ' ));
520
+ $ order ->save ();
521
+ Mage::getSingleton ('core/session ' )->addError ($ this ->__ ('Could not initiate WPF ' ));
522
+ return ;
523
+ }
524
+ } catch (Hypercharge \Errors \ValidationError $ e ) {
525
+ // no payment created in hypercharge because of local pre-validation errors
526
+ // show validation errors to customer
527
+ // $e->errors is an Array of Hash, format: [ { "property": String , "message" : String }, ... ]
528
+ $ order ->addStatusToHistory ($ order ->getStatus (), "Oops! An error occured! " . implode ("<br/> " , $ e ->errors ));
479
529
$ order ->save ();
480
- Mage::getSingleton ('core/session ' )->addError (
481
- $ mode ? $ response ['message ' ] : $ response ['technical_message ' ]);
530
+ Mage::getSingleton ('core/session ' )->addError ($ mode ? $ payment ->message : $ payment ->technical_message );
482
531
$ paymentInst ->setTransactionId ($ paymentData ['transaction_id ' ]);
483
532
return ;
484
- }
485
533
486
- // And some information aggregation before we leave
487
- Mage::helper ('bithypercharge ' )->logger ('Customer redirected to Hypercharge ' );
488
- $ order ->addStatusToHistory ($ order ->getStatus (), $ gate ->__ ('Customer was redirected to Hypercharge. ' ));
489
- $ order ->save ();
490
- $ paymentInst ->setLastTransactionId ($ response ['transaction_id ' ]);
491
-
492
- Mage::getSingleton ('core/session ' )->setHyperRedirectUrl ($ response ['redirect_url ' ]);
493
- if ($ modelPayment ->getConfigData ('use_iframe ' ) == 1 ) {
494
- return Mage::getUrl ('bit-hypercharge/wpfredirect/hypercharge ' );
495
- } elseif ($ modelPayment ->getConfigData ('use_iframe ' ) == 2 ) {
496
- Mage::getSingleton ('core/session ' )->setHyperReviewRedirect (1 );
497
- return false ;
498
- } else {
499
- return $ response ['redirect_url ' ];
534
+ } catch (Exception $ e ) {
535
+ Mage::helper ('bithypercharge ' )->logger ('WPF ERROR: ' . var_export ($ e ->getTraceAsString (), true ));
536
+ $ order ->addStatusToHistory ($ order ->getStatus (), "Oops! An error occured! " );
537
+ $ order ->save ();
538
+ Mage::getSingleton ('core/session ' )->addError ("Sorry for the inconvenience! An error occured! " );
539
+ $ paymentInst ->setTransactionId ($ paymentData ['transaction_id ' ]);
540
+ return ;
500
541
}
501
542
}
502
543
@@ -539,29 +580,28 @@ public function wpfResponse($post) {
539
580
// Check for existence of data
540
581
if (!$ hypercharge_channels || !$ post || !is_array ($ post ))
541
582
return ;
542
- if (!($ post ['wpf_status ' ] && ($ post ['wpf_status ' ] == 'error ' || $ post ['wpf_status ' ] == 'timeout ' )))
543
- if (!array_key_exists ('signature ' , $ post ) || !array_key_exists ('payment_transaction_channel_token ' , $ post ) || !array_key_exists ('payment_transaction_unique_id ' , $ post ) || !array_key_exists ('wpf_transaction_id ' , $ post ) || !array_key_exists ('payment_transaction_transaction_type ' , $ post ) || !array_key_exists ('wpf_unique_id ' , $ post ) || !array_key_exists ('notification_type ' , $ post ))
583
+ Mage::log (var_export ($ post , true ), null , "aabb.log " );
584
+ if (!($ post ['payment_status ' ] && ($ post ['payment_status ' ] == 'error ' || $ post ['payment_status ' ] == 'timeout ' )))
585
+ if (!array_key_exists ('signature ' , $ post ) || !array_key_exists ('payment_transaction_channel_token ' , $ post ) || !array_key_exists ('payment_transaction_unique_id ' , $ post ) || !array_key_exists ('payment_transaction_id ' , $ post ) || !array_key_exists ('payment_transaction_transaction_type ' , $ post ) || !array_key_exists ('payment_unique_id ' , $ post ) || !array_key_exists ('notification_type ' , $ post ))
544
586
return ;
545
587
546
588
//Get the information from the POST variables
547
589
$ signature = $ post ['signature ' ];
548
590
$ trx_channel = $ post ['payment_transaction_channel_token ' ];
549
591
$ trx_id = $ post ['payment_transaction_unique_id ' ];
550
- $ wpf_trx_id = $ post ['wpf_transaction_id ' ];
592
+ $ wpf_trx_id = $ post ['payment_transaction_id ' ];
551
593
$ trx_type = $ post ['payment_transaction_transaction_type ' ];
552
- $ wpf_id = $ post ['wpf_unique_id ' ];
594
+ $ wpf_id = $ post ['payment_unique_id ' ];
553
595
$ notification_type = $ post ['notification_type ' ];
554
596
$ wireId = null ;
555
597
if ($ post ['wire_reference_id ' ]) {
556
598
$ wireId = $ post ['wire_reference_id ' ];
557
599
}
558
600
559
601
$ xml = $ gate ->getTrxEndXml ($ wpf_id );
560
-
602
+ $ order_id = substr ( $ wpf_trx_id , 0 , strpos ( $ wpf_trx_id , ' - ' ));
561
603
try {
562
- $ order = Mage::getModel ('sales/order ' )
563
- ->loadByIncrementId ($ wpf_trx_id );
564
- //$order_id = $order->getRealOrderId();
604
+ $ order = Mage::getModel ('sales/order ' )->loadByIncrementId ($ order_id );
565
605
} catch (Exception $ e ) {
566
606
Mage::helper ('bithypercharge ' )->logger ("\n" . $ timestamp
567
607
. ' Transaction could not be found in database - '
@@ -577,9 +617,9 @@ public function wpfResponse($post) {
577
617
$ mode = !$ modelPayment ->getConfigData ('mode ' );
578
618
579
619
//Check if error or timeout
580
- if ($ post ['wpf_status ' ] && ($ post ['wpf_status ' ] == 'error ' || $ post ['wpf_status ' ] == 'timeout ' )) {
620
+ if ($ post ['payment_status ' ] && ($ post ['payment_status ' ] == 'error ' || $ post ['payment_status ' ] == 'timeout ' )) {
581
621
// cancel order
582
- if ($ post ['wpf_status ' ] == 'timeout ' ) {
622
+ if ($ post ['payment_status ' ] == 'timeout ' ) {
583
623
$ order ->getPayment ()
584
624
->setAdditionalInformation (
585
625
'Transaction Status ' , 'timeout ' )
0 commit comments