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

166 lines
5.6 KiB

  1. package pay
  2. import (
  3. "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/lib/local_alipay"
  4. local_wxpay "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/lib/local_wechat"
  5. "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/md"
  6. "github.com/pkg/errors"
  7. "github.com/syyongx/php2go"
  8. "strings"
  9. )
  10. //自有支付 支付宝
  11. func Alipay(req map[string]string) (interface{}, error) {
  12. var paySet = &md.PayData{
  13. PayAppCertSn: req["pay_app_cert_sn"],
  14. PayAlipayRootCertSn: req["pay_alipay_root_cert_sn"],
  15. PayAlipayrsaPublicKey: req["pay_alipayrsa_public_key"],
  16. PayAliUseType: req["pay_ali_use_type"],
  17. WxAppletFilepathUrl: req["wx_applet_filepath_url"],
  18. }
  19. if req["private_key"] == "" || req["app_id"] == "" {
  20. return "", errors.New("请在后台正确配置支付宝")
  21. }
  22. prd := false
  23. if req["cfg_prd"] == "1" {
  24. prd = true
  25. }
  26. var param interface{}
  27. var err error
  28. switch req["platform"] {
  29. case md.PLATFORM_ALIPAY_APPLET:
  30. param, err = local_alipay.TradeCreate(req["app_id"], req["private_key"], req["subject"], req["ord_id"], req["amount"], req["notify_url"], req["rsa"], req["pkcs"], paySet)
  31. case md.PLATFORM_WAP:
  32. if req["pay_type"] == "self" {
  33. param, err = local_alipay.TradeCreate(req["app_id"], req["private_key"], req["subject"], req["ord_id"], req["amount"], req["notify_url"], req["rsa"], req["pkcs"], paySet)
  34. } else if req["pay_type"] == "thirdParty" {
  35. var actualPayMethod string
  36. payConfigs := map[string]string{
  37. "ord_id": req["ord_id"],
  38. "notify_url": req["kudian_notify_url"],
  39. "subject": req["subject"],
  40. "amount": req["amount"],
  41. "pay_third_party_id": req["pay_third_party_id"],
  42. "user_id": req["user_id"],
  43. }
  44. if strings.Contains(req["browser"], "ali_pay_browser") && req["user_id"] != "" { //判断是否在支付宝内浏览器打开
  45. actualPayMethod = "JSPAI"
  46. payConfigs["trade_type"] = actualPayMethod
  47. } else {
  48. actualPayMethod = "H5"
  49. payConfigs["trade_type"] = actualPayMethod
  50. payConfigs["wap_mame"] = "支付"
  51. payConfigs["return_url"] = ""
  52. payConfigs["wap_url"] = req["wap_url"]
  53. }
  54. payParams := &md.KuDianAliAppPayParams{}
  55. payParams, err = KuDianAlipayConfig(req, payConfigs)
  56. if err != nil {
  57. return nil, err
  58. }
  59. var r string
  60. r, err = AliJsApiPayByKuDian(payParams, prd)
  61. if err != nil {
  62. return nil, err
  63. }
  64. if actualPayMethod == "H5" {
  65. r = php2go.URLEncode(r)
  66. }
  67. usePayMethod := "third_party"
  68. return map[string]string{
  69. "params": r,
  70. "actual_pay_method": actualPayMethod,
  71. "use_pay_method": usePayMethod,
  72. "orderId": req["ord_id"],
  73. }, nil
  74. }
  75. case md.PLATFORM_ANDROID, md.PLATFORM_IOS:
  76. if req["pay_type"] == "self" {
  77. param, err = local_alipay.TradeAppPay(req["app_id"], req["private_key"], req["subject"], req["ord_id"], req["amount"], req["notify_url"], req["rsa"], req["pkcs"], paySet)
  78. } else if req["pay_type"] == "thirdParty" {
  79. payConfigs := map[string]string{
  80. "ord_id": req["ord_id"],
  81. "notify_url": req["kudian_notify_url"],
  82. "subject": req["subject"],
  83. "amount": req["amount"],
  84. "pay_third_party_id": req["pay_third_party_id"],
  85. }
  86. payParams := &md.KuDianAliAppPayParams{}
  87. payParams, err = KuDianAlipayConfig(req, payConfigs)
  88. if err != nil {
  89. return nil, err
  90. }
  91. var r string
  92. r, err = AliAppPayByKuDian(payParams, prd)
  93. if err != nil {
  94. return nil, err
  95. }
  96. r = php2go.URLEncode(r)
  97. usePayMethod := "third_party"
  98. return map[string]string{
  99. "params": r,
  100. "use_pay_method": usePayMethod,
  101. "orderId": req["ord_id"],
  102. }, nil
  103. }
  104. default:
  105. return "", errors.New("渠道不存在")
  106. }
  107. if err != nil {
  108. return "", err
  109. }
  110. return param, nil
  111. }
  112. //自有支付 微信支付
  113. func WxPay(params map[string]string) (interface{}, error) {
  114. var r interface{}
  115. var err error
  116. prd := false
  117. if params["cfg_prd"] == "1" {
  118. prd = true
  119. }
  120. client := local_wxpay.NewClient(params["pay_wx_appid"], params["pay_wx_mch_id"], params["pay_wx_api_key"], true)
  121. switch params["platform"] {
  122. case md.PLATFORM_WX_APPLET:
  123. if params["pay_type"] == "self" { //自有支付
  124. r, err = local_wxpay.TradeMiniProgPay(client, params["subject"], params["ord_id"], params["amount"], params["notify_url"], params["third_party_wechat_openid"])
  125. } else if params["pay_type"] == "thirdParty" {
  126. payConfigs := map[string]string{
  127. "ord_id": params["ord_id"],
  128. "notify_url": params["ku_dian_notify_url"],
  129. "subject": params["subject"],
  130. "amount": params["amount"],
  131. "pay_third_party_id": params["pay_third_party_id"],
  132. }
  133. payParams := &md.KuDianWxPayParams{}
  134. payParams, err = KuDianWxConfig(params, payConfigs)
  135. if err != nil {
  136. return nil, err
  137. }
  138. r, err = WxAppletPayByKuDian(payParams, prd)
  139. r = map[string]interface{}{
  140. "params": r,
  141. "orderId": params["ord_id"],
  142. }
  143. }
  144. case md.PLATFORM_WAP:
  145. if strings.Contains(params["browser"], "wx_pay_browser") {
  146. r, err = local_wxpay.TradeJSAPIPay(client, params["subject"], params["ord_id"], params["amount"], params["notify_url"], params["third_party_wechat_openid"])
  147. } else {
  148. r, err = local_wxpay.TradeH5Pay(client, params["subject"], params["ord_id"], params["amount"], params["notify_url"])
  149. }
  150. case md.PLATFORM_ANDROID, md.PLATFORM_IOS, md.PLATFORM_JSAPI:
  151. r, err = local_wxpay.TradeAppPay(client, params["subject"], params["ord_id"], params["amount"], params["notify_url"])
  152. default:
  153. return nil, errors.New("渠道不存在")
  154. }
  155. if err != nil {
  156. return nil, err
  157. }
  158. return r, nil
  159. }