Razorpay for WooCommerce - Version 3.5.0

Version Description

  • Feature Auto Enable webhook.
  • Tested up to Woocommerce 6.4.1
Download this release

Release Info

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

Code changes from version 3.4.1 to 3.5.0

btn-1cc-checkout.js CHANGED
@@ -122,6 +122,7 @@ window.addEventListener('DOMContentLoaded', function() {
122
  }
123
  },
124
  showSpinner: function(state) {
 
125
  if (rzpSpinnerBackdrop == null) {
126
  rzpSpinnerBackdrop = document.getElementById('rzp-spinner-backdrop');
127
  }
122
  }
123
  },
124
  showSpinner: function(state) {
125
+ jQuery(document.body).trigger('wc_fragment_refresh');
126
  if (rzpSpinnerBackdrop == null) {
127
  rzpSpinnerBackdrop = document.getElementById('rzp-spinner-backdrop');
128
  }
includes/api/coupon-apply.php CHANGED
@@ -122,10 +122,11 @@ function applyCouponOnCart(WP_REST_Request $request)
122
  // to clear any residual notices
123
  $temp = wc_print_notices(true);
124
 
125
- WC()->cart->remove_coupon($couponCode);
126
  WC()->cart->empty_cart();
127
 
128
  $cart1cc = create1ccCart($orderId);
 
 
129
 
130
  if ($cart1cc) {
131
  $applyCoupon = WC()->cart->add_discount($couponCode);
122
  // to clear any residual notices
123
  $temp = wc_print_notices(true);
124
 
 
125
  WC()->cart->empty_cart();
126
 
127
  $cart1cc = create1ccCart($orderId);
128
+
129
+ WC()->cart->remove_coupon($couponCode);
130
 
131
  if ($cart1cc) {
132
  $applyCoupon = WC()->cart->add_discount($couponCode);
includes/api/order.php CHANGED
@@ -113,6 +113,10 @@ function createWcOrder(WP_REST_Request $request)
113
  updateOrderStatus($orderId, 'draft');
114
  } else {
115
  $orderId = $woocommerce->session->get(RZP_1CC_CART_HASH . $cartHash);
 
 
 
 
116
  }
117
  }
118
 
113
  updateOrderStatus($orderId, 'draft');
114
  } else {
115
  $orderId = $woocommerce->session->get(RZP_1CC_CART_HASH . $cartHash);
116
+
117
+ //To get the applied coupon details from cart object.
118
+ $coupons = WC()->cart->get_coupons();
119
+ $couponCode = !empty($coupons)? array_key_first($coupons): null;
120
  }
121
  }
122
 
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.9
6
- Stable tag: 3.4.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
  = 3.4.1 =
45
  * Bug fix in webhook.
46
  * Updated Razorpay SDK.
3
  Tags: razorpay, payments, india, woocommerce, ecommerce
4
  Requires at least: 3.9.2
5
  Tested up to: 5.9
6
+ Stable tag: 3.5.0
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
+ = 3.5.0 =
45
+ * Feature Auto Enable webhook.
46
+ * Tested up to Woocommerce 6.4.1
47
+
48
  = 3.4.1 =
49
  * Bug fix in webhook.
50
  * Updated Razorpay SDK.
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: 3.4.1
7
- * Stable tag: 3.4.1
8
  * Author: Team Razorpay
9
- * WC tested up to: 6.2.2
10
  * Author URI: https://razorpay.com
11
  */
12
 
@@ -57,6 +57,17 @@ function woocommerce_razorpay_init()
57
  const DEFAULT_DESCRIPTION = 'Pay securely by Credit or Debit card or Internet Banking through Razorpay.';
58
  const DEFAULT_SUCCESS_MESSAGE = 'Thank you for shopping with us. Your account has been charged and your transaction is successful. We will be processing your order soon.';
59
 
 
 
 
 
 
 
 
 
 
 
 
60
  protected $visibleSettings = array(
61
  'enabled',
62
  'title',
@@ -65,9 +76,6 @@ function woocommerce_razorpay_init()
65
  'key_secret',
66
  'payment_action',
67
  'order_success_message',
68
- 'enable_webhook',
69
- 'webhook_events',
70
- 'webhook_secret',
71
  'route_enable',
72
  );
73
 
@@ -274,34 +282,6 @@ function woocommerce_razorpay_init()
274
  'description' => __('Message to be displayed after a successful order', $this->id),
275
  'default' => __(STATIC::DEFAULT_SUCCESS_MESSAGE, $this->id),
276
  ),
