支付模块
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.

142 rader
4.8 KiB

  1. package pay
  2. import (
  3. "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/md"
  4. zhios_pay_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/utils"
  5. "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/utils/cache"
  6. "encoding/json"
  7. "fmt"
  8. "github.com/gin-gonic/gin"
  9. "github.com/pkg/errors"
  10. "time"
  11. )
  12. //酷点 - 支付宝APP支付
  13. func AliAppPayByKuDian(payParams *md.KuDianAliAppPayParams, isPrd bool) (string, error) {
  14. var url string
  15. if isPrd == true {
  16. url = md.ZHIOS_PAY_URL_PRD + "/kuDianPay/Pay/aliAppPay"
  17. } else {
  18. url = md.ZHIOS_PAY_URL_DEV + "/kuDianPay/Pay/aliAppPay"
  19. }
  20. zhios_pay_utils.FilePutContents("AliAppPayByKuDian", zhios_pay_utils.SerializeStr(map[string]interface{}{
  21. "data": payParams,
  22. }))
  23. bytes, err := zhios_pay_utils.CurlPost(url, zhios_pay_utils.Serialize(payParams), nil)
  24. if err != nil {
  25. return "", err
  26. }
  27. var result struct {
  28. Code int `json:"code"`
  29. Msg string `json:"msg"`
  30. Data map[string]string `json:"data"`
  31. }
  32. zhios_pay_utils.FilePutContents("AliAppPayByKuDian", zhios_pay_utils.SerializeStr(result))
  33. err = json.Unmarshal(bytes, &result)
  34. if err != nil {
  35. return "", err
  36. }
  37. if result.Code != 0 {
  38. return "", errors.New(result.Msg)
  39. }
  40. zhios_pay_utils.FilePutContents("AliAppPayByKuDian", zhios_pay_utils.SerializeStr(result.Data["data"]))
  41. return result.Data["url"], nil
  42. }
  43. // 酷点 - 支付宝JsAPi支付
  44. func AliJsApiPayByKuDian(payParams *md.KuDianAliAppPayParams, isPrd bool) (string, error) {
  45. var url string
  46. if isPrd == true {
  47. url = md.ZHIOS_PAY_URL_PRD + "/kuDianPay/Pay/aliJsApiPay"
  48. } else {
  49. url = md.ZHIOS_PAY_URL_DEV + "/kuDianPay/Pay/aliJsApiPay"
  50. }
  51. zhios_pay_utils.FilePutContents("AliAppPayByKuDian", zhios_pay_utils.SerializeStr(map[string]interface{}{
  52. "data": payParams,
  53. }))
  54. bytes, err := zhios_pay_utils.CurlPost(url, zhios_pay_utils.Serialize(payParams), nil)
  55. if err != nil {
  56. return "", err
  57. }
  58. var result struct {
  59. Code int `json:"code"`
  60. Msg string `json:"msg"`
  61. Data map[string]string `json:"data"`
  62. }
  63. zhios_pay_utils.FilePutContents("AliAppPayByKuDian", zhios_pay_utils.SerializeStr(result))
  64. err = json.Unmarshal(bytes, &result)
  65. if err != nil {
  66. return "", err
  67. }
  68. if result.Code != 0 {
  69. return "", errors.New(result.Msg)
  70. }
  71. zhios_pay_utils.FilePutContents("AliAppPayByKuDian", zhios_pay_utils.SerializeStr(result.Data["data"]))
  72. return result.Data["trade_no"], nil
  73. }
  74. // 酷点 - 微信小程序支付
  75. func WxAppletPayByKuDian(payParams *md.KuDianWxPayParams, isPrd bool) (interface{}, error) {
  76. var url string
  77. if isPrd == true {
  78. url = md.ZHIOS_PAY_URL_PRD + "/kuDianPay/Pay/wxJsAPi"
  79. } else {
  80. url = md.ZHIOS_PAY_URL_DEV + "/kuDianPay/Pay/wxJsAPi"
  81. }
  82. zhios_pay_utils.FilePutContents("WxAppletPayByKuDian", zhios_pay_utils.SerializeStr(map[string]interface{}{
  83. "data": payParams,
  84. }))
  85. bytes, err := zhios_pay_utils.CurlPost(url, zhios_pay_utils.Serialize(payParams), nil)
  86. if err != nil {
  87. return nil, err
  88. }
  89. var result struct {
  90. Code int `json:"code"`
  91. Msg string `json:"msg"`
  92. Data map[string]interface{} `json:"data"`
  93. }
  94. zhios_pay_utils.FilePutContents("WxAppletPayByKuDian", zhios_pay_utils.SerializeStr(result))
  95. err = json.Unmarshal(bytes, &result)
  96. if err != nil {
  97. return nil, err
  98. }
  99. if result.Code != 0 {
  100. return nil, errors.New(result.Msg)
  101. }
  102. zhios_pay_utils.FilePutContents("WxAppletPayByKuDian", zhios_pay_utils.SerializeStr(result.Data["data"]))
  103. return result.Data["data"], nil
  104. }
  105. // 酷点 - 检查返回参数
  106. func CheckAllCallbackParamsByKuDian(c *gin.Context) (interface{}, error) {
  107. var dataWx md.KuDianPayCallback
  108. if err := c.ShouldBindJSON(&dataWx); err != nil {
  109. fmt.Println(">>>>>>>>>CheckAllCallbackParamsByKuDian>>>>>>>>>>>>>>", err)
  110. return nil, err
  111. }
  112. var dataPay md.KuDianPayCallbackResp
  113. if err := json.Unmarshal([]byte(dataWx.Result), &dataPay); err != nil {
  114. fmt.Println(">>>>>>>>>CheckAllCallbackParamsByKuDian>>>>>>>>>>>>>>", err)
  115. return nil, err
  116. }
  117. zhios_pay_utils.FilePutContents("CheckAllCallbackParamsByKuDian", zhios_pay_utils.SerializeStr(dataPay))
  118. return dataPay, nil
  119. }
  120. func MakePayParamsByKuDian(url, uniqueIdentifierOne, uniqueIdentifierTwo, appletOriginalId, queryParams string, params map[string]interface{}) (map[string]string, error) {
  121. if params == nil {
  122. return nil, errors.New("参数解析错误")
  123. }
  124. data := md.MakePayParamsByKuDian{
  125. RequestUrl: url,
  126. Params: params,
  127. }
  128. redisKey := fmt.Sprintf(md.WX_APP_TO_APPLET_PAY_UNIQUE_IDENTIFIER, uniqueIdentifierOne, uniqueIdentifierTwo, zhios_pay_utils.AnyToString(time.Now().UnixNano()))
  129. cache.SetJson(redisKey, data, md.WX_APP_TO_APPLET_PAY_UNIQUE_IDENTIFIER_CACHE_TIME)
  130. var r = map[string]string{}
  131. r["use_pay_method"] = "third_party"
  132. r["applet_original_id"] = appletOriginalId
  133. r["wx_url"] = md.WX_APPLET_PAY_URL + redisKey + "&" + queryParams
  134. return r, nil
  135. }