Skip to content

Commit

Permalink
Merge pull request #9 from magently/master
Browse files Browse the repository at this point in the history
Complete payment functionality
  • Loading branch information
tilsammans committed Nov 17, 2015
2 parents fa8c3c5 + 1d6fb49 commit a7e9329
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ protected function _prepareSpecificInformation($transport = null)
}
$info = $this->getInfo();
$transport = new Varien_Object();
if (!empty($info['last_trans_id'])) {
if (!empty($info['last_trans_id']) && Mage::app()->getStore()->isAdmin()) {
$transport->setData('Transaction URL', 'https://merchant.sign2pay.com/en/purchases/'.$info['last_trans_id']);
}
$transport = parent::_prepareSpecificInformation($transport);
Expand Down
73 changes: 35 additions & 38 deletions app/code/community/Sign2pay/Payment/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,44 +163,41 @@ public function setStatusOnOrder($order, $status_code)
*
* @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'],
'user_params[city]' => $billing['city'],
'user_params[postal_code]' => $billing['postcode']
)
);
return $baseUrl.'?'.$query;

public function getSign2PayInitialRequest()
{
$session = Mage::getSingleton('checkout/session');
$quote = null;

if ($orderId = $session->getLastRealOrderId()) {
$order = Mage::getModel('sales/order')->loadByIncrementId($order);
$quote = $order->getQuote();
}

if (!$quote) {
$quote = Mage::getModel('sales/quote')->load($session->getSign2payQuoteId() ? $session->getSign2payQuoteId() : $session->getQuoteId());
}

$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['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();
$options['user_params[last_name]'] = $billaddress->getLastname();
$options['user_params[address]'] = implode(' ', (array) $billaddress->getStreet());
$options['user_params[city]'] = $billaddress->getCity();
$options['user_params[country]'] = $billaddress->getCountry();
$options['user_params[postal_code]'] = $billaddress->getPostcode();

return 'https://app.sign2pay.com/oauth/authorize?' . http_build_query($options);
}

}
77 changes: 38 additions & 39 deletions app/code/community/Sign2pay/Payment/Model/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function processPaymentRequest(array $data){
$client_secret = Mage::helper('sign2pay')->getSign2payClientSecret();

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

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

Expand Down Expand Up @@ -116,52 +116,32 @@ public function processPaymentRequest(array $data){
* @todo Add gateway authentication
* @todo Add order state validation
*/
public function processRequest(array $request)
public function processPaymentCaptureResponse(array $request)
{
$this->_request = $request;

$apiKey = Mage::helper('sign2pay')->getSign2payApiKey();

$orderId = $this->getRequestData('ref_id');
$merchantId = $this->getRequestData('merchant_id');
$purchaseId = $this->getRequestData('purchase_id');

$amount = $this->getRequestData('amount');
$status = $this->getRequestData('status');
$token = $this->getRequestData('token');
$timestamp = $this->getRequestData('timestamp');
$signature = $this->getRequestData('signature');

$orderId = Mage::getSingleton('checkout/session')->getLastRealOrderId();
$purchase_id = $this->getRequestData('purchase_id');
Mage::getSingleton('checkout/session')->setPurchaseId($purchase_id);

// Load appropriate order
$this->_order = Mage::getModel('sales/order')->loadByIncrementId($orderId);

if (!$this->_order->getId()) {
throw new Exception('Requested order with id ' . $orderId . ' does not exists.');
}

$result = array();

if ($this->_verifyResponse($apiKey, $token, $timestamp, $signature)) {
if ($status == 'mandate_valid') {
// 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['params'] = array(
'total' => $amount,
'id' => $orderId,
'purchase_id' => $purchaseId,
'signature' => true,
'status' => $status,
'authorization' => $timestamp,
);

// Register the payment capture
$this->_registerPaymentCapture();
} else {
// Register the payment failure
$this->_registerPaymentDenial();
}
if ($this->_verifyResponse($purchase_id)) {
// 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['params'] = array(
'purchase_id' => $purchaseId
);
Mage::getSingleton('checkout/session')->setPurchaseId($purchase_id);
// Register the payment capture
$this->_registerPaymentCapture();
} else {
// Register the payment failure
$this->_registerPaymentFailure();
Expand Down Expand Up @@ -201,9 +181,28 @@ public function cancel(Mage_Sales_Model_Order $order)
* @param string $signature
* @return boolean
*/
protected function _verifyResponse($apiKey, $token, $timestamp, $signature)
public function _verifyResponse($purchase_id)
{
return $signature === Mage::helper('sign2pay')->getSign2paySignature($apiKey, $token, $timestamp);
$client_id = Mage::helper('sign2pay')->getSign2payClientId();
$client_secret = Mage::helper('sign2pay')->getSign2payClientSecret();

$client = new Varien_Http_Client('https://app.sign2pay.com/api/v2/payment/status/'.$purchase_id);
$client->setMethod(Varien_Http_Client::GET);
$client->setAuth($client_id,$client_secret);

try{
$response = $client->request();
$body = json_decode($response->getBody());
if(array_key_exists('status', $body) || $body['status'] == 'processing'){
return true;
}
else{
return false;
}
} catch (Zend_Http_Client_Exception $e) {
Mage::logException($e);
return false;
}
}

/**
Expand Down
9 changes: 1 addition & 8 deletions app/code/community/Sign2pay/Payment/Model/Sign2pay.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,7 @@ class Sign2pay_Payment_Model_Sign2pay extends Mage_Payment_Model_Method_Abstract

public function getOrderPlaceRedirectUrl()
{
$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();
return Mage::getUrl('sign2pay/payment/redirect', array('_secure' => true));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

class Sign2pay_Payment_PaymentController extends Mage_Core_Controller_Front_Action
{
/*
/**
* Redirect action after placing an order with Sign2Pay payment
*
*/
public function redirectAction()
{
$session = Mage::getSingleton('checkout/session');
Expand All @@ -30,13 +30,13 @@ public function redirectAction()
Mage::helper('sign2pay')->setStatusOnOrder($order, Mage::getStoreConfig('payment/sign2pay/order_status', Mage::app()->getStore()));
$order->save();

$this->loadLayout();
$this->renderLayout();
}*/
$this->_redirectUrl(Mage::helper('sign2pay')->getSign2PayInitialRequest());
}


/**
* Response action is triggered when your gateway sends
* back a response after initial request
* back a response after the initial request
*/
public function responseAction()
{
Expand All @@ -47,7 +47,7 @@ public function responseAction()
$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']);
}
Expand All @@ -57,7 +57,7 @@ public function responseAction()

$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']);
}
Expand All @@ -73,8 +73,8 @@ public function responseAction()
Mage::log($payment);
return $this->_redirect('sign2pay/payment/cancel', array('_secure'=>true));
}
Mage::log($payment);
Mage::getSingleton('checkout/session')->setPurchaseId($payment['purchase_id']);