277
- 'enable_webhook' => array(
278
- 'title' => __('Enable Webhook', $this->id),
279
- 'type' => 'checkbox',
280
- 'description' => "<span>$webhookUrl</span><br/><br/>Instructions and guide to <a href='https://github.com/razorpay/razorpay-woocommerce/wiki/Razorpay-Woocommerce-Webhooks'>Razorpay webhooks</a>",
281
- 'label' => __('Enable Razorpay Webhook', $this->id),
282
- 'default' => 'no'
283
- ),
284
- 'webhook_events' => array(
285
- 'title' => __('Webhook Events', $this->id),
286
- 'type' => 'multiselect',
287
- 'description' => "",
288
- 'class' => 'wc-enhanced-select',
289
- 'default' => '',
290
- 'options' => array(
291
- RZP_Webhook::PAYMENT_AUTHORIZED => 'payment.authorized',
292
- RZP_Webhook::REFUNDED_CREATED => 'refund.created',
293
- RZP_Webhook::VIRTUAL_ACCOUNT_CREDITED => 'virtual_account.credited',
294
- ),
295
- 'custom_attributes' => array(
296
- 'data-placeholder' => __( 'Select Webhook Events', 'woocommerce' ),
297
- ),
298
- ),
299
- 'webhook_secret' => array(
300
- 'title' => __('Webhook Secret', $this->id),
301
- 'type' => 'text',
302
- 'description' => __('Webhook secret is used for webhook signature verification. This has to match the one added <a href="https://dashboard.razorpay.com/#/app/webhooks">here</a>', $this->id),
303
- 'default' => ''
304
- ),
305
  );
306
 
307
  do_action_ref_array( 'setup_extra_setting_fields', array( &$defaultFormFields ) );
@@ -316,15 +296,27 @@ function woocommerce_razorpay_init()
316
  }
317
 
318
  public function autoEnableWebhook()
319
- {
320
  $webhookExist = false;
321
  $webhookUrl = esc_url(admin_url('admin-post.php')) . '?action=rzp_wc_webhook';
322
 
323
  $key_id = $this->getSetting('key_id');
324
  $key_secret = $this->getSetting('key_secret');
325
- $enabled = $this->getSetting('enable_webhook');
326
- $secret = $this->getSetting('webhook_secret');
 
 
 
 
327
 
 
 
 
 
 
 
 
 
328
  //validating the key id and key secret set properly or not.
329
  if($key_id == null || $key_secret == null)
330
  {
@@ -338,18 +330,7 @@ function woocommerce_razorpay_init()
338
  return;
339
  }
340
 
341
- $eventsSubscribe = $this->getSetting('webhook_events');
342
-
343
- $prepareEventsData = [];
344
-
345
- if(empty($eventsSubscribe) == false)
346
- {
347
- foreach ($eventsSubscribe as $value)
348
- {
349
- $prepareEventsData[$value] = true;
350
- }
351
- }
352
-
353
  $domain = parse_url($webhookUrl, PHP_URL_HOST);
354
 
355
  $domain_ip = gethostbyname($domain);
@@ -367,66 +348,56 @@ function woocommerce_razorpay_init()
367
  error_log('Could not enable webhook for localhost');
368
  return;
369
  }
 
 
 
370
 
371
- if($enabled === 'no')
372
- {
373
- $data = [
374
- 'url' => $webhookUrl,
375
- 'active' => false,
376
- ];
377
- }
378
- else
379
- {
380
- //validating event is not empty
381
- if(empty($eventsSubscribe) === true)
382
  {
383
- ?>
384
- <div class="notice error is-dismissible" >
385
- <p><b><?php _e( 'At least one webhook event needs to be subscribed to enable webhook.'); ?><b></p>
386
- </div>
387
- <?php
388
-
389
- error_log('At least one webhook event needs to be subscribed to enable webhook.');
390
- return;
391
- }
392
-
393
- //validating webhook secret is not empty
394
- if(empty($secret) === true)
395
- {
396
- ?>
397
- <div class="notice error is-dismissible" >
398
- <p><b><?php _e( 'Webhook secret field can`t be empty.' ); ?><b></p>
399
- </div>
400
- <?php
401
-
402
- error_log('Webhook secret field can`t be empty.');
403
- return;
404
- }
405
-
406
- $data = [
407
- 'url' => $webhookUrl,
408
- 'active' => $enabled == 'yes' ? true: false,
409
- 'events' => $prepareEventsData,
410
- 'secret' => $secret,
411
- ];
412
-
413
- }
414
-
415
- $webhook = $this->webhookAPI("GET", "webhooks");
416
-
417
- if(count($webhook) > 0)
418
- {
419
- foreach ($webhook['items'] as $key => $value)
420
- {
421
- if($value['url'] === $webhookUrl)
422
  {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
423
  $webhookExist = true;
424
  $webhookId = $value['id'];
425
  }
426
  }
427
- }
428
-
429
- if($webhookExist)
430
  {
431
  $this->webhookAPI('PUT', "webhooks/".$webhookId, $data);
432
  }
@@ -617,6 +588,15 @@ function woocommerce_razorpay_init()
617
  */
618
  protected function getRazorpayPaymentParams($orderId)
