File: /var/www/html/breadsecret.com_bak20260330/StripeBackend/captureevent.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;
require_once('./stripe-php/init.php');
\Stripe\Stripe::setApiKey('sk_test_51IzXlzJXv3LbLUyO5MAV7N8HXLz0EhRraYDIOI4LDgG70gLQzYxar5ENqf5VH95GQH02du1OpjrXiqzleijJugyA00aN0Tn59J');
$payload = @file_get_contents('php://input');
$event = null;
try {
$event = \Stripe\Event::constructFrom(
json_decode($payload, true)
);
} catch (\UnexpectedValueException $e) {
// Invalid payload
http_response_code(400);
exit();
}
// Handle the event
switch ($event->type) {
case 'payment_intent.succeeded':
$paymentIntent = $event->data->object; // contains a \Stripe\PaymentIntent
handlePaymentIntentSucceeded($paymentIntent);
break;
default:
echo 'Received unknown event type ' . $event->type;
}
http_response_code(200);
function get_post_id_by_meta_key_and_value($key, $value) {
global $wpdb;
$meta = $wpdb->get_results("SELECT * FROM `".$wpdb->postmeta."` WHERE meta_key='".$wpdb->escape($key)."' AND meta_value='".$wpdb->escape($value)."'");
if (is_array($meta) && !empty($meta) && isset($meta[0])) {
$meta = $meta[0];
}
if (is_object($meta)) {
return $meta->post_id;
}
else {
return false;
}
}
function handlePaymentIntentSucceeded($paymentIntent) {
global $wpdb;
if($paymentIntent->amount_received == $paymentIntent->amount){
$order_id = get_post_id_by_meta_key_and_value("_stripe_intent_id", $paymentIntent->id);
$order = new WC_Order($order_id);
$log_title = "New Order";
$origin_status = $order->get_status();
if($origin_status=="failed"){
$log_title = "RePay";
}
$balanceTransaction = \Stripe\BalanceTransaction::retrieve([
'id' => $paymentIntent->charges->data[0]->balance_transaction
]);
update_post_meta($order_id, '_stripe_customer_id', $paymentIntent->customer);
update_post_meta($order_id, '_stripe_source_id', $paymentIntent->payment_method);
update_post_meta($order_id, '_stripe_intent_id', $paymentIntent->id);
update_post_meta($order_id, '_stripe_charge_captured', $paymentIntent->charges->data[0]->captured==1?"yes":"no");
update_post_meta($order_id, '_stripe_fee', $balanceTransaction->fee/100);
update_post_meta($order_id, '_stripe_net', $balanceTransaction->net/100);
update_post_meta($order_id, '_stripe_currency', $paymentIntent->currency);
update_post_meta($order_id, '_transaction_id', $paymentIntent->charges->data[0]->id);
$order->update_status('processing', '');
$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_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)){
write_log($delivery_date, $log_title, "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');
}
}
}
//update_post_meta($order_id, 'stock_out','1');
}
}
?>