Razorpay for WooCommerce - Version 2.4.0

Version Description

  • Added webhook for handling refund create and change order status
  • Bug fix for cart is reset if payment fails or is cancelled
  • Tested upto WordPress 5.2.4 and WooCommerce 3.7.1
Download this release

Release Info

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

Code changes from version 2.3.2 to 2.4.0

Files changed (3) hide show
  1. includes/razorpay-webhook.php +93 -1
  2. readme.txt +6 -1
  3. woo-razorpay.php +9 -10
includes/razorpay-webhook.php CHANGED
@@ -26,8 +26,9 @@ class RZP_Webhook
26
  const PAYMENT_AUTHORIZED = 'payment.authorized';
27
  const PAYMENT_FAILED = 'payment.failed';
28
  const SUBSCRIPTION_CANCELLED = 'subscription.cancelled';
 
29
 
30
- function __construct()
31
  {
32
  $this->razorpay = new WC_Razorpay(false);
33
 
@@ -41,10 +42,14 @@ class RZP_Webhook
41
  *
42
  * It passes on the webhook in the following cases:
43
  * - invoice_id set in payment.authorized
 
44
  * - Invalid JSON
45
  * - Signature mismatch
46
  * - Secret isn't setup
47
  * - Event not recognized
 
 
 
48
  */
49
  public function process()
50
  {
@@ -103,6 +108,9 @@ class RZP_Webhook
103
  case self::SUBSCRIPTION_CANCELLED:
104
  return $this->subscriptionCancelled($data);
105
 
 
 
 
106
  default:
107
  return;
108
  }
@@ -248,4 +256,88 @@ class RZP_Webhook
248
 
249
  return (int) round($order->order_total * 100);
250
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
251
  }
26
  const PAYMENT_AUTHORIZED = 'payment.authorized';
27
  const PAYMENT_FAILED = 'payment.failed';
28
  const SUBSCRIPTION_CANCELLED = 'subscription.cancelled';
29
+ const REFUNDED_CREATED = 'refund.created';
30
 
31
+ public function __construct()
32
  {
33
  $this->razorpay = new WC_Razorpay(false);
34
 
42
  *
43
  * It passes on the webhook in the following cases:
44
  * - invoice_id set in payment.authorized
45
+ * - order refunded
46
  * - Invalid JSON
47
  * - Signature mismatch
48
  * - Secret isn't setup
49
  * - Event not recognized
50
+ *
51
+ * @return void|WP_Error
52
+ * @throws Exception
53
  */
54
  public function process()
55
  {
108
  case self::SUBSCRIPTION_CANCELLED:
109
  return $this->subscriptionCancelled($data);
110
 
111
+ case self::REFUNDED_CREATED:
112
+ return $this->refundedCreated($data);
113
+
114
  default:
115
  return;
116
  }
256
 
257
  return (int) round($order->order_total * 100);
258
  }
259
+
260
+ /**
261
+ * Process Order Refund through Webhook
262
+ * @param array $data
263
+ * @return void|WP_Error
264
+ * @throws Exception
265
+ */
266
+ public function refundedCreated(array $data)
267
+ {
268
+ // We don't process subscription/invoice payments here
269
+ if (isset($data['payload']['payment']['entity']['invoice_id']) === true)
270
+ {
271
+ return;
272
+ }
273
+
274
+ $razorpayPaymentId = $data['payload']['refund']['entity']['payment_id'];
275
+
276
+ $payment = $this->getPaymentEntity($razorpayPaymentId, $data);
277
+
278
+ //
279
+ // Order entity should be sent as part of the webhook payload
280
+ //
281
+ $orderId = $payment['notes']['woocommerce_order_id'];
282
+
283
+ $order = new WC_Order($orderId);
284
+
285
+ // If it is already marked as unpaid, ignore the event
286
+ if ($order->needs_payment() === true)
287
+ {
288
+ return;
289
+ }
290
+
291
+ // If it's something else such as a WC_Order_Refund, we don't want that.
292
+ if( ! is_a( $order, 'WC_Order') )
293
+ {
294
+ $log = array(
295
+ 'Error' => 'Provided ID is not a WC Order',
296
+ );
297
+
298
+ error_log(json_encode($log));
299
+ }
300
+
301
+ if( 'refunded' == $order->get_status() )
302
+ {
303
+ $log = array(
304
+ 'Error' => 'Order has been already refunded for Order Id -'. $orderId,
305
+ );
306
+
307
+ error_log(json_encode($log));
308
+ }
309
+
310
+ $refund_amount = round(($data['payload']['refund']['entity']['amount'] / 100), 2);
311
+
312
+ $refund_reason = $data['payload']['refund']['entity']['notes']['comment'];
313
+
314
+ try
315
+ {
316
+ wc_create_refund( array(
317
+ 'amount' => $refund_amount,
318
+ 'reason' => $refund_reason,
319
+ 'order_id' => $orderId,
320
+ 'line_items' => array(),
321
+ 'refund_payment' => false
322
+ ));
323
+
324
+ }
325
+ catch (Exception $e)
326
+ {
327
+ //
328
+ // Capture will fail if the payment is already captured
329
+ //
330
+ $log = array(
331
+ 'message' => $e->getMessage(),
332
+ 'payment_id' => $razorpayPaymentId,
333
+ 'event' => $data['event']
334
+ );
335
+
336
+ error_log(json_encode($log));
337
+
338
+ }
339
+
340
+ // Graceful exit since payment is now refunded.
341
+ exit();
342
+ }
343
  }
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.2.4
6
- Stable tag: 2.3.2
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,11 @@ This is compatible with WooCommerce>=2.4, including the new 3.0 release. It has
41
 
