Razorpay for WooCommerce - Version 1.3.1

Version Description

Download this release

Release Info

Developer razorpay
Plugin Icon 128x128 Razorpay for WooCommerce
Version 1.3.1
Comparing to
See all releases

Code changes from version 1.3.0 to 1.3.1

Files changed (2) hide show
  1. razorpay-payments.php +95 -34
  2. readme.txt +2 -23
razorpay-payments.php CHANGED
@@ -3,7 +3,7 @@
3
  Plugin Name: WooCommerce Razorpay Payments
4
  Plugin URI: https://razorpay.com
5
  Description: Razorpay Payment Gateway Integration for WooCommerce
6
- Version: 1.3.0
7
  Author: Razorpay
8
  Author URI: https://razorpay.com
9
  */
@@ -23,6 +23,7 @@ function woocommerce_razorpay_init()
23
  {
24
  const BASE_URL = 'https://api.razorpay.com/';
25
  const API_VERSION = 'v1';
 
26
  const SESSION_KEY = 'razorpay_wc_order_id';
27
 
28
  public function __construct()
@@ -133,53 +134,112 @@ function woocommerce_razorpay_init()
133
  echo $this->generate_razorpay_form($order);
134
  }
135
 
 
 
 
 
 
136
  /**
137
  * Generate razorpay button link
138
  **/
