Skip to content

Commit

Permalink
Merge pull request #279 from razorpay/add_subscription_events
Browse files Browse the repository at this point in the history
Add supported subscription webhook events
  • Loading branch information
sonyy-m authored May 20, 2022
2 parents f527d01 + c6b2259 commit 1f16e0e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 33 deletions.
20 changes: 10 additions & 10 deletions includes/razorpay-webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
Expand Down Expand Up @@ -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.
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down
49 changes: 26 additions & 23 deletions woo-razorpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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())
{
Expand Down Expand Up @@ -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';

Expand Down Expand Up @@ -331,7 +334,7 @@ public function autoEnableWebhook()
return;
}


$domain = parse_url($webhookUrl, PHP_URL_HOST);

$domain_ip = gethostbyname($domain);
Expand Down Expand Up @@ -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,
Expand All @@ -396,7 +399,7 @@ public function autoEnableWebhook()
$webhookId = $value['id'];
}
}
}
}
if ($webhookExist)
{
$this->webhookAPI('PUT', "webhooks/".$webhookId, $data);
Expand Down Expand Up @@ -592,7 +595,7 @@ protected function getRazorpayPaymentParams($orderId)
{
if ($getWebhookFlag + 86400 < time())
{
$this->autoEnableWebhook();
$this->autoEnableWebhook();
}
}
else
Expand Down Expand Up @@ -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']);
Expand Down Expand Up @@ -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)
{
Expand All @@ -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();

}
}

Expand Down Expand Up @@ -1633,7 +1636,7 @@ protected function getShippingZone($zoneId)
return $zone;
}




// Update user billing and shipping information
Expand Down Expand Up @@ -1663,7 +1666,7 @@ protected function updateRecoverCartInfo($wcOrderId)
if (isset($cut_off_time))
{
$cartCutOffTime = intval($cutOffTime) * 60;
}
}
else
{
$cartCutOffTime = 60 * 60;
Expand All @@ -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',
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 1f16e0e

Please sign in to comment.