Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added logs for webhook debugging #529

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 11 additions & 7 deletions includes/razorpay-webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public function process()
$post = file_get_contents('php://input');

$data = json_decode($post, true);
rzpLogInfo("Time:" . time() . " webhook triggered" . json_encode($data));

if (json_last_error() !== 0) {
return;
Expand All @@ -101,15 +102,17 @@ public function process()
return;
}
if (isset($_SERVER['HTTP_X_RAZORPAY_SIGNATURE']) === true) {

$razorpayWebhookSecret = (empty($this->razorpay->getSetting('webhook_secret')) === false) ? $this->razorpay->getSetting('webhook_secret') : get_option('webhook_secret');


rzpLogInfo("Time:" . time() . " Order id:" . $orderId . " Webhook secret:" . $razorpayWebhookSecret);
//
// If the webhook secret isn't set on wordpress, return
//
if (empty($razorpayWebhookSecret) === true) {
$razorpayWebhookSecret = get_option('rzp_webhook_secret');
if (empty($razorpayWebhookSecret) === false) {
rzpLogInfo("Time:" . time() . "Order id:" . $orderId . " Webhook secret using get_option():" . $razorpayWebhookSecret);
$this->razorpay->update_option('webhook_secret', $razorpayWebhookSecret);
} else {
rzpLogInfo("Woocommerce orderId: $orderId webhook process exited due to secret not available");
Expand All @@ -127,6 +130,7 @@ public function process()
$log = array(
'message' => $e->getMessage(),
'data' => $data,
'signature' => $_SERVER['HTTP_X_RAZORPAY_SIGNATURE'],
'event' => 'razorpay.wc.signature.verify_failed',
);

Expand All @@ -136,25 +140,25 @@ public function process()
return;
}

if ($this->razorpay->isHposEnabled)
if ($this->razorpay->isHposEnabled)
{
$order = wc_get_order($orderId);
$rzpWebhookNotifiedAt = $order->get_meta('rzp_webhook_notified_at');
}
else
else
{
$rzpWebhookNotifiedAt = get_post_meta($orderId, "rzp_webhook_notified_at", true);
}


if ($rzpWebhookNotifiedAt === '')
{
if ($this->razorpay->isHposEnabled)
if ($this->razorpay->isHposEnabled)
{
$order->update_meta_data('rzp_webhook_notified_at', time());
$order->save();
}
else
else
{
update_post_meta($orderId, "rzp_webhook_notified_at", time());
}
Expand Down
9 changes: 9 additions & 0 deletions woo-razorpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -467,14 +467,18 @@ protected function triggerValidationInstrumentation($data)

public function autoEnableWebhook()
{
rzpLogInfo("Time:" . time() ." at autoEnableWebhook");
$webhookExist = false;
$webhookUrl = $this->getWebhookUrl();

$key_id = $this->getSetting('key_id');
$key_secret = $this->getSetting('key_secret');
$enabled = true;

$secret = empty($this->getSetting('webhook_secret')) ? $this->generateSecret() : $this->getSetting('webhook_secret');

rzpLogInfo("Time:" . time() . " webhook secret:" . $secret);

update_option('webhook_secret', $secret);
$getWebhookFlag = get_option('webhook_enable_flag');
$time = time();
Expand Down Expand Up @@ -606,6 +610,7 @@ public function autoEnableWebhook()
];
$webhookExist = true;
$webhookId = $value['id'];
rzpLogInfo("Time:" . $time . " webhook id:" . $webhookId . " webhook data" . json_encode($data));
}
}
}
Expand Down Expand Up @@ -981,13 +986,17 @@ protected function getRazorpayPaymentParams($order, $orderId)
{
if ($getWebhookFlag + 86400 < time())
{
rzpLogInfo("Time:" . $time . " Order id:" . $orderId . " calling auto enable webhook after 12 hrs");
$this->autoEnableWebhook();
rzpLogInfo("Time:" . $time . " Order id:" . $orderId . "webhook secret after auto webhook:".$this->getSetting('webhook_secret'));
}
}
else
{
rzpLogInfo("Time:" . $time . " Order id:" . $orderId . " calling auto enable webhook as webhook_enable_flag is empty");
update_option('webhook_enable_flag', $time);
$this->autoEnableWebhook();
rzpLogInfo("Time:" . $time . " Order id:" . $orderId . "webhook secret after auto webhook:".$this->getSetting('webhook_secret'));
}

rzpLogInfo("getRazorpayPaymentParams $orderId");
Expand Down