Skip to content

Commit

Permalink
Merge pull request #27 from razorpay/auto_capture
Browse files Browse the repository at this point in the history
Auto capture
  • Loading branch information
73SL4 authored Nov 24, 2016
2 parents 4f8407c + b867854 commit c734a8c
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 150 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This extension utilizes Razorpay API and provides seamless integration with Mage

Install the extension through magento connect in your account.

Once installed, it can be configured under Configuration->Payment Gateways. If you run into any problems, please make sure you are using the 2.0 magento connect extension and you have Stable set as your preferred state in the magento connect settings.
Once installed, it can be configured under Configuration->Payment Methods. If you run into any problems, please make sure you are using the 2.0 magento connect extension and you have Stable set as your preferred state in the magento connect settings.

If you do not see Razorpay in your gateway list, please clear your Magento Cache from your admin
panel (System -> Cache Management).
Expand Down
35 changes: 7 additions & 28 deletions app/code/community/Razorpay/Payments/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,18 @@ public function createOrder($order)
$url = $this->getRelativeUrl('order');

$data = array(
'receipt' => $orderId,
'amount' => $amount,
'currency' => $currency
'receipt' => $orderId,
'amount' => $amount,
'currency' => $currency,
'payment_capture' => 1
);

$response = $this->sendRequest($url, 'POST', $data);

Mage::getSingleton('core/session')->setRazorpayOrderID($response['id']);

$responseArray = array(
// order id has to be stored and fetched later from the db or session
'razorpay_order_id' => $response['id']
);

Expand All @@ -74,31 +78,6 @@ public function createOrder($order)
return $responseArray;
}

public function capturePayment($paymentId, $amount)
{
if ($this->isRazorpayEnabled())
{
$url = $this->getRelativeUrl('capture', array(
':id' => $paymentId
));

$data = array(
'amount' => $amount
);

$response = $this->sendRequest($url, 'POST', $data);

if ($response['status'] === 'captured')
{
return true;
}

Mage::throwException('CAPTURE_ERROR: Unable to capture payment ' . $paymentId);
}

Mage::throwException('MAGENTO_ERROR: Payment Method not available');
}

public function sendRequest($url, $method = 'POST', $content = array())
{
$ch = $this->getCurlHandle($url, $method, $content);
Expand Down
50 changes: 16 additions & 34 deletions app/code/community/Razorpay/Payments/Model/Paymentmethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Razorpay_Payments_Model_Paymentmethod extends Mage_Payment_Model_Method_Ab
protected $_isInitializeNeeded = false;
protected $_isGateway = true;
protected $_canAuthorize = true;
protected $_canCapture = true;
protected $_canCapture = false;
protected $_canCapturePartial = false;
protected $_canRefund = false;
protected $_canVoid = false;
Expand Down Expand Up @@ -110,54 +110,36 @@ public function captureOrder(Varien_Object $order, $amount)
return [$result, $error];
}

