Razorpay for WooCommerce - Version 2.7.2

Version Description

  • Buf fix in webhook.
  • Tested up to Woocommerce 5.5.1
Download this release

Release Info

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

Code changes from version 2.7.1 to 2.7.2

Files changed (3) hide show
  1. includes/razorpay-webhook.php +33 -4
  2. readme.txt +5 -1
  3. woo-razorpay.php +25 -11
includes/razorpay-webhook.php CHANGED
@@ -29,6 +29,14 @@ class RZP_Webhook
29
  const REFUNDED_CREATED = 'refund.created';
30
  const VIRTUAL_ACCOUNT_CREDITED = 'virtual_account.credited';
31
 
 
 
 
 
 
 
 
 
32
  public function __construct()
33
  {
34
  $this->razorpay = new WC_Razorpay(false);
@@ -68,6 +76,12 @@ class RZP_Webhook
68
  if (($enabled === 'yes') and
69
  (empty($data['event']) === false))
70
  {
 
 
 
 
 
 
71
  if (isset($_SERVER['HTTP_X_RAZORPAY_SIGNATURE']) === true)
72
  {
73
  $razorpayWebhookSecret = $this->razorpay->getSetting('webhook_secret');
@@ -147,7 +161,7 @@ class RZP_Webhook
147
  * @param array $data Webook Data
148
  */
149
  protected function paymentAuthorized(array $data)
150
- {
151
  // We don't process subscription/invoice payments here
152
  if (isset($data['payload']['payment']['entity']['invoice_id']) === true)
153
  {
@@ -157,7 +171,7 @@ class RZP_Webhook
157
  //
158
  // Order entity should be sent as part of the webhook payload
159
  //
160
- $orderId = $data['payload']['payment']['entity']['notes']['woocommerce_order_id'];
161
 
162
  $order = wc_get_order($orderId);
163
 
@@ -240,7 +254,7 @@ class RZP_Webhook
240
  //
241
  // Order entity should be sent as part of the webhook payload
242
  //
243
- $orderId = $data['payload']['payment']['entity']['notes']['woocommerce_order_id'];
244
 
245
  $order = wc_get_order($orderId);
246
 
@@ -331,6 +345,21 @@ class RZP_Webhook
331
  return $payment;
332
  }
333
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
334
  /**
335
  * Returns the order amount, rounded as integer
336
  * @param WC_Order $order WooCommerce Order instance
@@ -375,7 +404,7 @@ class RZP_Webhook
375
  //
376
  // Order entity should be sent as part of the webhook payload
377
  //
378
- $orderId = $payment['notes']['woocommerce_order_id'];
379
 
380
  $order = wc_get_order($orderId);
381
 
29
  const REFUNDED_CREATED = 'refund.created';
30
  const VIRTUAL_ACCOUNT_CREDITED = 'virtual_account.credited';
31
 
32
+ protected $eventsArray = [
33
+ self::PAYMENT_AUTHORIZED,
34
+ self::VIRTUAL_ACCOUNT_CREDITED,
35
+ self::REFUNDED_CREATED,
36
+ self::PAYMENT_FAILED,
37
+ self::SUBSCRIPTION_CANCELLED
38
+ ];
39
+
40
  public function __construct()
41
  {
42
  $this->razorpay = new WC_Razorpay(false);
76
  if (($enabled === 'yes') and
77
  (empty($data['event']) === false))
78
  {
79
+ // Skip the webhook if not the valid data and event
80
+ if ($this->shouldConsumeWebhook($data) === false)
81
+ {
82
+ return;
83
+ }
84
+
85
  if (isset($_SERVER['HTTP_X_RAZORPAY_SIGNATURE']) === true)
86
  {
87
  $razorpayWebhookSecret = $this->razorpay->getSetting('webhook_secret');
161
  * @param array $data Webook Data
162
  */
163
  protected function paymentAuthorized(array $data)
164
+ {
165
  // We don't process subscription/invoice payments here
166
  if (isset($data['payload']['payment']['entity']['invoice_id']) === true)
167
  {
171
  //
172
  // Order entity should be sent as part of the webhook payload
173
  //
174
+ $orderId = $data['payload']['payment']['entity']['notes']['woocommerce_order_number'];
175
 
176
  $order = wc_get_order($orderId);
177
 
254
  //
255
  // Order entity should be sent as part of the webhook payload
256
  //
257
+ $orderId = $data['payload']['payment']['entity']['notes']['woocommerce_order_number'];
258
 
259
  $order = wc_get_order($orderId);
260
 
345
  return $payment;
346
  }
347
 
348
+ /**
349
+ * Returns boolean false incase not proper webhook data
350
+ */
351
+ protected function shouldConsumeWebhook($data)
352
+ {
353
+ if ((isset($data['event']) === true) and
354
+ (in_array($data['event'], $this->eventsArray) === true) and
355
+ isset($data['payload']['payment']['entity']['notes']['woocommerce_order_number']) === true)
356
+ {
357
+ return true;
358
+ }
359
+
360
+ return false;
361
+ }
362
+
363
  /**
364
  * Returns the order amount, rounded as integer
365
  * @param WC_Order $order WooCommerce Order instance
404
  //
405
  // Order entity should be sent as part of the webhook payload
406
  //
407
+ $orderId = $payment['notes']['woocommerce_order_number'];
408
 
409
  $order = wc_get_order($orderId);
410
 
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: 5.7.2
6
- Stable tag: 2.7.1
7
  Requires PHP: 5.6
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
@@ -41,6 +41,10 @@ This is compatible with WooCommerce>=2.4, including the new 3.0 release. It has
41
 
42
  == Changelog ==
43
 
 
 
 
 
44
  = 2.7.1 =
45
  * Updated the Razorpay Order notes key from woocommerce_order_id to woocommerce_order_number.
46
 
3
  Tags: razorpay, payments, india, woocommerce, ecommerce
4
  Requires at least: 3.9.2
5
  Tested up to: 5.7.2
6
+ Stable tag: 2.7.2
7
  Requires PHP: 5.6
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
41
 
42
  == Changelog ==
43
 
44
+ = 2.7.2 =
45
+ * Buf fix in webhook.
46
+ * Tested up to Woocommerce 5.5.1
47
+
48
  = 2.7.1 =
49
  * Updated the Razorpay Order notes key from woocommerce_order_id to woocommerce_order_number.
50
 
woo-razorpay.php CHANGED
@@ -3,10 +3,10 @@
3
  * Plugin Name: Razorpay for WooCommerce
4
  * Plugin URI: https://razorpay.com
5
  * Description: Razorpay Payment Gateway Integration for WooCommerce
6
- * Version: 2.7.1
7
- * Stable tag: 2.7.1
8
  * Author: Team Razorpay
9
- * WC tested up to: 5.3.0
10
  * Author URI: https://razorpay.com
11
  */
12
 
@@ -239,7 +239,6 @@ function woocommerce_razorpay_init()
239
  'default' => '',
240
  'options' => array(
241
  RZP_Webhook::PAYMENT_AUTHORIZED => 'payment.authorized',
242
- RZP_Webhook::PAYMENT_FAILED => 'payment.failed',
243
  RZP_Webhook::REFUNDED_CREATED => 'refund.created',
244
  RZP_Webhook::VIRTUAL_ACCOUNT_CREDITED => 'virtual_account.credited',
245
  ),
@@ -279,11 +278,11 @@ function woocommerce_razorpay_init()
279
  {
280
  ?>
281
  <div class="notice error is-dismissible" >
282
- <p><b><?php _e( 'Key Id and Key Secret can`t be empty'); ?><b></p>
283
  </div>
284
  <?php
285
 
286
- error_log('Key Id and Key Secret are required to enable the webhook.');
287
  return;
288
  }
289
 
@@ -299,8 +298,20 @@ function woocommerce_razorpay_init()
299
  }
300
  }
301
 
302
- if(in_array($_SERVER['SERVER_ADDR'], ["127.0.0.1","::1"]))
 
 
 
 
303
  {
 
 
 
 
 
 
 
 
304
  error_log('Could not enable webhook for localhost');
305
  return;
306
  }
@@ -351,12 +362,15 @@ function woocommerce_razorpay_init()
351
 
352
  $webhook = $this->webhookAPI("GET", "webhooks");
353
 
354
- foreach ($webhook['items'] as $key => $value)
355
  {
356
- if($value['url'] === $webhookUrl)
357
  {
358
- $webhookExist = true;
359
- $webhookId = $value['id'];
 
 
 
360
  }
361
  }
362
 
3
  * Plugin Name: Razorpay for WooCommerce
4
  * Plugin URI: https://razorpay.com
5
  * Description: Razorpay Payment Gateway Integration for WooCommerce
6
+ * Version: 2.7.2
7
+ * Stable tag: 2.7.2
8
  * Author: Team Razorpay
9
+ * WC tested up to: 5.5.1
10
  * Author URI: https://razorpay.com
11
  */
12
 
239
  'default' => '',
240
  'options' => array(
241
  RZP_Webhook::PAYMENT_AUTHORIZED => 'payment.authorized',
 
242
  RZP_Webhook::REFUNDED_CREATED => 'refund.created',
243
  RZP_Webhook::VIRTUAL_ACCOUNT_CREDITED => 'virtual_account.credited',
244
  ),
278
  {
279
  ?>
280
  <div class="notice error is-dismissible" >
281
+ <p><b><?php _e( 'Key Id and Key Secret are required.'); ?><b></p>
282
  </div>
283
  <?php
284
 
285
+ error_log('Key Id and Key Secret are required.');
286
  return;
287
  }
288
 
298
  }
299
  }
300
 
301
+ $domain = parse_url($webhookUrl, PHP_URL_HOST);
302
+
303
+ $domain_ip = gethostbyname($domain);
304
+
305
+ if (!filter_var($domain_ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE))
306
  {
307
+ $this->update_option( 'enable_webhook', 'no' );
308
+
309
+ ?>
310
+ <div class="notice error is-dismissible" >
311
+ <p><b><?php _e( 'Could not enable webhook for localhost server.'); ?><b></p>
312
+ </div>
313
+ <?php
314
+
315
  error_log('Could not enable webhook for localhost');
316
  return;
317
  }
362
 
363
  $webhook = $this->webhookAPI("GET", "webhooks");
364
 
365
+ if(count($webhook) > 0)
366
  {
367
+ foreach ($webhook['items'] as $key => $value)
368
  {
369
+ if($value['url'] === $webhookUrl)
370
+ {
371
+ $webhookExist = true;
372
+ $webhookId = $value['id'];
373
+ }
374
  }
375
  }
376