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

200 lines
6.6 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. }
  136. func KuDianAlipayConfig(req, payConfig map[string]string) (*md.KuDianAliAppPayParams, error) {
  137. var payParams = &md.KuDianAliAppPayParams{
  138. OutTradeNo: payConfig["ord_id"],
  139. NotifyUrl: payConfig["notify_url"],
  140. Description: payConfig["subject"],
  141. TotalAmount: payConfig["amount"],
  142. Attach: req["mid"],
  143. MustParams: struct {
  144. ShopId string `json:"shop_id"`
  145. KuDianMchId string `json:"ku_dian_mch_id"`
  146. KuDianSecretKey string `json:"ku_dian_secret_key"`
  147. }{
  148. ShopId: payConfig["pay_third_party_id"],
  149. KuDianMchId: req["ku_dian_mch_id"],
  150. KuDianSecretKey: req["ku_dian_secret_key"],
  151. },
  152. }
  153. if payConfig["trade_type"] != "" {
  154. payParams.TradeType = payConfig["trade_type"]
  155. }
  156. if payConfig["wap_mame"] != "" {
  157. payParams.SenceInfo.WapName = payConfig["wap_mame"]
  158. }
  159. if payConfig["return_url"] != "" {
  160. payParams.SenceInfo.ReturnUrl = payConfig["return_url"]
  161. }
  162. if payConfig["wap_url"] != "" {
  163. payParams.SenceInfo.WapUrl = payConfig["wap_url"]
  164. }
  165. if payConfig["trade_type"] != "" {
  166. payParams.TradeType = payConfig["trade_type"]
  167. }
  168. return payParams, nil
  169. }
  170. func KuDianWxConfig(req, payConfig map[string]string) (*md.KuDianWxPayParams, error) {
  171. var payParams = &md.KuDianWxPayParams{
  172. OutTradeNo: payConfig["ord_id"],
  173. NotifyUrl: payConfig["notify_url"],
  174. Description: payConfig["subject"],
  175. TotalAmount: payConfig["amount"],
  176. AppId: req["wx_app_id"],
  177. OpenId: req["wx_open_id"],
  178. Attach: req["mid"],
  179. MustParams: struct {
  180. ShopId string `json:"shop_id"`
  181. KuDianMchId string `json:"ku_dian_mch_id"`
  182. KuDianSecretKey string `json:"ku_dian_secret_key"`
  183. }{
  184. ShopId: payConfig["pay_third_party_id"],
  185. KuDianMchId: req["ku_dian_mch_id"],
  186. KuDianSecretKey: req["ku_dian_secret_key"],
  187. },
  188. }
  189. return payParams, nil
  190. }