Mage::getModel('sign2pay/processor')->processPaymentCaptureResponse($payment);
return $this->_redirect('sign2pay/payment/success', array('_secure'=>true));

} catch (Exception $e) {
Expand All @@ -89,29 +89,17 @@ public function responseAction()
*/
public function successAction()
{

try{
Mage::getModel('sign2pay/processor')->_registerPaymentCapture();
}catch (Exception $e){
Mage::log($e);
}

Mage::getSingleton('checkout/session')->getQuote()->setIsActive(false)->save();

$session = Mage::getSingleton('checkout/session');

$order = Mage::getModel('sales/order')->loadByIncrementId($session->getLastRealOrderId());

$this->_redirect('checkout/onepage/success', array('_secure'=>true));
}

/*
* Failure action after gateway response
*
*/
public function failureAction()
{
//$session = Mage::getSingleton('checkout/session');
//$session->setQuoteId($session->getSign2payQuoteId(true));
$session = Mage::getSingleton('checkout/session');
$session->setQuoteId($session->getSign2payQuoteId(true));
Mage::getSingleton('checkout/session')->getQuote()->setIsActive(false)->save();
$this->_redirect('checkout/onepage/failure', array('_secure'=>true));
}
Expand All @@ -131,51 +119,10 @@ 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.");

}
$this->_redirect('checkout/cart');
}

/*
* Fetch payment related options required to process Sign2Pay payment
*
* @todo Check if this should process also order
*
public function fetchPaymentOptionsAction()
{
$session = Mage::getSingleton('checkout/session');
$quote = null;
if ($orderId = $session->getLastRealOrderId()) {
$order = Mage::getModel('sales/order')->loadByIncrementId($order);
$quote = $order->getQuote();
}
if (!$quote) {
$quote = Mage::getModel('sales/quote')->load($session->getSign2payQuoteId() ? $session->getSign2payQuoteId() : $session->getQuoteId());
}
$billaddress = $quote->getBillingAddress();
$options = array();
$options['checkout_type'] = "multi";
$options['first_name'] = $billaddress->getFirstname();
$options['last_name'] = $billaddress->getLastname();
$options['email'] = $billaddress->getEmail();
$options['address'] = implode(' ', (array) $billaddress->getStreet());
$options['city'] = $billaddress->getCity();
$options['country'] = $billaddress->getCountry();
$options['postal_code'] = $billaddress->getPostcode();
$options['amount'] = $quote->getGrandTotal() * 100;
if ($orderId = $quote->getReservedOrderId()) {
$options['ref_id'] = $orderId;
} else {
$options['ref_id'] = ($options['email'] && $options['last_name']) ? ($options['email'] . ',' + $options['last_name'] . ',' . time()) : 'leeg';
}
$jsonData = json_encode($options);
$this->getResponse()->setHeader('Content-type', 'application/json');
$this->getResponse()->setBody($jsonData);
}*/
}
20 changes: 0 additions & 20 deletions app/code/community/Sign2pay/Payment/etc/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,6 @@
<show_in_store>1</show_in_store>
</merchant_id>

<!--<application_token translate="label" backend_model="adminhtml/system_config_backend_encrypted">
<label>Application Token</label>
<comment>Located on your Sign2Pay Application settings page</comment>
<frontend_type>text</frontend_type>
<sort_order>7</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</application_token>-->

<client_id translate="label" backend_model="adminhtml/system_config_backend_encrypted">
<label>Client ID</label>
<comment>Located on your Sign2Pay Application settings page</comment>
Expand All @@ -207,16 +197,6 @@
<show_in_store>1</show_in_store>
</client_secret>

<api_token translate="label" backend_model="adminhtml/system_config_backend_encrypted">
<label>API Token</label>
<comment>Located on your Sign2Pay Application settings page</comment>
<frontend_type>text</frontend_type>
<sort_order>9</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</api_token>

<sort_order translate="label">
<label>Sort Order</label>
<frontend_type>text</frontend_type>
Expand Down

0 comments on commit a7e9329

Please sign in to comment.