海關179號

2020-10-27 12:00:05

1. 準備工作

  1. 第一步去海關注冊備案拿到ukey
  2. 去中華人民共和國海關總署下載179號檔案
  3. 新增丁鑫微信說明情況會邀請你入群,進群先看公告
    在這裡插入圖片描述

2. 接收海關請求

post方式(x-www-form-urlencoded)
從請求中獲取openReq引數的值,該引數值是一個符合json標準的字串,由於海關提交的請求是post方式(x-www-form-urlencoded),所以在url中是沒有引數的,必須要從表單中獲取該引數

海關資料範例
{"orderNo":"202009231454212140210352","sessionID":"032C3F56-0EE6-4558-B548-6C7A3451F07D","serviceTime":"1600852846372"}

接收完後需企業返回響應json格式資料 必須是json格式 10000表示成功 20000失敗

{ "code":"10000","message":"","serviceTime":1533271903898}

3. 組裝資料和上傳報文

使用接收到的sessionID就可以進行加簽(前期可以自己模擬請求)
使用ukey進行加簽獲取簽名(使用操作員ukey)
按照179號檔案進行資料拼裝

獲取簽名

1. 使用官方js請求時需按以下格式來請求

資料格式

"sessionID":"032C3F56-0EE6-4558-B548-6C7A3451F07D"||"payExchangeInfoHead":"{"guid":"E4766021-21AC-1AA2-21DD-E974DF93C11D","initalRequest":"<xml><appid><![CDATA[]]></appid><mch_id></mch_id><body><![CDATA[]]></body><nonce_str><![CDATA[]]></nonce_str><out_trade_no>202009231454212140210352</out_trade_no><total_fee>500</total_fee><spbill_create_ip><![CDATA[]]></spbill_create_ip><notify_url><![CDATA[]]></notify_url><openid><![CDATA[]]></openid><trade_type><![CDATA[JSAPI]]></trade_type><sign><![CDATA[]]></sign></xml>","initalResponse":"<xml><appid><![CDATA[]]></appid><bank_type><![CDATA[OTHERS]]></bank_type><cash_fee>500</cash_fee><fee_type><![CDATA[CNY]]></fee_type><is_subscribe><![CDATA[N]]></is_subscribe><mch_id></mch_id><nonce_str><![CDATA[]]></nonce_str><openid><![CDATA[]]></openid><out_trade_no></out_trade_no><result_code><![CDATA[SUCCESS]]></result_code><return_code><![CDATA[SUCCESS]]></return_code><sign><![CDATA[]]></sign><time_end>20200923145426</time_end><total_fee>500</total_fee><trade_type><![CDATA[JSAPI]]></trade_type><transaction_id>4200000681202009235085032319</transaction_id></xml>","ebpCode":"11111111","payCode":"4403169D3W","payTransactionId":"4200000681202009235085032319","totalAmount":5,"currency":"142","verDept":"3","payType":"4","tradingTime":"20200923145426","note":""}"||"payExchangeInfoLists":"[{"orderNo":"202009231454218421271832","goodsInfo":[{"gname":"濟州花梨精華面膜","itemLink":"https://填寫自己的商品地址"}],"recpAccount":"111111111","recpCode":"22222222222","recpName":"填寫在海關注冊的公司"}]"||"serviceTime":"1601282210417"

js請求範例
在這裡插入圖片描述

2. WebSocket請求

請求地址:
ws://127.0.0.1:61232
wss://wss.singlewindow.cn:61231

資料格式

{"_id":"1","_method":"cus-sec_SpcSignDataAsPEM","args":{"inData":"\"sessionID\":\"032C3F56-0EE6-4558-B548-6C7A3451F07D\"||\"payExchangeInfoHead\":\"{\"guid\":\"E4766021-21AC-1AA2-21DD-E974DF93C11D\",\"initalRequest\":\"<xml><appid><![CDATA[]]><\/appid><mch_id><\/mch_id><body><![CDATA[]]><\/body><nonce_str><![CDATA[]]><\/nonce_str><out_trade_no>202009231454212140210352<\/out_trade_no><total_fee>500<\/total_fee><spbill_create_ip><![CDATA[]]><\/spbill_create_ip><notify_url><![CDATA[https:\/\/www.qingwatour.com\/api\/notify\/index]]><\/notify_url><openid><![CDATA[]]><\/openid><trade_type><![CDATA[JSAPI]]><\/trade_type><sign><![CDATA[]]><\/sign><\/xml>\",\"initalResponse\":\"<xml><appid><![CDATA[]]><\/appid><bank_type><![CDATA[OTHERS]]><\/bank_type><cash_fee>500<\/cash_fee><fee_type><![CDATA[CNY]]><\/fee_type><is_subscribe><![CDATA[N]]><\/is_subscribe><mch_id><\/mch_id><nonce_str><![CDATA[]]><\/nonce_str><openid><![CDATA[]]><\/openid><out_trade_no>202009231454212140210352<\/out_trade_no><result_code><![CDATA[SUCCESS]]><\/result_code><return_code><![CDATA[SUCCESS]]><\/return_code><sign><![CDATA[]]><\/sign><time_end>20200923145426<\/time_end><total_fee>500<\/total_fee><trade_type><![CDATA[JSAPI]]><\/trade_type><transaction_id>4200000681202009235085032319<\/transaction_id><\/xml>\",\"ebpCode\":\"111111\",\"payCode\":\"4403169D3W\",\"payTransactionId\":\"4200000681202009235085032319\",\"totalAmount\":5,\"currency\":\"142\",\"verDept\":\"3\",\"payType\":\"4\",\"tradingTime\":\"20200923145426\",\"note\":\"\"}\"||\"payExchangeInfoLists\":\"[{\"orderNo\":\"202009231454218421271832\",\"goodsInfo\":[{\"gname\":\"\u6d4e\u5dde\u82b1\u68a8\u7cbe\u534e\u9762\u819c\",\"itemLink\":\"商品連結地址"}],\"recpAccount\":\"111111\",\"recpCode\":\"222222\",\"recpName\":\"測試測試"}]\"||\"serviceTime\":\"1601282210417\"","passwd":"88888888"}}

