-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckoutProcess.php
More file actions
236 lines (204 loc) · 9.34 KB
/
checkoutProcess.php
File metadata and controls
236 lines (204 loc) · 9.34 KB
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
<?php
session_start();
include_once("mypaypal.php");
include_once("mysql_conn.php");
$MainContent = "";
if($_POST) //Post Data received from Shopping cart page.
{
// To Do 6 (DIY): Check to ensure each product item saved in the associative
// array is not out of stock
foreach($_SESSION['Items'] as $key=>$item) {
$qry = "SELECT * FROM product WHERE ProductID =?" ;
$stmt = $conn->prepare($qry);
$stmt->bind_param("i", $item['productId']);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_array();
if($row["Quantity"]<$item['quantity']){
$MainContent .= "Product $item[productId] : $item[name] is out of stock! <br />";
$MainContent .= "Please return to shopping cart to amend your purchase<br />";
include("MasterTemplate.php");
exit;
}
}
// End of To Do 6
$paypal_data = '';
// Get all items from the shopping cart, concatenate to the variable $paypal_data
// $_SESSION['Items'] is an associative array
foreach($_SESSION['Items'] as $key=>$item) {
$paypal_data .= '&L_PAYMENTREQUEST_0_QTY'.$key.'='.urlencode($item["quantity"]);
$paypal_data .= '&L_PAYMENTREQUEST_0_AMT'.$key.'='.urlencode($item["price"]);
$paypal_data .= '&L_PAYMENTREQUEST_0_NAME'.$key.'='.urlencode($item["name"]);
$paypal_data .= '&L_PAYMENTREQUEST_0_NUMBER'.$key.'='.urlencode($item["productId"]);
}
//Data to be sent to PayPal
$padata = '&CURRENCYCODE='.urlencode($PayPalCurrencyCode).
'&PAYMENTACTION=Sale'.
'&ALLOWNOTE=1'.
'&PAYMENTREQUEST_0_CURRENCYCODE='.urlencode($PayPalCurrencyCode).
'&PAYMENTREQUEST_0_AMT='.urlencode($_SESSION["SubTotal"] +
$_SESSION["Tax"] +
$_SESSION["ShipCharge"]).
'&PAYMENTREQUEST_0_ITEMAMT='.urlencode($_SESSION["SubTotal"]).
'&PAYMENTREQUEST_0_SHIPPINGAMT='.urlencode($_SESSION["ShipCharge"]).
'&PAYMENTREQUEST_0_TAXAMT='.urlencode($_SESSION["Tax"]).
'&BRANDNAME='.urlencode("Giftany").
$paypal_data.
'&RETURNURL='.urlencode($PayPalReturnURL ).
'&CANCELURL='.urlencode($PayPalCancelURL);
//We need to execute the "SetExpressCheckOut" method to obtain paypal token
$httpParsedResponseAr = PPHttpPost('SetExpressCheckout', $padata, $PayPalApiUsername,
$PayPalApiPassword, $PayPalApiSignature, $PayPalMode);
//Respond according to message we receive from Paypal
if("SUCCESS" == strtoupper($httpParsedResponseAr["ACK"]) ||
"SUCCESSWITHWARNING" == strtoupper($httpParsedResponseAr["ACK"])) {
if($PayPalMode=='sandbox')
$paypalmode = '.sandbox';
else
$paypalmode = '';
//Redirect user to PayPal store with Token received.
$paypalurl ='https://www'.$paypalmode.
'.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token='.
$httpParsedResponseAr["TOKEN"].'';
header('Location: '.$paypalurl);
}
else {
//Show error message
$MainContent .= "<div style='color:red'><b>SetExpressCheckOut failed : </b>".
urldecode($httpParsedResponseAr["L_LONGMESSAGE0"])."</div>";
$MainContent .= "<pre>";
$MainContent .= print_r($httpParsedResponseAr);
$MainContent .= "</pre>";
}
}
//Paypal redirects back to this page using ReturnURL, We should receive TOKEN and Payer ID
if(isset($_GET["token"]) && isset($_GET["PayerID"]))
{
//we will be using these two variables to execute the "DoExpressCheckoutPayment"
//Note: we haven't received any payment yet.
$token = $_GET["token"];
$playerid = $_GET["PayerID"];
$paypal_data = '';
// Get all items from the shopping cart, concatenate to the variable $paypal_data
// $_SESSION['Items'] is an associative array
foreach($_SESSION['Items'] as $key=>$item)
{
$paypal_data .= '&L_PAYMENTREQUEST_0_QTY'.$key.'='.urlencode($item["quantity"]);
$paypal_data .= '&L_PAYMENTREQUEST_0_AMT'.$key.'='.urlencode($item["price"]);
$paypal_data .= '&L_PAYMENTREQUEST_0_NAME'.$key.'='.urlencode($item["name"]);
$paypal_data .= '&L_PAYMENTREQUEST_0_NUMBER'.$key.'='.urlencode($item["productId"]);
}
//Data to be sent to PayPal
$padata = '&TOKEN='.urlencode($token).
'&PAYERID='.urlencode($playerid).
'&PAYMENTREQUEST_0_PAYMENTACTION='.urlencode("SALE").
$paypal_data.
'&PAYMENTREQUEST_0_ITEMAMT='.urlencode($_SESSION["SubTotal"]).
'&PAYMENTREQUEST_0_TAXAMT='.urlencode($_SESSION["Tax"]).
'&PAYMENTREQUEST_0_SHIPPINGAMT='.urlencode($_SESSION["ShipCharge"]).
'&PAYMENTREQUEST_0_AMT='.urlencode($_SESSION["SubTotal"] +
$_SESSION["Tax"] +
$_SESSION["ShipCharge"]).
'&PAYMENTREQUEST_0_CURRENCYCODE='.urlencode($PayPalCurrencyCode);
//We need to execute the "DoExpressCheckoutPayment" at this point
//to receive payment from user.
$httpParsedResponseAr = PPHttpPost('DoExpressCheckoutPayment', $padata,
$PayPalApiUsername, $PayPalApiPassword,
$PayPalApiSignature, $PayPalMode);
//Check if everything went ok..
if("SUCCESS" == strtoupper($httpParsedResponseAr["ACK"]) ||
"SUCCESSWITHWARNING" == strtoupper($httpParsedResponseAr["ACK"]))
{
// To Do 5 (DIY): Update stock inventory in product table
// after successful checkout
$qry = "SELECT * FROM shopcartitem WHERE ShopCartID =?" ;
$stmt = $conn->prepare($qry);
$stmt->bind_param("i", $_SESSION["Cart"]);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_array()){
$qry = "UPDATE product SET Quantity = Quantity - ? WHERE ProductID=?";
$stmt = $conn->prepare($qry);
$stmt->bind_param("ii", $row["Quantity"], $row["ProductID"]);
$stmt->execute();
$stmt->close();
}
// End of To Do 5
// To Do 2: Update shopcart table, close the shopping cart (OrderPlaced=1)
$total = $_SESSION["SubTotal"] + $_SESSION["Tax"] + $_SESSION["ShipCharge"];
$qry = "UPDATE shopcart SET OrderPlaced = 1, Quantity=?, SubTotal=?, ShipCharge=?, Tax=?, Total=? WHERE ShopCartId=?";
$stmt = $conn->prepare($qry);
$stmt->bind_param("iddddi", $_SESSION["NumCartItem"],
$_SESSION["SubTotal"], $_SESSION["ShipCharge"], $_SESSION["Tax"], $total, $_SESSION["Cart"]);
$stmt->execute();
$stmt->close();
// End of To Do 2
//We need to execute the "GetTransactionDetails" API Call at this point
//to get customer details
$transactionID = urlencode(
$httpParsedResponseAr["PAYMENTINFO_0_TRANSACTIONID"]);
$nvpStr = "&TRANSACTIONID=".$transactionID;
$httpParsedResponseAr = PPHttpPost('GetTransactionDetails', $nvpStr,
$PayPalApiUsername, $PayPalApiPassword,
$PayPalApiSignature, $PayPalMode);
if("SUCCESS" == strtoupper($httpParsedResponseAr["ACK"]) ||
"SUCCESSWITHWARNING" == strtoupper($httpParsedResponseAr["ACK"]))
{
//gennerate order entry and feed back orderID information
//You may have more information for the generated order entry
//if you set those information in the PayPal test accounts.
$ShipName = addslashes(urldecode($httpParsedResponseAr["SHIPTONAME"]));
$ShipAddress = urldecode($httpParsedResponseAr["SHIPTOSTREET"]);
if (isset($httpParsedResponseAr["SHIPTOSTREET2"]))
$ShipAddress .= ' '.urldecode($httpParsedResponseAr["SHIPTOSTREET2"]);
if (isset($httpParsedResponseAr["SHIPTOCITY"]))
$ShipAddress .= ' '.urldecode($httpParsedResponseAr["SHIPTOCITY"]);
if (isset($httpParsedResponseAr["SHIPTOSTATE"]))
$ShipAddress .= ' '.urldecode($httpParsedResponseAr["SHIPTOSTATE"]);
$ShipAddress .= ' '.urldecode($httpParsedResponseAr["SHIPTOCOUNTRYNAME"]).
' '.urldecode($httpParsedResponseAr["SHIPTOZIP"]);
$ShipCountry = urldecode(
$httpParsedResponseAr["SHIPTOCOUNTRYNAME"]);
$ShipEmail = urldecode($httpParsedResponseAr["EMAIL"]);
// To Do 3: Insert an Order record with shipping information
// Get the Order ID and save it in session variable.
$qry = "INSERT INTO orderdata (ShipName, ShipAddress, ShipCountry, ShipEmail, ShopCartID)
VALUES(?,?,?,?,?)";
$stmt = $conn->prepare($qry);
$stmt->bind_param("ssssi", $ShipName, $ShipAddress, $ShipCountry, $ShipEmail, $_SESSION["Cart"]);
$stmt->execute();
$stmt->close();
$qry = "SELECT LAST_INSERT_ID() AS OrderID";
$result = $conn->query($qry);
$row = $result->fetch_array();
$_SESSION["OrderID"] = $row["OrderID"];
// End of To Do 3
$conn->close();
// To Do 4A: Reset the "Number of Items in Cart" session variable to zero.
$_SESSION["NumCartItem"] = 0;
// To Do 4B: Clear the session variable that contains Shopping Cart ID.
unset($_SESSION["Cart"]);
// To Do 4C: Redirect shopper to the order confirmed page.
header("Location: orderConfirmed.php");
exit;
}
else
{
$MainContent .= "<div style='color:red'><b>GetTransactionDetails failed:</b>".
urldecode($httpParsedResponseAr["L_LONGMESSAGE0"]).'</div>';
$MainContent .= "<pre>";
$MainContent .= print_r($httpParsedResponseAr);
$MainContent .= "</pre>";
$conn->close();
}
}
else {
$MainContent .= "<div style='color:red'><b>DoExpressCheckoutPayment failed : </b>".
urldecode($httpParsedResponseAr["L_LONGMESSAGE0"]).'</div>';
$MainContent .= "<pre>";
$MainContent .= print_r($httpParsedResponseAr);
$MainContent .= "</pre>";
}
}
include("MasterTemplate.php");
?>