Skip to content

Commit 4cf4b6d

Browse files
Added support for callback url (#37)
* Added support for callback url * Handling error case in callback url * Minor improvements * Refactored code and made methods smaller Minor improvements * Fixes undefined variable * Bug fixes * Minor cleanup * Minor bug fixes * Minor improvements
1 parent 0b27ce1 commit 4cf4b6d

File tree

1 file changed

+132
-66
lines changed

1 file changed

+132
-66
lines changed

razorpay-payments.php

Lines changed: 132 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -231,44 +231,40 @@ public function generate_razorpay_form($orderId)
231231
*/
232232
protected function getCheckoutArguments($order, $razorpayOrderId)
233233
{
234-
if (version_compare(WOOCOMMERCE_VERSION, '2.7.0', '>='))
235-
{
236-
$orderId = $order->get_id();
237-
}
238-
else
239-
{
240-
$orderId = $order->id;
241-
}
234+
$callbackUrl = get_site_url() . '/?wc-api=' . get_class($this);
235+
236+
$orderId = $this->getOrderId($order);
242237

243238
$productinfo = "Order $orderId";
244239

245240
$args = array(
246-
'key' => $this->key_id,
247-
'name' => get_bloginfo('name'),
248-
'currency' => get_woocommerce_currency(),
249-
'description' => $productinfo,
250-
'notes' => array(
241+
'key' => $this->key_id,
242+
'name' => get_bloginfo('name'),
243+
'currency' => get_woocommerce_currency(),
244+
'description' => $productinfo,
245+
'notes' => array(
251246
'woocommerce_order_id' => $orderId
252247
),
253-
'order_id' => $razorpayOrderId
248+
'order_id' => $razorpayOrderId,
249+
'callback_url' => $callbackUrl,
254250
);
255251

256-
$args['amount'] = $this->getOrderAmountAsInteger($order);
252+
$args['amount'] = $this->getOrderAmountAsInteger($order);
257253

258254
if (version_compare(WOOCOMMERCE_VERSION, '2.7.0', '>='))
259255
{
260256
$args['prefill'] = array(
261-
'name' => $order->get_billing_first_name() . ' ' . $order->get_billing_last_name(),
262-
'email' => $order->get_billing_email(),
263-
'contact' => $order->get_billing_phone(),
257+
'name' => $order->get_billing_first_name() . ' ' . $order->get_billing_last_name(),
258+
'email' => $order->get_billing_email(),
259+
'contact' => $order->get_billing_phone(),
264260
);
265261
}
266262
else
267263
{
268264
$args['prefill'] = array(
269-
'name' => $order->billing_first_name . ' ' . $order->billing_last_name,
270-
'email' => $order->billing_email,
271-
'contact' => $order->billing_phone,
265+
'name' => $order->billing_first_name . ' ' . $order->billing_last_name,
266+
'email' => $order->billing_email,
267+
'contact' => $order->billing_phone,
272268
);
273269
}
274270

@@ -409,6 +405,16 @@ protected function getOrderKey($order)
409405
return $order->order_key;
410406
}
411407

408+
protected function getOrderId($order)
409+
{
410+
if (version_compare(WOOCOMMERCE_VERSION, '2.7.0', '>='))
411+
{
412+
return $order->get_id();
413+
}
414+
415+
return $order->id;
416+
}
417+
412418
public function process_refund($orderId, $amount = null, $reason = '')
413419
{
414420
$order = new WC_Order($orderId);
@@ -495,72 +501,132 @@ function check_razorpay_response()
495501
{
496502
global $woocommerce;
497503

498-
$order_id = $woocommerce->session->get(self::SESSION_KEY);
504+
$orderId = $woocommerce->session->get(self::SESSION_KEY);
499505

500-
if ($order_id and !empty($_POST[self::RAZORPAY_PAYMENT_ID]))
501-
{
502-
$order = new WC_Order($order_id);
506+
$order = new WC_Order($orderId);
503507

504-
$amount = $this->getOrderAmountAsInteger($order);
508+
$razorpayPaymentId = null;
505509

510+
if ($orderId and !empty($_POST[self::RAZORPAY_PAYMENT_ID]))
511+
{
512+
$error = "";
506513
$success = false;
507-
$error = 'WOOCOMMERCE_ERROR: Payment to Razorpay Failed. ';
508-
509-
$api = $this->getRazorpayApiInstance();
510-
511-
$sessionKey = $this->getSessionKey($order_id);
512-
513-
$attributes = array(
514-
self::RAZORPAY_PAYMENT_ID => $_POST[self::RAZORPAY_PAYMENT_ID],
515-
'razorpay_order_id' => $woocommerce->session->get($sessionKey),
516-
'razorpay_signature' => $_POST['razorpay_signature'],
517-
);
518514

519515
try
520516
{
521-
$api->utility->verifyPaymentSignature($attributes);
522-
517+
$this->verifySignature($orderId);
523518
$success = true;
519+
$razorpayPaymentId = sanitize_text_field($_POST[self::RAZORPAY_PAYMENT_ID]);
524520
}
525521
catch (Errors\SignatureVerificationError $e)
526522
{
527-
$error .= $e->getMessage();
523+
$error = 'WOOCOMMERCE_ERROR: Payment to Razorpay Failed. ' . $e->getMessage();
528524
}
525+
}
526+
else
527+
{
528+
$success = false;
529+
$error = 'Customer cancelled the payment';
529530

530-
if ($success === true)
531-
{
532-
$this->msg['message'] = "Thank you for shopping with us. Your account has been charged and your transaction is successful. We will be processing your order soon. Order Id: $order_id";
533-
$this->msg['class'] = 'success';
534-
$order->payment_complete($attributes[self::RAZORPAY_PAYMENT_ID]);
535-
$order->add_order_note("Razorpay payment successful <br/>Razorpay Id: " . $attributes[self::RAZORPAY_PAYMENT_ID]);
536-
$order->add_order_note($this->msg['message']);
537-
$woocommerce->cart->empty_cart();
538-
}
539-
else
531+
$this->handleErrorCase($order);
532+
}
533+
534+
$this->updateOrder($order, $success, $error, $razorpayPaymentId);
535+
536+
$this->add_notice($this->msg['message'], $this->msg['class']);
537+
$redirectUrl = $this->get_return_url($order);
538+
wp_redirect($redirectUrl);
539+
exit;
540+
}
541+
542+
protected function verifySignature($orderId)
543+
{
544+
global $woocommerce;
545+
546+
$key_id = $this->key_id;
547+
$key_secret = $this->key_secret;
548+
549+
$api = new Api($key_id, $key_secret);
550+
551+
$sessionKey = $this->getSessionKey($orderId);
552+
553+
$attributes = array(
554+
'razorpay_payment_id' => $_POST['razorpay_payment_id'],
555+
'razorpay_order_id' => $woocommerce->session->get($sessionKey),
556+
'razorpay_signature' => $_POST['razorpay_signature'],
557+
);
558+
559+
$api->utility->verifyPaymentSignature($attributes);
560+
}
561+
562+
protected function getErrorMessage($orderId)
563+
{
564+
// We don't have a proper order id
565+
if ($orderId !== null)
566+
{
567+
$message = "An error occured while processing this payment";
568+
}
569+
if (isset($_POST['error']) === true)
570+
{
571+
$error = $_POST['error'];
572+
573+
$message = 'An error occured. Description : ' . $error['description'] . '. Code : ' . $error['code'];
574+
575+
if (isset($error['field']) === true)
540576
{
541-
$this->msg['class'] = 'error';
542-
$this->msg['message'] = 'Thank you for shopping with us. However, the payment failed.';
543-
$order->add_order_note("Transaction Declined: $error<br/>");
544-
$order->add_order_note("Payment Failed. Please check Razorpay Dashboard. <br/> Razorpay Id: " . $attributes[self::RAZORPAY_PAYMENT_ID]);
545-
$order->update_status('failed');
577+
$message .= 'Field : ' . $error['field'];
546578
}
547579
}
548-
// We don't have a proper order id
549580
else
550581
{
551-
if ($order_id !== null)
582+
$message = 'An error occured. Please contact administrator for assistance';
583+
}
584+
585+
return $message;
586+
}
587+
588+
/**
589+
* Modifies existing order and handles success case
590+
*
591+
* @param $success, & $order
592+
*/
593+
protected function updateOrder(& $order, $success, $errorMessage, $razorpayPaymentId)
594+
{
595+
global $woocommerce;
596+
597+
$orderId = $this->getOrderId($order);
598+
599+
if ($success === true)
600+
{
601+
$this->msg['message'] = "Thank you for shopping with us. Your account has been charged and your transaction is successful. We will be processing your order soon. Order Id: $orderId";
602+
$this->msg['class'] = 'success';
603+
604+
$order->payment_complete();
605+
$order->add_order_note("Razorpay payment successful <br/>Razorpay Id: $razorpayPaymentId");
606+
$order->add_order_note($this->msg['message']);
607+
$woocommerce->cart->empty_cart();
608+
}
609+
else
610+
{
611+
$this->msg['class'] = 'error';
612+
$this->msg['message'] = 'Thank you for shopping with us. However, the payment failed.';
613+
614+
if ($razorpayPaymentId)
552615
{
553-
$order = new WC_Order($order_id);
554-
$order->update_status('failed');
555-
$order->add_order_note('Customer cancelled the payment');
616+
$order->add_order_note("Payment Failed. Please check Razorpay Dashboard. <br/> Razorpay Id: $razorpayPaymentId");
556617
}
557-
$this->msg['class'] = 'error';
558-
$this->msg['message'] = "An error occured while processing this payment";
618+
619+
$order->add_order_note("Transaction Failed: $errorMessage<br/>");
620+
$order->update_status('failed');
559621
}
560-
$this->add_notice($this->msg['message'], $this->msg['class']);
561-
$redirectUrl = $this->get_return_url($order);
562-
wp_redirect($redirectUrl);
563-
exit;
622+
}
623+
624+
protected function handleErrorCase(& $order)
625+
{
626+
$orderId = $this->getOrderId($order);
627+
628+
$this->msg['class'] = 'error';
629+
$this->msg['message'] = $this->getErrorMessage($orderId);
564630
}
565631

566632
/**

0 commit comments

Comments
 (0)