File: //proc/thread-self/cwd/ecpay_notify.php
<?php
require_once 'db.php'; // 引入資料庫連線
try {
// 從綠界收到的 POST 資料
$userID = $_POST['CustomField1'] ?? null; // 你在訂單建立時傳入的 user id
$ecpay_number = $_POST['MerchantTradeNo'] ?? ''; // 你在訂單建立時傳入的 廠商流水號
$paymentDate = $_POST['PaymentDate'] ?? ''; // 付款時間,如 2025/04/11 16:40:33
$rtnCode = $_POST['RtnCode'] ?? ''; //回傳狀態碼(重要),1扣款成功,非1 扣款失敗
$tradeamt = $_POST['TradeAmt'] ?? '';//回傳扣款金額。
if ($userID && $paymentDate) {
// 僅在扣款成功(rtnCode != 1)時更新付款資料
if ($rtnCode == 1) {
// 清理付款日期格式:轉換成 yyyy-mm-dd
$dateOnly = date('Y-m-d', strtotime($paymentDate));
// 更新付款成功資訊
$stmt = $conn->prepare("UPDATE filemaker SET ecpay = ?, ecpay_number = ?, rtn_code = ? WHERE user = ?");
$stmt->bind_param("ssii", $dateOnly, $ecpay_number, $rtnCode, $userID);
$stmt->execute();
$stmt->close();
// Discord Webhook URL
$webhook_url = "https://discord.com/api/webhooks/1363708454992085002/bUsepML8AzEyEQK5SFYjx1N0N6mLJ5uN6SQOpMOoiVw6PmbrQm_sIw37uZb_SxTwuRYX";
// 生成完整的消息内容
#$message = "[$iden$name] $decoded_mess\n==================================================";
//$message = "用戶[$userID]定期定額付款成功,流水號[$ecpay_number]。";
$message = [
"embeds" => [[
"title" => "用戶[$userID]",
"description" => "狀態:付款成功。\n扣款金額:`{$tradeamt}。\n扣款時間:{$paymentDate}。\n流水號:{$ecpay_number}`。",
"color" => hexdec("00cc66")
]]
];
// 创建 JSON 数据
//$json_data = json_encode(["content" => $message], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$json_data = json_encode($message, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
// 使用 cURL 发送请求到 Discord Webhook
$ch = curl_init($webhook_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// 执行请求并关闭 cURL
$response = curl_exec($ch);
curl_close($ch);
}
}
// 回應綠界:成功收到
echo '1|OK';
} catch (Exception $e) {
// 失敗回應給綠界,並可選擇記 log
error_log("ECPay Notify Error: " . $e->getMessage());
echo '0|Fail';
}
?>