diff --git a/includes/razorpay-webhook.php b/includes/razorpay-webhook.php index 64a9565a..fa4b959f 100644 --- a/includes/razorpay-webhook.php +++ b/includes/razorpay-webhook.php @@ -200,7 +200,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. @@ -225,12 +225,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); @@ -309,7 +309,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. @@ -334,12 +334,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); @@ -376,7 +376,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 @@ -464,7 +464,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; } @@ -515,10 +515,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 e680cee9..faf6bf9f 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'; @@ -331,7 +334,7 @@ public function autoEnableWebhook() return; } - + $domain = parse_url($webhookUrl, PHP_URL_HOST); $domain_ip = gethostbyname($domain); @@ -361,31 +364,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, @@ -396,7 +399,7 @@ public function autoEnableWebhook() $webhookId = $value['id']; } } - } + } if ($webhookExist) { $this->webhookAPI('PUT', "webhooks/".$webhookId, $data); @@ -592,7 +595,7 @@ protected function getRazorpayPaymentParams($orderId) { if ($getWebhookFlag + 86400 < time()) { - $this->autoEnableWebhook(); + $this->autoEnableWebhook(); } } else @@ -1443,7 +1446,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']); @@ -1472,13 +1475,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) { @@ -1494,12 +1497,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(); - + } } @@ -1633,7 +1636,7 @@ protected function getShippingZone($zoneId) return $zone; } - + // Update user billing and shipping information @@ -1663,7 +1666,7 @@ protected function updateRecoverCartInfo($wcOrderId) if (isset($cut_off_time)) { $cartCutOffTime = intval($cutOffTime) * 60; - } + } else { $cartCutOffTime = 60 * 60; @@ -1679,7 +1682,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', @@ -1786,7 +1789,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) @@ -1929,7 +1932,7 @@ function addMiniCheckoutButton() $tempTest = RZP_PATH . 'templates/rzp-mini-checkout-btn.php'; load_template( $tempTest, false, array() ); } - + } //To add 1CC button on product page.