-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Razorpay plugin to 0.1.0 for Magento 1.x
Update Razorpay plugin to 0.1.0 for Magento 1.x
- Loading branch information
Showing
21 changed files
with
863 additions
and
703 deletions.
There are no files selected for viewing
Binary file not shown.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 [email protected] 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]; | ||
} | ||
} |
Oops, something went wrong.