13

海关179号对接

 2 years ago
source link: https://its201.com/article/qq_39393201/109207579
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

1. 准备工作

  1. 第一步去海关注册备案拿到ukey
  2. 去中华人民共和国海关总署下载179号文档对接前先看一遍文档
  3. https://www.chinaport.gov.cn/pages/news/news-detail.html?jumpNoticeId=1878(群公告很重要)

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请求示例
海关179 js加签,海关179号对接,海关165号对接

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":[]}

4. 上传报文

请求地址: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="}

返回信息

{
    "code":"20006","message":"上传失败,入库失败 java.sql.SQLException: ORA-00001: 违反唯一约束条件 (sessionID重复)","total":0,"serviceTime":1603684304670}

返回以上json如上就基本完成,因为这里sessionID是自己生成的或者第二次上传,才会返回sessionID重复。可以去海关http://ceb1.chinaport.gov.cn/填写测试订单号进行线上模拟测试
填写海关1791,165线上测试订单,海关179号对接,海关165号对接

 {
    "code":"20004","message":"企业实时数据获取验签证书未在服务系统注册","total":0,"serviceTime":1603693566761}

未上传证书编号和证书,这里需要注意的事项!!!证书编号需要小写,并且页面上传的和上传支付数据的证书编号保持一致!!! 如果已上传请检查证书是否一致
海光179,165注册服务地址,海关179号对接,海关165号对接

{
    "code":"20005","message":"验签失败","total":0,"serviceTime":1603696878548}

查看数据格式是否出现问题,查看证书是否一致
有一些坑需要注意
文档中的payExchangeInfoList应该是payExchangeInfoLists
serviceTime必须是字符串类型
totalAmoun不是string类型
查看上传数据和加签数据的时间guid是否一致

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

测试完成

第一次写这么长的文档可能有点乱,大佬勿喷,小弟十分感谢!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK