蛋蛋星球-制度模式
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

93 rader
3.0 KiB

  1. package alipay
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/go-pay/gopay"
  7. "github.com/go-pay/gopay/pkg/xhttp"
  8. )
  9. // alipay.trade.customs.declare(统一收单报关接口)
  10. // 文档地址:https://opendocs.alipay.com/apis/api_29/alipay.trade.customs.declare
  11. func (a *Client) TradeCustomsDeclare(ctx context.Context, bm gopay.BodyMap) (aliRsp *TradeCustomsDeclareRsp, err error) {
  12. err = bm.CheckEmptyError("out_request_no", "trade_no", "merchant_customs_code", "merchant_customs_name", "amount", "customs_place")
  13. if err != nil {
  14. return nil, err
  15. }
  16. var bs []byte
  17. if bs, err = a.doAliPay(ctx, bm, "alipay.trade.customs.declare"); err != nil {
  18. return nil, err
  19. }
  20. aliRsp = new(TradeCustomsDeclareRsp)
  21. if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil {
  22. return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs))
  23. }
  24. if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil {
  25. return aliRsp, err
  26. }
  27. signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn)
  28. aliRsp.SignData = signData
  29. return aliRsp, a.autoVerifySignByCert(aliRsp.Sign, signData, signDataErr)
  30. }
  31. // alipay.acquire.customs(报关接口)
  32. // 文档地址:https://opendocs.alipay.com/pre-open/01x3kh
  33. func (a *Client) AcquireCustoms(ctx context.Context, bm gopay.BodyMap) (aliRspBs []byte, err error) {
  34. err = bm.CheckEmptyError("partner", "out_request_no", "trade_no", "merchant_customs_code", "amount", "customs_place", "merchant_customs_name")
  35. if err != nil {
  36. return nil, err
  37. }
  38. bs, err := a.doAliPayCustoms(ctx, bm, "alipay.acquire.customs")
  39. if err != nil {
  40. return nil, err
  41. }
  42. return bs, nil
  43. }
  44. // alipay.overseas.acquire.customs.query(报关查询接口)
  45. // 文档地址:https://opendocs.alipay.com/pre-open/01x3ki
  46. func (a *Client) AcquireCustomsQuery(ctx context.Context, bm gopay.BodyMap) (aliRspBs []byte, err error) {
  47. err = bm.CheckEmptyError("partner", "out_request_nos")
  48. if err != nil {
  49. return nil, err
  50. }
  51. bs, err := a.doAliPayCustoms(ctx, bm, "alipay.overseas.acquire.customs.query")
  52. if err != nil {
  53. return nil, err
  54. }
  55. return bs, nil
  56. }
  57. // 向支付宝发送请求
  58. func (a *Client) doAliPayCustoms(ctx context.Context, bm gopay.BodyMap, service string) (bs []byte, err error) {
  59. bm.Set("service", service).
  60. Set("_input_charset", "utf-8")
  61. bm.Remove("sign_type")
  62. bm.Remove("sign")
  63. sign, err := a.getRsaSign(bm, RSA)
  64. if err != nil {
  65. return nil, fmt.Errorf("GetRsaSign Error: %v", err)
  66. }
  67. bm.Set("sign_type", RSA).Set("sign", sign)
  68. if a.DebugSwitch == gopay.DebugOn {
  69. a.logger.Debugf("Alipay_Request: %s", bm.JsonBody())
  70. }
  71. // request
  72. res, bs, err := a.hc.Req(xhttp.TypeFormData).Post("https://mapi.alipay.com/gateway.do").SendString(bm.EncodeURLParams()).EndBytes(ctx)
  73. if err != nil {
  74. return nil, err
  75. }
  76. if a.DebugSwitch == gopay.DebugOn {
  77. a.logger.Debugf("Alipay_Response: %d, %s", res.StatusCode, string(bs))
  78. }
  79. if res.StatusCode != 200 {
  80. return nil, fmt.Errorf("HTTP Request Error, StatusCode = %d", res.StatusCode)
  81. }
  82. return bs, nil
  83. }