619
  {
 
 
 
 
 
 
 
 
 
620
  rzpLogInfo("getRazorpayPaymentParams $orderId");
621
  $razorpayOrderId = $this->createOrGetRazorpayOrderId($orderId);
622
 
3
  * Plugin Name: Razorpay for WooCommerce
4
  * Plugin URI: https://razorpay.com
5
  * Description: Razorpay Payment Gateway Integration for WooCommerce
6
+ * Version: 3.5.0
7
+ * Stable tag: 3.5.0
8
  * Author: Team Razorpay
9
+ * WC tested up to: 6.4.1
10
  * Author URI: https://razorpay.com
11
  */
12
 
57
  const DEFAULT_DESCRIPTION = 'Pay securely by Credit or Debit card or Internet Banking through Razorpay.';
58
  const DEFAULT_SUCCESS_MESSAGE = 'Thank you for shopping with us. Your account has been charged and your transaction is successful. We will be processing your order soon.';
59
 
60
+ protected $supportedWebhookEvents = array(
61
+ 'payment.authorized',
62
+ 'refund.created',
63
+ 'virtual_account.credited'
64
+ );
65
+
66
+ protected $defaultWebhookEvents = array(
67
+ 'payment.authorized' => true,
68
+ 'refund.created' => true
69
+ );
70
+
71
  protected $visibleSettings = array(
72
  'enabled',
73
  'title',
76
  'key_secret',
77
  'payment_action',
78
  'order_success_message',
 
 
 
79
  'route_enable',
80
  );
81
 
282
  'description' => __('Message to be displayed after a successful order', $this->id),
283
  'default' => __(STATIC::DEFAULT_SUCCESS_MESSAGE, $this->id),
284
  ),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
285
  );
286
 
287
  do_action_ref_array( 'setup_extra_setting_fields', array( &$defaultFormFields ) );
296
  }
297
 
298
  public function autoEnableWebhook()
299
+ {
300
  $webhookExist = false;
301
  $webhookUrl = esc_url(admin_url('admin-post.php')) . '?action=rzp_wc_webhook';
302
 
303
  $key_id = $this->getSetting('key_id');
304
  $key_secret = $this->getSetting('key_secret');
305
+ $enabled = true;
306
+ $alphanumericString = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-=~!@#$%^&*()_+,./<>?;:[]{}|abcdefghijklmnopqrstuvwxyz';
307
+ $secret = substr(str_shuffle($alphanumericString), 0, 20);
308
+
309
+ $getWebhookFlag = get_option('webhook_enable_flag');
310
+ $time = time();
311
 
312
+ if (empty($getWebhookFlag))
313
+ {
314
+ add_option('webhook_enable_flag', $time);
315
+ }
316
+ else
317
+ {
318
+ update_option('webhook_enable_flag', $time);
319
+ }
320
  //validating the key id and key secret set properly or not.
321
  if($key_id == null || $key_secret == null)
322
  {
330
  return;
331
  }
332
 
333
+
 
 
 
 
 
 
 
 
 
 
 
334
  $domain = parse_url($webhookUrl, PHP_URL_HOST);
335
 
336
  $domain_ip = gethostbyname($domain);
348
  error_log('Could not enable webhook for localhost');
349
  return;
350
  }
351
+ $skip = 0;
352
+ $count = 10;
353
+ $webhookItems= [];
354
 
355
+ do {
356
+ $webhook = $this->webhookAPI("GET", "webhooks?count=".$count."&skip=".$skip);
357
+ $skip += 10;
358
+ if ($webhook['count'] > 0)
 
 
 
 
 
 
 
359
  {
360
+ foreach ($webhook['items'] as $key => $value)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
361
  {
362
+ $webhookItems[] = $value;
363
+ }
364
+ }
365
+ } while ( $webhook['count'] >= 1);
366
+
367
+ $data = [
368
+ 'url' => $webhookUrl,
369
+ 'active' => $enabled,
370
+ 'events' => $this->defaultWebhookEvents,
371
+ 'secret' => $secret,
372
+ ];
373
+
374
+ if (count($webhookItems) > 0)
375
+ {
376
+ foreach ($webhookItems as $key => $value)
377
+ {
378
+ if ($value['url'] === $webhookUrl)
379
+ {
380
+ foreach ($value['events'] as $evntkey => $evntval)
381
+ {
382
+ if (($evntval == 1) and
383
+ (in_array($evntkey, $this->supportedWebhookEvents) === true))
384
+ {
385
+ $this->defaultWebhookEvents[$evntkey] = true;
386
+ }
387
+ }
388
+
389
+ $data = [
390
+ 'url' => $webhookUrl,
391
+ 'active' => $enabled,
392
+ 'events' => $this->defaultWebhookEvents,
393
+ 'secret' => $secret,
394
+ ];
395
  $webhookExist = true;
396
  $webhookId = $value['id'];
397
  }
398
  }
399
+ }
400
+ if ($webhookExist)
 
401
  {
402
  $this->webhookAPI('PUT', "webhooks/".$webhookId, $data);
403
  }
588
  */
589
  protected function getRazorpayPaymentParams($orderId)
590
  {
591
+ $getWebhookFlag = get_option('webhook_enable_flag');
592
+
593
+ if (!empty($getWebhookFlag))
594
+ {
595
+ if ($getWebhookFlag + 86400 < time())
596
+ {
597
+ $this->autoEnableWebhook();
598
+ }
599
+ }
600
  rzpLogInfo("getRazorpayPaymentParams $orderId");
601
  $razorpayOrderId = $this->createOrGetRazorpayOrderId($orderId);
602