Buat Form,
Ubah status send Form Jadi POST
jangan lupa masukan webhook url
*Note : Semua Action After submit Bakal tidak befungsi
CODE
<?php
require_once($_SERVER[‘DOCUMENT_ROOT’].’/wp-load.php’);
function send_order_confirmation_email($data) {
$to = ’[email protected],[email protected]’; // Replace with the recipient email address
$subject = ‘#’.$data[‘externalId’].’ Ada Pesanan Website ‘. $data[‘productName’] . ‘ dari ‘ . $data[‘firstName’].’ ‘. $data[‘lastName’] . ‘ dengan domain ‘. $data[‘domain’];// Construct the email body using the order data
$body .= “Data Pemesanan:\n”;
$body .= “Product: ” . $data[‘productName’] . “\n”;
$body .= “Domain: ” . $data[‘domain’] . “\n”;
$body .= “Total Price: Rp.” . $data[‘finalPrice’].”\n”;
$body .= “\n”;
$body .= “\n”;
$body .= “Data Customer:\n”;
$body .= “Customer Name: ” . $data[‘firstName’] . ” ” . $data[‘lastName’] . “\n”;
$body .= “Email: ” . $data[’email’] . “\n”;
$body .= “Phone: +62” . $data[‘phone’] . “\n”;
$body .= “Address: ” . $data[‘address’] . “\n”;
$body .= “Additional Notes: ” . $data[‘additionalParam’];// Send the email
$email_sent = wp_mail($to, $subject, $body);}
function create_pesanan_post($data) {
// Create a new ‘pesanan’ post
$new_post = array(
‘post_title’ => wp_strip_all_tags($data[‘domain’]),
‘post_status’ => ‘publish’,
‘post_type’ => ‘pesanan’,
);$post_id = wp_insert_post($new_post);
if (is_wp_error($post_id)) {
error_log(‘Error creating post: ‘ . $post_id->get_error_message());
} else {
error_log(‘Post created successfully: ‘ . $post_id);}
// Check if post is created successfully
if ($post_id != 0) {
// Add custom fields to the post
add_post_meta($post_id, ‘kode_invoice’, $data[‘externalId’]);
add_post_meta($post_id, ‘tanggal_pemesanan’, $data[‘orderDate’]);
add_post_meta($post_id, ‘nama_depan’, $data[‘firstName’]);
add_post_meta($post_id, ‘nama_belakang’, $data[‘lastName’]);
add_post_meta($post_id, ‘user_email’, $data[’email’]);
add_post_meta($post_id, ‘user_phone’, $data[‘phone’]);
add_post_meta($post_id, ‘user_alamat’, $data[‘address’]);
add_post_meta($post_id, ‘keterangan’, $data[‘additionalParam’]);
add_post_meta($post_id, ‘user_website’, $data[‘productName’]);
add_post_meta($post_id, ‘website_harga’, $data[‘finalPrice’]);
} else {
// Handle the error, e.g., log it or send a notification
}
}// Initialize variables with default values
$domain = $orderDate = $firstName = $lastName = $email = $phone = $address = $productName = $productPrice = $additionalParam = “”;
$quantity = 1; // Quantity is always 1// Check if the form fields are set and assign their values to variables
$domain = filter_input(INPUT_POST, ‘domain’, FILTER_SANITIZE_STRING); // Customer chosen domain
$orderDate = filter_input(INPUT_POST, ‘tanggal’, FILTER_SANITIZE_STRING); // Order date
$firstName = filter_input(INPUT_POST, ‘namadepan’, FILTER_SANITIZE_STRING); // First name
$lastName = filter_input(INPUT_POST, ‘namabelakang’, FILTER_SANITIZE_STRING); // Last name
$email = filter_input(INPUT_POST, ’email’, FILTER_SANITIZE_EMAIL); // Customer email
$phone = filter_input(INPUT_POST, ‘tlp’, FILTER_SANITIZE_STRING); // Customer phone
$address = filter_input(INPUT_POST, ‘alamat’, FILTER_SANITIZE_STRING); // Customer address
$productName = filter_input(INPUT_POST, ‘website’, FILTER_SANITIZE_STRING); // Product name
$productPrice = filter_input(INPUT_POST, ‘harga’, FILTER_SANITIZE_STRING); // Product price
$priceWithoutCurrency = str_replace(“Rp.”, “”, $productPrice); // Remove “Rp.”
$priceWithoutDots = str_replace(“.”, “”, $priceWithoutCurrency); // Remove dots
$finalPrice = (int)$priceWithoutDots; // Convert to integer$additionalParam = filter_input(INPUT_POST, ‘keterangan’, FILTER_SANITIZE_STRING); // User note
// Rest of your Xendit API integration code
$apiKey = ‘xnd_production_Kode:’; //live
// $apiKey = ‘xnd_development_Kode:’; //demo
$externalId = ‘XXXX-‘ . time(); //PREFIX
$apiUrl = ‘https://api.xendit.co/v2/invoices’;// Prepare data for the API request
$data = [
‘external_id’ => $externalId,
‘amount’ => $finalPrice,
‘customer’ => [
‘given_names’ => $firstName . ‘ ‘ . $lastName,
’email’ => $email,
‘mobile_number’ => ‘+62’ . $phone, // Assuming all numbers are Indonesian, thus ‘+62’ is prefixed
‘addresses’ => [
[
‘country’ => ‘ID’,
‘street_line1’ => $address,]
]
],
‘description’ => ‘Siapngeweb.com – Payment for ‘ . $productName .’ With Free domain ‘. $domain,
];// Setup cURL
$ch = curl_init($apiUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
‘Content-Type: application/json’,
‘Authorization: Basic ‘ . base64_encode($apiKey)
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));// Execute the API request
$response = curl_exec($ch);// Check for errors
if (curl_errno($ch)) {
// Handle errors (log them and display an error message to the user)
curl_close($ch);
return;
}// Close cURL
curl_close($ch);// Handle the response
$responseData = json_decode($response, true);
if (isset($responseData[‘invoice_url’])) {
// Prepare data for post creation
$postData = array(
‘externalId’ => $externalId,
‘domain’ => $domain,
‘orderDate’ => $orderDate,
‘firstName’ => $firstName,
‘lastName’ => $lastName,
’email’ => $email,
‘phone’ => $phone,
‘address’ => $address,
‘additionalParam’ => $additionalParam,
‘productName’ => $productName,
‘finalPrice’ => $finalPrice,
);// Create a new ‘pesanan’ post
create_pesanan_post($postData);
send_order_confirmation_email($postData);}
// Handle the response
$responseData = json_decode($response, true);
if (isset($responseData[‘invoice_url’])) {
// Redirect to the Xendit payment page
header(‘Location: ‘ . $responseData[‘invoice_url’]);
exit;
}?>