昨天把 Webhook api 準備好了,然後怎麼樣呢
是的,一直回應 E3501 – 訊息內容錯誤
,今天就是我的 debug 之日
// app/Http/Controllers/Controller.php
public function create_order(Request $request)
{
$sinopac = $this->initSinopac();
$data = [
'ShopNo' => $sinopac->shop_no,
'OrderNo' => date('YmdHis'),
'Amount' => random_int(4000, 10000),
'CurrencyID' => 'TWD',
'PrdtName' => '大河',
'ReturnURL' => $request->getBaseUrl() . '/order_reply',
'BackendURL' => $request->getBaseUrl() . '/receive_msg',
'PayType' => 'A',
'ATMParam' => [
'ExpireDate' => date('Ymd', time() + 604800),
],
];
$data = $sinopac->requestDataset('OrderCreate', $data);
$message = $sinopac->callApi('https://apisbx.sinopac.com/funBIZ/QPay.WebAPI/api/Order', $data);
return $message;
}
// routes/api.php
Route::post('/create_order', [\App\Http\Controllers\Controller::class, 'create_order']);
追蹤了好一陣自發現 calculateHashId 那邊有個問題,dechex 轉換回來後沒有補齊4位
修改一下函數後長這樣
// app/Http/Controllers/Sinopac.php
public function calcHashId(): string
{
$a = $b = '';
$length = strlen($this->key_a1);
for ($i = 0; $i < $length; $i += 4) {
$part_of_a = dechex(hexdec(substr($this->key_a1, $i, 4)) ^ hexdec(substr($this->key_a2, $i, 4)));
$part_of_b = dechex(hexdec(substr($this->key_b1, $i, 4)) ^ hexdec(substr($this->key_b2, $i, 4)));
$a .= str_pad($part_of_a, 4, "0");
$b .= str_pad($part_of_b, 4, "0");
}
return strtoupper($a . $b);
}
第二個蟲是發生在 encryptMessage,沒有做 key 的排序
public function encryptMessage($data, $key, $iv): string
{
ksort($data);
$data = json_encode(array_filter((array)($data)));
$padding = 16 - (strlen($data) % 16);
$data .= str_repeat(chr($padding), $padding);
$encrypt = openssl_encrypt($data, 'AES-256-CBC', $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);
return strtoupper(bin2hex($encrypt));
}
E3501 – 訊息內容錯誤
,滿天飛的蟲阿RRRRRRRR,花了一堆時間抓蟲...明天把範例程式搬過來測試好了,明天見