File: //proc/self/cwd/ecpay_subscription.php
<?php
// 測試串接請使用綠界測試參數(正式上線請替換)
$merchantID = '3400296';
$hashKey = 'XxShkqxTo7kjadON';
$hashIV = 'HZDgcHTqaaKCLKpl';
// 接收資料
$userID = $_POST['user'];
//$plan = $_POST['plan'];
$amount = $_POST['month'];
//$userID = "2025022410";
$plan = "月扣";
//$amount = "449";
$tradeNo = $userID . time(); // 訂單編號
$tradeDate = date('Y/m/d H:i:s');
$params = [
'MerchantID' => $merchantID,
'MerchantTradeNo' => $tradeNo,
'MerchantTradeDate' => $tradeDate,
'PaymentType' => 'aio',
'TotalAmount' => $amount,
'TradeDesc' => urlencode("寬頻定期扣款 - $plan"),
'ItemName' => "$plan 寬頻每月定期扣款",
'ReturnURL' => 'https://www.ysnet.com.tw/ecpay_notify.php',
'ClientBackURL' => 'https://www.ysnet.com.tw/寬頻會員登入/',
//'ClientBackURL' => 'https://www.ysnet.com.tw/ecpay_notify.php/',
'ChoosePayment' => 'Credit',
'PeriodAmount' => $amount,
'PeriodType' => 'M',
'Frequency' => 1,
'ExecTimes' => 999,
'PeriodReturnURL' => 'https://www.ysnet.com.tw/period_notify.php',
'CustomField1' => $userID,
];
// 產生 CheckMacValue
function generateCheckMacValue($params, $hashKey, $hashIV) {
ksort($params);
$encoded = http_build_query($params);
$encoded = urldecode($encoded);
$raw = "HashKey=$hashKey&$encoded&HashIV=$hashIV";
$encoded = urlencode($raw);
$encoded = strtolower($encoded);
$encoded = str_replace(['%2d','%5f','%2e','%21','%2a','%28','%29','%20'],
['-','_','.','!','*','(',')','+'], $encoded);
return strtoupper(hash('sha256', $encoded));
}
$params['CheckMacValue'] = generateCheckMacValue($params, $hashKey, $hashIV);
// 自動送出表單到綠界
echo '<form id="ecpayForm" method="post" action="https://payment.ecpay.com.tw/Cashier/AioCheckOut/V5">';
foreach ($params as $key => $value) {
echo "<input type='hidden' name='$key' value='$value'>";
}
echo '</form>';
echo "<script>document.getElementById('ecpayForm').submit();</script>";