蛋蛋星球-制度模式
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

112 linhas
3.7 KiB

  1. package alipay
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. "github.com/go-pay/gopay"
  8. )
  9. // 解析支付宝支付异步通知的参数到BodyMap
  10. // req:*http.Request
  11. // 返回参数bm:Notify请求的参数
  12. // 返回参数err:错误信息
  13. // 文档:https://opendocs.alipay.com/open/203/105286
  14. func ParseNotifyToBodyMap(req *http.Request) (bm gopay.BodyMap, err error) {
  15. if err = req.ParseForm(); err != nil {
  16. return nil, err
  17. }
  18. var form map[string][]string = req.Form
  19. bm = make(gopay.BodyMap, len(form)+1)
  20. for k, v := range form {
  21. if len(v) == 1 {
  22. bm.Set(k, v[0])
  23. }
  24. }
  25. return
  26. }
  27. // 通过 url.Values 解析支付宝支付异步通知的参数到BodyMap
  28. // value:url.Values
  29. // 返回参数notifyReq:Notify请求的参数
  30. // 返回参数err:错误信息
  31. // 文档:https://opendocs.alipay.com/open/203/105286
  32. func ParseNotifyByURLValues(value url.Values) (bm gopay.BodyMap, err error) {
  33. bm = make(gopay.BodyMap, len(value)+1)
  34. for k, v := range value {
  35. if len(v) == 1 {
  36. bm.Set(k, v[0])
  37. }
  38. }
  39. return
  40. }
  41. // Deprecated
  42. // 推荐使用 ParseNotifyToBodyMap(),以防阿里云通知参数变动,NotifyRequest 无法解析。
  43. // 解析支付宝支付异步通知的参数到Struct
  44. // req:*http.Request
  45. // 返回参数notifyReq:Notify请求的参数
  46. // 返回参数err:错误信息
  47. // 文档:https://opendocs.alipay.com/open/203/105286
  48. func ParseNotifyResult(req *http.Request) (notifyReq *NotifyRequest, err error) {
  49. notifyReq = new(NotifyRequest)
  50. if err = req.ParseForm(); err != nil {
  51. return
  52. }
  53. notifyReq.NotifyTime = req.Form.Get("notify_time")
  54. notifyReq.NotifyType = req.Form.Get("notify_type")
  55. notifyReq.NotifyId = req.Form.Get("notify_id")
  56. notifyReq.AppId = req.Form.Get("app_id")
  57. notifyReq.Charset = req.Form.Get("charset")
  58. notifyReq.Version = req.Form.Get("version")
  59. notifyReq.SignType = req.Form.Get("sign_type")
  60. notifyReq.Sign = req.Form.Get("sign")
  61. notifyReq.AuthAppId = req.Form.Get("auth_app_id")
  62. notifyReq.TradeNo = req.Form.Get("trade_no")
  63. notifyReq.OutTradeNo = req.Form.Get("out_trade_no")
  64. notifyReq.OutBizNo = req.Form.Get("out_biz_no")
  65. notifyReq.BuyerId = req.Form.Get("buyer_id")
  66. notifyReq.BuyerLogonId = req.Form.Get("buyer_logon_id")
  67. notifyReq.SellerId = req.Form.Get("seller_id")
  68. notifyReq.SellerEmail = req.Form.Get("seller_email")
  69. notifyReq.TradeStatus = req.Form.Get("trade_status")
  70. notifyReq.TotalAmount = req.Form.Get("total_amount")
  71. notifyReq.ReceiptAmount = req.Form.Get("receipt_amount")
  72. notifyReq.InvoiceAmount = req.Form.Get("invoice_amount")
  73. notifyReq.BuyerPayAmount = req.Form.Get("buyer_pay_amount")
  74. notifyReq.PointAmount = req.Form.Get("point_amount")
  75. notifyReq.RefundFee = req.Form.Get("refund_fee")
  76. notifyReq.Subject = req.Form.Get("subject")
  77. notifyReq.Body = req.Form.Get("body")
  78. notifyReq.GmtCreate = req.Form.Get("gmt_create")
  79. notifyReq.GmtPayment = req.Form.Get("gmt_payment")
  80. notifyReq.GmtRefund = req.Form.Get("gmt_refund")
  81. notifyReq.GmtClose = req.Form.Get("gmt_close")
  82. notifyReq.PassbackParams = req.Form.Get("passback_params")
  83. billList := req.Form.Get("fund_bill_list")
  84. if billList != gopay.NULL {
  85. bills := make([]*FundBillListInfo, 0)
  86. if err = json.Unmarshal([]byte(billList), &bills); err != nil {
  87. return nil, fmt.Errorf(`"fund_bill_list" json.Unmarshal(%s):%w`, billList, err)
  88. }
  89. notifyReq.FundBillList = bills
  90. } else {
  91. notifyReq.FundBillList = nil
  92. }
  93. detailList := req.Form.Get("voucher_detail_list")
  94. if detailList != gopay.NULL {
  95. details := make([]*NotifyVoucherDetail, 0)
  96. if err = json.Unmarshal([]byte(detailList), &details); err != nil {
  97. return nil, fmt.Errorf(`"voucher_detail_list" json.Unmarshal(%s):%w`, detailList, err)
  98. }
  99. notifyReq.VoucherDetailList = details
  100. } else {
  101. notifyReq.VoucherDetailList = nil
  102. }
  103. return
  104. }