蛋蛋星球-制度模式
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

240 lines
9.4 KiB

  1. package alipay
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/go-pay/gopay"
  7. )
  8. // alipay.trade.royalty.relation.bind(分账关系绑定)
  9. // 文档地址:https://opendocs.alipay.com/open/c21931d6_alipay.trade.royalty.relation.bind
  10. func (a *Client) TradeRelationBind(ctx context.Context, bm gopay.BodyMap) (aliRsp *TradeRelationBindResponse, err error) {
  11. err = bm.CheckEmptyError("receiver_list", "out_request_no")
  12. if err != nil {
  13. return nil, err
  14. }
  15. var bs []byte
  16. if bs, err = a.doAliPay(ctx, bm, "alipay.trade.royalty.relation.bind"); err != nil {
  17. return nil, err
  18. }
  19. aliRsp = new(TradeRelationBindResponse)
  20. if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil {
  21. return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs))
  22. }
  23. if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil {
  24. return aliRsp, err
  25. }
  26. signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn)
  27. aliRsp.SignData = signData
  28. return aliRsp, a.autoVerifySignByCert(aliRsp.Sign, signData, signDataErr)
  29. }
  30. // alipay.trade.royalty.relation.unbind(分账关系解绑)
  31. // 文档地址:https://opendocs.alipay.com/open/3613f4e1_alipay.trade.royalty.relation.unbind
  32. func (a *Client) TradeRelationUnbind(ctx context.Context, bm gopay.BodyMap) (aliRsp *TradeRelationUnbindResponse, err error) {
  33. err = bm.CheckEmptyError("receiver_list", "out_request_no")
  34. if err != nil {
  35. return nil, err
  36. }
  37. var bs []byte
  38. if bs, err = a.doAliPay(ctx, bm, "alipay.trade.royalty.relation.unbind"); err != nil {
  39. return nil, err
  40. }
  41. aliRsp = new(TradeRelationUnbindResponse)
  42. if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil {
  43. return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs))
  44. }
  45. if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil {
  46. return aliRsp, err
  47. }
  48. signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn)
  49. aliRsp.SignData = signData
  50. return aliRsp, a.autoVerifySignByCert(aliRsp.Sign, signData, signDataErr)
  51. }
  52. // alipay.trade.royalty.relation.batchquery(分账关系查询)
  53. // 文档地址:https://opendocs.alipay.com/open/1860be54_alipay.trade.royalty.relation.batchquery
  54. func (a *Client) TradeRelationBatchQuery(ctx context.Context, bm gopay.BodyMap) (aliRsp *TradeRelationBatchQueryResponse, err error) {
  55. err = bm.CheckEmptyError("out_request_no")
  56. if err != nil {
  57. return nil, err
  58. }
  59. var bs []byte
  60. if bs, err = a.doAliPay(ctx, bm, "alipay.trade.royalty.relation.batchquery"); err != nil {
  61. return nil, err
  62. }
  63. aliRsp = new(TradeRelationBatchQueryResponse)
  64. if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil {
  65. return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs))
  66. }
  67. if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil {
  68. return aliRsp, err
  69. }
  70. signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn)
  71. aliRsp.SignData = signData
  72. return aliRsp, a.autoVerifySignByCert(aliRsp.Sign, signData, signDataErr)
  73. }
  74. // alipay.trade.settle.confirm(统一收单确认结算接口)
  75. // 文档地址:https://opendocs.alipay.com/open/028xqy
  76. func (a *Client) TradeSettleConfirm(ctx context.Context, bm gopay.BodyMap) (aliRsp *TradeSettleConfirmResponse, err error) {
  77. err = bm.CheckEmptyError("out_request_no", "trade_no")
  78. if err != nil {
  79. return nil, err
  80. }
  81. var bs []byte
  82. if bs, err = a.doAliPay(ctx, bm, "alipay.trade.settle.confirm"); err != nil {
  83. return nil, err
  84. }
  85. aliRsp = new(TradeSettleConfirmResponse)
  86. if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil {
  87. return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs))
  88. }
  89. if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil {
  90. return aliRsp, err
  91. }
  92. signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn)
  93. aliRsp.SignData = signData
  94. return aliRsp, a.autoVerifySignByCert(aliRsp.Sign, signData, signDataErr)
  95. }
  96. // alipay.trade.order.settle(统一收单交易结算接口)
  97. // 文档地址:https://opendocs.alipay.com/open/c3b24498_alipay.trade.order.settle
  98. func (a *Client) TradeOrderSettle(ctx context.Context, bm gopay.BodyMap) (aliRsp *TradeOrderSettleResponse, err error) {
  99. err = bm.CheckEmptyError("out_request_no", "trade_no", "royalty_parameters")
  100. if err != nil {
  101. return nil, err
  102. }
  103. var bs []byte
  104. if bs, err = a.doAliPay(ctx, bm, "alipay.trade.order.settle"); err != nil {
  105. return nil, err
  106. }
  107. aliRsp = new(TradeOrderSettleResponse)
  108. if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil {
  109. return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs))
  110. }
  111. if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil {
  112. return aliRsp, err
  113. }
  114. signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn)
  115. aliRsp.SignData = signData
  116. return aliRsp, a.autoVerifySignByCert(aliRsp.Sign, signData, signDataErr)
  117. }
  118. // alipay.trade.order.settle.query(交易分账查询接口)
  119. // 文档地址:https://opendocs.alipay.com/open/02pj6l
  120. func (a *Client) TradeOrderSettleQuery(ctx context.Context, bm gopay.BodyMap) (aliRsp *TradeOrderSettleQueryResponse, err error) {
  121. err = bm.CheckEmptyError("settle_no")
  122. if err != nil {
  123. return nil, err
  124. }
  125. var bs []byte
  126. if bs, err = a.doAliPay(ctx, bm, "alipay.trade.order.settle.query"); err != nil {
  127. return nil, err
  128. }
  129. aliRsp = new(TradeOrderSettleQueryResponse)
  130. if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil {
  131. return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs))
  132. }
  133. if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil {
  134. return aliRsp, err
  135. }
  136. signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn)
  137. aliRsp.SignData = signData
  138. return aliRsp, a.autoVerifySignByCert(aliRsp.Sign, signData, signDataErr)
  139. }
  140. // alipay.trade.order.onsettle.query(分账剩余金额查询)
  141. // 文档地址:https://opendocs.alipay.com/open/d87dc009_alipay.trade.order.onsettle.query
  142. func (a *Client) TradeOrderOnSettleQuery(ctx context.Context, bm gopay.BodyMap) (aliRsp *TradeOrderOnSettleQueryResponse, err error) {
  143. err = bm.CheckEmptyError("trade_no")
  144. if err != nil {
  145. return nil, err
  146. }
  147. var bs []byte
  148. if bs, err = a.doAliPay(ctx, bm, "alipay.trade.order.onsettle.query"); err != nil {
  149. return nil, err
  150. }
  151. aliRsp = new(TradeOrderOnSettleQueryResponse)
  152. if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil {
  153. return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs))
  154. }
  155. if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil {
  156. return aliRsp, err
  157. }
  158. signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn)
  159. aliRsp.SignData = signData
  160. return aliRsp, a.autoVerifySignByCert(aliRsp.Sign, signData, signDataErr)
  161. }
  162. // alipay.trade.royalty.rate.query(分账比例查询)
  163. // 文档地址:https://opendocs.alipay.com/open/6f314ee9_alipay.trade.royalty.rate.query
  164. func (a *Client) TradeRoyaltyRateQuery(ctx context.Context, bm gopay.BodyMap) (aliRsp *TradeRoyaltyRateQueryResponse, err error) {
  165. err = bm.CheckEmptyError("out_request_no")
  166. if err != nil {
  167. return nil, err
  168. }
  169. var bs []byte
  170. if bs, err = a.doAliPay(ctx, bm, "alipay.trade.royalty.rate.query"); err != nil {
  171. return nil, err
  172. }
  173. aliRsp = new(TradeRoyaltyRateQueryResponse)
  174. if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil {
  175. return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs))
  176. }
  177. if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil {
  178. return aliRsp, err
  179. }
  180. signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn)
  181. aliRsp.SignData = signData
  182. return aliRsp, a.autoVerifySignByCert(aliRsp.Sign, signData, signDataErr)
  183. }
  184. // alipay.security.risk.customerrisk.send(商户数据同步)
  185. // 文档地址:https://opendocs.alipay.com/open/02qth4
  186. func (a *Client) SecurityCustomerRiskSend(ctx context.Context, bm gopay.BodyMap) (aliRsp *SecurityCustomerRiskSendRsp, err error) {
  187. err = bm.CheckEmptyError("process_code", "trade_no")
  188. if err != nil {
  189. return nil, err
  190. }
  191. var bs []byte
  192. if bs, err = a.doAliPay(ctx, bm, "alipay.security.risk.customerrisk.send"); err != nil {
  193. return nil, err
  194. }
  195. aliRsp = new(SecurityCustomerRiskSendRsp)
  196. if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil {
  197. return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs))
  198. }
  199. if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil {
  200. return aliRsp, err
  201. }
  202. signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn)
  203. aliRsp.SignData = signData
  204. return aliRsp, a.autoVerifySignByCert(aliRsp.Sign, signData, signDataErr)
  205. }
  206. // alipay.pay.app.marketing.consult(商户前置内容咨询接口)
  207. // 文档地址:https://opendocs.alipay.com/pre-open/296d225f_alipay.pay.app.marketing.consult
  208. func (a *Client) PayAppMarketingConsult(ctx context.Context, bm gopay.BodyMap) (aliRsp *PayAppMarketingConsultRsp, err error) {
  209. err = bm.CheckEmptyError("biz_scene", "total_amount", "product_code")
  210. if err != nil {
  211. return nil, err
  212. }
  213. var bs []byte
  214. if bs, err = a.doAliPay(ctx, bm, "alipay.pay.app.marketing.consult"); err != nil {
  215. return nil, err
  216. }
  217. aliRsp = new(PayAppMarketingConsultRsp)
  218. if err = json.Unmarshal(bs, aliRsp); err != nil || aliRsp.Response == nil {
  219. return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs))
  220. }
  221. if err = bizErrCheck(aliRsp.Response.ErrorResponse); err != nil {
  222. return aliRsp, err
  223. }
  224. signData, signDataErr := a.getSignData(bs, aliRsp.AlipayCertSn)
  225. aliRsp.SignData = signData
  226. return aliRsp, a.autoVerifySignByCert(aliRsp.Sign, signData, signDataErr)
  227. }