Skip to content

Commit

Permalink
Update Razorpay plugin to 0.1.0 for Magento 1.x
Browse files Browse the repository at this point in the history
Update Razorpay plugin to 0.1.0 for Magento 1.x
  • Loading branch information
73SL4 committed Apr 5, 2016
1 parent 0d6b843 commit af9b7bc
Show file tree
Hide file tree
Showing 21 changed files with 863 additions and 703 deletions.
Binary file not shown.
42 changes: 0 additions & 42 deletions app/code/community/Razorpay/Payments/Block/Iframe.php

This file was deleted.

6 changes: 6 additions & 0 deletions app/code/community/Razorpay/Payments/Block/Placeorder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

class Razorpay_Payments_Block_Placeorder extends Mage_Core_Block_Template
{

}
91 changes: 0 additions & 91 deletions app/code/community/Razorpay/Payments/Block/Redirect.php

This file was deleted.

32 changes: 32 additions & 0 deletions app/code/community/Razorpay/Payments/Block/Setuputils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

class Razorpay_Payments_Block_Setuputils extends Mage_Core_Block_Template
{
const KEY_ID = 'payment/razorpay/key_id';
const MERCHANT_NAME = 'payment/razorpay/merchant_name_override';

/**
* Returns key_id from store config
*
* @return string
*/
public function getKeyId()
{
return Mage::getStoreConfig(self::KEY_ID);
}

/*
* Returns merchant_name from store config
*
* @return string
*/
public function getMerchantName()
{
return Mage::getStoreConfig(self::MERCHANT_NAME);
}

protected function _toHtml()
{
return parent::_toHtml();
}
}
180 changes: 155 additions & 25 deletions app/code/community/Razorpay/Payments/Helper/Data.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,155 @@
<?php

/*
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the MIT License
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/MIT
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* @category Razorpay
* @package Razorpay Payments (razorpay.com)
* @copyright Copyright (c) 2015 Razorpay
* @license http://opensource.org/licenses/MIT MIT License
*/

class Razorpay_Payments_Helper_Data extends Mage_Core_Helper_Abstract
{

}
<?php

class Razorpay_Payments_Helper_Data extends Mage_Core_Helper_Abstract
{
const CONFIG_PATH_RAZORPAY_ENABLED = 'payment/razorpay/active';

const BASE_URL = 'https://api.razorpay.com/v1/';

const PAYMENT_MODEL = 'razorpay_payments/paymentmethod';

const KEY_ID = 'key_id';
const KEY_SECRET = 'key_secret';

protected $successHttpCodes = array(200, 201, 202, 203, 204, 205, 206, 207, 208, 226);

public function __construct()
{
$this->urls = array(
'order' => self::BASE_URL . 'orders',
'payment' => self::BASE_URL . 'payments',
'capture' => self::BASE_URL . 'payments/:id/capture',
'refund' => self::BASE_URL . 'payments/:id/refund'
);

$this->userAgent = Mage::getModel(self::PAYMENT_MODEL)->_getChannel();
}

public function isRazorpayEnabled()
{
return Mage::getStoreConfigFlag(self::CONFIG_PATH_RAZORPAY_ENABLED);
}

public function createOrder($receipt, $amount)
{
if ($this->isRazorpayEnabled())
{
$currency = Razorpay_Payments_Model_Paymentmethod::CURRENCY;

$url = $this->getRelativeUrl('order');

$postData = array(
'receipt' => $receipt,
'amount' => $amount,
'currency' => $currency
);

$response = $this->sendRequest($url, $postData);

$returnArray = array(
'razorpay_order_id' => $response['id']
);

return $returnArray;
}

throw new Exception('MAGENTO_ERROR: Payment Method not available');
}

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

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

$response = $this->sendRequest($url, $postData);

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

throw new Exception('CAPTURE_ERROR: Unable to capture payment ' . $paymentId);
}

throw new Exception('MAGENTO_ERROR: Payment Method not available');
}

public function sendRequest($url, $content, $method = 'POST')
{
$paymentModel = Mage::getModel(self::PAYMENT_MODEL);

$keyId = $paymentModel->getConfigData(self::KEY_ID);
$keySecret = $paymentModel->getConfigData(self::KEY_SECRET);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, $keyId . ":" . $keySecret);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);

if ($method === 'POST')
{
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
}

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
$httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);

$responseArray = array();

if ($response === false)
{
$error = 'CURL_ERROR: ' . curl_error($ch);

throw new Exception($error);
}
else
{
if (!empty($response))
{
$responseArray = json_decode($response, true);
}

if (in_array($httpStatus, $this->successHttpCodes, true) and isset($responseArray['error']) === false)
{
return $responseArray;
}
else
{
if (!empty($responseArray['error']['code']))
{
$error = $responseArray['error']['code'].": ".$responseArray['error']['description'];
}
else
{
$error = "RAZORPAY_ERROR: Invalid Response <br/>".$response;
}

throw new Exception($error);
}
}
}

public function getRelativeUrl($name, $data = null)
{
if ($data)
{
return strtr($this->urls[$name], $data);
}

return $this->urls[$name];
}
}
Loading

0 comments on commit af9b7bc

Please sign in to comment.