面包店
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

147 lines
5.1 KiB

  1. package svc
  2. import (
  3. "applet/app/pay/md"
  4. svcComm "applet/app/svc"
  5. "applet/app/utils"
  6. "applet/app/utils/logx"
  7. "encoding/json"
  8. "encoding/xml"
  9. "fmt"
  10. "github.com/gin-gonic/gin"
  11. "github.com/iGoogle-ink/gopay"
  12. "io/ioutil"
  13. "net/url"
  14. )
  15. // PayFuncList 支付参数
  16. var PayFuncList = map[string]map[string]func(*gin.Context) (interface{}, error){
  17. }
  18. // PayCallbackFuncList 回调
  19. var PayCallbackFuncList = map[string]map[string]func(*gin.Context){
  20. }
  21. // PayRefundFuncList 退款
  22. var PayRefundFuncList = map[string]map[string]func(*gin.Context, string){
  23. // 自营商品商品
  24. md.MallGoods: {
  25. md.BALANCE_PAY: nil,
  26. md.ALIPAY: nil,
  27. md.WX_PAY: nil,
  28. },
  29. }
  30. // PayStatusFuncList 支付状态
  31. var PayStatusFuncList = map[string]func(*gin.Context) (interface{}, error){
  32. // 商学院
  33. // md.BusinessCollege: PayStatus,
  34. }
  35. // GetAllPayMethodList 获取支付列表
  36. func GetAllPayMethodList(c *gin.Context) map[string]map[string]string {
  37. user := svcComm.GetUser(c)
  38. return map[string]map[string]string{
  39. md.BALANCE_PAY: {
  40. "name": md.PayMethod[md.BALANCE_PAY],
  41. "sub_name": "当前余额:¥" + user.Profile.FinValid,
  42. "type": md.BALANCE_PAY,
  43. "icon": svcComm.OffImageFormat(c, "balance.png"),
  44. },
  45. md.WX_PAY: {
  46. "name": md.PayMethod[md.WX_PAY],
  47. "sub_name": "使用微信支付",
  48. "type": md.WX_PAY,
  49. "icon": svcComm.OffImageFormat(c, "wx_pay.png"),
  50. },
  51. md.ALIPAY: {
  52. "name": md.PayMethod[md.ALIPAY],
  53. "sub_name": "使用支付宝支付",
  54. "type": md.ALIPAY,
  55. "icon": svcComm.OffImageFormat(c, "alipay.png"),
  56. },
  57. }
  58. }
  59. // GetAllPayMethodArray 获取支付列表--接口用
  60. func GetAllPayMethodArray(c *gin.Context) []map[string]string {
  61. user := svcComm.GetUser(c)
  62. return []map[string]string{
  63. {
  64. "name": md.PayMethod[md.BALANCE_PAY],
  65. "sub_name": "当前余额:¥" + user.Profile.FinValid,
  66. "type": md.BALANCE_PAY,
  67. "icon": svcComm.ImageFormat(c, "balance.png"),
  68. },
  69. {
  70. "name": md.PayMethod[md.WX_PAY],
  71. "sub_name": "使用微信支付",
  72. "type": md.WX_PAY,
  73. "icon": svcComm.ImageFormat(c, "wx_pay.png"),
  74. },
  75. {
  76. "name": md.PayMethod[md.ALIPAY],
  77. "sub_name": "使用支付宝支付",
  78. "type": md.ALIPAY,
  79. "icon": svcComm.ImageFormat(c, "alipay.png"),
  80. },
  81. }
  82. }
  83. func CheckAllCallbackParams(c *gin.Context) (interface{}, string, string) {
  84. body, _ := ioutil.ReadAll(c.Request.Body)
  85. dataAlipay, _ := GetAlipayCallbackParams(body)
  86. if dataAlipay != nil && dataAlipay.PayMethod == md.ALIPAY {
  87. return dataAlipay, dataAlipay.OrderType, dataAlipay.PayMethod
  88. }
  89. dataWxPay, _ := GetWxPayCallbackParams(body)
  90. if dataWxPay != nil && dataWxPay.PayMethod == md.WX_PAY {
  91. return dataWxPay, dataWxPay.OrderType, dataWxPay.PayMethod
  92. }
  93. return nil, "", ""
  94. }
  95. // GetAlipayCallbackParams 支付宝参数解析
  96. func GetAlipayCallbackParams(body []byte) (*md.AliPayCallback, error) {
  97. //decodeArgs := "order_type=privilege_card&pay_method=alipay&master_id=123456&gmt_create=2021-01-06+15%3A33%3A18&charset=utf-8&seller_email=1666296478%40qq.com&subject=%E6%B5%8B%E8%AF%95&sign=ILbQtP7E51hcdKaroi%2FKlefltw%2BaSOeaqgvBhYIcIuRy5yy440OIOdxPEvhTITA%2BHnA1Lgf3STepzzHVmvvTFIGeKL%2FVvz%2FLSI7vXGpUBxcw2entVZCzhmblGAB8hiK4EOHBIfUAQiyo9ePWl63p%2B%2BvS4CQuZe8SFXrFaj2bTnRyBPemxekudU8TP8tZd630SzHx7kOHYqxTYZ7kHMrLK0fjIdSWBHtSD2cocm%2FxVQ3wYXUyyfZwwTpcXLW9ao97Uj1hZSnknuFRtMfvE57D5f4W3cB%2Bnp%2B39xpuvfbBlbXXllzKUfkWVBqV6zebYMnzaogwVN%2FDXgp74BQOzkJYvA%3D%3D&buyer_id=2088012352245491&invoice_amount=0.01&notify_id=2021010600222153319045491449160553&fund_bill_list=%5B%7B%22amount%22%3A%220.01%22%2C%22fundChannel%22%3A%22PCREDIT%22%7D%5D&notify_type=trade_status_sync&trade_status=TRADE_SUCCESS&receipt_amount=0.01&app_id=2016120103683451&buyer_pay_amount=0.01&sign_type=RSA2&seller_id=2088221294035253&gmt_payment=2021-01-06+15%3A33%3A18&notify_time=2021-01-06+15%3A33%3A19&passback_params=706160991839039994&version=1.0&out_trade_no=791161468268877545&total_amount=0.01&trade_no=2021010622001445491401511921&auth_app_id=2016120103683451&buyer_logon_id=150****0420&point_amount=0.00"
  98. decodeArgs, err := url.QueryUnescape(string(body))
  99. if err != nil {
  100. _ = logx.Warn(err)
  101. return nil, logx.Warn("回调参数解码错误")
  102. }
  103. data, err := url.ParseQuery(decodeArgs)
  104. if err != nil {
  105. return nil, err
  106. }
  107. dataMap := make(map[string]interface{})
  108. for k := range data {
  109. dataMap[k] = data.Get(k)
  110. }
  111. callbackStr := utils.Serialize(dataMap)
  112. fmt.Println("支付宝回调数据", string(callbackStr))
  113. var args md.AliPayCallback
  114. if err := json.Unmarshal(callbackStr, &args); err != nil {
  115. return nil, logx.Warn(err)
  116. }
  117. return &args, nil
  118. }
  119. // GetWxPayCallbackParams 微信回调参数解析
  120. func GetWxPayCallbackParams(body []byte) (*md.WxPayCallback, error) {
  121. dataMap := make(gopay.BodyMap)
  122. if err := xml.Unmarshal(body, &dataMap); err != nil {
  123. return nil, fmt.Errorf("xml.Unmarshal(%s):%w", string(body), err)
  124. }
  125. callbackStr := utils.Serialize(dataMap)
  126. fmt.Println("微信回调数据", string(callbackStr))
  127. var args md.WxPayCallback
  128. err := json.Unmarshal(callbackStr, &args)
  129. if err != nil {
  130. fmt.Println("err3")
  131. return nil, logx.Warn(err)
  132. }
  133. return &args, nil
  134. }