Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

class Sign2pay_Payment_Block_Adminhtml_Notifications extends Mage_Adminhtml_Block_Template
{
public function getMessage()
{
try {
if (Mage::app()->getRequest()->getParam('section') != 'payment') return '';

$payments = Mage::getSingleton('payment/config')->getActiveMethods();

if (!isset($payments['sign2pay'])) return '';

foreach (Mage::app()->getStore()->getAvailableCurrencyCodes(true) as $code) {
if ($code != 'EUR') {
return Mage::helper('sign2pay')->__('Sign2Pay only support payments in Euro. It won\'t be available for orders in other currencies.');
}
}

return '';
} catch (Exception $e) {
return '';
}
}
}
3 changes: 3 additions & 0 deletions app/code/community/Sign2pay/Payment/Block/Info/Sign2pay.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ protected function _prepareSpecificInformation($transport = null)
}
$info = $this->getInfo();
$transport = new Varien_Object();
if (!empty($info['last_trans_id'])) {
$transport->setData('Transaction URL', 'https://merchant.sign2pay.com/en/purchases/'.$info['last_trans_id']);
}
$transport = parent::_prepareSpecificInformation($transport);
return $transport;
}
Expand Down
105 changes: 105 additions & 0 deletions app/code/community/Sign2pay/Payment/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,26 @@ public function getSign2payApiKey()
return Mage::getStoreConfig('payment/sign2pay/api_token',Mage::app()->getStore());
}

/**
* Retrive Sign2Pay Client ID.
*
* @return string
*/
public function getSign2payClientId()
{
return Mage::getStoreConfig('payment/sign2pay/client_id',Mage::app()->getStore());
}

/**
* Retrive Sign2Pay Client Secret token.
*
* @return string
*/
public function getSign2payClientSecret()
{
return Mage::getStoreConfig('payment/sign2pay/client_secret',Mage::app()->getStore());
}

/**
* Retrive sign2pay options.
*
Expand Down Expand Up @@ -60,11 +80,28 @@ public function getSign2PaySignature($apiKey, $token, $timestamp)
);
}

/**
* Build and return url to module's response action.
*
* @return array
*/
public function getRedirectUri()
{
$redirect_uri = Mage::getUrl('sign2pay/payment/response', array('_secure' => true));
$redirect_uri = preg_replace('/index.php\/sign2pay/', 'sign2pay', $redirect_uri);
return rtrim($redirect_uri,"/");
}

/**
* Attach payment scripts.
*/
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');

Expand All @@ -79,6 +116,28 @@ public function attachPaymentScripts(array $additional = array())
);
}

/**
* Attach to session and return user sign2pay checkout session hash.
*
* @return string
*/
private function userStateHash(){
$hash = Mage::helper('core')->getRandomString(16);
Mage::getSingleton('checkout/session')->setSign2PayUserHash($hash);
return $hash;
}

/**
* Attach to session and return sign2pay checkout session hash.
*
* @return string
*/
private function sign2PayCheckoutHash($id){
$hash = Mage::helper('core')->getRandomString(16);
Mage::getSingleton('checkout/session')->setSign2PayCheckoutHash($hash);
return $hash;
}

/**
* Set status on order
*
Expand All @@ -98,4 +157,50 @@ public function setStatusOnOrder($order, $status_code)
$order->setState($status['state'], $status['status']);
}

/**
* Prepare and return initial Sign2Pay request
* @todo device unical id
*
* @return string
*/
public function getSign2PayInitialRequest(){
$quote = Mage::getSingleton('checkout/session')->getQuote();

Mage::log($quote->getData());
$amount = $quote['grand_total']*100;
$amount = preg_replace('/[^0-9]/', '', $amount);
$ref_id = $this->sign2PayCheckoutHash($quote['reserved_order_id']);

$billing = $quote->getBillingAddress();

$baseUrl = 'https://app.sign2pay.com/oauth/authorize';
$client_id = $this->getSign2payClientId();
$redirect_uri = $this->getRedirectUri();

$scope = 'payment';
$state = $this->userStateHash();
$response_type = 'code';
$device_uid = 'test';

$query = http_build_query(array(
'client_id' => $client_id,
'redirect_uri' => $redirect_uri,
'scope' => $scope,
'amount' => $amount,
'state' => $state,
'ref_id' => $ref_id,
'response_type' => $response_type,
'device_uid' => $device_uid,
'user_params[identifier]' => $billing['email'],
'user_params[first_name]' => $billing['firstname'],
'user_params[last_name]' => $billing['lastname'],
'user_params[address]' => $billing['street'],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@magently can you include the house number as well? It will give more accurate risk insight.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Frankly mobile number and country as well if accessible....

'user_params[city]' => $billing['city'],
'user_params[postal_code]' => $billing['postcode']
)
);
return $baseUrl.'?'.$query;

}

}
89 changes: 87 additions & 2 deletions app/code/community/Sign2pay/Payment/Model/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,86 @@ public function getRequestData($key = null)
}

