dah baca sendiri
<?php
require_once($_SERVER[‘DOCUMENT_ROOT’].’/wp-load.php’);
function handle_xendit_webhook() {
ob_start(); // Start output buffering to prevent premature output// Two different expected tokens from Xendit
$expected_live = ‘token’;
$expected_demo = ‘token’;// Choose which token to use
$expected_token = $expected_live; // Change to ….// Retrieve token sent in the webhook request header
$received_token = $_SERVER[‘HTTP_X_CALLBACK_TOKEN’] ?? ”;// Validate the token
if ($received_token !== $expected_token) {
// If token is invalid, stop processing and return 403 Forbidden
ob_end_flush(); // Send output buffer & turn off buffering
http_response_code(403);
die(‘Invalid webhook token’);
}// Get the JSON POST body and decode it
$json = file_get_contents(‘php://input’);
$data = json_decode($json);// Assume the post update is not successful by default
$post_updated = false;// Check if data is valid
if (isset($data->external_id) && isset($data->status)) {
// Attempt to update the custom post type ‘pesanan’
$post_updated = update_pesanan_status($data->external_id, $data->status);
} else {
// Log error if data is invalid
error_log(‘Invalid data received: ‘ . $json);
}// Additional processing or redirect logic can be added here
// Clear the output buffer
ob_end_flush();
}function update_pesanan_status($external_id, $status) {
$args = array(
‘post_type’ => ‘pesanan’,
‘meta_query’ => array(
array(
‘key’ => ‘kode_invoice’,
‘value’ => $external_id,
‘compare’ => ‘=’
)
),
‘posts_per_page’ => 1 // Assuming kode_invoice is unique, we only need one post.
);$query = new WP_Query($args);
$post_updated = false;if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
update_post_meta(get_the_ID(), ‘status_pembayaran’, $status);
$post_updated = true;
// Log for successful update
error_log(‘Status_pembayaran updated for kode_invoice: ‘ . $external_id);
}
} else {
// Log for no post found
error_log(‘No posts found with kode_invoice: ‘ . $external_id);
}
// Reset post data after query
wp_reset_postdata();
return $post_updated;
}handle_xendit_webhook();
?>