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

148 lines
5.1 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. zhios_pay_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/utils"
  7. "fmt"
  8. "github.com/iGoogle-ink/gopay"
  9. v3 "github.com/iGoogle-ink/gopay/wechat/v3"
  10. "github.com/iGoogle-ink/gotil/xlog"
  11. "github.com/pkg/errors"
  12. )
  13. //自有支付 支付宝
  14. func Alipay(args map[string]string) (string, error) {
  15. var paySet = &md.PayData{
  16. PayAppCertSn: args["pay_app_cert_sn"],
  17. PayAlipayRootCertSn: args["pay_alipay_root_cert_sn"],
  18. PayAlipayrsaPublicKey: args["pay_alipayrsa_public_key"],
  19. PayAliUseType: args["pay_ali_use_type"],
  20. WxAppletFilepathUrl: args["wx_applet_filepath_url"],
  21. }
  22. if args["private_key"] == "" || args["app_id"] == "" {
  23. return "", errors.New("请在后台正确配置支付宝")
  24. }
  25. var param interface{}
  26. var err error
  27. switch args["platform"] {
  28. case md.PLATFORM_ALIPAY_APPLET:
  29. param, err = local_alipay.TradeCreate(args["app_id"], args["private_key"], args["subject"], args["ord_id"], args["amount"], args["notify_url"], args["rsa"], args["pkcs"], paySet)
  30. case md.PLATFORM_WAP:
  31. param, err = local_alipay.TradeWapPay(args["app_id"], args["private_key"], args["subject"], args["ord_id"], args["amount"], args["notify_url"], args["rsa"], args["pkcs"], args["page_url"], paySet)
  32. case md.PLATFORM_ANDROID, md.PLATFORM_IOS:
  33. param, err = local_alipay.TradeAppPay(args["app_id"], args["private_key"], args["subject"], args["ord_id"], args["amount"], args["notify_url"], args["rsa"], args["pkcs"], paySet)
  34. default:
  35. return "", errors.New("支付平台不存在")
  36. }
  37. if err != nil {
  38. fmt.Println("支付宝错误日志")
  39. fmt.Println(param)
  40. fmt.Println(err)
  41. return "", errors.New("支付宝订单创建失败")
  42. }
  43. return zhios_pay_utils.AnyToString(param), nil
  44. }
  45. //自有支付 微信支付
  46. // app支付v2
  47. func WxAppPay(params *md.WxPayParams) (map[string]string, error) {
  48. appId := params.PayWxAppid
  49. mchId := params.PayWxMchId
  50. apiKey := params.PayWxApiKey
  51. notifyUrl := params.NotifyUrl
  52. client := local_wxpay.NewClient(appId, mchId, apiKey, true)
  53. r, err := local_wxpay.TradeAppPay(client, params.Subject, params.OrdId, params.Amount, notifyUrl)
  54. return r, err
  55. }
  56. // H5支付v2
  57. func WxH5Pay(params *md.WxPayParams) (map[string]string, error) {
  58. appId := params.PayWxAppid
  59. mchId := params.PayWxMchId
  60. apiKey := params.PayWxApiKey
  61. notifyUrl := params.NotifyUrl
  62. client := local_wxpay.NewClient(appId, mchId, apiKey, true)
  63. r, err := local_wxpay.TradeH5Pay(client, params.Subject, params.OrdId, params.Amount, notifyUrl)
  64. return r, err
  65. }
  66. // 小程序v2
  67. func WxMiniProgPay(params *md.WxPayParams) (map[string]string, error) {
  68. appId := params.PayWxAppid
  69. mchId := params.PayWxMchId
  70. apiKey := params.PayWxApiKey
  71. notifyUrl := params.NotifyUrl
  72. client := local_wxpay.NewClient(appId, mchId, apiKey, true)
  73. if params.ThirdPartyWechatOpenid == "" {
  74. return nil, errors.New("请先授权微信")
  75. }
  76. r, err := local_wxpay.TradeMiniProgPay(client, params.Subject, params.OrdId, params.Amount, notifyUrl, params.ThirdPartyWechatOpenid)
  77. return r, err
  78. }
  79. // app支付V3
  80. func WxAppPayV3(params *md.WxPayParams) (map[string]string, error) {
  81. appId := params.PayWxAppid
  82. mchId := params.PayWxMchId
  83. notifyUrl := params.NotifyUrl
  84. SerialNo := params.PayWxApiKey
  85. ApiV3Key := params.PayWxApiKey
  86. PKContent := params.PayWxApiKey
  87. client, err := v3.NewClientV3(appId, mchId, SerialNo, ApiV3Key, PKContent)
  88. if err != nil {
  89. xlog.Error(err)
  90. return nil, err
  91. }
  92. client.DebugSwitch = gopay.DebugOff
  93. r, err := local_wxpay.TradeAppPayV3(client, params.Subject, params.OrdId, params.Amount, notifyUrl)
  94. return r, err
  95. }
  96. // 微信JSAPI支付
  97. func WxAppJSAPIPay(params *md.WxPayParams) (map[string]string, error) {
  98. appId := params.PayWxAppid
  99. mchId := params.PayWxMchId
  100. apiKey := params.PayWxApiKey
  101. notifyUrl := params.NotifyUrl
  102. client := local_wxpay.NewClient(appId, mchId, apiKey, true)
  103. if params.ThirdPartyWechatOpenid == "" {
  104. return nil, errors.New("请先授权微信")
  105. }
  106. r, err := local_wxpay.TradeJSAPIPay(client, params.Subject, params.OrdId, params.Amount, notifyUrl, params.ThirdPartyWechatOpenid)
  107. return r, err
  108. }
  109. // H5支付V3
  110. func WxH5PayV3(params *md.WxPayParams) (string, error) {
  111. appId := params.PayWxAppid
  112. mchId := params.PayWxMchId
  113. apiKey := params.PayWxApiKey
  114. notifyUrl := params.NotifyUrl
  115. client := local_wxpay.NewClient(appId, mchId, apiKey, false)
  116. _, err := local_wxpay.TradeH5Pay(client, params.Subject, params.OrdId, params.Amount, notifyUrl)
  117. return "", err
  118. }
  119. // 小程序V3
  120. func WxMiniProgPayV3(params *md.WxPayParams) (string, error) {
  121. appId := params.PayWxAppid
  122. mchId := params.PayWxMchId
  123. notifyUrl := params.NotifyUrl
  124. SerialNo := params.PayWxApiKey
  125. ApiV3Key := params.PayWxApiKey
  126. PKContent := params.PayWxApiKey
  127. client, err := v3.NewClientV3(appId, mchId, SerialNo, ApiV3Key, PKContent)
  128. if err != nil {
  129. xlog.Error(err)
  130. return "", err
  131. }
  132. client.DebugSwitch = gopay.DebugOff
  133. r, err := local_wxpay.TradeMiniProgPayV3(client, params.Subject, params.OrdId, params.Amount, notifyUrl)
  134. return r, err
  135. }