42
  == Changelog ==
43
 
 
 
 
 
 
44
  = 2.3.2 =
45
  * Added RAZORPAY ORDER ID in order notes.
46
  * Tested upto WordPress 5.2.4 and WooCommerce 3.7.1
3
  Tags: razorpay, payments, india, woocommerce, ecommerce
4
  Requires at least: 3.9.2
5
  Tested up to: 5.2.4
6
+ Stable tag: 2.4.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
+ = 2.4.0 =
45
+ * Added webhook for handling refund create and change order status
46
+ * Bug fix for cart is reset if payment fails or is cancelled
47
+ * Tested upto WordPress 5.2.4 and WooCommerce 3.7.1
48
+
49
  = 2.3.2 =
50
  * Added RAZORPAY ORDER ID in order notes.
51
  * Tested upto WordPress 5.2.4 and WooCommerce 3.7.1
woo-razorpay.php CHANGED
@@ -3,8 +3,8 @@
3
  * Plugin Name: Razorpay for WooCommerce
4
  * Plugin URI: https://razorpay.com
5
  * Description: Razorpay Payment Gateway Integration for WooCommerce
6
- * Version: 2.3.2
7
- * Stable tag: 2.3.2
8
  * Author: Team Razorpay
9
  * WC tested up to: 3.7.1
10
  * Author URI: https://razorpay.com
@@ -641,8 +641,8 @@ function woocommerce_razorpay_init()
641
 
642
  $data['preference']['image'] = $merchantPreferences['options']['image'];
643
 
644
- return $this->hostCheckoutScripts($data);
645
-
646
  } else {
647
  $this->enqueueCheckoutScripts($data);
648
 
@@ -801,19 +801,18 @@ EOT;
801
  {
802
  $success = false;
803
  $error = 'Customer cancelled the payment';
804
- $this->handleErrorCase($order);
805
-
806
  }
807
  else
808
  {
809
  $success = false;
810
  $error = "Payment Failed.";
 
811
 
812
- $this->updateOrder($order, $success, $error, $razorpayPaymentId);
 
813
 
814
- wp_redirect(wc_get_checkout_url());
815
- exit;
816
- }
817
  }
818
 
819
  $this->updateOrder($order, $success, $error, $razorpayPaymentId);
3
  * Plugin Name: Razorpay for WooCommerce
4
  * Plugin URI: https://razorpay.com
5
  * Description: Razorpay Payment Gateway Integration for WooCommerce
6
+ * Version: 2.4.0
7
+ * Stable tag: 2.4.0
8
  * Author: Team Razorpay
9
  * WC tested up to: 3.7.1
10
  * Author URI: https://razorpay.com
641
 
642
  $data['preference']['image'] = $merchantPreferences['options']['image'];
643
 
644
+ return $this->hostCheckoutScripts($data);
645
+
646
  } else {
647
  $this->enqueueCheckoutScripts($data);
648
 
801
  {
802
  $success = false;
803
  $error = 'Customer cancelled the payment';
 
 
804
  }
805
  else
806
  {
807
  $success = false;
808
  $error = "Payment Failed.";
809
+ }
810
 
811
+ $this->handleErrorCase($order);
812
+ $this->updateOrder($order, $success, $error, $razorpayPaymentId);
813
 
814
+ wp_redirect(wc_get_checkout_url());
815
+ exit;
 
816
  }
817
 
818
  $this->updateOrder($order, $success, $error, $razorpayPaymentId);