forked from razorpay/razorpay-woocommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
woo-razorpay.php
979 lines (822 loc) · 32.5 KB
/
woo-razorpay.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
<?php
/*
* Plugin Name: Razorpay for WooCommerce
* Plugin URI: https://razorpay.com
* Description: Razorpay Payment Gateway Integration for WooCommerce
* Version: 2.4.1
* Stable tag: 2.4.1
* Author: Team Razorpay
* WC tested up to: 3.7.1
* Author URI: https://razorpay.com
*/
if ( ! defined( 'ABSPATH' ) )
{
exit; // Exit if accessed directly
}
require_once __DIR__.'/includes/razorpay-webhook.php';
require_once __DIR__.'/razorpay-sdk/Razorpay.php';
require_once ABSPATH . 'wp-admin/includes/plugin.php';
use Razorpay\Api\Api;
use Razorpay\Api\Errors;
add_action('plugins_loaded', 'woocommerce_razorpay_init', 0);
add_action('admin_post_nopriv_rzp_wc_webhook', 'razorpay_webhook_init', 10);
function woocommerce_razorpay_init()
{
if (!class_exists('WC_Payment_Gateway'))
{
return;
}
class WC_Razorpay extends WC_Payment_Gateway
{
// This one stores the WooCommerce Order Id
const SESSION_KEY = 'razorpay_wc_order_id';
const RAZORPAY_PAYMENT_ID = 'razorpay_payment_id';
const RAZORPAY_ORDER_ID = 'razorpay_order_id';
const RAZORPAY_SIGNATURE = 'razorpay_signature';
const RAZORPAY_WC_FORM_SUBMIT = 'razorpay_wc_form_submit';
const INR = 'INR';
const CAPTURE = 'capture';
const AUTHORIZE = 'authorize';
const WC_ORDER_ID = 'woocommerce_order_id';
const DEFAULT_LABEL = 'Credit Card/Debit Card/NetBanking';
const DEFAULT_DESCRIPTION = 'Pay securely by Credit or Debit card or Internet Banking through Razorpay.';
const DEFAULT_SUCCESS_MESSAGE = 'Thank you for shopping with us. Your account has been charged and your transaction is successful. We will be processing your order soon.';
protected $visibleSettings = array(
'enabled',
'title',
'description',
'key_id',
'key_secret',
'payment_action',
'order_success_message',
'enable_webhook',
'webhook_secret',
);
public $form_fields = array();
public $supports = array(
'products',
'refunds'
);
/**
* Can be set to true if you want payment fields
* to show on the checkout (if doing a direct integration).
* @var boolean
*/
public $has_fields = false;
/**
* Unique ID for the gateway
* @var string
*/
public $id = 'razorpay';
/**
* Title of the payment method shown on the admin page.
* @var string
*/
public $method_title = 'Razorpay';
/**
* Description of the payment method shown on the admin page.
* @var string
*/
public $method_description = 'Allow customers to securely pay via Razorpay (Credit/Debit Cards, NetBanking, UPI, Wallets)';
/**
* Icon URL, set in constructor
* @var string
*/
public $icon;
/**
* TODO: Remove usage of $this->msg
*/
protected $msg = array(
'message' => '',
'class' => '',
);
/**
* Return Wordpress plugin settings
* @param string $key setting key
* @return mixed setting value
*/
public function getSetting($key)
{
return $this->settings[$key];
}
protected function getCustomOrdercreationMessage()
{
$message = $this->getSetting('order_success_message');
if (isset($message) === false)
{
$message = STATIC::DEFAULT_SUCCESS_MESSAGE;
}
return $message;
}
/**
* @param boolean $hooks Whether or not to
* setup the hooks on
* calling the constructor
*/
public function __construct($hooks = true)
{
$this->icon = plugins_url('images/logo.png' , __FILE__);
$this->init_form_fields();
$this->init_settings();
// TODO: This is hacky, find a better way to do this
// See mergeSettingsWithParentPlugin() in subscriptions for more details.
if ($hooks)
{
$this->initHooks();
}
$this->title = $this->getSetting('title');
}
protected function initHooks()
{
add_action('init', array(&$this, 'check_razorpay_response'));
add_action('woocommerce_receipt_' . $this->id, array($this, 'receipt_page'));
add_action('woocommerce_api_' . $this->id, array($this, 'check_razorpay_response'));
$cb = array($this, 'process_admin_options');
if (version_compare(WOOCOMMERCE_VERSION, '2.0.0', '>='))
{
add_action("woocommerce_update_options_payment_gateways_{$this->id}", $cb);
}
else
{
add_action('woocommerce_update_options_payment_gateways', $cb);
}
}
public function init_form_fields()
{
$webhookUrl = esc_url(admin_url('admin-post.php')) . '?action=rzp_wc_webhook';
$defaultFormFields = array(
'enabled' => array(
'title' => __('Enable/Disable', $this->id),
'type' => 'checkbox',
'label' => __('Enable this module?', $this->id),
'default' => 'yes'
),
'title' => array(
'title' => __('Title', $this->id),
'type'=> 'text',
'description' => __('This controls the title which the user sees during checkout.', $this->id),
'default' => __(static::DEFAULT_LABEL, $this->id)
),
'description' => array(
'title' => __('Description', $this->id),
'type' => 'textarea',
'description' => __('This controls the description which the user sees during checkout.', $this->id),
'default' => __(static::DEFAULT_DESCRIPTION, $this->id)
),
'key_id' => array(
'title' => __('Key ID', $this->id),
'type' => 'text',
'description' => __('The key Id and key secret can be generated from "API Keys" section of Razorpay Dashboard. Use test or live for test or live mode.', $this->id)
),
'key_secret' => array(
'title' => __('Key Secret', $this->id),
'type' => 'text',
'description' => __('The key Id and key secret can be generated from "API Keys" section of Razorpay Dashboard. Use test or live for test or live mode.', $this->id)
),
'payment_action' => array(
'title' => __('Payment Action', $this->id),
'type' => 'select',
'description' => __('Payment action on order compelete', $this->id),
'default' => self::CAPTURE,
'options' => array(
self::AUTHORIZE => 'Authorize',
self::CAPTURE => 'Authorize and Capture'
)
),
'order_success_message' => array(
'title' => __('Order Completion Message', $this->id),
'type' => 'textarea',
'description' => __('Message to be displayed after a successful order', $this->id),
'default' => __(STATIC::DEFAULT_SUCCESS_MESSAGE, $this->id),
),
'enable_webhook' => array(
'title' => __('Enable Webhook', $this->id),
'type' => 'checkbox',
'description' => "<span>$webhookUrl</span><br/><br/>Instructions and guide to <a href='https://github.com/razorpay/razorpay-woocommerce/wiki/Razorpay-Woocommerce-Webhooks'>Razorpay webhooks</a>",
'label' => __('Enable Razorpay Webhook <a href="https://dashboard.razorpay.com/#/app/webhooks">here</a> with the URL listed below.', $this->id),
'default' => 'no'
),
'webhook_secret' => array(
'title' => __('Webhook Secret', $this->id),
'type' => 'text',
'description' => __('Webhook secret is used for webhook signature verification. This has to match the one added <a href="https://dashboard.razorpay.com/#/app/webhooks">here</a>', $this->id),
'default' => ''
),
);
foreach ($defaultFormFields as $key => $value)
{
if (in_array($key, $this->visibleSettings, true))
{
$this->form_fields[$key] = $value;
}
}
}
public function admin_options()
{
echo '<h3>'.__('Razorpay Payment Gateway', $this->id) . '</h3>';
echo '<p>'.__('Allows payments by Credit/Debit Cards, NetBanking, UPI, and multiple Wallets') . '</p>';
echo '<table class="form-table">';
// Generate the HTML For the settings form.
$this->generate_settings_html();
echo '</table>';
}
public function get_description()
{
return $this->getSetting('description');
}
/**
* Receipt Page
* @param string $orderId WC Order Id
**/
function receipt_page($orderId)
{
echo $this->generate_razorpay_form($orderId);
}
/**
* Returns key to use in session for storing Razorpay order Id
* @param string $orderId Razorpay Order Id
* @return string Session Key
*/
protected function getOrderSessionKey($orderId)
{
return self::RAZORPAY_ORDER_ID . $orderId;
}
/**
* Given a order Id, find the associated
* Razorpay Order from the session and verify
* that is is still correct. If not found
* (or incorrect), create a new Razorpay Order
*
* @param string $orderId Order Id
* @return mixed Razorpay Order Id or Exception
*/
protected function createOrGetRazorpayOrderId($orderId)
{
global $woocommerce;
$sessionKey = $this->getOrderSessionKey($orderId);
$create = false;
try
{
$razorpayOrderId = $woocommerce->session->get($sessionKey);
// If we don't have an Order
// or the if the order is present in session but doesn't match what we have saved
if (($razorpayOrderId === null) or
(($razorpayOrderId and ($this->verifyOrderAmount($razorpayOrderId, $orderId)) === false)))
{
$create = true;
}
else
{
return $razorpayOrderId;
}
}
// Order doesn't exist or verification failed
// So try creating one
catch (Exception $e)
{
$create = true;
}
if ($create)
{
try
{
return $this->createRazorpayOrderId($orderId, $sessionKey);
}
// For the bad request errors, it's safe to show the message to the customer.
catch (Errors\BadRequestError $e)
{
return $e;
}
// For any other exceptions, we make sure that the error message
// does not propagate to the front-end.
catch (Exception $e)
{
return new Exception("Payment failed");
}
}
}
/**
* Returns redirect URL post payment processing
* @return string redirect URL
*/
private function getRedirectUrl()
{
return get_site_url() . '/wc-api/' . $this->id;
}
/**
* Specific payment parameters to be passed to checkout
* for payment processing
* @param string $orderId WC Order Id
* @return array payment params
*/
protected function getRazorpayPaymentParams($orderId)
{
$razorpayOrderId = $this->createOrGetRazorpayOrderId($orderId);
if ($razorpayOrderId === null)
{
throw new Exception('RAZORPAY ERROR: Razorpay API could not be reached');
}
else if ($razorpayOrderId instanceof Exception)
{
$message = $razorpayOrderId->getMessage();
throw new Exception("RAZORPAY ERROR: Order creation failed with the message: '$message'.");
}
return [
'order_id' => $razorpayOrderId
];
}
/**
* Generate razorpay button link
* @param string $orderId WC Order Id
**/
public function generate_razorpay_form($orderId)
{
$order = new WC_Order($orderId);
try
{
$params = $this->getRazorpayPaymentParams($orderId);
}
catch (Exception $e)
{
return $e->getMessage();
}
$checkoutArgs = $this->getCheckoutArguments($order, $params);
$html = '<p>'.__('Thank you for your order, please click the button below to pay with Razorpay.', $this->id).'</p>';
$html .= $this->generateOrderForm($checkoutArgs);
return $html;
}
/**
* default parameters passed to checkout
* @param WC_Order $order WC Order
* @return array checkout params
*/
private function getDefaultCheckoutArguments($order)
{
$callbackUrl = $this->getRedirectUrl();
$orderId = $order->get_order_number();
$productinfo = "Order $orderId";
$mod_version = get_plugin_data(plugin_dir_path(__FILE__) . 'woo-razorpay.php')['Version'];
return array(
'key' => $this->getSetting('key_id'),
'name' => get_bloginfo('name'),
'currency' => self::INR,
'description' => $productinfo,
'notes' => array(
'woocommerce_order_id' => $orderId
),
'callback_url' => $callbackUrl,
'prefill' => $this->getCustomerInfo($order),
'_' => array(
'integration' => 'woocommerce',
'integration_version' => $mod_version,
'integration_parent_version' => WOOCOMMERCE_VERSION,
),
);
}
/**
* @param WC_Order $order
* @return string currency
*/
private function getOrderCurrency($order)
{
if (version_compare(WOOCOMMERCE_VERSION, '2.7.0', '>='))
{
return $order->get_currency();
}
return $order->get_order_currency();
}
/**
* Returns array of checkout params
*/
private function getCheckoutArguments($order, $params)
{
$args = $this->getDefaultCheckoutArguments($order);
$currency = $this->getOrderCurrency($order);
// The list of valid currencies is at https://razorpay.freshdesk.com/support/solutions/articles/11000065530-what-currencies-does-razorpay-support-
$args = array_merge($args, $params);
return $args;
}
public function getCustomerInfo($order)
{
if (version_compare(WOOCOMMERCE_VERSION, '2.7.0', '>='))
{
$args = array(
'name' => $order->get_billing_first_name() . ' ' . $order->get_billing_last_name(),
'email' => $order->get_billing_email(),
'contact' => $order->get_billing_phone(),
);
}
else
{
$args = array(
'name' => $order->billing_first_name . ' ' . $order->billing_last_name,
'email' => $order->billing_email,
'contact' => $order->billing_phone,
);
}
return $args;
}
protected function createRazorpayOrderId($orderId, $sessionKey)
{
// Calls the helper function to create order data
global $woocommerce;
$api = $this->getRazorpayApiInstance();
$data = $this->getOrderCreationData($orderId);
try
{
$razorpayOrder = $api->order->create($data);
}
catch (Exception $e)
{
return $e;
}
$razorpayOrderId = $razorpayOrder['id'];
$woocommerce->session->set($sessionKey, $razorpayOrderId);
//update it in order comments
$order = new WC_Order($orderId);
$order->add_order_note("Razorpay OrderId: $razorpayOrderId");
return $razorpayOrderId;
}
protected function verifyOrderAmount($razorpayOrderId, $orderId)
{
$order = new WC_Order($orderId);
$api = $this->getRazorpayApiInstance();
try
{
$razorpayOrder = $api->order->fetch($razorpayOrderId);
}
catch (Exception $e)
{
$message = $e->getMessage();
return "RAZORPAY ERROR: Order fetch failed with the message '$message'";
}
$orderCreationData = $this->getOrderCreationData($orderId);
$razorpayOrderArgs = array(
'id' => $razorpayOrderId,
'amount' => $orderCreationData['amount'],
'currency' => $orderCreationData['currency'],
'receipt' => (string) $orderId,
);
$orderKeys = array_keys($razorpayOrderArgs);
foreach ($orderKeys as $key)
{
if ($razorpayOrderArgs[$key] !== $razorpayOrder[$key])
{
return false;
}
}
return true;
}
private function getOrderCreationData($orderId)
{
$order = new WC_Order($orderId);
$data = array(
'receipt' => $orderId,
'amount' => (int) round($order->get_total() * 100),
'currency' => $this->getOrderCurrency($order),
'payment_capture' => ($this->getSetting('payment_action') === self::AUTHORIZE) ? 0 : 1,
'notes' => array(
self::WC_ORDER_ID => (string) $orderId,
),
);
return $data;
}
private function enqueueCheckoutScripts($data)
{
if($data === 'checkoutForm')
{
wp_register_script('razorpay_wc_script', plugin_dir_url(__FILE__) . 'script.js',
null, null);
}
else
{
wp_register_script('razorpay_wc_script', plugin_dir_url(__FILE__) . 'script.js',
array('razorpay_checkout'));
wp_register_script('razorpay_checkout',
'https://checkout.razorpay.com/v1/checkout.js',
null, null);
}
wp_localize_script('razorpay_wc_script',
'razorpay_wc_checkout_vars',
$data
);
wp_enqueue_script('razorpay_wc_script');
}
private function hostCheckoutScripts($data)
{
$url = Api::getFullUrl("checkout/embedded");
$formFields = "";
foreach ($data as $fieldKey => $val) {
if(in_array($fieldKey, array('notes', 'prefill', '_')))
{
foreach ($data[$fieldKey] as $field => $fieldVal) {
$formFields .= "<input type='hidden' name='$fieldKey" ."[$field]"."' value='$fieldVal'> \n";
}
}
}
return '<form method="POST" action="'.$url.'" id="checkoutForm">
<input type="hidden" name="key_id" value="'.$data['key'].'">
<input type="hidden" name="order_id" value="'.$data['order_id'].'">
<input type="hidden" name="name" value="'.$data['name'].'">
<input type="hidden" name="description" value="'.$data['description'].'">
<input type="hidden" name="image" value="'.$data['preference']['image'].'">
<input type="hidden" name="callback_url" value="'.$data['callback_url'].'">
<input type="hidden" name="cancel_url" value="'.$data['cancel_url'].'">
'. $formFields .'
</form>';
}
/**
* Generates the order form
**/
function generateOrderForm($data)
{
$redirectUrl = $this->getRedirectUrl();
$data['cancel_url'] = wc_get_checkout_url();
$api = new Api($this->getSetting('key_id'),"");
$merchantPreferences = $api->request->request("GET", "preferences");
if(isset($merchantPreferences['options']['redirect']) && $merchantPreferences['options']['redirect'] === true)
{
$this->enqueueCheckoutScripts('checkoutForm');
$data['preference']['image'] = $merchantPreferences['options']['image'];
return $this->hostCheckoutScripts($data);
} else {
$this->enqueueCheckoutScripts($data);
return <<<EOT
<form name='razorpayform' action="$redirectUrl" method="POST">
<input type="hidden" name="razorpay_payment_id" id="razorpay_payment_id">
<input type="hidden" name="razorpay_signature" id="razorpay_signature" >
<!-- This distinguishes all our various wordpress plugins -->
<input type="hidden" name="razorpay_wc_form_submit" value="1">
</form>
<p id="msg-razorpay-success" class="woocommerce-info woocommerce-message" style="display:none">
Please wait while we are processing your payment.
</p>
<p>
<button id="btn-razorpay">Pay Now</button>
<button id="btn-razorpay-cancel" onclick="document.razorpayform.submit()">Cancel</button>
</p>
EOT;
}
}
/**
* Gets the Order Key from the Order
* for all WC versions that we suport
*/
protected function getOrderKey($order)
{
$orderKey = null;
if (version_compare(WOOCOMMERCE_VERSION, '3.0.0', '>='))
{
return $order->get_order_key();
}
return $order->order_key;
}
public function process_refund($orderId, $amount = null, $reason = '')
{
$order = new WC_Order($orderId);
if (! $order or ! $order->get_transaction_id())
{
return new WP_Error('error', __('Refund failed: No transaction ID', 'woocommerce'));
}
$client = $this->getRazorpayApiInstance();
$paymentId = $order->get_transaction_id();
$data = array(
'amount' => (int) round($amount * 100),
'notes' => array(
'reason' => $reason,
'order_id' => $orderId
)
);
try
{
$refund = $client->payment
->fetch($paymentId)
->refund($data);
$order->add_order_note(__( 'Refund Id: ' . $refund->id, 'woocommerce' ));
return true;
}
catch(Exception $e)
{
return new WP_Error('error', __($e->getMessage(), 'woocommerce'));
}
}
/**
* Process the payment and return the result
**/
function process_payment($order_id)
{
global $woocommerce;
$order = new WC_Order($order_id);
$woocommerce->session->set(self::SESSION_KEY, $order_id);
$orderKey = $this->getOrderKey($order);
if (version_compare(WOOCOMMERCE_VERSION, '2.1', '>='))
{
return array(
'result' => 'success',
'redirect' => add_query_arg('key', $orderKey, $order->get_checkout_payment_url(true))
);
}
else if (version_compare(WOOCOMMERCE_VERSION, '2.0.0', '>='))
{
return array(
'result' => 'success',
'redirect' => add_query_arg('order', $order->get_id(),
add_query_arg('key', $orderKey, $order->get_checkout_payment_url(true)))
);
}
else
{
return array(
'result' => 'success',
'redirect' => add_query_arg('order', $order->get_id(),
add_query_arg('key', $orderKey, get_permalink(get_option('woocommerce_pay_page_id'))))
);
}
}
public function getRazorpayApiInstance()
{
return new Api($this->getSetting('key_id'), $this->getSetting('key_secret'));
}
/**
* Check for valid razorpay server callback
**/
function check_razorpay_response()
{
global $woocommerce;
$orderId = $woocommerce->session->get(self::SESSION_KEY);
$order = new WC_Order($orderId);
//
// If the order has already been paid for
// redirect user to success page
//
if ($order->needs_payment() === false)
{
$this->redirectUser($order);
}
$razorpayPaymentId = null;
if ($orderId and !empty($_POST[self::RAZORPAY_PAYMENT_ID]))
{
$error = "";
$success = false;
try
{
$this->verifySignature($orderId);
$success = true;
$razorpayPaymentId = sanitize_text_field($_POST[self::RAZORPAY_PAYMENT_ID]);
}
catch (Errors\SignatureVerificationError $e)
{
$error = 'WOOCOMMERCE_ERROR: Payment to Razorpay Failed. ' . $e->getMessage();
}
}
else
{
if($_POST[self::RAZORPAY_WC_FORM_SUBMIT] ==1)
{
$success = false;
$error = 'Customer cancelled the payment';
}
else
{
$success = false;
$error = "Payment Failed.";
}
$this->handleErrorCase($order);
$this->updateOrder($order, $success, $error, $razorpayPaymentId);
wp_redirect(wc_get_checkout_url());
exit;
}
$this->updateOrder($order, $success, $error, $razorpayPaymentId);
$this->redirectUser($order);
}
protected function redirectUser($order)
{
$redirectUrl = $this->get_return_url($order);
wp_redirect($redirectUrl);
exit;
}
protected function verifySignature($orderId)
{
global $woocommerce;
$api = $this->getRazorpayApiInstance();
$attributes = array(
self::RAZORPAY_PAYMENT_ID => $_POST[self::RAZORPAY_PAYMENT_ID],
self::RAZORPAY_SIGNATURE => $_POST[self::RAZORPAY_SIGNATURE],
);
$sessionKey = $this->getOrderSessionKey($orderId);
$attributes[self::RAZORPAY_ORDER_ID] = $woocommerce->session->get($sessionKey);
$api->utility->verifyPaymentSignature($attributes);
}
protected function getErrorMessage($orderId)
{
// We don't have a proper order id
if ($orderId !== null)
{
$message = 'An error occured while processing this payment';
}
if (isset($_POST['error']) === true)
{
$error = $_POST['error'];
$description = htmlentities($error['description']);
$code = htmlentities($error['code']);
$message = 'An error occured. Description : ' . $description . '. Code : ' . $code;
if (isset($error['field']) === true)
{
$fieldError = htmlentities($error['field']);
$message .= 'Field : ' . $fieldError;
}
}
else
{
$message = 'An error occured. Please contact administrator for assistance';
}
return $message;
}
/**
* Modifies existing order and handles success case
*
* @param $success, & $order
*/
public function updateOrder(& $order, $success, $errorMessage, $razorpayPaymentId, $webhook = false)
{
global $woocommerce;
$orderId = $order->get_order_number();
if (($success === true) and ($order->needs_payment() === true))
{
$this->msg['message'] = $this->getCustomOrdercreationMessage() . " Order Id: $orderId";
$this->msg['class'] = 'success';
$order->payment_complete($razorpayPaymentId);
$order->add_order_note("Razorpay payment successful <br/>Razorpay Id: $razorpayPaymentId");
if (isset($woocommerce->cart) === true)
{
$woocommerce->cart->empty_cart();
}
}
else
{
$this->msg['class'] = 'error';
$this->msg['message'] = $errorMessage;
if ($razorpayPaymentId)
{
$order->add_order_note("Payment Failed. Please check Razorpay Dashboard. <br/> Razorpay Id: $razorpayPaymentId");
}
$order->add_order_note("Transaction Failed: $errorMessage<br/>");
$order->update_status('failed');
}
if ($webhook === false)
{
$this->add_notice($this->msg['message'], $this->msg['class']);
}
}
protected function handleErrorCase(& $order)
{
$orderId = $order->get_order_number();
$this->msg['class'] = 'error';
$this->msg['message'] = $this->getErrorMessage($orderId);
}
/**
* Add a woocommerce notification message
*
* @param string $message Notification message
* @param string $type Notification type, default = notice
*/
protected function add_notice($message, $type = 'notice')
{
global $woocommerce;
$type = in_array($type, array('notice','error','success'), true) ? $type : 'notice';
// Check for existence of new notification api. Else use previous add_error
if (function_exists('wc_add_notice'))
{
wc_add_notice($message, $type);
}
else
{
// Retrocompatibility WooCommerce < 2.1
switch ($type)
{
case "error" :
$woocommerce->add_error($message);
break;
default :
$woocommerce->add_message($message);
break;
}
}
}
}
/**
* Add the Gateway to WooCommerce
**/
function woocommerce_add_razorpay_gateway($methods)
{
$methods[] = 'WC_Razorpay';
return $methods;
}
add_filter('woocommerce_payment_gateways', 'woocommerce_add_razorpay_gateway' );
}
// This is set to a priority of 10
function razorpay_webhook_init()
{
$rzpWebhook = new RZP_Webhook();
$rzpWebhook->process();
}