-
Notifications
You must be signed in to change notification settings - Fork 31
Add support for currency switcher plugin #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
6c5f1ff
e52f7eb
408dce6
ebcbb24
0159f33
be83eac
9ffa51b
dfc30f0
0e3f766
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace Razorpay\Woocommerce\Errors; | ||
|
||
require_once __DIR__.'/../../razorpay-sdk/Razorpay.php'; | ||
|
||
use Razorpay\Api\Errors as ApiErrors; | ||
|
||
class ErrorCode extends ApiErrors\ErrorCode | ||
{ | ||
const INVALID_CURRENCY_ERROR_CODE = 'INVALID_CURRENCY_ERROR'; | ||
const INVALID_CURRENCY_ERROR_MESSAGE = 'The selected currency is invalid.'; | ||
|
||
const WOOCS_MISSING_ERROR_CODE = 'WOOCS_MISSING_ERROR'; | ||
const WOOCS_MISSING_ERROR_MESSAGE = 'The Woocommerce Currency Switcher plugin is missing.'; | ||
|
||
const WOOCS_CURRENCY_MISSING_ERROR_CODE = 'WOOCS_CURRENCY_MISSING_ERROR'; | ||
const WOOCS_CURRENCY_MISSING_ERROR_MESSAGE = 'The current currency and INR needs to be configured in Woocommerce Currency Switcher plugin'; | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,9 +14,11 @@ | |
} | ||
|
||
require_once __DIR__.'/includes/razorpay-webhook.php'; | ||
require_once __DIR__.'/includes/Errors/ErrorCode.php'; | ||
|
||
use Razorpay\Api\Api; | ||
use Razorpay\Api\Errors; | ||
use Razorpay\Woocommerce\Errors as WooErrors; | ||
|
||
require_once __DIR__.'/razorpay-sdk/Razorpay.php'; | ||
|
||
|
@@ -33,15 +35,17 @@ function woocommerce_razorpay_init() | |
class WC_Razorpay extends WC_Payment_Gateway | ||
{ | ||
// This one stores the WooCommerce Order Id | ||
const SESSION_KEY = 'razorpay_wc_order_id'; | ||
const RAZORPAY_PAYMENT_ID = 'razorpay_payment_id'; | ||
const SESSION_KEY = 'razorpay_wc_order_id'; | ||
const RAZORPAY_PAYMENT_ID = 'razorpay_payment_id'; | ||
|
||
|
||
public function __construct() | ||
{ | ||
$this->id = 'razorpay'; | ||
$this->method_title = 'Razorpay'; | ||
$this->icon = plugins_url('images/logo.png' , __FILE__); | ||
$this->has_fields = false; | ||
|
||
$this->init_form_fields(); | ||
$this->init_settings(); | ||
$this->title = $this->settings['title']; | ||
|
@@ -176,7 +180,6 @@ function payment_fields() | |
**/ | ||
function receipt_page($order) | ||
{ | ||
echo '<p>'.__('Thank you for your order, please click the button below to pay with Razorpay.', 'razorpay').'</p>'; | ||
echo $this->generate_razorpay_form($order); | ||
} | ||
|
||
|
@@ -191,7 +194,7 @@ protected function getSessionKey($orderId) | |
* that is is still correct. If not found | ||
* (or incorrect), create a new Razorpay Order | ||
* @param string $orderId Order Id | ||
* @return mixed Razorpay Order Id or null | ||
* @return mixed Razorpay Order Id or Exception | ||
*/ | ||
protected function createOrGetRazorpayOrderId($orderId) | ||
{ | ||
|
@@ -228,13 +231,18 @@ protected function createOrGetRazorpayOrderId($orderId) | |
{ | ||
try | ||
{ | ||
return $this->createRazorpayOrderId( | ||
$orderId, $sessionKey); | ||
return $this->createRazorpayOrderId($orderId, $sessionKey); | ||
} | ||
catch(Exception $e) | ||
// For the bad request errors, it's safe to show the message to the customer. | ||
catch (Errors\BadRequestError $e) | ||
{ | ||
// Order creation failed | ||
return null; | ||
return $e; | ||
} | ||
// For any other exceptions, we make sure that the error message | ||
// does not propagate to the front-end. | ||
catch (Exception $e) | ||
{ | ||
return new Exception("Payment failed"); | ||
} | ||
} | ||
} | ||
|
@@ -251,14 +259,16 @@ public function generate_razorpay_form($orderId) | |
|
||
$razorpayOrderId = $this->createOrGetRazorpayOrderId($orderId); | ||
|
||
if ($razorpayOrderId === null) | ||
if(is_a($razorpayOrderId, 'Exception')) | ||
{ | ||
return 'RAZORPAY ERROR: Api could not be reached'; | ||
$message = $razorpayOrderId->getMessage(); | ||
return 'RAZORPAY ERROR: Order creation failed with the message \'' . $message . '\''; | ||
} | ||
|
||
$checkoutArgs = $this->getCheckoutArguments($order, $razorpayOrderId); | ||
|
||
$html = $this->generateOrderForm($redirectUrl, $checkoutArgs); | ||
$html = '<p>'.__('Thank you for your order, please click the button below to pay with Razorpay.', 'razorpay').'</p>'; | ||
$html .= $this->generateOrderForm($redirectUrl, $checkoutArgs); | ||
|
||
return $html; | ||
} | ||
|
@@ -275,17 +285,25 @@ protected function getCheckoutArguments($order, $razorpayOrderId) | |
$productinfo = "Order $orderId"; | ||
|
||
$args = array( | ||
'key' => $this->key_id, | ||
'name' => get_bloginfo('name'), | ||
'currency' => get_woocommerce_currency(), | ||
'description' => $productinfo, | ||
'notes' => array( | ||
'woocommerce_order_id' => $orderId | ||
), | ||
'order_id' => $razorpayOrderId, | ||
'callback_url' => $callbackUrl, | ||
'key' => $this->key_id, | ||
'name' => get_bloginfo('name'), | ||
// Harcoding currency to INR, since if not INR, an exception gets thrown | ||
'currency' => 'INR', | ||
// 'currency' => get_woocommerce_currency(), | ||
'description' => $productinfo, | ||
'notes' => array( | ||
'woocommerce_order_id' => $orderId | ||
), | ||
'order_id' => $razorpayOrderId, | ||
'callback_url' => $callbackUrl | ||
); | ||
|
||
if ($order->get_currency() !== 'INR') | ||
{ | ||
$args['display_currency'] = $order->get_currency(); | ||
$args['display_amount'] = $order->get_total(); | ||
} | ||
|
||
$args['amount'] = $this->getOrderAmountAsInteger($order); | ||
|
||
if (version_compare(WOOCOMMERCE_VERSION, '2.7.0', '>=')) | ||
|
@@ -329,13 +347,73 @@ protected function createRazorpayOrderId($orderId, $sessionKey) | |
$api = $this->getRazorpayApiInstance(); | ||
|
||
$data = $this->getOrderCreationData($orderId); | ||
|
||
if ($data['currency'] !== 'INR') | ||
{ | ||
if (class_exists('WOOCS')) | ||
{ | ||
$data = $this->convertCurrencyWoocs($data); | ||
} | ||
else | ||
{ | ||
throw new Errors\BadRequestError( | ||
WooErrors\ErrorCode::WOOCS_MISSING_ERROR_MESSAGE, | ||
WooErrors\ErrorCode::WOOCS_MISSING_ERROR_CODE, | ||
400 | ||
); | ||
} | ||
} | ||
|
||
$razorpay_order = $api->order->create($data); | ||
|
||
$razorpayOrderId = $razorpay_order['id']; | ||
|
||
$woocommerce->session->set($sessionKey, $razorpayOrderId); | ||
|
||
return $razorpayOrderId; | ||
|
||
} | ||
|
||
/** | ||
* Convert the currency to INR using rates fetched from Woocommerce Currency Switcher plugin | ||
* | ||
* @param Array $data | ||
* | ||
* @return Array | ||
* | ||
**/ | ||
protected function convertCurrencyWoocs($data) | ||
{ | ||
global $WOOCS; | ||
|
||
$currencies = $WOOCS->get_currencies(); | ||
|
||
$value = $data['amount'] * $currencies[$WOOCS->current_currency]['rate']; | ||
|
||
if (array_key_exists('INR', $currencies) and array_key_exists($data['currency'], $currencies)) | ||
{ | ||
// If the currenct currency is the same as the default currency set in WooCommerce, | ||
// Currency Switcher plugin sets the rate of currenct currency as 0, because of which | ||
// we need to set this to 1 here if it's value is 0 | ||
$current_currency_rate = ($currencies[$data['currency']]['rate'] == 0 ? 1 : $currencies[$data['currency']]['rate']); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. camelCase everywhere, unless wordpress hook-related things |
||
|
||
// Convert the currency to INR using the rates fetched from the Currency Switcher plugin | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are there any specific instructions someone would need to follow to add INR in this list? Can you write a helper guide at https://github.com/razorpay/razorpay-woocommerce/wiki/Multi-Currency (screenshots will be very helpful) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added the instructions to get this working on the above wiki page. |
||
$data['amount'] = round( | ||
(($data['amount'] * $currencies['INR']['rate']) / $current_currency_rate), | ||
0 | ||
); | ||
$data['currency'] = 'INR'; | ||
return $data; | ||
} | ||
else | ||
{ | ||
throw new Errors\BadRequestError( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does this show up during a payment attempt? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added screenshots below. |
||
WooErrors\ErrorCode::WOOCS_CURRENCY_MISSING_ERROR_MESSAGE, | ||
WooErrors\ErrorCode::WOOCS_CURRENCY_MISSING_ERROR_CODE, | ||
400 | ||
); | ||
|
||
} | ||
} | ||
|
||
protected function verifyOrderAmount($razorpayOrderId, $orderId) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,4 +14,4 @@ public static function exists($code) | |
|
||
return defined(get_class() . '::' . $code); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
closing bracket should align on left