iT邦幫忙

2021 iThome 鐵人賽

DAY 2
0
永豐金融APIs

永豐Vue一下-從生活尋找靈感系列 第 10

[day10]串接API實測-訂單建置API

訂單建置API設定

  • 建立預計導入API的參數清單
class ApiSource(BaseModel):
    householder:str 
    Amount:int #配合規格,自動補上小數點後兩位00
    OrderNo:str
    PayType:str
  • 訂單建置API

    • 程式
    @app.post("/sinopacApi_Order", summary="永豐建置訂單API" )# , response_model=ApiResultGet
    async def sinopacApi_Order(apiparams: ApiSource):
        start = time.time()
        # ShopNo 由主辦單位提供,此處示例以 xxx 取代
        Shop_No = {'ShopNo':"NAxxx_001"}
        NonceValue=getNonce(Shop_No)
    
        SHAValue ,IVValue= getSHAValue(NonceValue)
    
        hashID = getAESKey()
    
        lastDate= datetime.date.today() + datetime.timedelta(days=1)
        print("lastDate",str(lastDate).replace("-",""))
        print("apiparams.PayType => ",apiparams.PayType)
        OrderNo_total=apiparams.PayType+"_"+apiparams.OrderNo+"_"+str(datetime.datetime.now().timestamp())
        if apiparams.PayType=="A":
            send_message_ori = {
            "ShopNo": Shop_No["ShopNo"],
            "OrderNo": OrderNo_total,
            "Amount": apiparams.Amount*100,
            "CurrencyID": "TWD",
            "PayType": apiparams.PayType, #管理費僅用匯款,無信用卡繳費模式
            "ATMParam": {"ExpireDate":str(lastDate).replace("-","")},
            "CardParam": {},
            "ConvStoreParam": {},
            "PrdtName": "order",
            "Param1": apiparams.householder,  
            "Param2": apiparams.OrderNo,     
            "ReturnURL": "http://10.11.22.113:8803/QPay.ApiClient/Store/Return",
            "BackendURL": "http://10.11.22.113:8803/QPay.ApiClient/AutoPush/PushSuccess",
        }
    
    
        elif apiparams.PayType=="C":
            send_message_ori = {
            "ShopNo": Shop_No["ShopNo"],
            "OrderNo": OrderNo_total,
            "Amount": apiparams.Amount*100,
            "CurrencyID": "TWD",
            "PayType": apiparams.PayType, #管理費僅用匯款,無信用卡繳費模式
            "ATMParam": {},
            "CardParam": {"AutoBilling":"Y"},
            "ConvStoreParam": {},
            "PrdtName": "order",
            "Param1": apiparams.householder,  
            "Param2": apiparams.OrderNo,     
            "ReturnURL": "http://10.11.22.113:8803/QPay.ApiClient/Store/Return",
            "BackendURL": "http://10.11.22.113:8803/QPay.ApiClient/AutoPush/PushSuccess",
            }
    
        print('send_message_ori => ',send_message_ori)
    
        send_message_TextHash=getMessageTextHash(send_message_ori,NonceValue,hashID)
        SignValue = getSignValue(send_message_TextHash)
    
        jsonText=json.dumps(send_message_ori, ensure_ascii=False).replace(' ', "")
        print(jsonText)
        aes_encrypt_str = AES_CBC_Encrpt(hashID, IVValue, jsonText)
    
        send_POST_message={
          "Version": "1.0.0",
          "ShopNo": Shop_No["ShopNo"],
          "APIService": "OrderCreate",
          "Sign": SignValue,
          "Nonce": NonceValue,
          "Message": aes_encrypt_str
          }
    
        print('send_POST_message => ',send_POST_message)
    
        requests_link = 'https://apisbx.sinopac.com/funBIZ/QPay.WebAPI/api/Order'    
        r = requests.post(url = requests_link, json = send_POST_message )
        print('status_code => ',r.status_code)
        # print('status_json => ',r.json())
        print('status_Nonce => ',r.json()["Nonce"])
    
        NonceValue=r.json()["Nonce"]
        SHAValue ,IVValue= getSHAValue(NonceValue)
        hashID = getAESKey()
        aes_decrypt_str = AES_CBC_Decrypt(hashID, iv=IVValue, data=r.json()["Message"])
    
        print(f'解密結果為: {aes_decrypt_str}')
        decrypt_dist=json.loads(aes_decrypt_str)
        r.close()
        return {"message": "success", 'time': time.time() - start ,"decrypt_dist" : decrypt_dist}
    
    • 執行
    uvicorn app.main:app --host 0.0.0.0 --port 80
    
    • 測試
      直接使用fastapi自建的swagger測試。
      1. ATM匯款
        • 設定
          https://ithelp.ithome.com.tw/upload/images/20210924/20140924J7AedjgSYJ.png

        • 執行結果
          以下為執行結果,主要得到的資訊為查詢完成("Description": "S0000 – 處理成功"),同時也可以得到匯款帳,提供客戶直接使用ATM匯入費用。
          https://ithelp.ithome.com.tw/upload/images/20210924/20140924wB2tQb696Q.png

      2. 信用卡匯款
        • 設定
          https://ithelp.ithome.com.tw/upload/images/20210924/20140924xoFL7Z9VPw.png
        • 執行結果
          從得到的資訊中,可以得到信用卡繳費網址
          https://ithelp.ithome.com.tw/upload/images/20210924/20140924bxUkUXH1YK.png
          測試輸入規格書提供的卡號跟相關資訊資訊,即可線上刷卡
          https://ithelp.ithome.com.tw/upload/images/20210924/20140924Ezm3s3AcBC.png

訂單建立之後,就是要來查看繳費紀錄囉~明天見


上一篇
[day9]API就位囉~串接永豐API規劃
下一篇
[day11]串接API實測-訂單單筆查詢API
系列文
永豐Vue一下-從生活尋找靈感30

尚未有邦友留言

立即登入留言