1+ <?php namespace App \Controllers ;
2+ require_once (APPPATH ."Libraries/razorpay/razorpay-php/Razorpay.php " );
3+ use Razorpay \Api \Api ;
4+ use Razorpay \Api \Errors \SignatureVerificationError ;
5+ class Home extends BaseController
6+ {
7+
8+ public function index () {
9+ $ data ['page_desc ' ] = 'Pricing ' ;
10+ $ data ['template_file ' ] = 'pricing/pricing ' ;
11+ return $ this ->load_view ($ data );
12+ }
13+ public function buynow ($ price ){
14+
15+ $ api = new Api (RAZOR_KEY , RAZOR_SECRET_KEY );
16+ /**
17+ * You can calculate payment amount as per your logic
18+ * Always set the amount from backend for security reasons
19+ */
20+ $ razorpayPricing = $ api ->order ->create (array (
21+ 'receipt ' => rand (),
22+ 'amount ' => $ price *100 , // 2000 rupees in paise
23+ 'currency ' => 'INR ' ,
24+ 'payment_capture ' => 1 // auto capture
25+ ));
26+
27+ $ amount = $ razorpayPricing ['amount ' ];
28+ $ razorpayOrderId = $ razorpayPricing ['id ' ];
29+
30+ $ postData =['name ' =>'Sample ' ,'email ' =>'sample@email.com ' ,'contact ' =>'9876543210 ' ];
31+ $ pricingDetails = ['title ' =>'Subscription ' ];
32+ $ data = $ this ->prepareData ($ amount ,$ razorpayOrderId ,$ postData ,$ pricingDetails );
33+
34+ //save data in database here;
35+
36+ $ data ['template_file ' ] = 'inc/rezorpay ' ;
37+ $ data ['page_title ' ] = 'Pay Now ' ;
38+ $ data ['page_desc ' ] = 'Pay Now ' ;
39+ $ data ['data ' ] = $ data ;
40+ $ data ['add_custom_js ' ] = "
41+ options.handler = function (response){
42+ document.getElementById('razorpay_payment_id').value = response.razorpay_payment_id;
43+ document.getElementById('razorpay_signature').value = response.razorpay_signature;
44+ document.getElementById('razorpay_order_id').value = ' " .$ razorpayOrderId ."';
45+ document.razorpayform.submit();
46+ };
47+
48+ // Boolean whether to show image inside a white frame. (default: true)
49+ options.theme.image_padding = false;
50+
51+ options.modal = {
52+ ondismiss: function() {
53+ console.log('This code runs when the popup is closed');
54+ },
55+ backdropclose: false
56+ };
57+
58+ var rzp = new Razorpay(options);
59+ $(document).ready(function(e){
60+ console.log('window loaded');
61+ $('#rzp-button1').click();
62+ rzp.open();
63+ event.preventDefault();
64+ });
65+ " ;
66+ return $ this ->load_view ($ data , 'layout ' );
67+
68+ }
69+ /**
70+ * This function verifies the payment,after successful payment
71+ */
72+ public function verify ()
73+ {
74+ //log_message('error','post==='.json_encode($_POST));
75+ $ success = true ;
76+ $ error = "payment_failed " ;
77+ if (empty ($ _POST ['razorpay_payment_id ' ]) === false ) {
78+ $ api = new Api (RAZOR_KEY , RAZOR_SECRET_KEY );
79+ try {
80+ $ attributes = array (
81+ 'razorpay_order_id ' => $ _POST ['razorpay_order_id ' ],
82+ 'razorpay_payment_id ' => $ _POST ['razorpay_payment_id ' ],
83+ 'razorpay_signature ' => $ _POST ['razorpay_signature ' ]
84+ );
85+ $ result = $ api ->utility ->verifyPaymentSignature ($ attributes );
86+ //log_message('error','result==='.json_encode($result));
87+ } catch (SignatureVerificationError $ e ) {
88+ $ success = false ;
89+ $ result = $ e ->getMessage ();
90+ log_message ('error ' ,'error=== ' .json_encode ($ result ));
91+ }
92+ }
93+ if ($ success === true ) {
94+ /**
95+ * Call this function from where ever you want
96+ * to save save data before of after the payment
97+ */
98+ //redirectsuccess
99+ $ this ->setSession (['razorpay_order_id ' =>$ _POST ['razorpay_order_id ' ]]);
100+ return redirect ()->to (base_url ('home/success ' ));
101+ }
102+ else {
103+ //redirect failed
104+ return redirect ()->to (base_url ('home/failed ' ));
105+ }
106+ }
107+
108+ /**
109+ * This function preprares payment parameters
110+ * @param $amount
111+ * @param $razorpayOrderId
112+ * @return array
113+ */
114+ public function prepareData ($ amount ,$ razorpayOrderId ,$ postData ,$ pricingDetails )
115+ {
116+
117+ $ data = array (
118+ "key " => RAZOR_KEY ,
119+ "amount " => $ amount ,
120+ "name " => $ pricingDetails ['title ' ],
121+ "description " => "Pay for subscription " ,
122+ "image " => "" ,
123+ "prefill " => array (
124+ "name " => $ postData ['name ' ],
125+ "email " => $ postData ['email ' ],
126+ "contact " => $ postData ['contact ' ]
127+ ),
128+ "notes " => array (
129+ "address " => "Mohali,India " ,
130+ "merchant_order_id " => rand (),
131+ ),
132+ "theme " => array (
133+ "color " => "#F37254 "
134+ ),
135+ "order_id " => $ razorpayOrderId ,
136+ );
137+ return $ data ;
138+ }
139+ /**
140+ * This is a function called when payment successfull,
141+ * and shows the success message
142+ */
143+ public function success ()
144+ {
145+ if (!$ this ->session ->has ('razorpay_order_id ' )){
146+ return redirect ()->to (base_url ());
147+ }
148+ $ data ['razorpay_order_id ' ] = $ this ->isSession ('razorpay_order_id ' );
149+ $ this ->session ->remove ('razorpay_order_id ' );
150+ $ data ['template_file ' ] = 'pricing/success ' ;
151+ $ data ['page_title ' ] = 'Success ' ;
152+ $ data ['page_desc ' ] = 'Success ' ;
153+ return $ this ->load_view ($ data , 'layout ' );
154+ }
155+ /**
156+ * This is a function called when payment failed,
157+ * and shows the error message
158+ */
159+ public function paymentFailed ()
160+ {
161+ $ data ['template_file ' ] = 'pricing/error ' ;
162+ $ data ['page_title ' ] = 'Success ' ;
163+ $ data ['page_desc ' ] = 'Success ' ;
164+ return $ this ->load_view ($ data , 'layout ' );
165+ }
166+
167+ }
0 commit comments