Skip to content

Commit ca411df

Browse files
committed
add alipay customs interface
1 parent d76f831 commit ca411df

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

alipay/customs.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package alipay
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
7+
"github.com/go-pay/gopay"
8+
"github.com/go-pay/gopay/pkg/xhttp"
9+
"github.com/go-pay/gopay/pkg/xlog"
10+
)
11+
12+
// alipay.trade.customs.declare(统一收单报关接口)
13+
// 文档地址:https://opendocs.alipay.com/apis/api_29/alipay.trade.customs.declare
14+
func (a *Client) TradeCustomsDeclare(bm gopay.BodyMap) (aliRsp *TradeCustomsDeclareRsp, err error) {
15+
err = bm.CheckEmptyError("out_request_no", "trade_no", "merchant_customs_code", "merchant_customs_name", "amount", "customs_place")
16+
if err != nil {
17+
return nil, err
18+
}
19+
var bs []byte
20+
if bs, err = a.doAliPay(bm, "alipay.trade.customs.declare"); err != nil {
21+
return nil, err
22+
}
23+
aliRsp = new(TradeCustomsDeclareRsp)
24+
if err = json.Unmarshal(bs, aliRsp); err != nil {
25+
return nil, err
26+
}
27+
if aliRsp.Response != nil && aliRsp.Response.Code != "10000" {
28+
info := aliRsp.Response
29+
return nil, fmt.Errorf(`{"code":"%s","msg":"%s","sub_code":"%s","sub_msg":"%s"}`, info.Code, info.Msg, info.SubCode, info.SubMsg)
30+
}
31+
signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn)
32+
aliRsp.SignData = signData
33+
return aliRsp, a.autoVerifySignByCert(aliRsp.Sign, signData, signDataErr)
34+
}
35+
36+
// alipay.acquire.customs(报关接口)
37+
// 文档地址:https://opendocs.alipay.com/pre-open/01x3kh
38+
func (a *Client) AcquireCustoms(bm gopay.BodyMap) (aliRspBs []byte, err error) {
39+
err = bm.CheckEmptyError("partner", "out_request_no", "trade_no", "merchant_customs_code", "amount", "customs_place", "merchant_customs_name")
40+
if err != nil {
41+
return nil, err
42+
}
43+
bs, err := a.doAliPayCustoms(bm, "alipay.acquire.customs")
44+
if err != nil {
45+
return nil, err
46+
}
47+
return bs, nil
48+
}
49+
50+
// alipay.overseas.acquire.customs.query(报关查询接口)
51+
// 文档地址:https://opendocs.alipay.com/pre-open/01x3ki
52+
func (a *Client) AcquireCustomsQuery(bm gopay.BodyMap) (aliRspBs []byte, err error) {
53+
err = bm.CheckEmptyError("partner", "out_request_nos")
54+
if err != nil {
55+
return nil, err
56+
}
57+
bs, err := a.doAliPayCustoms(bm, "alipay.overseas.acquire.customs.query")
58+
if err != nil {
59+
return nil, err
60+
}
61+
return bs, nil
62+
}
63+
64+
// 向支付宝发送请求
65+
func (a *Client) doAliPayCustoms(bm gopay.BodyMap, service string) (bs []byte, err error) {
66+
bm.Set("service", service).
67+
Set("_input_charset", "utf-8")
68+
bm.Remove("sign_type")
69+
bm.Remove("sign")
70+
71+
sign, err := GetRsaSign(bm, RSA, a.PrivateKeyType, a.PrivateKey)
72+
if err != nil {
73+
return nil, fmt.Errorf("GetRsaSign Error: %v", err)
74+
}
75+
76+
bm.Set("sign_type", RSA).Set("sign", sign)
77+
if a.DebugSwitch == gopay.DebugOn {
78+
req, _ := json.Marshal(bm)
79+
xlog.Debugf("Alipay_Request: %s", req)
80+
}
81+
param := FormatURLParam(bm)
82+
// request
83+
httpClient := xhttp.NewClient()
84+
res, bs, errs := httpClient.Type(xhttp.TypeForm).Post("https://mapi.alipay.com/gateway.do").SendString(param).EndBytes()
85+
if len(errs) > 0 {
86+
return nil, errs[0]
87+
}
88+
if a.DebugSwitch == gopay.DebugOn {
89+
xlog.Debugf("Alipay_Response: %s%d %s%s", xlog.Red, res.StatusCode, xlog.Reset, string(bs))
90+
}
91+
if res.StatusCode != 200 {
92+
return nil, fmt.Errorf("HTTP Request Error, StatusCode = %d", res.StatusCode)
93+
}
94+
return bs, nil
95+
}

alipay/model.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,3 +1041,23 @@ type MerchantItemFileUpload struct {
10411041
MaterialId string `json:"material_id"` // 文件在商品中心的素材标识(素材ID长期有效)
10421042
MaterialKey string `json:"material_key"` // 文件在商品中心的素材标示,创建/更新商品时使用
10431043
}
1044+
1045+
// ===============================================================
1046+
type TradeCustomsDeclareRsp struct {
1047+
Response *TradeCustomsDeclare `json:"alipay_trade_customs_declare_response"`
1048+
AlipayCertSn string `json:"alipay_cert_sn,omitempty"`
1049+
SignData string `json:"-"`
1050+
Sign string `json:"sign"`
1051+
}
1052+
1053+
type TradeCustomsDeclare struct {
1054+
ErrorResponse
1055+
TradeNo string `json:"trade_no,omitempty"`
1056+
AlipayDeclareNo string `json:"alipay_declare_no"`
1057+
PayCode string `json:"pay_code,omitempty"`
1058+
PayTransactionId string `json:"pay_transaction_id,omitempty"`
1059+
TotalAmount string `json:"total_amount,omitempty"`
1060+
Currency string `json:"currency,omitempty"`
1061+
VerDept string `json:"ver_dept,omitempty"`
1062+
IdentityCheck string `json:"identity_check,omitempty"`
1063+
}

0 commit comments

Comments
 (0)