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