Skip to content

Commit

Permalink
Merge pull request #270 from razorpay/webhook-payment-pending
Browse files Browse the repository at this point in the history
Added payment pending event handler in webhook script
  • Loading branch information
ChetanGN authored May 16, 2022
2 parents c9f05df + faa5612 commit 0cc4017
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 3 deletions.
78 changes: 78 additions & 0 deletions includes/razorpay-webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class RZP_Webhook
*/
const PAYMENT_AUTHORIZED = 'payment.authorized';
const PAYMENT_FAILED = 'payment.failed';
const PAYMENT_PENDING = 'payment.pending';
const SUBSCRIPTION_CANCELLED = 'subscription.cancelled';
const REFUNDED_CREATED = 'refund.created';
const VIRTUAL_ACCOUNT_CREDITED = 'virtual_account.credited';
Expand All @@ -36,6 +37,7 @@ class RZP_Webhook
self::VIRTUAL_ACCOUNT_CREDITED,
self::REFUNDED_CREATED,
self::PAYMENT_FAILED,
self::PAYMENT_PENDING,
self::SUBSCRIPTION_CANCELLED,
self::SUBSCRIPTION_PAUSED,
self::SUBSCRIPTION_RESUMED,
Expand Down Expand Up @@ -123,6 +125,9 @@ public function process()
case self::PAYMENT_FAILED:
return $this->paymentFailed($data);

case self::PAYMENT_PENDING:
return $this->paymentPending($data);

case self::SUBSCRIPTION_CANCELLED:
return $this->subscriptionCancelled($data);

Expand Down Expand Up @@ -283,6 +288,79 @@ protected function paymentAuthorized(array $data)
exit;
}

/**
* Handling the payment pending webhook to handle COD orders
*
* @param array $data Webook Data
*/
protected function paymentPending(array $data)
{
// We don't process subscription/invoice payments here
if (isset($data['payload']['payment']['entity']['invoice_id']) === true) {
return;
}

if (isset($data['payload']['payment']['entity']['method']) != 'cod' ) {
return;
}

//
// Order entity should be sent as part of the webhook payload
//
$orderId = $data['payload']['payment']['entity']['notes']['woocommerce_order_number'];

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.
$transientData = get_transient('webhook_trigger_count_for_' . $orderId);

if (empty($transientData) || $transientData == 1) {
rzpLogInfo("Woocommerce orderId: $orderId with transientData: $transientData webhook halted for 60 sec");

sleep(60);
}

$triggerCount = !empty($transientData) ? ($transientData + 1) : 1;

set_transient('webhook_trigger_count_for_' . $orderId, $triggerCount, 180);

$orderStatus = $order->get_status();
rzpLogInfo("Woocommerce orderId: $orderId order status: $orderStatus");

// If it is already marked as paid, ignore the event
if ($orderStatus != 'draft' && $order->needs_payment() === false) {
rzpLogInfo("Woocommerce orderId: $orderId webhook process exited with need payment status :". $order->needs_payment());

return;
}

if($orderStatus == 'draft')
{
updateOrderStatus($orderId, 'wc-pending');
}

$razorpayPaymentId = $data['payload']['payment']['entity']['id'];

$payment = $this->getPaymentEntity($razorpayPaymentId, $data);

$success = false;
$errorMessage = 'The payment has failed.';

if ($payment['status'] === 'pending' && $data['payload']['payment']['entity']['method'] == 'cod' && !empty($razorpayPaymentId)) {
$success = true;

$this->razorpay->updateOrder($order, $success, $errorMessage, $razorpayPaymentId, null, true);
rzpLogInfo("Woocommerce orderId: $orderId webhook process finished the update order function for COD");
}

// Graceful exit since payment is now processed.
exit;
}

/**
* Handling the virtual account credited webhook
*
Expand Down
5 changes: 4 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Contributors: razorpay
Tags: razorpay, payments, india, woocommerce, ecommerce
Requires at least: 3.9.2
Tested up to: 5.9
Stable tag: 3.5.1
Stable tag: 3.6.0
Requires PHP: 5.6
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Expand Down Expand Up @@ -41,6 +41,9 @@ This is compatible with WooCommerce>=2.4, including the new 3.0 release. It has

== Changelog ==

= 3.6.0 =
* New webhook event i.e payment.pending has been added to handle the magic checkout COD orders

= 3.5.1 =
* Bug fix for magic checkout blank order issue.

Expand Down
5 changes: 3 additions & 2 deletions woo-razorpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* Plugin Name: Razorpay for WooCommerce
* Plugin URI: https://razorpay.com
* Description: Razorpay Payment Gateway Integration for WooCommerce
* Version: 3.5.1
* Stable tag: 3.5.1
* Version: 3.6.0
* Stable tag: 3.6.0
* Author: Team Razorpay
* WC tested up to: 6.4.1
* Author URI: https://razorpay.com
Expand Down Expand Up @@ -59,6 +59,7 @@ class WC_Razorpay extends WC_Payment_Gateway

protected $supportedWebhookEvents = array(
'payment.authorized',
'payment.pending',
'refund.created',
'virtual_account.credited'
);
Expand Down

0 comments on commit 0cc4017

Please sign in to comment.