iT邦幫忙

2021 iThome 鐵人賽

DAY 15
0
永豐金融APIs

30天全端挑戰!React+Spring Boot+Mongo DB 串接永豐API 打造金融網站系列 第 15

[Day 15] - 初探永豐銀行線上收款API - 豐收款 - HASH ID計算(2) +IV計算

接續昨天未完成的作業

依據規格書,HASH ID 是由給定的4組hash值算出
https://ithelp.ithome.com.tw/upload/images/20210930/20128973nbZVSlW7Ja.png
我先以寫死的值來測試
https://ithelp.ithome.com.tw/upload/images/20210930/20128973fKPze6JUJV.png
結果無誤
https://ithelp.ithome.com.tw/upload/images/20210930/20128973xNKXqZELdh.png

接下來就建了一個QpayHelper.java
拿來放跟豐收款相關的方法

public class QpayHelper {
}

首先將取得nonce的方法移進來

private String getNonce(){
    HttpUtil util = new HttpUtil();
    String shopNo= SystemConfigUtil.systemConfMap.get("ShopNo");
    String nonceUrl= SystemConfigUtil.systemConfMap.get("Qpay_API_Url_Nonce");

    Map<String,String> map = new HashMap<String,String>();
    map.put("ShopNo", shopNo);

    String json;
    String nonce="";
    try {
        json = new ObjectMapper().writeValueAsString(map);
        nonce=util.post(nonceUrl, json);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return nonce;
}

再來是取得HASH ID,一樣從前面的測試直接COPY過來,參數
改成從systemConfigMap取

    private String getHashID(){
        BigInteger hashA1 = new BigInteger(
        SystemConfigUtil.systemConfMap.get("HASH_ID_A1"), 16);
        BigInteger hashA2 = new BigInteger(
        SystemConfigUtil.systemConfMap.get("HASH_ID_A2"), 16);
        BigInteger hashB1 = new BigInteger(
        SystemConfigUtil.systemConfMap.get("HASH_ID_B1"), 16);
        BigInteger hashB2 = new BigInteger(
        SystemConfigUtil.systemConfMap.get("HASH_ID_B2"), 16);
        BigInteger hashA = hashA1.xor(hashA2);
        BigInteger hashB = hashB1.xor(hashB2);
        String hashID =hashA.toString(16)+hashB.toString(16);
        return hashID.toUpperCase();
    }

依據規格書,取得Nonce之後還要將Nonce轉為sha256,IV即是SHA256後的末16位
這邊我直接使用apache的codec了,加到pom:

		<dependency>
    		<groupId>commons-codec</groupId>
    		<artifactId>commons-codec</artifactId>
    		<version>1.10</version>
		</dependency>

nonce傳入後直接使用DigestUtils的方法轉sha256,很方便

private String getIV(String nonce){
    String sha256hex = DigestUtils.sha256Hex(nonce); 
    String iv = sha256hex.substring(sha256hex.length()-16, sha256hex.length()).toUpperCase();
    return iv;
}

取得sha256的值後取後16位並轉大寫就完成了

可以到永豐提供的驗證頁面看看自己的程式算出來的值是否正確

這邊不得不說,JAVA真的是資源很豐富的語言,要什麼功能都有函式庫可以call,
不過很多功能的底層是如何實現的,有時間的話我覺得還是可以看看source code
才不會只知道程式跑得動、不知道自己在做什麼

不過今天也快沒時間了,先用再說/images/emoticon/emoticon25.gif

明天來開始實作sign簽章的部分!


上一篇
[Day 14] - 初探永豐銀行線上收款API - 豐收款 - HASH ID計算(1)
下一篇
[Day 16] - 初探永豐銀行線上收款API - 豐收款 - Sign值計算(1)
系列文
30天全端挑戰!React+Spring Boot+Mongo DB 串接永豐API 打造金融網站30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言