/**
* Exchange hashed credentials for token (second step of Authrature)
*
*
* @return string (encoded json)
*/
public function processTokenExchangeRequest(array $data){
//start variables preparation
$client_id = Mage::helper('sign2pay')->getSign2payClientId();
$client_secret = Mage::helper('sign2pay')->getSign2payClientSecret();
$state = Mage::getSingleton('checkout/session')->getSign2PayUserHash();
$code = $data['code'];
$redirect_uri = Mage::helper('sign2pay')->getRedirectUri();

$request_body = array(
'client_id' => $client_id,
'state' => $state,
'code' => $code,
'redirect_uri' => $redirect_uri
);
//end variables preparation


/*==========================================start request preparation==========================*/
$client = new Varien_Http_Client('https://app.sign2pay.com/oauth/token');
$client->setMethod(Varien_Http_Client::POST);

$client->setAuth($client_id,$client_secret);
$client->setParameterPost($request_body);

try{
$response = $client->request();
return $response->getBody();
} catch (Zend_Http_Client_Exception $e) {
Mage::logException($e);
}

}

/**
* Exchange token for payment id (third step of Authrature)
*
*
* @return string (encoded json)
*/
public function processPaymentRequest(array $data){
//start variables preparation
$client_id = Mage::helper('sign2pay')->getSign2payClientId();
$client_secret = Mage::helper('sign2pay')->getSign2payClientSecret();

$quote = Mage::getSingleton('checkout/session')->getQuote();
$amount = preg_replace('/[^0-9]/', '', $quote['grand_total']);

$ref_id = Mage::getSingleton('checkout/session')->getSign2PayCheckoutHash();

$request_body = array(
'client_id' => $client_id,
'amount' => $amount,
'ref_id' => $ref_id,
'token' => $data['access_token']['token']
);
//end variables preparation


/*==========================================start request preparation==========================*/
$client = new Varien_Http_Client('https://app.sign2pay.com/api/v2/payment/authorize/capture');
$client->setMethod(Varien_Http_Client::POST);

$client->setAuth($client_id,$client_secret);
$client->setParameterPost($request_body);
try{
$response = $client->request();
return $response->getBody();
} catch (Zend_Http_Client_Exception $e) {
Mage::logException($e);
}

}


/*
* Get gateway data, validate and run corresponding handler
*
* @param array $request
Expand Down Expand Up @@ -129,10 +209,15 @@ protected function _verifyResponse($apiKey, $token, $timestamp, $signature)
/**
* Process completed payment (either full or partial)
*/
protected function _registerPaymentCapture()
public function _registerPaymentCapture()
{

$session = $session = Mage::getSingleton('checkout/session');
$purchaseId = $session->getPurchaseId();
$this->_order = Mage::getModel('sales/order')->loadByIncrementId($session->getLastRealOrderId());
$payment = $this->_order->getPayment();
$payment->setTransactionId($this->getRequestData('purchase_id'))

$payment->setTransactionId($purchaseId)
->setCurrencyCode('EUR')
->setIsTransactionClosed(0)
->registerCaptureNotification(
Expand Down
9 changes: 8 additions & 1 deletion app/code/community/Sign2pay/Payment/Model/Sign2pay.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ class Sign2pay_Payment_Model_Sign2pay extends Mage_Payment_Model_Method_Abstract

public function getOrderPlaceRedirectUrl()
{
return Mage::getUrl('sign2pay/payment/redirect', array('_secure' => true));
$session = Mage::getSingleton('checkout/session');
$order = Mage::getModel('sales/order')->loadByIncrementId($session->getLastRealOrderId());

Mage::getSingleton('checkout/session')->getQuote()->setIsActive(true)->save();
Mage::helper('sign2pay')->setStatusOnOrder(
$order, Mage::getStoreConfig('payment/sign2pay/order_status', Mage::app()->getStore()));

return Mage::helper('sign2pay')->getSign2PayInitialRequest();
}

/**
Expand Down
Loading