把加簽資料替換成你自己的資訊

獲取到簽名格式

{"Result":true,"Data":["KT1m7MkJ98/O5s33P0BpF34b7+gyW5WEpMvaTB+Ji0O9H3FabJuoRj8s2DxbglUbqA9mFDoCXbljgt+GaO73HE0eO8vl+eWx981S5hYumdK7sVovMTqkCKs211ZsB3edVUVfDau3D8ew8rwoda/zOBVBiDKmo3J5iQ5INxN3oF8=","證書"],"Error":[]}

上傳報文

請求地址:https://customs.chinaport.gov.cn/ceb2grab/grab/realTimeDataUpload
方式:POST
引數名:payExInfoStr
引數型別: JSON

{"sessionID":"032C3F56-0EE6-4558-B548-6C7A3451F07D","payExchangeInfoHead":{"guid":"E4766021-21AC-1AA2-21DD-E974DF93C11D","initalRequest":"<xml><appid><![CDATA[xwxwxwxwx112121]]></appid><mch_id>11111111</mch_id><body><![CDATA[測試測試]]></body><nonce_str><![CDATA[ashdgahgdhaghgliqwueyqu1]]></nonce_str><out_trade_no>202009231454212140210352</out_trade_no><total_fee>500</total_fee><spbill_create_ip><![CDATA[192.168.0.1]]></spbill_create_ip><notify_url><![CDATA[https://www.baidu.com]]></notify_url><openid><![CDATA[adasyduiyasdhjxzycua-Q]]></openid><trade_type><![CDATA[JSAPI]]></trade_type><sign><![CDATA[asd1sa56d4545wqe44wq5]]></sign></xml>","initalResponse":"<xml><appid><![CDATA[xwxwxwxwx112121]]></appid><bank_type><![CDATA[OTHERS]]></bank_type><cash_fee>500</cash_fee><fee_type><![CDATA[CNY]]></fee_type><is_subscribe><![CDATA[N]]></is_subscribe><mch_id>11111111</mch_id><nonce_str><![CDATA[ashdgahgdhaghgliqwueyqu1]]></nonce_str><openid><![CDATA[adasyduiyasdhjxzycua-Q]]></openid><out_trade_no>202009231454212140210352</out_trade_no><result_code><![CDATA[SUCCESS]]></result_code><return_code><![CDATA[SUCCESS]]></return_code><sign><![CDATA[asgdhasgdhasgdhgasgdhasgdh]]></sign><time_end>20200923145426</time_end><total_fee>500</total_fee><trade_type><![CDATA[JSAPI]]></trade_type><transaction_id>4200000681202009235085032319</transaction_id></xml>","ebpCode":"請替換","payCode":"4403169D3W","payTransactionId":"4200000681202009235085032319","totalAmount":5,"currency":"142","verDept":"3","payType":"4","tradingTime":"20200923145426","note":""},"payExchangeInfoLists":[{"orderNo":"202009231454218421271832","goodsInfo":[{"gname":"濟州花梨精華面膜","itemLink":"請替換"}],"recpAccount":"請替換","recpCode":"請替換","recpName":"請替換"}],"serviceTime":"1601282210417","certNo":"證書號要小寫","signValue":"KT1m7MkJ98/O5s33P0BpF34b7+gyW5WEpMvaTB+Ji0O9H3FabJuoRj8s2DxbglUbqA9mFDoCXbljgt+GaO73HE0eO8vl+eWx981S5hYumdK7sVovMTqkCKs211ZsB3edVUVfDau3D8ew8rwoda/zOBVBiDKmo3J5iQ5INxN3oF8="}

4. 返回資訊

{"code":"20006","message":"上傳失敗,入庫失敗 java.sql.SQLException: ORA-00001: 違反唯一約束條件 (sessionID重複)","total":0,"serviceTime":1603684304670}

返回以上json如上就基本完成,因為這裡sessionID是自己生成的或者第二次上傳,才會返回sessionID重複。可以去海關http://ceb1.chinaport.gov.cn/填寫測試訂單號進行線上模擬測試
在這裡插入圖片描述

 {"code":"20004","message":"企業實時資料獲取驗簽證書未在服務系統註冊","total":0,"serviceTime":1603693566761}

未上傳證書編號和證書,這裡需要注意的事項!!!證書編號需要小寫,並且頁面上傳的和上傳支付資料的證書編號保持一致!!! 如果已上傳請檢查證書是否一致
在這裡插入圖片描述

{"code":"20005","message":"驗籤失敗","total":0,"serviceTime":1603696878548}

檢視資料格式是否出現問題,檢視證書是否一致
有一些坑需要注意
檔案中的payExchangeInfoList應該是payExchangeInfoLists
serviceTime必須是字串型別
totalAmoun不是string型別

{"code":"10000","message":"上傳成功","total":0,"serviceTime":1603696878548}

測試完成

第一次寫這麼長的檔案可能有點亂,大佬勿噴,小弟十分感謝!