初投稿で失礼があったらすみません。
ChatGPTと数週間相談していたのですが、ついぞうまくいかないのでご相談します。
当方はJsの知識がほとんどありませんので、初歩的なところで躓いているかもしれません。
どなたかお知恵を拝借できますと幸いです。
目的
kViewerにボタンを設置し、別サイトにデータを送りたい
(別サイト:三井住友カードの決済ステーション 口座振替受付サービス)
発生している問題
別サイトへの画面遷移はできる(認証はされる)が、エラーになってしまう。
おそらく、遷移先サイトの指定する文字コード(Shift-JIS)でハッシュ値を送信できていないためである。
↑文字コードが違う可能性が高いと判断した根拠
①cust_name = ‘テスト太郎’ に対して、「日本語全角又は英数字全角で入力してください」というエラーが出る
②リクエストヘッダーに content-type application/x-www-form-urlencoded と表示されている
質問
kviewerに設置したボタンから、ハッシュ値をShift-JISで送信する方法が知りたい
試したこと
-
formタグに
accept-charset=“Shift_JIS”
enctype=“application/x-www-form-urlencoded; charset=Shift_JIS”
を設定 → 効かずUTF-8送信 -
hidden inputの値を TextEncoder(‘shift-jis’) でバイト列化 → %xx化して送信
→ fetch()で送るとCORSエラー、form.submit()ではUTF-8送信に戻る -
をJSで追加 → Chromeでは無視
-
SJIS変換後にform.submit() → 送信成功するがUTF-8のまま
→ SMBC側で文字化け(940006 / 960007) -
トヨクモに問合せ
→回答待ち。ただ、サポート外なのでどうかなというところ
最新のコード(一部伏字***)
(function() {
'use strict';
const waitForViewer = setInterval(() => {
const header = document.querySelector('.kv-header');
if (!header) return;
clearInterval(waitForViewer);
if (document.getElementById('smbcButton')) return;
const bill_no = '999999990001';
const version = '130';
const shop_cd = '*******';
const syuno_co_cd = '*****';
const koushin_kbn = '0';
const shop_pass = '**********';
const shoporder_no = '0000000000000000000000000';
const cust_name = 'テスト太郎';
const cust_kana = 'テストタロウ';
async function sha256Hex(s) {
const data = new TextEncoder().encode(s);
const buf = await crypto.subtle.digest('SHA-256', data);
return Array.from(new Uint8Array(buf))
.map(b => b.toString(16).padStart(2, '0'))
.join('');
}
// SJIS変換
async function toSJIS(str) {
try {
const encoder = new TextEncoder('shift-jis');
const bytes = encoder.encode(str);
return new TextDecoder('shift-jis').decode(bytes);
} catch {
return str;
}
}
const btn = document.createElement('button');
btn.id = 'smbcButton';
btn.textContent = '口座振替登録画面へ';
btn.style.cssText = `
display:inline-block;
margin:16px 0;
padding:10px 24px;
background-color:#006400;
color:#fff;
border:none;
border-radius:6px;
cursor:pointer;
font-size:15px;
box-shadow:0 2px 5px rgba(0,0,0,0.15);
`;
header.insertAdjacentElement('afterend', btn);
btn.addEventListener('click', async () => {
const fs = await sha256Hex(shop_cd + syuno_co_cd + bill_no + shoporder_no + shop_pass);
console.log('=== SMBC送信内容 ===');
console.log({ version, shop_cd, syuno_co_cd, koushin_kbn, bill_no, shoporder_no, cust_name, cust_kana, fs });
alert('コンソールの送信内容を確認してからOKを押してください');
const form = document.createElement('form');
form.method = 'POST';
form.action = 'https://www.paymentstation.jp/customertest/sf/at/kokkzmoshikomi/begin.do';
form.acceptCharset = 'Shift_JIS';
// SJIS化してhiddenで送信
const params = {
version,
shop_cd,
syuno_co_cd,
koushin_kbn,
bill_no,
shoporder_no,
cust_name: await toSJIS(cust_name),
cust_kana: await toSJIS(cust_kana),
fs
};
Object.entries(params).forEach(([name, value]) => {
const input = document.createElement('input');
input.type = 'hidden';
input.name = name;
input.value = value;
form.appendChild(input);
});
document.body.appendChild(form);
form.submit(); // ← これでCORS回避して実際に画面遷移!
});
console.log('SMBCボタン生成完了', { bill_no });
}, 800);
})();
以上、なにとぞお願いします…![]()
![]()
![]()