File: /var/www/html/test.breadsecret.com_bak/qfpay/payment_success.php
<?php
//error_reporting(E_ALL);
//ini_set('display_errors', 1);
require_once('../wp-load.php');
date_default_timezone_set("Asia/Hong_Kong");
global $wpdb, $current_site;
//echo "success:";
/*
echo "<pre>";
print_r($_REQUEST);
echo "</pre>";
*/
?>
<head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/js/bootstrap.bundle.min.js"></script>
</head>
<div style="position: fixed; top: 40%; left: 0; right: 0; bottom: 0; text-align: center;">
<!--<input type='button' name='back' id='back' value='open' class='btn btn-md btn-dark' onclick='redirect()'>-->
請返回BSB應用程式繼續處理訂單流程<br>
Please go back to BSB app for further processing.
<!--<a href="bsb://bread_secret_app?order_id=">TEST</a>-->
</div>
<?php
if($_REQUEST['source'] == "bsb" && $_REQUEST['type'] == "order"){
//echo "IN";
$order_id = $_REQUEST['id'];
$order = new WC_Order($order_id);
if($order->has_status('pending')){
update_post_meta($order_id, 'gw_url', $_REQUEST['gw_url']);
update_post_meta($order_id, 'return_url', (empty($_SERVER['HTTPS']) ? 'http' : 'https') . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");
update_post_meta($order_id, 'txn', $_REQUEST['trade_no']);
if(isset($_REQUEST['ref_no']) && $_REQUEST['ref_no']!=""){
update_post_meta($order_id, 'out_trade_no', $_REQUEST['ref_no']);
} else {
update_post_meta($order_id, 'out_trade_no', $_REQUEST['out_trade_no']);
}
$order->update_status('processing', '');
$order_number = $order->get_order_number();
$customer_id = $order->get_customer_id();
$device_token = get_user_meta($customer_id, "device_token", true);
//if($device_token && !empty($device_token))){
$title = "Bread Secret Bakery (TEST)";
$message = "New Order #".$order_number." is received";
$type = "new_order";
$ref_id = $order_id;
$wpdb->insert(
'bsb_notification_center',
array(
'hash'=>wp_generate_uuid4(),
'user_id'=>$customer_id,
'title'=>$title,
'message'=>$message,
'device_token'=>$device_token,
'type'=>$type,
'reference_id'=>$ref_id
)
);
$notification_id = $wpdb->insert_id;
initPushNotificationAppDevice($title, $message, $device_token, $type, $ref_id, $notification_id);
//}
$delivery_date = get_post_meta( $order_id, 'delivery_date', true );
$adjust_stock = get_post_meta( $order_id, 'adjust_stock', true );
$stock_out = get_post_meta( $order_id, 'stock_out', true );
//wc_create_order_note($order_id, "Delivery Date: ".sanitize_text_field($delivery_date), false, true);
wc_create_order_note($order_id, "Stripe charge complete (Charge ID: ".sanitize_text_field($paymentIntent->charges->data[0]->id).")", false, true);
// adjust stock action //
//adjust_stock_action($order_id);
if($order->has_status('processing') || $order->has_status('completed')){
if($stock_out!=1){
foreach ( $order->get_items() as $item_id => $item ) {
$prod = $item->get_product();
$sku = $prod->get_sku();
$variation_id = $item->get_variation_id();
$product = wc_get_product(wc_get_product_id_by_sku($sku));
$stock_sku = get_master_sku($sku);
$unit_qty = 1;
if(has_master_sku($sku)){
$description = $product->get_name();
$unit_qty = get_master_sku_unit_qty($sku);
}else{
if($variation_id!=0){
$description = get_name_from_variation_id($product->get_name(),$variation_id);
$unit_qty = get_variation_unit_qty($variation_id);
}else{
$description = $product->get_name(); // make a unique key
$unit_qty = 1;
}
}
$quantity = $item->get_quantity()*$unit_qty;
if(isset($delivery_date)){
$qty_from = get_sku_stock_by_date($stock_sku, $delivery_date);
//$qty_from = get_sku_stock_by_date_remake($stock_sku, $delivery_date);
$qty_to = $qty_from - $quantity;
$sql_update = "UPDATE sku_stock SET `".$stock_sku."` = '".$qty_to."' WHERE stock_date = '".$delivery_date."' LIMIT 1";
if($wpdb->query($sql_update)){
update_stock_remake($delivery_date, $stock_sku, $qty_to);
write_log($delivery_date, "New Order", "Front End", $order_id, $stock_sku, $description, $qty_from, $qty_to, get_current_user_id(), get_client_ip(), $sql_update);
}
}
}
if(isset($delivery_date)){
wc_create_order_note($order_id, "Delivery Date: ".sanitize_text_field($delivery_date), false, true);
update_post_meta($order_id, 'stock_out','1');
update_post_meta($order_id, 'adjust_stock','Y');
}
}
}
}
}
//header("Location: bsb://bread_secret_app?order_id=".$_REQUEST['id']);
echo "<meta http-equiv='refresh' content='3; url=bsb://bread_secret_app?order_id=".$_REQUEST['id']."'>";
?>
<script type='text/javascript'>
function redirect(){
window.open("bsb://bread_secret_app?order_id=<?=$_REQUEST['id'];?>");
}
//BSB.postMessage('<?php echo $order_id;?>');
</script>