From 44edba7e9978e5c3cd82ad3d1fe94174dff262e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Tatarczuk?= Date: Fri, 20 Nov 2015 11:56:35 +0100 Subject: [PATCH 1/9] Revert payment script, clean them up, add dynamic logo to sign2pay label --- .../Sign2pay/Payment/Block/Form/Sign2pay.php | 12 ++ .../Sign2pay/Payment/Block/Redirect.php | 4 +- .../Sign2pay/Payment/Helper/Data.php | 97 +++++------- .../Sign2pay/Payment/Model/Observer.php | 10 +- .../Payment/controllers/PaymentController.php | 11 ++ .../community/Sign2pay/Payment/etc/config.xml | 2 + .../community/Sign2pay/Payment/etc/system.xml | 20 +++ .../template/sign2pay/form/sign2pay.phtml | 2 +- js/sign2pay/payment.js | 147 +++--------------- 9 files changed, 108 insertions(+), 197 deletions(-) diff --git a/app/code/community/Sign2pay/Payment/Block/Form/Sign2pay.php b/app/code/community/Sign2pay/Payment/Block/Form/Sign2pay.php index 7bed8d6..1b2cf66 100644 --- a/app/code/community/Sign2pay/Payment/Block/Form/Sign2pay.php +++ b/app/code/community/Sign2pay/Payment/Block/Form/Sign2pay.php @@ -6,6 +6,12 @@ protected function _construct() { parent::_construct(); $this->setTemplate('sign2pay/form/sign2pay.phtml'); + + if (Mage::getStoreConfig('payment/sign2pay/logo_enabled', Mage::app()->getStore())) { + $this + ->setMethodLabelAfterHtml($this->_getMarkHtml()) + ->setMethodTitle(' ' . Mage::getStoreConfig('payment/sign2pay/title', Mage::app()->getStore())); + } } protected function _prepareLayout() @@ -21,6 +27,12 @@ protected function _prepareLayout() } } + protected function _getMarkHtml() + { + $height = Mage::getStoreConfig('payment/sign2pay/logo_width', Mage::app()->getStore()); + return ''; + } + public function getOrderId() { $lastOrderId = Mage::getSingleton('checkout/session')->getLastOrderId(); diff --git a/app/code/community/Sign2pay/Payment/Block/Redirect.php b/app/code/community/Sign2pay/Payment/Block/Redirect.php index ee11e16..cedbd53 100644 --- a/app/code/community/Sign2pay/Payment/Block/Redirect.php +++ b/app/code/community/Sign2pay/Payment/Block/Redirect.php @@ -15,9 +15,7 @@ protected function _prepareLayout() parent::_prepareLayout(); $this->setTemplate('sign2pay/redirect.phtml'); - Mage::helper('sign2pay')->attachPaymentScripts(array( - 'initialize' => true - )); + Mage::helper('sign2pay')->attachPaymentScripts(); return $this; } diff --git a/app/code/community/Sign2pay/Payment/Helper/Data.php b/app/code/community/Sign2pay/Payment/Helper/Data.php index 8ceb67c..4dc9f96 100644 --- a/app/code/community/Sign2pay/Payment/Helper/Data.php +++ b/app/code/community/Sign2pay/Payment/Helper/Data.php @@ -2,36 +2,7 @@ class Sign2pay_Payment_Helper_Data extends Mage_Core_Helper_Abstract { - - /** - * Retrive Sign2Pay merchant id. - * - * @return string - */ - public function getSign2payMerchantId() - { - return Mage::getStoreConfig('payment/sign2pay/merchant_id',Mage::app()->getStore()); - } - - /** - * Retrive Sign2Pay token. - * - * @return string - */ - public function getSign2payToken() - { - return Mage::getStoreConfig('payment/sign2pay/application_token',Mage::app()->getStore()); - } - - /** - * Retrive Sign2Pay api key. - * - * @return string - */ - public function getSign2payApiKey() - { - return Mage::getStoreConfig('payment/sign2pay/api_token',Mage::app()->getStore()); - } + const LOGO_URL = 'https://app.sign2pay.com/api/v2/banks/logo.gif'; /** * Retrive Sign2Pay Client ID. @@ -53,19 +24,6 @@ public function getSign2payClientSecret() return Mage::getStoreConfig('payment/sign2pay/client_secret',Mage::app()->getStore()); } - /** - * Retrive sign2pay options. - * - * @return array - */ - public function getSign2PayOptions() - { - return array( - 'merchantId' => $this->getSign2payMerchantId(), - 'token' => $this->getSign2payToken(), - ); - } - /** * Retrive sign2pay options. * @@ -97,15 +55,10 @@ public function getRedirectUri() */ public function attachPaymentScripts(array $additional = array()) { - /** - * @todo Properly remove/update this functionality - */ - return; - Mage::app()->getLayout()->getBlock('head')->addJs('sign2pay/jquery.min.js'); Mage::app()->getLayout()->getBlock('head')->addJs('sign2pay/payment.js'); - $options = $additional + $this->getSign2PayOptions(); + $options = $additional; $options['baseUrl'] = Mage::getBaseUrl(); $script = 'window.s2pOptions = ' . json_encode($options) . ';'; @@ -180,27 +133,19 @@ public function getQuote() } /** - * Prepare and return initial Sign2Pay request - * @todo device unical id + * Get all payment options applicable to sign2pay request * - * @return string + * @return array */ - public function getSign2PayInitialRequest() + public function getPaymentOptions() { $quote = $this->getQuote(); $billaddress = $quote->getBillingAddress(); $options = array(); - $options['client_id'] = $this->getSign2payClientId(); - $options['redirect_uri'] = $this->getRedirectUri(); $options['amount'] = $quote->getGrandTotal() * 100; - $options['response_type'] = 'code'; - $options['device_uid'] = 'test'; $options['locale'] = preg_replace('/_.*$/', '', Mage::app()->getLocale()->getLocaleCode()); - $options['state'] = $this->userStateHash(); - $options['scope'] = 'payment'; - $options['ref_id'] = $this->sign2PayCheckoutHash($quote->getReservedOrderId()); $options['user_params[identifier]'] = $billaddress->getEmail(); $options['user_params[first_name]'] = $billaddress->getFirstname(); @@ -210,6 +155,38 @@ public function getSign2PayInitialRequest() $options['user_params[country]'] = $billaddress->getCountry(); $options['user_params[postal_code]'] = $billaddress->getPostcode(); + return $options; + } + + /** + * Get payment logo url + * + * @return string + */ + public function getPaymentLogoUrl() + { + $options = $this->getPaymentOptions(); + return static::LOGO_URL . (!empty($options['user_params[identifier]']) ? ('?email=' . md5($options['user_params[identifier]'])) : ''); + } + + /** + * Prepare and return initial Sign2Pay request + * @todo device unical id + * + * @return string + */ + public function getSign2PayInitialRequest() + { + $options = $this->getPaymentOptions(); + + $options['client_id'] = $this->getSign2payClientId(); + $options['redirect_uri'] = $this->getRedirectUri(); + $options['ref_id'] = $this->sign2PayCheckoutHash($quote->getReservedOrderId()); + $options['response_type'] = 'code'; + $options['device_uid'] = 'test'; + $options['state'] = $this->userStateHash(); + $options['scope'] = 'payment'; + return 'https://app.sign2pay.com/oauth/authorize?' . http_build_query($options); } diff --git a/app/code/community/Sign2pay/Payment/Model/Observer.php b/app/code/community/Sign2pay/Payment/Model/Observer.php index 528ee36..4b6be67 100644 --- a/app/code/community/Sign2pay/Payment/Model/Observer.php +++ b/app/code/community/Sign2pay/Payment/Model/Observer.php @@ -25,15 +25,15 @@ public function afterBlockHtml($observer) | Mage_Payment_Model_Method_Abstract::CHECK_ZERO_TOTAL )) { - $this->_addInitializeRiskAssessment($transport); + $this->_addUpdate($transport); } } catch (Exception $e) { // Add anyway - $this->_addInitializeRiskAssessment($transport); + $this->_addUpdate($transport); } } else { // Add anyway - $this->_addInitializeRiskAssessment($transport); + $this->_addUpdate($transport); } } } @@ -41,9 +41,9 @@ public function afterBlockHtml($observer) /** * Add riska assessment trigger to transport */ - protected function _addInitializeRiskAssessment($transport) + protected function _addUpdate($transport) { - $html = ''; + $html = ''; $transport->setHtml($transport->getHtml() . $html); } } diff --git a/app/code/community/Sign2pay/Payment/controllers/PaymentController.php b/app/code/community/Sign2pay/Payment/controllers/PaymentController.php index 84310e4..7c030d2 100644 --- a/app/code/community/Sign2pay/Payment/controllers/PaymentController.php +++ b/app/code/community/Sign2pay/Payment/controllers/PaymentController.php @@ -125,4 +125,15 @@ public function cancelAction() $this->_redirect('checkout/cart'); } + /** + * Fetch payment logo url + */ + public function fetchPaymentLogoAction() + { + $options['logo'] = Mage::helper('sign2pay')->getPaymentLogoUrl(); + + $jsonData = json_encode($options); + $this->getResponse()->setHeader('Content-type', 'application/json'); + $this->getResponse()->setBody($jsonData); + } } diff --git a/app/code/community/Sign2pay/Payment/etc/config.xml b/app/code/community/Sign2pay/Payment/etc/config.xml index a2ccc54..ebbd637 100644 --- a/app/code/community/Sign2pay/Payment/etc/config.xml +++ b/app/code/community/Sign2pay/Payment/etc/config.xml @@ -64,6 +64,8 @@ sale 1 mage_world + 1 + 20 diff --git a/app/code/community/Sign2pay/Payment/etc/system.xml b/app/code/community/Sign2pay/Payment/etc/system.xml index 6ee8fa0..74490fb 100644 --- a/app/code/community/Sign2pay/Payment/etc/system.xml +++ b/app/code/community/Sign2pay/Payment/etc/system.xml @@ -197,6 +197,26 @@ validate-number + + + Enable logo on the checkout page + select + adminhtml/system_config_source_yesno + 3 + 1 + 1 + 1 + + + + + text + 4 + 1 + 1 + 1 + + // diff --git a/js/sign2pay/payment.js b/js/sign2pay/payment.js index 10ed2c6..35a6e71 100644 --- a/js/sign2pay/payment.js +++ b/js/sign2pay/payment.js @@ -6,7 +6,7 @@ throw new Error('Sign2Pay: ' + msg); } - var Sign2Pay = (function (settings) { + var Sign2Pay = (function () { /** * Sign2Pay constructor @@ -15,63 +15,25 @@ */ function Sign2Pay(settings) { - this.merchantId = settings.merchantId || throwError('No merchant id'); - this.token = settings.token || throwError('No token'); this.baseUrl = settings.baseUrl || throwError('No base url'); // Remove protocol from base url this.baseUrl = this.baseUrl.replace(/^http:/, ''); - - if (settings.initialize) this.initializePayment(); - } - - /** - * @var Default options - */ - - Sign2Pay.prototype.defaultOptions = { - domain: "sign2pay.com", - el: "#sign2pay" - } - - /** - * Initialize Sign2Pay transport with given options - * - * @param object options - */ - Sign2Pay.prototype.initTransport = function(options) { - if (!this.scriptAttached) { - this.scriptAttached = true; - $('head').append(''); - } - - var interval; - interval = setInterval(function() { - if (typeof window.s2p !== 'object' || typeof window.s2p.options !== 'object') return; - clearInterval(interval); - window.sign2PayOptions = options; - window.s2p.options.initTransport(); - }); } /** - * Fetches all payment related options + * Fetches payment logo * * @param function callback */ - Sign2Pay.prototype.fetchPaymentOptions = function(callback) { + Sign2Pay.prototype.fetchPaymentLogo = function(callback) { var self = this; - $.ajax(this.baseUrl + 'sign2pay/payment/fetchPaymentOptions', { + $.ajax(this.baseUrl + 'sign2pay/payment/fetchPaymentLogo', { type: 'POST', dataType: 'json', success: function(options) { - var options = $.extend(self.defaultOptions, options, { - merchant_id: self.merchantId, - token: self.token - }); - callback(options); }, error: function(err) { @@ -82,117 +44,46 @@ } /** - * Perform riskAssessment + * Perform logo update */ - Sign2Pay.prototype.riskAssessment = function() { + Sign2Pay.prototype.logoUpdate = function() { var self = this; - // Disable payment method - $('input[name="payment[method]"][value="sign2pay"]').attr('disabled', 'disabled'); + var $mark = $('#sign2pay-mark'); - var callback = function(options) { - var errors = []; + if (!$mark.size()) return; - $.each(options, function(key, value) { - if (!value) { - errors.push(key); - } - }); - - if (errors.length) { - console.log('[RA] Missing values for: ' + errors.join(', ')); - return; - } - - options['success'] = function() { - // Enable the Sign2Pay payment method - $('input[name="payment[method]"][value="sign2pay"]').removeAttr('disabled'); - }; - - options['checkout_type'] = 'multi'; - - self.initTransport(options); + var callback = function(options) { + $mark.attr('src', options.logo); } - this.fetchPaymentOptions(callback); + this.fetchPaymentLogo(callback); }; /** - * Initialize payment + * Perform updates */ - Sign2Pay.prototype.initializePayment = function() { - var self = this; - - var unloadCallback = function() { - return 'Leaving this screen might prevent you from completing this purchase.'; - }; - - var closeCallback = function() {}; - - var cancelCallback = function() { - $(window).unbind('beforeunload', unloadCallback); - window.location = self.baseUrl + "sign2pay/payment/cancel"; - }; - - var callback = function(options) { - options['launch'] = "on_load"; - options['map'] = {}; - - options['success'] = function() { - var el = $(this.el).get(0); - $('.s2p-button-text', el).remove(); - $('.s2p-button-banks', el) - .append('Pay with Sign2Pay') - .append('Cancel') - .children('.btn-cancel') - .on('click', function() { - cancelCallback(); - return false; - }); - $('.loading, .s2p-text', el).remove(); - }; - - options['error'] = function() { - alert('There was a problem during Sign2Pay initialization. Your ref_id is ' + options['ref_id'] + '.'); - - closeCallback = cancelCallback; - - var el = $(this.el).get(0); - $(".loading", el).hide(); - }; - - options['close'] = function(a) { - closeCallback(); - }; - - self.initTransport(options); - }; - - $(window).on('beforeunload', unloadCallback); - this.fetchPaymentOptions(callback); - } + Sign2Pay.prototype.update = function() { + this.logoUpdate(); + }; return Sign2Pay; })(); $(window).load(function() { - if (!s2pOptions || !s2pOptions['merchantId'] || !s2pOptions['token']) { - throwError('The Sign2Pay Module is enabled, but you are missing required settings.'); - } else { - window.sign2payPayment = new Sign2Pay(s2pOptions); - } + window.sign2payPayment = new Sign2Pay(s2pOptions); }); - window.initializeRiskAssessment = function() { + window.updateSign2pay = function() { var interval; interval = setInterval(function() { if (typeof window.sign2payPayment !== 'object') return; clearInterval(interval); - // Perform risk assessment - window.sign2payPayment.riskAssessment(); + // Perform update + window.sign2payPayment.update(); }); }; From ea0cf27cc385530dc765a78a1544813d051036d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Tatarczuk?= Date: Fri, 20 Nov 2015 12:00:47 +0100 Subject: [PATCH 2/9] Move payment amount getting to one helper method --- app/code/community/Sign2pay/Payment/Helper/Data.php | 13 ++++++++++++- .../community/Sign2pay/Payment/Model/Processor.php | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/app/code/community/Sign2pay/Payment/Helper/Data.php b/app/code/community/Sign2pay/Payment/Helper/Data.php index 4dc9f96..fc44e79 100644 --- a/app/code/community/Sign2pay/Payment/Helper/Data.php +++ b/app/code/community/Sign2pay/Payment/Helper/Data.php @@ -132,6 +132,17 @@ public function getQuote() return $quote; } + /** + * Get payment amount + * + * @return int + */ + public function getPaymentAmount() + { + $quote = $this->getQuote(); + return $quote->getGrandTotal() * 100; + } + /** * Get all payment options applicable to sign2pay request * @@ -144,7 +155,7 @@ public function getPaymentOptions() $billaddress = $quote->getBillingAddress(); $options = array(); - $options['amount'] = $quote->getGrandTotal() * 100; + $options['amount'] = $this->getPaymentAmount(); $options['locale'] = preg_replace('/_.*$/', '', Mage::app()->getLocale()->getLocaleCode()); $options['user_params[identifier]'] = $billaddress->getEmail(); diff --git a/app/code/community/Sign2pay/Payment/Model/Processor.php b/app/code/community/Sign2pay/Payment/Model/Processor.php index 9dada9c..6384eb1 100644 --- a/app/code/community/Sign2pay/Payment/Model/Processor.php +++ b/app/code/community/Sign2pay/Payment/Model/Processor.php @@ -84,7 +84,7 @@ public function processPaymentRequest(array $data){ $request_body = array( 'client_id' => $client_id, - 'amount' => $quote->getGrandTotal() * 100, + 'amount' => Mage::helper('sign2pay')->getPaymentAmount(), 'ref_id' => $ref_id, 'token' => $data['access_token']['token'] ); From cb4cd3fbf258a75d3a6521891a5f7a8b1939fc5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cezary=20=C5=BBegle=C5=84?= Date: Fri, 20 Nov 2015 15:08:26 +0100 Subject: [PATCH 3/9] Add exceptions based error handling --- .../Sign2pay/Payment/Model/Processor.php | 63 +++++++++++++++++-- .../Payment/controllers/PaymentController.php | 41 ++---------- 2 files changed, 64 insertions(+), 40 deletions(-) diff --git a/app/code/community/Sign2pay/Payment/Model/Processor.php b/app/code/community/Sign2pay/Payment/Model/Processor.php index 6384eb1..2833ec0 100644 --- a/app/code/community/Sign2pay/Payment/Model/Processor.php +++ b/app/code/community/Sign2pay/Payment/Model/Processor.php @@ -28,13 +28,69 @@ public function getRequestData($key = null) return isset($this->_request[$key]) ? $this->_request[$key] : null; } + /** + * General payment method responsible for the flow of the operations + * + * @param array returned by sign2pay api after the initial request + * + */ + public function performPayment(array $initial_response) + {Mage::log('test4'); + try{ + $this->validateInitialResponse($initial_response); + $token_response = json_decode($this->sendTokenExchangeRequest($initial_response), true); + if (empty($token_response['access_token']['token'])) { + if (!empty($token_response['error_description'])) { + Mage::getSingleton('checkout/session')->addError($token_response['error_description']); + } + throw new Exception('Token is missing'); + } + + $payemnt = json_decode($this->sendPaymentRequest($token_response), true); + if (empty($payment['purchase_id'])) { + if (!empty($payment['error_description'])) { + Mage::getSingleton('checkout/session')->addError($payment['error_description']); + } + throw new Exception('Purchase ID is missing'); + } + + $this->processPaymentCaptureResponse($payment); + } + catch (Exception $e) { + if (empty(Mage::getSingleton('checkout/session')->getMessages()->getItems())){ + Mage::getSingleton('checkout/session')->addError($e->getMessage()); + } + return Mage::app()->getResponse()->setRedirect('cancel', array('_secure'=>true)); + } + } + + + /** + * Validate the initial response + * add error to session and throw exception if something's not right + * + * @param array returned by sign2pay api after the initial request + * + */ + public function validateInitialResponse(array $initial_response) + { + if ($initial_response['state'] !== Mage::getSingleton('checkout/session')->getSign2PayUserHash() + || array_key_exists('error', $initial_response)) { + if (!empty($initial_response['error_description'])) { + Mage::getSingleton('checkout/session')->addError($initial_response['error_description']); + } + throw new Exception('Could not validate the response'); + } + } + /** * Exchange hashed credentials for token (second step of Authrature) * * * @return string (encoded json) */ - public function processTokenExchangeRequest(array $data){ + public function sendTokenExchangeRequest(array $data) + { //start variables preparation $client_id = Mage::helper('sign2pay')->getSign2payClientId(); $client_secret = Mage::helper('sign2pay')->getSign2payClientSecret(); @@ -64,7 +120,6 @@ public function processTokenExchangeRequest(array $data){ } catch (Zend_Http_Client_Exception $e) { Mage::logException($e); } - } /** @@ -73,7 +128,7 @@ public function processTokenExchangeRequest(array $data){ * * @return string (encoded json) */ - public function processPaymentRequest(array $data){ + public function sendPaymentRequest(array $data){ //start variables preparation $client_id = Mage::helper('sign2pay')->getSign2payClientId(); $client_secret = Mage::helper('sign2pay')->getSign2payClientSecret(); @@ -96,7 +151,7 @@ public function processPaymentRequest(array $data){ $client->setAuth($client_id,$client_secret); $client->setParameterPost($request_body); - try{ + try { $response = $client->request(); return $response->getBody(); } catch (Zend_Http_Client_Exception $e) { diff --git a/app/code/community/Sign2pay/Payment/controllers/PaymentController.php b/app/code/community/Sign2pay/Payment/controllers/PaymentController.php index 7c030d2..baa4801 100644 --- a/app/code/community/Sign2pay/Payment/controllers/PaymentController.php +++ b/app/code/community/Sign2pay/Payment/controllers/PaymentController.php @@ -40,48 +40,17 @@ public function redirectAction() */ public function responseAction() { - if (!$this->getRequest()->isGet()) { - return $this->_redirect('sign2pay/payment/cancel', array('_secure'=>true)); - } try { - $data = $this->getRequest()->getParams(); - if (!is_array($data) || $data['state'] !== Mage::getSingleton('checkout/session')->getSign2PayUserHash() - || array_key_exists('error', $data)){ - - if(is_array($data)){ - Mage::getSingleton('checkout/session')->addError($data['error_description']); - } - Mage::log($data); - return $this->_redirect('sign2pay/payment/cancel', array('_secure'=>true)); - } - - $result = json_decode(Mage::getModel('sign2pay/processor')->processTokenExchangeRequest($data), true); - if (!is_array($result) || array_key_exists('error', $result)){ - - if(is_array($result)){ - Mage::getSingleton('checkout/session')->addError($data['error_description']); - } - Mage::log($result); - return $this->_redirect('sign2pay/payment/cancel', array('_secure'=>true)); + if (!$this->getRequest()->isGet()) { + throw new Exception('Wrong request method'); } - - $payment = json_decode(Mage::getModel('sign2pay/processor')->processPaymentRequest($result), true); - if (!is_array($payment) || array_key_exists('error', $payment)){ - if(is_array($payment)){ - Mage::getSingleton('checkout/session')->addError($data['error_description']); - } - Mage::log($payment); - return $this->_redirect('sign2pay/payment/cancel', array('_secure'=>true)); - } - - Mage::getModel('sign2pay/processor')->processPaymentCaptureResponse($payment); - return $this->_redirect('sign2pay/payment/success', array('_secure'=>true)); + $data = $this->getRequest()->getParams(); + $payment = Mage::getModel('sign2pay/processor')->performPayment($data); } catch (Exception $e) { Mage::logException($e); $this->getResponse()->setHttpResponseCode(500); } - } /** @@ -119,7 +88,7 @@ public function cancelAction() Mage::getModel('sign2pay/processor')->cancel($order); } Mage::helper('sign2pay/checkout')->restoreQuote(); - Mage::getSingleton('checkout/session')->addError("You've cancelled the Sign2Pay screen."); + Mage::getSingleton('checkout/session')->addError("The payment has been cancelled."); } $this->_redirect('checkout/cart'); From c2f437fe8f54d9add04d51aec28bbb2a208deb39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cezary=20=C5=BBegle=C5=84?= Date: Mon, 23 Nov 2015 12:19:50 +0100 Subject: [PATCH 4/9] Add mobile number with country code to the initial request --- .../Sign2pay/Payment/Helper/CountryCodes.php | 268 ++++++++++++++++++ .../Sign2pay/Payment/Helper/Data.php | 9 + .../Sign2pay/Payment/Model/Processor.php | 36 ++- .../Payment/controllers/PaymentController.php | 2 + 4 files changed, 296 insertions(+), 19 deletions(-) create mode 100644 app/code/community/Sign2pay/Payment/Helper/CountryCodes.php diff --git a/app/code/community/Sign2pay/Payment/Helper/CountryCodes.php b/app/code/community/Sign2pay/Payment/Helper/CountryCodes.php new file mode 100644 index 0000000..b42778b --- /dev/null +++ b/app/code/community/Sign2pay/Payment/Helper/CountryCodes.php @@ -0,0 +1,268 @@ + '93', + 'AX' => '35818', + 'NL' => '31', + 'AN' => '599', + 'AL' => '355', + 'DZ' => '213', + 'AS' => '685', + 'AD' => '376', + 'AO' => '244', + 'AI' => '1264', + 'AQ' => '672', + 'AG' => '1268', + 'AE' => '971', + 'AR' => '54', + 'AM' => '374', + 'AW' => '297', + 'AU' => '61', + 'AZ' => '994', + 'BS' => '1242', + 'BH' => '973', + 'BD' => '880', + 'BB' => '1242', + 'BE' => '32', + 'BZ' => '501', + 'BJ' => '229', + 'BM' => '1441', + 'BT' => '975', + 'BO' => '591', + 'BA' => '387', + 'BW' => '267', + 'BV' => '47', + 'BR' => '55', + 'GB' => '44', + 'IO' => '246', + 'VG' => '1284', + 'BN' => '673', + 'BG' => '359', + 'BF' => '226', + 'BI' => '257', + 'KY' => '1345', + 'CL' => '56', + 'CK' => '682', + 'CR' => '506', + 'DJ' => '253', + 'DM' => '1767', + 'DO' => '1809', + 'EC' => '593', + 'EG' => '20', + 'SV' => '503', + 'ER' => '291', + 'ES' => '34', + 'ZA' => '27', + 'GS' => '500', + 'KR' => '82', + 'ET' => '251', + 'FK' => '500', + 'FJ' => '679', + 'PH' => '63', + 'FO' => '298', + 'GA' => '241', + 'GM' => '220', + 'GE' => '995', + 'GH' => '233', + 'GI' => '350', + 'GD' => '1473', + 'GL' => '299', + 'GP' => '590', + 'GU' => '1671', + 'GT' => '502', + 'GG' => '44', + 'GN' => '224', + 'GW' => '245', + 'GY' => '592', + 'HT' => '509', + 'HM' => '61', + 'HN' => '504', + 'HK' => '852', + 'SJ' => '47', + 'ID' => '62', + 'IN' => '91', + 'IQ' => '964', + 'IR' => '98', + 'IE' => '353', + 'IS' => '354', + 'IL' => '972', + 'IT' => '39', + 'TL' => '670', + 'AT' => '43', + 'JM' => '1876', + 'JP' => '81', + 'YE' => '967', + 'JE' => '44', + 'JO' => '962', + 'CX' => '61', + 'KH' => '855', + 'CM' => '237', + 'CA' => '1', + 'CV' => '238', + 'KZ' => '7', + 'KE' => '254', + 'CF' => '236', + 'CN' => '86', + 'KG' => '996', + 'KI' => '686', + 'CO' => '57', + 'KM' => '269', + 'CG' => '242', + 'CD' => '243', + 'CC' => '61', + 'GR' => '30', + 'HR' => '385', + 'CU' => '53', + 'KW' => '965', + 'CY' => '357', + 'LA' => '856', + 'LV' => '371', + 'LS' => '266', + 'LB' => '961', + 'LR' => '231', + 'LY' => '218', + 'LI' => '423', + 'LT' => '370', + 'LU' => '352', + 'EH' => '21228', + 'MO' => '853', + 'MG' => '261', + 'MK' => '389', + 'MW' => '265', + 'MV' => '960', + 'MY' => '60', + 'ML' => '223', + 'MT' => '356', + 'IM' => '44', + 'MA' => '212', + 'MH' => '692', + 'MQ' => '596', + 'MR' => '222', + 'MU' => '230', + 'YT' => '262', + 'MX' => '52', + 'FM' => '691', + 'MD' => '373', + 'MC' => '377', + 'MN' => '976', + 'ME' => '382', + 'MS' => '1664', + 'MZ' => '258', + 'MM' => '95', + 'NA' => '264', + 'NR' => '674', + 'NP' => '977', + 'NI' => '505', + 'NE' => '227', + 'NG' => '234', + 'NU' => '683', + 'NF' => '672', + 'NO' => '47', + 'CI' => '255', + 'OM' => '968', + 'PK' => '92', + 'PW' => '680', + 'PS' => '970', + 'PA' => '507', + 'PG' => '675', + 'PY' => '595', + 'PE' => '51', + 'PN' => '870', + 'KP' => '850', + 'MP' => '1670', + 'PT' => '351', + 'PR' => '1', + 'PL' => '48', + 'GQ' => '240', + 'QA' => '974', + 'FR' => '33', + 'GF' => '594', + 'PF' => '689', + 'TF' => '33', + 'RO' => '40', + 'RW' => '250', + 'SE' => '46', + 'RE' => '262', + 'SH' => '290', + 'KN' => '1869', + 'LC' => '1758', + 'VC' => '1784', + 'BL' => '590', + 'MF' => '1599', + 'PM' => '508', + 'DE' => '49', + 'SB' => '677', + 'ZM' => '260', + 'WS' => '685', + 'SM' => '378', + 'SA' => '966', + 'SN' => '221', + 'RS' => '381', + 'SC' => '248', + 'SL' => '232', + 'SG' => '65', + 'SK' => '421', + 'SI' => '386', + 'SO' => '252', + 'LK' => '94', + 'SD' => '249', + 'FI' => '358', + 'SR' => '594', + 'CH' => '41', + 'SZ' => '268', + 'SY' => '963', + 'ST' => '239', + 'TJ' => '992', + 'TW' => '886', + 'TZ' => '255', + 'DK' => '45', + 'TH' => '66', + 'TG' => '228', + 'TK' => '690', + 'TO' => '676', + 'TT' => '1868', + 'TN' => '216', + 'TR' => '90', + 'TM' => '993', + 'TC' => '1649', + 'TV' => '688', + 'TD' => '235', + 'CZ' => '420', + 'UG' => '256', + 'UA' => '380', + 'HU' => '36', + 'UY' => '598', + 'NC' => '687', + 'NZ' => '64', + 'UZ' => '998', + 'BY' => '375', + 'VU' => '678', + 'VA' => '39', + 'VE' => '58', + 'RU' => '7', + 'VN' => '84', + 'EE' => '372', + 'WF' => '681', + 'US' => '1', + 'VI' => '1340', + 'UM' => '1', + 'ZW' => '263' + ); + + if (array_key_exists($country, $country_codes)) { + return $country_codes[$country]; + } + return null; + } +} diff --git a/app/code/community/Sign2pay/Payment/Helper/Data.php b/app/code/community/Sign2pay/Payment/Helper/Data.php index fc44e79..4a9c416 100644 --- a/app/code/community/Sign2pay/Payment/Helper/Data.php +++ b/app/code/community/Sign2pay/Payment/Helper/Data.php @@ -154,6 +154,14 @@ public function getPaymentOptions() $billaddress = $quote->getBillingAddress(); + $telephone = trim($billaddress->getTelephone()); + if (substr($telephone, 0, 1) !== "+") { + $country_code = Mage::helper('sign2pay/CountryCodes')->getCountryCallingCode($billaddress->getCountry()); + if (substr($telephone, 0, strlen($country_code)) !== $country_code) { + $telephone = '+'.$country_code.$telephone; + } + } + $options = array(); $options['amount'] = $this->getPaymentAmount(); $options['locale'] = preg_replace('/_.*$/', '', Mage::app()->getLocale()->getLocaleCode()); @@ -165,6 +173,7 @@ public function getPaymentOptions() $options['user_params[city]'] = $billaddress->getCity(); $options['user_params[country]'] = $billaddress->getCountry(); $options['user_params[postal_code]'] = $billaddress->getPostcode(); + $options['user_params[mobile]'] = $telephone; return $options; } diff --git a/app/code/community/Sign2pay/Payment/Model/Processor.php b/app/code/community/Sign2pay/Payment/Model/Processor.php index 2833ec0..d2f5ccd 100644 --- a/app/code/community/Sign2pay/Payment/Model/Processor.php +++ b/app/code/community/Sign2pay/Payment/Model/Processor.php @@ -32,34 +32,32 @@ public function getRequestData($key = null) * General payment method responsible for the flow of the operations * * @param array returned by sign2pay api after the initial request - * + * */ public function performPayment(array $initial_response) - {Mage::log('test4'); + { try{ $this->validateInitialResponse($initial_response); $token_response = json_decode($this->sendTokenExchangeRequest($initial_response), true); if (empty($token_response['access_token']['token'])) { if (!empty($token_response['error_description'])) { - Mage::getSingleton('checkout/session')->addError($token_response['error_description']); + throw new Exception($token_response['error_description']); } throw new Exception('Token is missing'); } - $payemnt = json_decode($this->sendPaymentRequest($token_response), true); + $payment = json_decode($this->sendPaymentRequest($token_response), true); if (empty($payment['purchase_id'])) { if (!empty($payment['error_description'])) { - Mage::getSingleton('checkout/session')->addError($payment['error_description']); + throw new Exception($payment['error_description']); } throw new Exception('Purchase ID is missing'); } - $this->processPaymentCaptureResponse($payment); + return $this->processPaymentCaptureResponse($payment); } catch (Exception $e) { - if (empty(Mage::getSingleton('checkout/session')->getMessages()->getItems())){ - Mage::getSingleton('checkout/session')->addError($e->getMessage()); - } + Mage::getSingleton('checkout/session')->addError($e->getMessage()); return Mage::app()->getResponse()->setRedirect('cancel', array('_secure'=>true)); } } @@ -70,14 +68,15 @@ public function performPayment(array $initial_response) * add error to session and throw exception if something's not right * * @param array returned by sign2pay api after the initial request - * + * */ public function validateInitialResponse(array $initial_response) { if ($initial_response['state'] !== Mage::getSingleton('checkout/session')->getSign2PayUserHash() - || array_key_exists('error', $initial_response)) { + || array_key_exists('error', $initial_response)) { + if (!empty($initial_response['error_description'])) { - Mage::getSingleton('checkout/session')->addError($initial_response['error_description']); + throw new Exception($initial_response['error_description']); } throw new Exception('Could not validate the response'); } @@ -174,8 +173,8 @@ public function processPaymentCaptureResponse(array $request) $this->_request = $request; $orderId = Mage::getSingleton('checkout/session')->getLastRealOrderId(); - $purchase_id = $this->getRequestData('purchase_id'); - Mage::getSingleton('checkout/session')->setPurchaseId($purchase_id); + $purchaseId = $this->getRequestData('purchase_id'); + Mage::getSingleton('checkout/session')->setPurchaseId($purchaseId); // Load appropriate order $this->_order = Mage::getModel('sales/order')->loadByIncrementId($orderId); @@ -184,15 +183,15 @@ public function processPaymentCaptureResponse(array $request) } $result = array(); - if ($this->_verifyResponse($purchase_id)) { + if ($this->_verifyResponse($purchaseId)) { // Payment was successful, so update the order's state // and send order email and move to the success page $result['status'] = 'success'; - $result['redirect_to'] = Mage::getBaseUrl() . 'sign2pay/payment/success'; + $result['redirect_to'] = 'sign2pay/payment/success'; $result['params'] = array( 'purchase_id' => $purchaseId ); - Mage::getSingleton('checkout/session')->setPurchaseId($purchase_id); + Mage::getSingleton('checkout/session')->setPurchaseId($purchaseId); // Register the payment capture $this->_registerPaymentCapture(); } else { @@ -203,13 +202,12 @@ public function processPaymentCaptureResponse(array $request) if (!$result) { // There is a problem in the response we got $result['status'] = 'failure'; - $result['redirect_to'] = Mage::getBaseUrl() . 'sign2pay/payment/failure'; + $result['redirect_to'] = 'sign2pay/payment/failure'; $result['params'] = array( 'ref_id' => $orderId, 'message' => Mage::helper('sign2pay')->__('Sorry, but we could not process your payment at this time.'), ); } - return $result; } diff --git a/app/code/community/Sign2pay/Payment/controllers/PaymentController.php b/app/code/community/Sign2pay/Payment/controllers/PaymentController.php index baa4801..2735d82 100644 --- a/app/code/community/Sign2pay/Payment/controllers/PaymentController.php +++ b/app/code/community/Sign2pay/Payment/controllers/PaymentController.php @@ -47,6 +47,8 @@ public function responseAction() $data = $this->getRequest()->getParams(); $payment = Mage::getModel('sign2pay/processor')->performPayment($data); + return $this->_redirect($payment['redirect_to'], array('_secure'=>true)); + } catch (Exception $e) { Mage::logException($e); $this->getResponse()->setHttpResponseCode(500); From 48cd872ddcc859696125caef07ad7ee1bde0c567 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Tatarczuk?= Date: Tue, 24 Nov 2015 11:06:36 +0100 Subject: [PATCH 5/9] Change country codes helper class name, improve phone processing in payment options data helper --- .../Sign2pay/Payment/Helper/CountryCodes.php | 5 +++-- app/code/community/Sign2pay/Payment/Helper/Data.php | 11 ++++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/app/code/community/Sign2pay/Payment/Helper/CountryCodes.php b/app/code/community/Sign2pay/Payment/Helper/CountryCodes.php index b42778b..a029a83 100644 --- a/app/code/community/Sign2pay/Payment/Helper/CountryCodes.php +++ b/app/code/community/Sign2pay/Payment/Helper/CountryCodes.php @@ -1,12 +1,12 @@ '263' ); + $country = strtoupper($country); if (array_key_exists($country, $country_codes)) { return $country_codes[$country]; } diff --git a/app/code/community/Sign2pay/Payment/Helper/Data.php b/app/code/community/Sign2pay/Payment/Helper/Data.php index 4a9c416..b03167b 100644 --- a/app/code/community/Sign2pay/Payment/Helper/Data.php +++ b/app/code/community/Sign2pay/Payment/Helper/Data.php @@ -155,11 +155,12 @@ public function getPaymentOptions() $billaddress = $quote->getBillingAddress(); $telephone = trim($billaddress->getTelephone()); - if (substr($telephone, 0, 1) !== "+") { - $country_code = Mage::helper('sign2pay/CountryCodes')->getCountryCallingCode($billaddress->getCountry()); - if (substr($telephone, 0, strlen($country_code)) !== $country_code) { - $telephone = '+'.$country_code.$telephone; - } + $telephone = preg_replace('/[^0-9+]/', '', $telephone); + $telephone = preg_replace('/^0*/', '+'); + + if (substr($telephone, 0, 1) !== '+') { + $country_code = Mage::helper('sign2pay/countryCodes')->getCountryCallingCode($billaddress->getCountry()); + $telephone = '+' . $country_code . $telephone; } $options = array(); From 2226842acfdabcc0d54242d64ae3f11f3e065354 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Tatarczuk?= Date: Tue, 24 Nov 2015 11:11:35 +0100 Subject: [PATCH 6/9] Remove unecessary result from payment capture processor method --- .../Sign2pay/Payment/Model/Processor.php | 17 ++--------------- .../Payment/controllers/PaymentController.php | 4 ++-- 2 files changed, 4 insertions(+), 17 deletions(-) diff --git a/app/code/community/Sign2pay/Payment/Model/Processor.php b/app/code/community/Sign2pay/Payment/Model/Processor.php index d2f5ccd..aa0ada0 100644 --- a/app/code/community/Sign2pay/Payment/Model/Processor.php +++ b/app/code/community/Sign2pay/Payment/Model/Processor.php @@ -156,11 +156,10 @@ public function sendPaymentRequest(array $data){ } catch (Zend_Http_Client_Exception $e) { Mage::logException($e); } - } - /* + /** * Get gateway data, validate and run corresponding handler * * @param array $request @@ -186,11 +185,6 @@ public function processPaymentCaptureResponse(array $request) if ($this->_verifyResponse($purchaseId)) { // Payment was successful, so update the order's state // and send order email and move to the success page - $result['status'] = 'success'; - $result['redirect_to'] = 'sign2pay/payment/success'; - $result['params'] = array( - 'purchase_id' => $purchaseId - ); Mage::getSingleton('checkout/session')->setPurchaseId($purchaseId); // Register the payment capture $this->_registerPaymentCapture(); @@ -200,15 +194,8 @@ public function processPaymentCaptureResponse(array $request) } if (!$result) { - // There is a problem in the response we got - $result['status'] = 'failure'; - $result['redirect_to'] = 'sign2pay/payment/failure'; - $result['params'] = array( - 'ref_id' => $orderId, - 'message' => Mage::helper('sign2pay')->__('Sorry, but we could not process your payment at this time.'), - ); + throw new Exception('Sorry, but we could not process your payment at this time.'); } - return $result; } /** diff --git a/app/code/community/Sign2pay/Payment/controllers/PaymentController.php b/app/code/community/Sign2pay/Payment/controllers/PaymentController.php index 2735d82..ecbb58c 100644 --- a/app/code/community/Sign2pay/Payment/controllers/PaymentController.php +++ b/app/code/community/Sign2pay/Payment/controllers/PaymentController.php @@ -45,9 +45,9 @@ public function responseAction() throw new Exception('Wrong request method'); } $data = $this->getRequest()->getParams(); - $payment = Mage::getModel('sign2pay/processor')->performPayment($data); + $payment = Mage::getModel('sign2pay/processor')->performPayment($data); - return $this->_redirect($payment['redirect_to'], array('_secure'=>true)); + return $this->_redirect('sign2pay/payment/success', array('_secure'=>true)); } catch (Exception $e) { Mage::logException($e); From 3f5237e06b746a32cec0588065e563389a3c02be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cezary=20=C5=BBegle=C5=84?= Date: Tue, 24 Nov 2015 12:29:56 +0100 Subject: [PATCH 7/9] Fix getSign2PayInitialRequest and processPaymentCaptureResponse methods --- app/code/community/Sign2pay/Payment/Helper/Data.php | 1 + app/code/community/Sign2pay/Payment/Model/Processor.php | 4 ---- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/app/code/community/Sign2pay/Payment/Helper/Data.php b/app/code/community/Sign2pay/Payment/Helper/Data.php index b03167b..6346f41 100644 --- a/app/code/community/Sign2pay/Payment/Helper/Data.php +++ b/app/code/community/Sign2pay/Payment/Helper/Data.php @@ -199,6 +199,7 @@ public function getPaymentLogoUrl() public function getSign2PayInitialRequest() { $options = $this->getPaymentOptions(); + $quote = $this->getQuote(); $options['client_id'] = $this->getSign2payClientId(); $options['redirect_uri'] = $this->getRedirectUri(); diff --git a/app/code/community/Sign2pay/Payment/Model/Processor.php b/app/code/community/Sign2pay/Payment/Model/Processor.php index aa0ada0..d8e540c 100644 --- a/app/code/community/Sign2pay/Payment/Model/Processor.php +++ b/app/code/community/Sign2pay/Payment/Model/Processor.php @@ -180,7 +180,6 @@ public function processPaymentCaptureResponse(array $request) if (!$this->_order->getId()) { throw new Exception('Requested order with id ' . $orderId . ' does not exists.'); } - $result = array(); if ($this->_verifyResponse($purchaseId)) { // Payment was successful, so update the order's state @@ -191,9 +190,6 @@ public function processPaymentCaptureResponse(array $request) } else { // Register the payment failure $this->_registerPaymentFailure(); - } - - if (!$result) { throw new Exception('Sorry, but we could not process your payment at this time.'); } } From ccd181217d2a78e37254de2b6c7576ed1d22ccd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cezary=20=C5=BBegle=C5=84?= Date: Tue, 24 Nov 2015 13:14:13 +0100 Subject: [PATCH 8/9] Fix mobile country code generating regex --- app/code/community/Sign2pay/Payment/Helper/Data.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/community/Sign2pay/Payment/Helper/Data.php b/app/code/community/Sign2pay/Payment/Helper/Data.php index 6346f41..df782f6 100644 --- a/app/code/community/Sign2pay/Payment/Helper/Data.php +++ b/app/code/community/Sign2pay/Payment/Helper/Data.php @@ -156,7 +156,7 @@ public function getPaymentOptions() $telephone = trim($billaddress->getTelephone()); $telephone = preg_replace('/[^0-9+]/', '', $telephone); - $telephone = preg_replace('/^0*/', '+'); + $telephone = preg_replace('/^0+/', '+', $telephone); if (substr($telephone, 0, 1) !== '+') { $country_code = Mage::helper('sign2pay/countryCodes')->getCountryCallingCode($billaddress->getCountry()); From 321180792a165c221451ad1a18a23210c782e909 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Tatarczuk?= Date: Tue, 24 Nov 2015 16:37:23 +0100 Subject: [PATCH 9/9] Increase version to 0.7.4 --- app/code/community/Sign2pay/Payment/etc/config.xml | 2 +- package.xml | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/app/code/community/Sign2pay/Payment/etc/config.xml b/app/code/community/Sign2pay/Payment/etc/config.xml index ebbd637..b90ca3d 100644 --- a/app/code/community/Sign2pay/Payment/etc/config.xml +++ b/app/code/community/Sign2pay/Payment/etc/config.xml @@ -2,7 +2,7 @@ - 0.7.3 + 0.7.4 diff --git a/package.xml b/package.xml index e5cb57b..5bbff4b 100644 --- a/package.xml +++ b/package.xml @@ -1,7 +1,7 @@ Sign2Pay_Mobile_Payments - 0.7.3 + 0.7.4 beta Sign2Pay Commercial License community @@ -13,11 +13,13 @@ Analysing hundreds of data points in a single signature, Sign2Pay provides the s Compatible with: 1.7, 1.8, 1.9 Changes: -- Fix payment amount in payment request +- Add dynamic bank logo to payment method label +- Add mobile phone number to authorize request + Sign2PaySign2Payteam@sign2pay.com - 2015-11-19 - - + 2015-11-24 + + 5.3.06.0.0