@@ -231,44 +231,40 @@ public function generate_razorpay_form($orderId)
231
231
*/
232
232
protected function getCheckoutArguments ($ order , $ razorpayOrderId )
233
233
{
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 );
242
237
243
238
$ productinfo = "Order $ orderId " ;
244
239
245
240
$ 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 (
251
246
'woocommerce_order_id ' => $ orderId
252
247
),
253
- 'order_id ' => $ razorpayOrderId
248
+ 'order_id ' => $ razorpayOrderId ,
249
+ 'callback_url ' => $ callbackUrl ,
254
250
);
255
251
256
- $ args ['amount ' ] = $ this ->getOrderAmountAsInteger ($ order );
252
+ $ args ['amount ' ] = $ this ->getOrderAmountAsInteger ($ order );
257
253
258
254
if (version_compare (WOOCOMMERCE_VERSION , '2.7.0 ' , '>= ' ))
259
255
{
260
256
$ 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 (),
264
260
);
265
261
}
266
262
else
267
263
{
268
264
$ 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 ,
272
268
);
273
269
}
274
270
@@ -409,6 +405,16 @@ protected function getOrderKey($order)
409
405
return $ order ->order_key ;
410
406
}
411
407
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
+
412
418
public function process_refund ($ orderId , $ amount = null , $ reason = '' )
413
419
{
414
420
$ order = new WC_Order ($ orderId );
@@ -495,72 +501,132 @@ function check_razorpay_response()
495
501
{
496
502
global $ woocommerce ;
497
503
498
- $ order_id = $ woocommerce ->session ->get (self ::SESSION_KEY );
504
+ $ orderId = $ woocommerce ->session ->get (self ::SESSION_KEY );
499
505
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 );
503
507
504
- $ amount = $ this -> getOrderAmountAsInteger ( $ order ) ;
508
+ $ razorpayPaymentId = null ;
505
509
510
+ if ($ orderId and !empty ($ _POST [self ::RAZORPAY_PAYMENT_ID ]))
511
+ {
512
+ $ error = "" ;
506
513
$ 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
- );
518
514
519
515
try
520
516
{
521
- $ api ->utility ->verifyPaymentSignature ($ attributes );
522
-
517
+ $ this ->verifySignature ($ orderId );
523
518
$ success = true ;
519
+ $ razorpayPaymentId = sanitize_text_field ($ _POST [self ::RAZORPAY_PAYMENT_ID ]);
524
520
}
525
521
catch (Errors \SignatureVerificationError $ e )
526
522
{
527
- $ error .= $ e ->getMessage ();
523
+ $ error = ' WOOCOMMERCE_ERROR: Payment to Razorpay Failed. ' . $ e ->getMessage ();
528
524
}
525
+ }
526
+ else
527
+ {
528
+ $ success = false ;
529
+ $ error = 'Customer cancelled the payment ' ;
529
530
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 )
540
576
{
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 ' ];
546
578
}
547
579
}
548
- // We don't have a proper order id
549
580
else
550
581
{
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 )
552
615
{
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 " );
556
617
}
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 ' );
559
621
}
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 );
564
630
}
565
631
566
632
/**
0 commit comments