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

pay_by_kudian.go 7.1 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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) (md.KuDianPayCallbackResp, error) {
  107. var dataWx md.KuDianPayCallback
  108. var dataPay md.KuDianPayCallbackResp
  109. if err := c.ShouldBindJSON(&dataWx); err != nil {
  110. fmt.Println(">>>>>>>>>CheckAllCallbackParamsByKuDian>>>>>>>>>>>>>>", err)
  111. return dataPay, err
  112. }
  113. if err := json.Unmarshal([]byte(dataWx.Result), &dataPay); err != nil {
  114. fmt.Println(">>>>>>>>>CheckAllCallbackParamsByKuDian>>>>>>>>>>>>>>", err)
  115. return dataPay, err
  116. }
  117. zhios_pay_utils.FilePutContents("CheckAllCallbackParamsByKuDian", zhios_pay_utils.SerializeStr(dataPay))
  118. return dataPay, nil
  119. }
  120. /**
  121. * MakePayParamsByKuDian(构造酷点支付参数[APP跳转小程序支付])
  122. * @param url string 请求URL
  123. * @param uniqueIdentifierOne string 标识占位符-第一个 (示例: 如 masterId)
  124. * @param uniqueIdentifierTwo string 标识占位符-第二个 (示例: 如 uid)
  125. * @param appletOriginalId string 小程序原始id
  126. * @param queryParams string url上参数(示例: "&ord_id=123131313434343&state_zh=待支付&total=6.66")
  127. * @param params map 请求参数
  128. */
  129. func MakePayParamsByKuDian(url, uniqueIdentifierOne, uniqueIdentifierTwo, appletOriginalId, queryParams string, params map[string]interface{}) (map[string]string, error) {
  130. if params == nil {
  131. return nil, errors.New("参数解析错误")
  132. }
  133. data := md.MakePayParamsByKuDian{
  134. RequestUrl: url,
  135. Params: params,
  136. }
  137. redisKey := fmt.Sprintf(md.WX_APP_TO_APPLET_PAY_UNIQUE_IDENTIFIER, uniqueIdentifierOne, uniqueIdentifierTwo, zhios_pay_utils.AnyToString(time.Now().UnixNano()))
  138. cache.SetJson(redisKey, data, md.WX_APP_TO_APPLET_PAY_UNIQUE_IDENTIFIER_CACHE_TIME)
  139. var r = map[string]string{}
  140. r["use_pay_method"] = "third_party"
  141. r["applet_original_id"] = appletOriginalId
  142. r["wx_url"] = md.WX_APPLET_PAY_URL + redisKey + "&" + queryParams
  143. return r, nil
  144. }
  145. func KuDianAlipayConfig(req, payConfig map[string]string) (*md.KuDianAliAppPayParams, error) {
  146. var payParams = &md.KuDianAliAppPayParams{
  147. OutTradeNo: payConfig["ord_id"],
  148. NotifyUrl: payConfig["notify_url"],
  149. Description: payConfig["subject"],
  150. TotalAmount: payConfig["amount"],
  151. Attach: req["mid"],
  152. MustParams: struct {
  153. ShopId string `json:"shop_id"`
  154. KuDianMchId string `json:"ku_dian_mch_id"`
  155. KuDianSecretKey string `json:"ku_dian_secret_key"`
  156. }{
  157. ShopId: payConfig["pay_third_party_id"],
  158. KuDianMchId: req["ku_dian_mch_id"],
  159. KuDianSecretKey: req["ku_dian_secret_key"],
  160. },
  161. }
  162. if payConfig["trade_type"] != "" {
  163. payParams.TradeType = payConfig["trade_type"]
  164. }
  165. if payConfig["wap_mame"] != "" {
  166. payParams.SenceInfo.WapName = payConfig["wap_mame"]
  167. }
  168. if payConfig["return_url"] != "" {
  169. payParams.SenceInfo.ReturnUrl = payConfig["return_url"]
  170. }
  171. if payConfig["wap_url"] != "" {
  172. payParams.SenceInfo.WapUrl = payConfig["wap_url"]
  173. }
  174. if payConfig["trade_type"] != "" {
  175. payParams.TradeType = payConfig["trade_type"]
  176. }
  177. return payParams, nil
  178. }
  179. func KuDianWxConfig(req, payConfig map[string]string) (*md.KuDianWxPayParams, error) {
  180. var payParams = &md.KuDianWxPayParams{
  181. OutTradeNo: payConfig["ord_id"],
  182. NotifyUrl: payConfig["notify_url"],
  183. Description: payConfig["subject"],
  184. TotalAmount: payConfig["amount"],
  185. AppId: req["wx_app_id"],
  186. OpenId: req["wx_open_id"],
  187. Attach: req["mid"],
  188. MustParams: struct {
  189. ShopId string `json:"shop_id"`
  190. KuDianMchId string `json:"ku_dian_mch_id"`
  191. KuDianSecretKey string `json:"ku_dian_secret_key"`
  192. }{
  193. ShopId: payConfig["pay_third_party_id"],
  194. KuDianMchId: req["ku_dian_mch_id"],
  195. KuDianSecretKey: req["ku_dian_secret_key"],
  196. },
  197. }
  198. return payParams, nil
  199. }