-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckout.php
384 lines (347 loc) · 15.9 KB
/
checkout.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
<?php
require('top.php');
if(!isset($_SESSION['cart']) || count($_SESSION['cart'])==0){
?>
<script>
window.location.href='index.php';
</script>
<?php
}
$cart_total=0;
$errMsg="";
$address='';
$city='';
$pincode='';
if(isset($_POST['submit'])){
$address=get_safe_value($con,$_POST['address']);
$city=get_safe_value($con,$_POST['city']);
$pincode=get_safe_value($con,$_POST['pincode']);
$payment_type=get_safe_value($con,$_POST['payment_type']);
$user_id=$_SESSION['USER_ID'];
foreach($_SESSION['cart'] as $key=>$val){
foreach($val as $key1=>$val1) {
$resAttr=mysqli_fetch_assoc(mysqli_query($con,"select price from product_attributes where id='$key1'"));
$price=$resAttr['price'];
$qty=$val1['qty'];
$cart_total=$cart_total+($price*$qty);
}
}
$total_price=$cart_total;
$payment_status='pending';
if($payment_type=='cod'){
$payment_status='success';
}
$order_status='1';
$added_on=date('Y-m-d h:i:s');
$txnid = substr(hash('sha256', mt_rand() . microtime()), 0, 20);
if(isset($_SESSION['COUPON_ID'])){
$coupon_id=$_SESSION['COUPON_ID'];
$coupon_code=$_SESSION['COUPON_CODE'];
$coupon_value=$_SESSION['COUPON_VALUE'];
$total_price=$total_price-$coupon_value;
unset($_SESSION['COUPON_ID']);
unset($_SESSION['COUPON_CODE']);
unset($_SESSION['COUPON_VALUE']);
}else{
$coupon_id='';
$coupon_code='';
$coupon_value='';
}
mysqli_query($con,"insert into `order`(user_id,address,city,pincode,payment_type,payment_status,order_status,added_on,total_price,txnid,coupon_id,coupon_code,coupon_value) values('$user_id','$address','$city','$pincode','$payment_type','$payment_status','$order_status','$added_on','$total_price','$txnid','$coupon_id','$coupon_code','$coupon_value')");
$order_id=mysqli_insert_id($con);
foreach($_SESSION['cart'] as $key=>$val){
foreach($val as $key1=>$val1) {
$resAttr=mysqli_fetch_assoc(mysqli_query($con,"select price from product_attributes where id='$key1'"));
$price=$resAttr['price'];
$qty=$val1['qty'];
mysqli_query($con,"insert into `order_detail`(order_id,product_id,product_attr_id,qty,price) values('$order_id','$key','$key1','$qty','$price')");
}
}
if($payment_type=='instamojo'){
$userArr=mysqli_fetch_assoc(mysqli_query($con,"select * from users where id='$user_id'"));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://test.instamojo.com/api/1.1/payment-requests/');
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array("X-Api-Key:".INSTAMOJO_KEY,"X-Auth-Token:".INSTAMOJO_TOKEN)
);
$payload = Array(
'purpose' => 'Buy Product',
'amount' => $total_price,
'phone' => $userArr['mobile'],
'buyer_name' => $userArr['name'],
'redirect_url' => INSTAMOJO_REDIRECT,
'send_email' => false,
'send_sms' => false,
'email' => $userArr['email'],
'allow_repeated_payments' => false
);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($payload));
$response = curl_exec($ch);
curl_close($ch);
$response=json_decode($response);
if(isset($response->payment_request->id)){
unset($_SESSION['cart']);
$_SESSION['TID']=$response->payment_request->id;
mysqli_query($con,"update `order` set txnid='".$response->payment_request->id."' where id='".$order_id."'");
?>
<script>
window.location.href='<?php echo $response->payment_request->longurl?>';
</script>
<?php
}else{
if(isset($response->message)){
$errMsg.="<div class='instamojo_error'>";
foreach($response->message as $key=>$val){
$errMsg.=strtoupper($key).' : '.$val[0].'<br/>';
}
$errMsg.="</div>";
}else{
echo "Something went wrong";
}
}
}else{
sentInvoice($con,$order_id);
$obj=new add_to_cart();
$obj->emptyProduct();
?>
<script>
window.location.href='thank_you.php';
</script>
<?php
}
}
?>
<div class="ht__bradcaump__area" style="background: rgba(0, 0, 0, 0) url(images/bg/4.jpg) no-repeat scroll center center / cover ;">
<div class="ht__bradcaump__wrap">
<div class="container">
<div class="row">
<div class="col-xs-12">
<div class="bradcaump__inner">
<nav class="bradcaump-inner">
<a class="breadcrumb-item" href="index.php">Home</a>
<span class="brd-separetor"><i class="zmdi zmdi-chevron-right"></i></span>
<span class="breadcrumb-item active">checkout</span>
</nav>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- End Bradcaump area -->
<!-- cart-main-area start -->
<div class="checkout-wrap ptb--100">
<div class="container">
<div class="row">
<div class="col-md-8">
<?php echo $errMsg?>
<div class="checkout__inner">
<div class="accordion-list">
<div class="accordion">
<?php
$accordion_class='accordion__title';
if(!isset($_SESSION['USER_LOGIN'])){
$accordion_class='accordion__hide';
?>
<div class="accordion__title">
Checkout Method
</div>
<div class="accordion__body">
<div class="accordion__body__form">
<div class="row">
<div class="col-md-6">
<div class="checkout-method__login">
<form id="login-form" method="post">
<h5 class="checkout-method__title">Login</h5>
<div class="single-input">
<input type="text" name="login_email" id="login_email" placeholder="Your Email*" style="width:100%">
<span class="field_error" id="login_email_error"></span>
</div>
<div class="single-input">
<input type="password" name="login_password" id="login_password" placeholder="Your Password*" style="width:100%">
<span class="field_error" id="login_password_error"></span>
</div>
<p class="require">* Required fields</p>
<div class="dark-btn">
<button type="button" class="fv-btn" onclick="user_login()">Login</button>
</div>
<div class="form-output login_msg">
<p class="form-messege field_error"></p>
</div>
</form>
</div>
</div>
<div class="col-md-6">
<div class="checkout-method__login">
<form action="#">
<h5 class="checkout-method__title">Register</h5>
<div class="single-input">
<input type="text" name="name" id="name" placeholder="Your Name*" style="width:100%">
<span class="field_error" id="name_error"></span>
</div>
<div class="single-input">
<input type="text" name="email" id="email" placeholder="Your Email*" style="width:100%">
<span class="field_error" id="email_error"></span>
</div>
<div class="single-input">
<input type="text" name="mobile" id="mobile" placeholder="Your Mobile*" style="width:100%">
<span class="field_error" id="mobile_error"></span>
</div>
<div class="single-input">
<input type="password" name="password" id="password" placeholder="Your Password*" style="width:100%">
<span class="field_error" id="password_error"></span>
</div>
<div class="dark-btn">
<button type="button" class="fv-btn" onclick="user_register()">Register</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<?php }else{
$lastOrderDetailsRes=mysqli_query($con,"select address,city,pincode from `order` where user_id='".$_SESSION['USER_ID']."'");
if(mysqli_num_rows($lastOrderDetailsRes)>0){
$lastOrderDetailsRow=mysqli_fetch_assoc($lastOrderDetailsRes);
$address=$lastOrderDetailsRow['address'];
$city=$lastOrderDetailsRow['city'];
$pincode=$lastOrderDetailsRow['pincode'];
}
}
?>
<div class="<?php echo $accordion_class?>">
Address Information
</div>
<form method="post">
<div class="accordion__body">
<div class="bilinfo">
<div class="row">
<div class="col-md-12">
<div class="single-input">
<input type="text" name="address" placeholder="Street Address" required value="<?php echo $address?>">
</div>
</div>
<div class="col-md-6">
<div class="single-input">
<input type="text" name="city" placeholder="City/State" required value="<?php echo $city?>">
</div>
</div>
<div class="col-md-6">
<div class="single-input">
<input type="text" name="pincode" placeholder="Post code/ zip" required value="<?php echo $pincode?>">
</div>
</div>
</div>
</div>
</div>
<div class="<?php echo $accordion_class?>">
payment information
</div>
<div class="accordion__body">
<div class="paymentinfo">
<div class="single-method">
COD <input type="radio" name="payment_type" value="COD" required/>
Card<input type="radio" name="payment_type" value="instamojo" required/>
</div>
<div class="single-method">
</div>
</div>
</div>
<input type="submit" name="submit" class="fv-btn"/>
</form>
</div>
</div>
</div>
</div>
<div class="col-md-4">
<div class="order-details">
<h5 class="order-details__title">Your Order</h5>
<div class="order-details__item">
<?php
$cart_total=0;
foreach($_SESSION['cart'] as $key=>$val){
//$productArr=get_product($con,'','',$key);
//prx($productArr);
foreach($val as $key1=>$val1){
$resAttr=mysqli_fetch_assoc(mysqli_query($con,"select product_attributes.*,color_master.color,rom_master.size from product_attributes
left join color_master on product_attributes.color_id=color_master.id and color_master.status=1
left join rom_master on product_attributes.size_id=rom_master.id and rom_master.status=1
where product_attributes.id='$key1'"));
$productArr=get_product($con,'','',$key,'','','','',$key1);
$pname=$productArr[0]['name'];
$mrp=$productArr[0]['mrp'];
$price=$productArr[0]['price'];
$image=$productArr[0]['image'];
$qty=$val1['qty'];
$cart_total=$cart_total+($price*$qty);
?>
<div class="single-item">
<div class="single-item__thumb">
<img src="<?php echo PRODUCT_IMAGE_SITE_PATH.$image?>" />
</div>
<div class="single-item__content">
<a href="#"><?php echo $pname?> * <?php echo $qty?></a>
<span class="price"><?php echo $price*$qty?></span>
</div>
<div class="single-item__remove">
<a href="javascript:void(0)" onclick="manage_cart('<?php echo $key?>','remove')"><i class="icon-trash icons"></i></a>
</div>
</div>
<?php } } ?>
</div>
<div class="ordre-details__total" id="coupon_box">
<h5>Coupon Value</h5>
<span class="price" id="coupon_price"></span>
</div>
<div class="ordre-details__total">
<h5>Order total</h5>
<span class="price" id="order_total_price"><?php echo $cart_total?></span>
</div>
<div class="ordre-details__total bilinfo">
<input type="textbox" id="coupon_str" class="coupon_style mr5"/> <input type="button" name="submit" class="fv-btn coupon_style" value="Apply Coupon" onclick="set_coupon()"/>
</div>
<div id="coupon_result"></div>
</div>
</div>
</div>
</div>
</div>
<script>
function set_coupon(){
var coupon_str=jQuery('#coupon_str').val();
if(coupon_str!=''){
jQuery('#coupon_result').html('');
jQuery.ajax({
url:'set_coupon.php',
type:'post',
data:'coupon_str='+coupon_str,
success:function(result){
var data=jQuery.parseJSON(result);
if(data.is_error=='yes'){
jQuery('#coupon_box').hide();
jQuery('#coupon_result').html(data.dd);
jQuery('#order_total_price').html(data.result);
}
if(data.is_error=='no'){
jQuery('#coupon_box').show();
jQuery('#coupon_price').html(data.dd);
jQuery('#order_total_price').html(data.result);
}
}
});
}
}
</script>
<?php
if(isset($_SESSION['COUPON_ID'])){
unset($_SESSION['COUPON_ID']);
unset($_SESSION['COUPON_CODE']);
unset($_SESSION['COUPON_VALUE']);
}
require('footer.php');
?>