CMXX - Placeorder initial version

This commit is contained in:
“VeLiTi”
2025-02-13 11:23:15 +01:00
parent 2eaf83c3fe
commit 3424c9fadc
14 changed files with 446 additions and 377 deletions

View File

@@ -29,9 +29,9 @@ $weighttotal = 0;
$checkout_input = [
"selected_country" => isset($_POST['address_country']) ? $_POST['address_country'] : $account['address_country'],
"selected_shipment_method" => isset($_POST['shipping_method']) ? $_POST['shipping_method'] : null,
"selected_shipment_method" => isset($_POST['shipping_method']) ? $_POST['shipping_method'] : '',
"business_type" => 'b2c',
"discount_code" => isset($_SESSION['discount']) ? $_SESSION['discount'] : null
"discount_code" => isset($_SESSION['discount']) ? $_SESSION['discount'] : ''
];
$selected_shipping_method_name = '';
@@ -39,9 +39,9 @@ $shipping_methods_available = [];
// Error array, output errors on the form
$errors = [];
// ---------------------------------------
// ---------------------------------------
// ---------------------------------------
// ---------------------------------------------
// End defaults --------------------------------
// ---------------------------------------------
// Redirect the user if the shopping cart is empty
if (empty($_SESSION['cart'])) {
@@ -56,16 +56,17 @@ if (isset($_SESSION['account_loggedin'])) {
// Fetch the account from the database and return the result as an Array
$account = $stmt->fetch(PDO::FETCH_ASSOC);
}
// Update discount code
if (isset($_POST['discount_code']) && !empty($_POST['discount_code'])) {
$_SESSION['discount'] = $_POST['discount_code'];
} else if (isset($_POST['discount_code']) && empty($_POST['discount_code']) && isset($_SESSION['discount'])) {
unset($_SESSION['discount']);
}
//-------------------------------
// If there are products in cart handle the checkout
//-------------------------------
if ($products_in_cart) {
//Calculate shopping_cart
@@ -103,7 +104,6 @@ if ($products_in_cart) {
//-------------------------------
//Place order
//-------------------------------
// Make sure when the user submits the form all data was submitted and shopping cart is not empty
if (isset($_POST['method'], $_POST['first_name'], $_POST['last_name'], $_POST['address_street'], $_POST['address_city'], $_POST['address_state'], $_POST['address_zip'], $_POST['address_country'], $_POST['address_phone'], $_SESSION['cart']) && !isset($_POST['update'])) {
$account_id = null;
@@ -146,19 +146,28 @@ if (isset($_POST['method'], $_POST['first_name'], $_POST['last_name'], $_POST['a
}
if (!$errors && $products_in_cart) {
//Process checkout
//Calculate shopping_cart
$payload = json_encode(array("cart" => $products_in_cart, "checkout_input" => $checkout_input, "customer_details" => $account), JSON_UNESCAPED_UNICODE);
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Process checkout => add payment_method to checkout_input array
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
$checkout_input['payment_method'] = $_POST['method'];
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Calculate shopping_cart based on session
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
$payload = json_encode(array("cart" => $_SESSION['cart'], "checkout_input" => $checkout_input, "customer_details" => $account), JSON_UNESCAPED_UNICODE);
$place_order = ioAPIv2('/v2/placeorder/',$payload,$clientsecret);
$place_order = json_decode($products_in_cart,true);
$place_order = json_decode($place_order,true);
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Check if transaction is succesfull and send order confirmation to customer
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
if ($place_order['error'] == '' && $place_order['id'] != ''){
//SEND CONFIRMATION TO CUSTOMER
send_order_details_email(
$account['email'],
$products_in_cart,
$place_order['products_checked-out'],
$account['first_name'],
$account['last_name'],
$account['address_street'],
@@ -173,22 +182,14 @@ if (isset($_POST['method'], $_POST['first_name'], $_POST['last_name'], $_POST['a
$place_order['payment_amount'],
$place_order['transaction_id']
);
}
//Pay on delivery = 2
if (pay_on_delivery_enabled && $place_order['payment_method'] == 2){
//header('Location: ' . url('index.php?page=placeorder'));
//exit;
}
/*
//Disable giftcard
if (isset($_SESSION['discount'])){
if (preg_match("/[#][0-9]/", $_SESSION['discount']) == 1){
useGiftCart($pdo, $_SESSION['discount']);
}
}
// Authenticate the user
if ($account_id != null) {
// Log the user in with the details provided
@@ -197,87 +198,22 @@ if (isset($_POST['method'], $_POST['first_name'], $_POST['last_name'], $_POST['a
$_SESSION['account_id'] = $account_id;
$_SESSION['account_role'] = $account ? $account['role'] : 'Member';
}
// Send order details to the specified email address
send_order_details_email(
isset($account['email']) && !empty($account['email']) ? $account['email'] : $_POST['email'],
$products_in_cart,
$_POST['first_name'],
$_POST['last_name'],
$_POST['address_street'],
$_POST['address_city'],
$_POST['address_state'],
$_POST['address_zip'],
$_POST['address_country'],
$subtotal,
$discounttotal,
$shippingtotal,
$taxtotal,
$payment_amount,
$order_id
);
header('Location: ' . url('index.php?page=placeorder'));
exit;
*/
}
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Mollie ++++++++++++++++++++++++++++++++++++++++++++++++++++
//Pay on delivery = 2
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
if (mollie_enabled && $_POST['method'] == 'mollie') {
// Process Normal Checkout
// Generate unique transaction ID
$transaction_id = strtoupper(uniqid('SC') . substr(md5(mt_rand()), 0, 5));
// Insert transaction into database
$stmt = $pdo->prepare('INSERT INTO transactions (txn_id, payment_amount, payment_status, created, payer_email, first_name, last_name, address_street, address_city, address_state, address_zip, address_country, account_id, payment_method, shipping_method, shipping_amount, discount_code, address_phone, tax_amount) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)');
$stmt->execute([
$transaction_id,
$payment_amount,
default_payment_status,
date('Y-m-d H:i:s'),
isset($account['email']) && !empty($account['email']) ? $account['email'] : $_POST['email'],
$_POST['first_name'],
$_POST['last_name'],
$_POST['address_street'],
$_POST['address_city'],
$_POST['address_state'],
$_POST['address_zip'],
$_POST['address_country'],
$account_id,
'Debit/Credit',
$selected_shipping_method_name,
$shippingtotal,
isset($_SESSION['discount']) ? $_SESSION['discount'] : '',
$_POST['address_phone'],
$taxtotal
]);
// Get order ID
$order_id = $pdo->lastInsertId();
// Iterate products and deduct quantities
foreach ($products_in_cart as $product) {
// For every product in the shopping cart insert a new transaction into our database
$stmt = $pdo->prepare('INSERT INTO transactions_items (txn_id, item_id, item_price, item_quantity, item_options) VALUES (?,?,?,?,?)');
$stmt->execute([ $transaction_id, $product['id'], $product['final_price'], $product['quantity'], $product['options'] ]);
// Update product quantity in the products table
$stmt = $pdo->prepare('UPDATE products SET quantity = quantity - ? WHERE quantity > 0 AND id = ?');
$stmt->execute([ $product['quantity'], $product['id'] ]);
// Deduct option quantities
if ($product['options']) {
$options = explode(',', $product['options']);
foreach ($options as $opt) {
$option_name = explode('-', $opt)[0];
$option_value = explode('-', $opt)[1];
$stmt = $pdo->prepare('UPDATE products_options SET quantity = quantity - ? WHERE quantity > 0 AND title = ? AND (name = ? OR name = "")');
$stmt->execute([ $product['quantity'], $option_name, $option_value ]);
}
}
}
// Authenticate the user
if ($account_id != null) {
// Log the user in with the details provided
session_regenerate_id();
$_SESSION['account_loggedin'] = TRUE;
$_SESSION['account_id'] = $account_id;
$_SESSION['account_role'] = $account ? $account['role'] : 'Member';
}
if (pay_on_delivery_enabled && $place_order['payment_method'] == 2){
header('Location: ' . url('index.php?page=placeorder'));
exit;
}
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Mollie = 0 ++++++++++++++++++++++++++++++++++++++++++++++++++
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
if (mollie_enabled && $_POST['method'] == 0) {
try {
/*
@@ -291,8 +227,8 @@ if (isset($_POST['method'], $_POST['first_name'], $_POST['last_name'], $_POST['a
* Generate a unique order id for this example. It is important to include this unique attribute
* in the redirectUrl (below) so a proper return page can be shown to the customer.
*/
$orderId = $transaction_id;
$value = number_format(($subtotal-$discounttotal)+$shippingtotal,2,'.','');
$orderId = $place_order['transaction_id'];
$value = number_format($place_order['payment_amount'],2,'.','');
/*
* Determine the url parts to these example files.
@@ -333,25 +269,6 @@ if (isset($_POST['method'], $_POST['first_name'], $_POST['last_name'], $_POST['a
* This request should always be a GET, thus we enforce 303 http response code
*/
// Send order details to the specified email address
send_order_details_email(
isset($account['email']) && !empty($account['email']) ? $account['email'] : $_POST['email'],
$products_in_cart,
$_POST['first_name'],
$_POST['last_name'],
$_POST['address_street'],
$_POST['address_city'],
$_POST['address_state'],
$_POST['address_zip'],
$_POST['address_country'],
$subtotal,
$discounttotal,
$shippingtotal,
$taxtotal,
$payment_amount,
$order_id
);
// Send customer to checkout
header("Location: " . $payment->getCheckoutUrl(), true, 303);
@@ -362,89 +279,36 @@ if (isset($_POST['method'], $_POST['first_name'], $_POST['last_name'], $_POST['a
}
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// PayPal Payment + +++++++++++++++++++++++++++++++++++++++++
// PayPal Payment = 1 +++++++++++++++++++++++++++++++++++++++++
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
if (paypal_enabled && $_POST['method'] == 'paypal') {
// Process Normal Checkout first then do PayPal related
// Generate unique transaction ID
$transaction_id = strtoupper(uniqid('SC') . substr(md5(mt_rand()), 0, 5));
// Insert transaction into database
$stmt = $pdo->prepare('INSERT INTO transactions (txn_id, payment_amount, payment_status, created, payer_email, first_name, last_name, address_street, address_city, address_state, address_zip, address_country, account_id, payment_method, shipping_method, shipping_amount, discount_code, address_phone, tax_amount) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)');
$stmt->execute([
$transaction_id,
$payment_amount,
default_payment_status,
date('Y-m-d H:i:s'),
isset($account['email']) && !empty($account['email']) ? $account['email'] : $_POST['email'],
$_POST['first_name'],
$_POST['last_name'],
$_POST['address_street'],
$_POST['address_city'],
$_POST['address_state'],
$_POST['address_zip'],
$_POST['address_country'],
$account_id,
'paypal',
$selected_shipping_method_name,
$shippingtotal,
isset($_SESSION['discount']) ? $_SESSION['discount'] : '',
$_POST['address_phone'],
$taxtotal
]);
// Get order ID
$order_id = $pdo->lastInsertId();
// Iterate products and deduct quantities
foreach ($products_in_cart as $product) {
// For every product in the shopping cart insert a new transaction into our database
$stmt = $pdo->prepare('INSERT INTO transactions_items (txn_id, item_id, item_price, item_quantity, item_options) VALUES (?,?,?,?,?)');
$stmt->execute([ $transaction_id, $product['id'], $product['final_price'], $product['quantity'], $product['options'] ]);
// Update product quantity in the products table
$stmt = $pdo->prepare('UPDATE products SET quantity = quantity - ? WHERE quantity > 0 AND id = ?');
$stmt->execute([ $product['quantity'], $product['id'] ]);
// Deduct option quantities
if ($product['options']) {
$options = explode(',', $product['options']);
foreach ($options as $opt) {
$option_name = explode('-', $opt)[0];
$option_value = explode('-', $opt)[1];
$stmt = $pdo->prepare('UPDATE products_options SET quantity = quantity - ? WHERE quantity > 0 AND title = ? AND (name = ? OR name = "")');
$stmt->execute([ $product['quantity'], $option_name, $option_value ]);
}
}
}
if ($account_id != null) {
// Log the user in with the details provided
session_regenerate_id();
$_SESSION['account_loggedin'] = TRUE;
$_SESSION['account_id'] = $account_id;
$_SESSION['account_role'] = $account ? $account['role'] : 'Member';
}
//Process Payment
require_once __DIR__."/lib/paypal/paypal.php";
$base = PAYPAL_URL;
$id = PAYPAL_CLIENT_ID;
$secret = PAYPAL_CLIENT_SECRET;
$base = PAYPAL_URL;
$id = PAYPAL_CLIENT_ID;
$secret = PAYPAL_CLIENT_SECRET;
//init input
$order = $place_order['transaction_id'];
$price = number_format($place_order['payment_amount'],2,'.','');
$currency = "EUR";
//make payment
$paypal = new paypalCurl();
$paypal->init($id,$secret,$base);
$result = $paypal->makePaymentURL($order,$price,$currency);
//init input
$order = $transaction_id;
$price = $payment_amount;
$currency = "EUR";
//make payment
$paypal = new paypalCurl();
$paypal->init($id,$secret,$base);
$result = $paypal->makePaymentURL($order,$price,$currency);
if ($result->status === true) {
header("location:". $result->url);
die;
}
else { //raise error
echo $result->msg;
die;
}
if ($result->status === true) {
header("location:". $result->url);
die;
}
else { //raise error
echo $result->msg;
die;
}
}
}
@@ -563,7 +427,7 @@ $view .= ' </select>
foreach($products_in_cart['cart_details']['products'] as $product){
$view .= ' <tr>
<td><img src="'.$img_url.$product['meta']['img'].'" width="35" height="35" alt="'.$product['meta']['name'].'"></td>
<td><img src="'.img_url.$product['meta']['img'].'" width="35" height="35" alt="'.$product['meta']['name'].'"></td>
<td>'.$product['quantity'].' x '.$product['meta']['name'].'</td>
<td class="price">'.currency_code.''.number_format($product['options_price'] * $product['quantity'],2).'</td>
</tr>';