<?php
require_once('../database/header.php');
?>

<div class="main-panel">
<div class="content-wrapper">

<div class="row">
<div class="col-md-12 stretch-card">
  
<!-- DataTales Example -->
<div class="card shadow mb-4">
<div class="card-header py-3 bg-primary">
<h6 class="m-0 font-weight-bold text-white">创建支付链接</h6>
</div>
<div class="card-body">

<?php
// Initialize a variable for the response message
$response_message = '';

// Check if form is submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Get form data
$amount = $_POST['amount'];
$purpose = $_POST['purpose'];
$customer_name = $_POST['customer_name'];
$mobile_number = $_POST['mobile_number'];
$merchant_name = $_POST['merchant_name']; // Get merchant_name from the form

//$domain = $_SERVER['HTTP_HOST']; // Dynamically fetch the domain name

// Set the API URL

$domain = $_SERVER['HTTP_HOST'];


$apiUrl = "https://$domain/api/create-order.php"; // Replace with actual API URL

$redirect_url  = "https://$domain/partner/success.php"; 

$orderid = 'LINK' . random_int(1000000000, 9999999999);  // e.g. TXN1234567890

// Prepare the data to send in the POST request
$data = [
'api_key' => $userdata['api_key'], // Corrected user API key
'orderid' => $orderid, // This should ideally be dynamic or generated
'amount' => $amount,
'purpose' => $purpose,
'customer_name' => $customer_name,
'mobile_number' => $mobile_number,
'merchant_name' => $merchant_name, 
'redirect_url' => $redirect_url, 
];

// Initialize cURL session
$ch = curl_init();

// Set cURL options
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']);

// Execute the cURL request
$response = curl_exec($ch);

// Check for errors in cURL
if (curl_errno($ch)) {
echo 'cURL Error: ' . curl_error($ch);
} else {
// Decode the response
$response_data = json_decode($response, true);

// Handle the response
if ($response_data['status'] == 'SUCCESS') {
// Success message and payment link redirection
$payment_link = $response_data['payment_link'];
$response_message = '
<div class="alert alert-success alert-has-icon alert-position">
<div class="alert-icon"><i class="far fa-lightbulb"></i></div>
<div class="alert-body">
<div class="alert-title">交易处理成功！</div>
<p>您的支付链接已就绪。正在重定向到支付页面...</p>
</div>
</div>
<script>
setTimeout(function() {
window.location = "' . $payment_link . '"; // Redirect to the payment link
}, 3000); // Delay redirect by 3 seconds
</script>';
} else {
// Error message
$response_message = '
<div class="alert alert-danger alert-has-icon alert-position">
<div class="alert-icon"><i class="far fa-times-circle"></i></div>
<div class="alert-body">
<div class="alert-title">错误: ' . $response_data['message'] . '</div>
</div>
</div>';
}
}

// Close cURL session
curl_close($ch);
}

// Display the response message if available
if ($response_message) {
echo $response_message;
}
?>

<!-- Payment Link Creation Form -->
<form class="user" action="" method="POST">
<div class="form-group row">
<div class="col-sm-6 mb-3 mb-sm-0">
<label for="amount" class="m-0 font-weight-bold text-dark">金额</label>
<input required="required" type="text" class="form-control" name="amount" id="amount" placeholder="输入金额" onkeyup="this.value = this.value.toUpperCase();" onblur="this.value = this.value.toUpperCase();">
</div>
<div class="col-sm-6">
<label for="purpose" class="m-0 font-weight-bold text-dark">支付目的</label>
<input required="required" type="text" class="form-control" name="purpose" id="purpose" placeholder="支付目的" onkeyup="this.value = this.value.toUpperCase();" onblur="this.value = this.value.toUpperCase();">
</div>
</div>

<div class="form-group row">
<div class="col-sm-6 mb-3 mb-sm-0">
<label for="customer_name" class="m-0 font-weight-bold text-dark">客户姓名</label>
<input required="required" type="text" class="form-control" name="customer_name" id="customer_name" placeholder="客户姓名" onkeyup="this.value = this.value.toUpperCase();" onblur="this.value = this.value.toUpperCase();">
</div>
<div class="col-sm-6">
<label for="mobile_number" class="m-0 font-weight-bold text-dark">手机号码</label>
<input required="required" type="text" class="form-control" name="mobile_number" id="mobile_number" placeholder="手机号码" onkeyup="this.value = this.value.toUpperCase();" onblur="this.value = this.value.toUpperCase();">
</div>
</div>

<div class="form-group row">
<div class="col-sm-12">
<label for="merchant_name" class="m-0 font-weight-bold text-dark">商户名称</label>
<select required="required" class="form-control" name="merchant_name" id="merchant_name">
<option value="">选择商户</option>
<option value="Amazon">Amazon UPI</option>
<option value="SBI">SBI Yono Business</option>
<option value="HDFC">HDFC SmartHub</option>
<option value="Bajaj">Bajaj Business</option>
<option value="Mobikwik">Mobikwik UPI</option>
<option value="Bharatpe">Bharatpe Business</option>
<option value="Paytm">Paytm Business</option>
<option value="Freecharge">Freecharge UPI</option>

</select>
</div>
</div>

<button type="submit" class="btn btn-info btn-user btn-block">创建支付链接</button>
</form>

</div>
</div>


</div>
</div></div>
<!-- /.container-fluid -->
<!-- End of Main Content -->

<?php
require_once('../database/footer.php');
?>