public function capturePayment($response)
public function validateSignature($response)
{
$order = Mage::getModel('sales/order');
$payment = Mage::getModel('sales/order_payment');
$order->loadByIncrementId($response['magento_order_id']);

$payment->setOrder($order);
$requestFields = Mage::app()->getRequest()->getPost();

$success = false;
$paymentId = $requestFields['razorpay_payment_id'];

$paymentId = null;
$razorpay_payment_id = $response['razorpay_payment_id'];
$razorpay_order_id = Mage::getSingleton('core/session')->getRazorpayOrderID();

if (empty($response['razorpay_payment_id']) === false)
{
$paymentId = $response['razorpay_payment_id'];
$key_secret = $this->getConfigData('key_secret');

$signature = hash_hmac('sha256', $razorpay_order_id . "|" . $razorpay_payment_id, $key_secret);

try
{
$amount = $order->getBaseGrandTotal();

list($success, $error) = $this->captureOrder($payment, $amount);
}
catch (\Exception $e)
{
$success = false;
}
}
$session = Mage::getSingleton('checkout/session');
$order = Mage::getModel('sales/order');
$order->loadByIncrementId($session->getLastRealOrderId());

if ($success === true)
if (hash_equals($signature , $response['razorpay_signature']))
{
$success = true;
$order->sendNewOrderEmail();
$order->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true);
$order->addStatusHistoryComment('Payment Successful. Razorpay Payment Id:'.$paymentId);
$order->save();
}
else
{
if (isset($error))
{
$desc = $error . (isset($paymentId) ? '. Payment ID: ' . $paymentId : '. Most probably user closed the popup.');
}
else
{
$desc = 'Payment failed. Most probably user closed the popup.';
}

$success = false;
$order->setState(Mage_Sales_Model_Order::STATE_CANCELED, true);
$order->addStatusHistoryComment($desc);
$order->addStatusHistoryComment('Payment failed. Most probably user closed the popup.');
$order->save();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ public function successAction()

$model = Mage::getModel('razorpay_payments/paymentmethod');

$success = $model->capturePayment($response);
$success = $model->validateSignature($response);

// Unsetting the session variable upon completion of signature verification
Mage::getSingleton('core/session')->unsRazorpayOrderID();

if ($success === true)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

var onSuccess = function(data) {
$('razorpay_payment_id').value = data.razorpay_payment_id;
$('razorpay_order_id').value = data.razorpay_order_id;
$('razorpay_signature').value = data.razorpay_signature;

$('razorpay').submit();
};
Expand All @@ -18,7 +20,9 @@

<form name="razorpay" id="razorpay" method="post" action="<?php echo $this->getSuccessUrl(); ?>">
<input id="razorpay_payment_id" name="razorpay_payment_id" type="hidden" value=""/>
<input id="magento_order_id" name="magento_order_id" type="hidden" value="<?php echo $this->getMagentoOrderId(); ?>"/>
<input id="razorpay_order_id" name="razorpay_order_id" type="hidden" value=""/>
<input id="razorpay_signature" name="razorpay_signature" type="hidden" value=""/>
<input id="magento_order_id" name="magento_order_id" type="hidden" value="<?php echo $this->getMagentoOrderId(); ?>"/>
</form>

<h1>Please wait...</h1>
Expand Down
180 changes: 95 additions & 85 deletions package.xml
Original file line number Diff line number Diff line change
@@ -1,85 +1,95 @@
<?xml version="1.0"?>
<package>
<name>Razorpay</name>
<version>1.1.10</version>
<stability>stable</stability>
<license>MIT</license>
<channel>community</channel>
<extends/>
<summary>Razorpay extension to accept payments from multiple banks and wallets.</summary>
<description>Razorpay is a payment aggregator to accept payments from various method including netbanking, credit card, debit card, wallets etc.</description>
<notes>This version uses the Razorpay API v1 for the best possible experience.</notes>
<authors>
<author>
<name>Vivek</name>
<user>crims0n</user>
<email>vivek@razorpay.com</email>
</author>
</authors>
<date>2016-09-02</date>
<time>03:21:30</time>
<contents>
<target name="magecommunity">
<dir name="Razorpay">
<dir name="Payments">
<dir name="Block">
<file hash="15049195e1e3d519298a3bd5095990fe" name="Checkout.php"/>
</dir>
<dir name="Helper">
<file hash="25f10d720b3a9f3679b1a4a5b9764fb4" name="Data.php"/>
<file hash="308c55e7a7d56a27fcfb3983169c3491" name="ca-bundle.crt"/>
</dir>
<dir name="Model">
<file hash="e5d83b6288add31a60aa8e2656e47887" name="Paymentmethod.php"/>
</dir>
<dir name="controllers">
<file hash="6402350485984f971ef298ffff759084" name="CheckoutController.php"/>
</dir>
<dir name="etc">
<file hash="ee05ae1c78daa8f263de0f3c6d25a9df" name="config.xml"/>
<file hash="3045e0b5d2d2d0b52e47c23a64f99445" name="system.xml"/>
</dir>
</dir>
</dir>
</target>
<target name="magedesign">
<dir name="frontend">
<dir name="base">
<dir name="default">
<dir name="layout">
<file hash="733e8107f58a1a76fc13f5b11090a6df" name="razorpay.xml"/>
</dir>
<dir name="template">
<dir name="razorpay">
<file hash="f475fa98caab729800fe098b59208db9" name="checkout.phtml"/>
</dir>
</dir>
</dir>
</dir>
</dir>
</target>
<target name="mageetc">
<dir name="modules">
<file hash="84978ab3d49e7f70fc8e1aa00cb2dc36" name="Razorpay_Payments.xml"/>
</dir>
</target>
<target name="mage">
<dir>
<dir name="js">
<dir name="razorpay">
<file hash="7d8379be889bfe8445515c48f111e79f" name="razorpay-utils.js"/>
</dir>
</dir>
</dir>
</target>
</contents>
<compatible/>
<dependencies>
<required>
<php>
<min>5.4.45</min>
<max>5.6.24</max>
</php>
</required>
</dependencies>
</package>
<?xml version="1.0"?>
<package>
<name>Razorpay</name>
<version>1.1.10</version>
<stability>stable</stability>
<license>MIT</license>
<channel>community</channel>
<extends/>
<summary>Razorpay extension to accept payments from multiple banks and wallets.</summary>
<description>Razorpay is a payment aggregator to accept payments from various method including netbanking, credit card, debit card, wallets etc.</description>
<notes>This version uses the Razorpay API v1 for the best possible experience.</notes>
<authors>
<author>
<name>Abhay Rana</name>
<user>captn3mO</user>
<email>nemo@razorpay.com</email>
</author>
<author>
<name>Vivek Kumar</name>
<user>crims0n</user>
<email>vivek@razorpay.com</email>
</author>
<author>
<name>Mayank Amencherla</name>
<user>mayank</user>
<email>mayank.amencherla@razorpay.com</email>
</author>
</authors>
<date>2016-10-18</date>
<time>15:37:59</time>
<contents>
<target name="magecommunity">
<dir name="Razorpay">
<dir name="Payments">
<dir>
<dir name="Block">
<file hash="15049195e1e3d519298a3bd5095990fe" name="Checkout.php"/>
</dir>
<dir name="Helper">
<file hash="bbf4955d0095e8f53022936eca12aab6" name="Data.php"/>
<file hash="308c55e7a7d56a27fcfb3983169c3491" name="ca-bundle.crt"/>
</dir>
<dir name="Model">
<file hash="3a06966d04bb508ff85ec8a355a7f943" name="Paymentmethod.php"/>
</dir>
<dir name="controllers">
<file hash="d7b1742fe6f37803208a93795915a81b" name="CheckoutController.php"/>
</dir>
<dir name="etc">
<file hash="ee05ae1c78daa8f263de0f3c6d25a9df" name="config.xml"/>
<file hash="3045e0b5d2d2d0b52e47c23a64f99445" name="system.xml"/>
</dir>
</dir>
</dir>
</dir>
</target>
<target name="magedesign">
<dir name="frontend">
<dir name="base">
<dir name="default">
<dir name="layout">
<file hash="733e8107f58a1a76fc13f5b11090a6df" name="razorpay.xml"/>
</dir>
<dir name="template">
<dir name="razorpay">
<file hash="568d98738658141ef1ce47edc5363494" name="checkout.phtml"/>
</dir>
</dir>
</dir>
</dir>
</dir>
</target>
<target name="mageetc">
<dir name="modules">
<file hash="84978ab3d49e7f70fc8e1aa00cb2dc36" name="Razorpay_Payments.xml"/>
</dir>
</target>
<target name="mage">
<dir name="js">
<dir name="razorpay">
<file hash="7d8379be889bfe8445515c48f111e79f" name="razorpay-utils.js"/>
</dir>
</dir>
</target>
</contents>
<compatible/>
<dependencies>
<required>
<php>
<min>5.4.45</min>
<max>5.6.26</max>
</php>
</required>
</dependencies>
</package>

0 comments on commit c734a8c

Please sign in to comment.