139
- public function generate_razorpay_form($order_id)
140
  {
141
  global $woocommerce;
142
- $order = new WC_Order($order_id);
 
143
  $redirect_url = get_site_url() . '/?wc-api=' . get_class($this);
144
- $productinfo = "Order $order_id";
 
 
 
 
145
  try
146
  {
147
- $api = new Api($this->key_id, $this->key_secret);
148
- // Calls the helper function to create order data
149
- $data = $this->get_order_creation_data($order_id);
150
-
151
- $razorpay_order = $api->order->create($data);
152
-
153
- $woocommerce->session->set('razorpay_order_id', $razorpay_order['id']);
154
- $razorpay_args = array(
155
- 'key' => $this->key_id,
156
- 'name' => get_bloginfo('name'),
157
- 'amount' => $order->order_total * 100,
158
- 'currency' => get_woocommerce_currency(),
159
- 'description' => $productinfo,
160
- 'prefill' => array(
161
- 'name' => $order->billing_first_name." ".$order->billing_last_name,
162
- 'email' => $order->billing_email,
163
- 'contact' => $order->billing_phone
164
- ),
165
- 'notes' => array(
166
- 'woocommerce_order_id' => $order_id
167
- ),
168
- 'order_id' => $razorpay_order['id']
169
- );
170
- $json = json_encode($razorpay_args);
171
- $html = $this->generate_order_form($redirect_url, $json);
172
- return $html;
173
  }
174
  catch (Exception $e)
175
  {
176
  echo "RAZORPAY ERROR: Api could not be reached";
177
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
178
  }
179
 
180
- /**
181
- * Creates order data
182
- **/
183
  function get_order_creation_data($order_id)
184
  {
185
  $order = new WC_Order($order_id);
@@ -335,7 +395,8 @@ EOT;
335
 
336
  else
337
  {
338
- $razorpay_order_id = $woocommerce->session->get('razorpay_order_id');
 
339
  $razorpay_signature = $_POST['razorpay_signature'];
340
 
341
  $signature = hash_hmac('sha256', $razorpay_order_id . '|' . $razorpay_payment_id, $key_secret);
3
  Plugin Name: WooCommerce Razorpay Payments
4
  Plugin URI: https://razorpay.com
5
  Description: Razorpay Payment Gateway Integration for WooCommerce
6
+ Version: 1.3.1
7
  Author: Razorpay
8
  Author URI: https://razorpay.com
9
  */
23
  {
24
  const BASE_URL = 'https://api.razorpay.com/';
25
  const API_VERSION = 'v1';
26
+ // This one stores the WooCommerce Order Id
27
  const SESSION_KEY = 'razorpay_wc_order_id';
28
 
29
  public function __construct()
134
  echo $this->generate_razorpay_form($order);
135
  }
136
 
137
+ protected function getSessionKey($orderId)
138
+ {
139
+ return "razorpay_order_id.$orderId";
140
+ }
141
+
142
  /**
143
  * Generate razorpay button link
144
  **/
145
+ public function generate_razorpay_form($orderId)
146
  {
147
  global $woocommerce;
148
+ $order = new WC_Order($orderId);
149
+
150
  $redirect_url = get_site_url() . '/?wc-api=' . get_class($this);
151
+ $productinfo = "Order $orderId";
152
+ $api = new Api($this->key_id, $this->key_secret);
153
+
154
+ $sessionKey = $this->getSessionKey($orderId);
155
+
156
  try
157
  {
158
+ $razorpayOrderId = $woocommerce->session->get($sessionKey);
159
+
160
+ // If we don't have an Order
161
+ // or the if the order is present in session but doesn't match what we have saved
162
+ if (($razorpayOrderId === null) or
163
+ (($razorpayOrderId and ($this->verifyOrderAmount($razorpayOrderId, $orderId)) === false)))
164
+ {
165
+ $razorpayOrderId = $this->createRazorpayOrderId(
166
+ $orderId, $sessionKey);
167
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168
  }
169
  catch (Exception $e)
170
  {
171
  echo "RAZORPAY ERROR: Api could not be reached";
172
  }
173
+
174
+ $razorpay_args = array(
175
+ 'key' => $this->key_id,
176
+ 'name' => get_bloginfo('name'),
177
+ 'amount' => $order->order_total*100,
178
+ 'currency' => get_woocommerce_currency(),
179
+ 'description' => $productinfo,
180
+ 'prefill' => array(
181
+ 'name' => $order->billing_first_name." ".$order->billing_last_name,
182
+ 'email' => $order->billing_email,
183
+ 'contact' => $order->billing_phone
184
+ ),
185
+ 'notes' => array(
186
+ 'woocommerce_order_id' => $orderId
187
+ ),
188
+ 'order_id' => $razorpayOrderId
189
+ );
190
+
191
+ $json = json_encode($razorpay_args);
192
+
193
+ $html = $this->generate_order_form($redirect_url,$json,$orderId);
194
+
195
+ return $html;
196
+ }
197
+
198
+ protected function createRazorpayOrderId($orderId, $sessionKey)
199
+ {
200
+ // Calls the helper function to create order data
201
+ global $woocommerce;
202
+
203
+ $api = new Api($this->key_id, $this->key_secret);
204
+
205
+ $data = $this->get_order_creation_data($orderId);
206
+ $razorpay_order = $api->order->create($data);
207
+
208
+ $razorpayOrderId = $razorpay_order['id'];
209
+
210
+ $woocommerce->session->set($sessionKey, $razorpayOrderId);
211
+
212
+ return $razorpayOrderId;
213
+ }
214
+
215
+ protected function verifyOrderAmount($razorpayOrderId, $orderId)
216
+ {
217
+ $order = new WC_Order($orderId);
218
+
219
+ $api = new Api($this->key_id, $this->key_secret);
220
+
221
+ $razorpayOrder = $api->order->fetch($razorpayOrderId);
222
+
223
+ $razorpayOrderArgs = array(
224
+ 'id' => $razorpayOrderId,
225
+ 'amount' => (int) $order->order_total*100,
226
+ 'currency' => get_woocommerce_currency(),
227
+ 'receipt' => (string) $orderId,
228
+ );
229
+
230
+ $orderKeys = array_keys($razorpayOrderArgs);
231
+
232
+ foreach ($orderKeys as $key)
233
+ {
234
+ if ($razorpayOrderArgs[$key] !== $razorpayOrder[$key])
235
+ {
236
+ return false;
237
+ }
238
+ }
239
+
240
+ return true;
241
  }
242
 
 
 
 
243
  function get_order_creation_data($order_id)
244
  {
245
  $order = new WC_Order($order_id);
395
 
396
  else
397
  {
398
+ $sessionKey = $this->getSessionKey($order_id);
399
+ $razorpay_order_id = $woocommerce->session->get($sessionKey);
400
  $razorpay_signature = $_POST['razorpay_signature'];
401
 
402
  $signature = hash_hmac('sha256', $razorpay_order_id . '|' . $razorpay_payment_id, $key_secret);
readme.txt CHANGED
@@ -3,7 +3,7 @@ Contributors: razorpay
3
  Tags: razorpay, payments, india, woocommerce, ecommerce
4
  Requires at least: 3.9.2
5
  Tested up to: 4.7
6
- Stable tag: 1.3.0
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
@@ -17,28 +17,7 @@ This is compatible with both 2.4 and 2.5 series of WooCommerce.
17
 
18
  == Installation ==
19
 
20
- 1. Download the plugin from [github releases](https://github.com/razorpay/razorpay-woocommerce/archive/1.2.11.zip)
21
- 2. Ensure you have latest version of WooCommerce plugin installed
22
- 3. Unzip and upload contents of the plugin to your /wp-content/plugins/ directory
23
- 4. Activate the plugin through the 'Plugins' menu in WordPress
24
-
25
- If you have downloaded the plugin from GitHub or elsewhere, make sure
26
- that the directory is named `woo-razorpay`.
27
-
28
- This is how your directory structure should look like:
29
-
30
- ```
31
- wordpress/
32
- wp-content/
33
- plugins/
34
- woo-razorpay/
35
- ca-bundle.crt
36
- images/
37
- LICENSE
38
- razorpay-payments.php
39
- readme.txt
40
- woocommerce/
41
- ```
42
 
43
  == Configuration ==
44
 
3
  Tags: razorpay, payments, india, woocommerce, ecommerce
4
  Requires at least: 3.9.2
5
  Tested up to: 4.7
6
+ Stable tag: 1.3.1
7
  License: GPLv2 or later
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
 
17
 
18
  == Installation ==
19
 
20
+ Install the plugin from the [Wordpress Plugin Directory](https://wordpress.org/plugins/woo-razorpay/).
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  == Configuration ==
23