|
| 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 | +} |
0 commit comments