HEX
Server: Apache/2.4.37 (CentOS Stream) OpenSSL/1.1.1k
System: Linux ysnet.com.tw 4.18.0-553.5.1.el8.x86_64 #1 SMP Tue May 21 05:46:01 UTC 2024 x86_64
User: test (521)
PHP: 7.4.33
Disabled: NONE
Upload Files
File: /var/www/test/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>";