From 2eb57601282bdb7f2cfc204a7723a557b991f8cc Mon Sep 17 00:00:00 2001 From: "mudududla.sony" Date: Fri, 20 May 2022 13:45:12 +0530 Subject: [PATCH] Add supported subscription webhook events --- includes/razorpay-webhook.php | 20 +++++++------- woo-razorpay.php | 51 ++++++++++++++++++----------------- 2 files changed, 37 insertions(+), 34 deletions(-) diff --git a/includes/razorpay-webhook.php b/includes/razorpay-webhook.php index 8b3a450d..c0fe3b2f 100644 --- a/includes/razorpay-webhook.php +++ b/includes/razorpay-webhook.php @@ -203,7 +203,7 @@ protected function paymentAuthorized(array $data) rzpLogInfo("Woocommerce orderId: $orderId webhook process intitiated for payment authorized event"); if(!empty($orderId)) - { + { $order = $this->checkIsObject($orderId); } //To give the priority to callback script to compleate the execution fist adding this locking. @@ -228,12 +228,12 @@ protected function paymentAuthorized(array $data) return; } - + if($orderStatus == 'draft') { updateOrderStatus($orderId, 'wc-pending'); } - + $razorpayPaymentId = $data['payload']['payment']['entity']['id']; $payment = $this->getPaymentEntity($razorpayPaymentId, $data); @@ -312,7 +312,7 @@ protected function paymentPending(array $data) rzpLogInfo("Woocommerce orderId: $orderId webhook process intitiated for COD method payment pending event"); if(!empty($orderId)) - { + { $order = $this->checkIsObject($orderId); } //To give the priority to callback script to compleate the execution fist adding this locking. @@ -337,12 +337,12 @@ protected function paymentPending(array $data) return; } - + if($orderStatus == 'draft') { updateOrderStatus($orderId, 'wc-pending'); } - + $razorpayPaymentId = $data['payload']['payment']['entity']['id']; $payment = $this->getPaymentEntity($razorpayPaymentId, $data); @@ -379,7 +379,7 @@ protected function virtualAccountCredited(array $data) $orderId = $data['payload']['payment']['entity']['notes']['woocommerce_order_number']; if(!empty($orderId)) - { + { $order = $this->checkIsObject($orderId); } // If it is already marked as paid, ignore the event @@ -467,7 +467,7 @@ protected function shouldConsumeWebhook($data) { if ((isset($data['event']) === true) and (in_array($data['event'], $this->eventsArray) === true) and - isset($data['payload']['payment']['entity']['notes']['woocommerce_order_number']) === true) { + (isset($data['payload']['payment']['entity']['notes']['woocommerce_order_number']) === true or isset($data['payload']['subscription']['entity']['notes']['woocommerce_order_id']) === true)) { return true; } @@ -518,10 +518,10 @@ public function refundedCreated(array $data) $orderId = $payment['notes']['woocommerce_order_number']; if(!empty($orderId)) - { + { $order = $this->checkIsObject($orderId); } - + // If it is already marked as unpaid, ignore the event if ($order->needs_payment() === true) { return; diff --git a/woo-razorpay.php b/woo-razorpay.php index 08bd7b0d..81d3a9b5 100644 --- a/woo-razorpay.php +++ b/woo-razorpay.php @@ -61,7 +61,10 @@ class WC_Razorpay extends WC_Payment_Gateway 'payment.authorized', 'payment.pending', 'refund.created', - 'virtual_account.credited' + 'virtual_account.credited', + 'subscription.cancelled', + 'subscription.paused', + 'subscription.resumed' ); protected $defaultWebhookEvents = array( @@ -157,7 +160,7 @@ public function __construct($hooks = true) $this->icon = "https://cdn.razorpay.com/static/assets/logo/payment.svg"; // 1cc flags should be enabled only if merchant has access to 1cc feature $is1ccAvailable = false; - + // Load preference API call only for administrative interface page. if (is_admin()) { @@ -297,7 +300,7 @@ public function init_form_fields() } public function autoEnableWebhook() - { + { $webhookExist = false; $webhookUrl = esc_url(admin_url('admin-post.php')) . '?action=rzp_wc_webhook'; @@ -306,7 +309,7 @@ public function autoEnableWebhook() $enabled = true; $alphanumericString = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-=~!@#$%^&*()_+,./<>?;:[]{}|abcdefghijklmnopqrstuvwxyz'; $secret = substr(str_shuffle($alphanumericString), 0, 20); - + $getWebhookFlag = get_option('webhook_enable_flag'); $time = time(); @@ -331,7 +334,7 @@ public function autoEnableWebhook() return; } - + $domain = parse_url($webhookUrl, PHP_URL_HOST); $domain_ip = gethostbyname($domain); @@ -362,31 +365,31 @@ public function autoEnableWebhook() { $webhookItems[] = $value; } - } + } } while ( $webhook['count'] >= 1); - + $data = [ 'url' => $webhookUrl, 'active' => $enabled, 'events' => $this->defaultWebhookEvents, 'secret' => $secret, ]; - + if (count($webhookItems) > 0) - { + { foreach ($webhookItems as $key => $value) { if ($value['url'] === $webhookUrl) - { + { foreach ($value['events'] as $evntkey => $evntval) { - if (($evntval == 1) and + if (($evntval == 1) and (in_array($evntkey, $this->supportedWebhookEvents) === true)) { $this->defaultWebhookEvents[$evntkey] = true; } } - + $data = [ 'url' => $webhookUrl, 'active' => $enabled, @@ -397,7 +400,7 @@ public function autoEnableWebhook() $webhookId = $value['id']; } } - } + } if ($webhookExist) { $this->webhookAPI('PUT', "webhooks/".$webhookId, $data); @@ -593,7 +596,7 @@ protected function getRazorpayPaymentParams($orderId) { if ($getWebhookFlag + 86400 < time()) { - $this->autoEnableWebhook(); + $this->autoEnableWebhook(); } } rzpLogInfo("getRazorpayPaymentParams $orderId"); @@ -1439,7 +1442,7 @@ public function update1ccOrderWC(& $order, $wcOrderId, $razorpayPaymentId) if ($isStoreShippingEnabled == 'yes') { - foreach ($shippingData as $key => $value) + foreach ($shippingData as $key => $value) { $item = new WC_Order_Item_Shipping(); //$item->set_method_id($test[$key]['rate_id']); @@ -1468,13 +1471,13 @@ public function update1ccOrderWC(& $order, $wcOrderId, $razorpayPaymentId) wc_update_order_item_meta( $itemId, $itemkey, $itemval); } } - + } } else { $item = new WC_Order_Item_Shipping(); - + // if shipping charges zero if ($razorpayData['shipping_fee'] == 0) { @@ -1490,12 +1493,12 @@ public function update1ccOrderWC(& $order, $wcOrderId, $razorpayPaymentId) $item->set_total( $razorpayData['shipping_fee']/100 ); $order->add_item( $item ); - + $item->save(); } // Calculate totals and save $order->calculate_totals(); - + } } @@ -1629,7 +1632,7 @@ protected function getShippingZone($zoneId) return $zone; } - + // Update user billing and shipping information @@ -1659,7 +1662,7 @@ protected function updateRecoverCartInfo($wcOrderId) if (isset($cut_off_time)) { $cartCutOffTime = intval($cutOffTime) * 60; - } + } else { $cartCutOffTime = 60 * 60; @@ -1675,7 +1678,7 @@ protected function updateRecoverCartInfo($wcOrderId) $userType = 'GUEST'; $userId = get_post_meta($wcOrderId, 'abandoned_user_id', true); } - + $results = $wpdb->get_results( // phpcs:ignore $wpdb->prepare( 'SELECT * FROM `' . $wpdb->prefix . 'ac_abandoned_cart_history_lite` WHERE user_id = %s AND cart_ignored = %s AND recovered_cart = %s AND user_type = %s', @@ -1782,7 +1785,7 @@ function updateVendorDetails($shippingFee, $vendorId, $orderId) 'SELECT * FROM `' . $wpdb->prefix . 'wcfm_marketplace_orders` WHERE vendor_id = %d AND order_id = %d', $vendorId, $orderId - ) + ) ); if (count($commission) > 0) @@ -1925,7 +1928,7 @@ function addMiniCheckoutButton() $tempTest = RZP_PATH . 'templates/rzp-mini-checkout-btn.php'; load_template( $tempTest, false, array() ); } - + } //To add 1CC button on product page.