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

136 lines
5.8 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/pkg/errors"
  9. )
  10. /**
  11. 自有支付 支付宝
  12. req 传参
  13. ord_id //订单号
  14. amount //总金额
  15. subject //描述
  16. order_type //订单类型
  17. notify_url //自有支付回调地址
  18. platform //平台 由前端header传来的
  19. pay_app_cert_sn //应用公钥证书 值
  20. pay_alipayrsa_public_key //支付宝公钥证书 值
  21. pay_ali_use_type //支付宝选择版本 0旧版 1新版
  22. private_key //支付宝商户私钥 数据库字段 旧版 pay_ali_private_key 新版 pay_ali_new_private_key
  23. app_id //支付宝appId 数据库字段 旧版 pay_ali_app_id 新版 pay_ali_new_app_id
  24. rsa //支付宝密钥长度 旧版使用 数据库字段 pay_ali_key_len_type
  25. pkcs //支付宝密钥格式 旧版使用 数据库字段 pay_ali_key_format_type
  26. page_url //支付宝wap支付成功跳转链接
  27. */
  28. func AlipayCofing(args map[string]string) *md.PayData {
  29. var paySet = &md.PayData{
  30. PayAppCertSn: args["pay_app_cert_sn"],
  31. PayAlipayrsaPublicKey: args["pay_alipayrsa_public_key"],
  32. PayAliUseType: args["pay_ali_use_type"],
  33. }
  34. return paySet
  35. }
  36. func AlipayApp(args map[string]string) (string, error) {
  37. paySet := AlipayCofing(args)
  38. if args["private_key"] == "" || args["app_id"] == "" {
  39. return "", errors.New("请在后台正确配置支付宝")
  40. }
  41. 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)
  42. if err != nil {
  43. fmt.Println("支付宝错误日志")
  44. fmt.Println(param)
  45. fmt.Println(err)
  46. return "", errors.New("支付宝订单创建失败")
  47. }
  48. return zhios_pay_utils.AnyToString(param), nil
  49. }
  50. func AlipayPc(args map[string]string) (string, error) {
  51. paySet := AlipayCofing(args)
  52. if args["private_key"] == "" || args["app_id"] == "" {
  53. return "", errors.New("请在后台正确配置支付宝")
  54. }
  55. param, err := local_alipay.TradeAppPc(args["app_id"], args["private_key"], args["subject"], args["ord_id"], args["amount"], args["notify_url"], args["rsa"], args["pkcs"], paySet)
  56. if err != nil {
  57. fmt.Println("支付宝错误日志")
  58. fmt.Println(param)
  59. fmt.Println(err)
  60. return "", errors.New("支付宝订单创建失败")
  61. }
  62. return zhios_pay_utils.AnyToString(param), nil
  63. }
  64. func AlipayWap(args map[string]string) (string, error) {
  65. paySet := AlipayCofing(args)
  66. if args["private_key"] == "" || args["app_id"] == "" {
  67. return "", errors.New("请在后台正确配置支付宝")
  68. }
  69. 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)
  70. if err != nil {
  71. fmt.Println("支付宝错误日志")
  72. fmt.Println(param)
  73. fmt.Println(err)
  74. return "", errors.New("支付宝订单创建失败")
  75. }
  76. return zhios_pay_utils.AnyToString(param), nil
  77. }
  78. func AlipayApplet(args map[string]string) (string, error) {
  79. paySet := AlipayCofing(args)
  80. if args["private_key"] == "" || args["app_id"] == "" {
  81. return "", errors.New("请在后台正确配置支付宝")
  82. }
  83. 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)
  84. if err != nil {
  85. fmt.Println("支付宝错误日志")
  86. fmt.Println(param)
  87. fmt.Println(err)
  88. return "", errors.New("支付宝订单创建失败")
  89. }
  90. return zhios_pay_utils.AnyToString(param), nil
  91. }
  92. // 微信app支付v2
  93. func WxAppPay(params map[string]string) (map[string]string, error) {
  94. client := local_wxpay.NewClient(params["pay_wx_appid"], params["pay_wx_mch_id"], params["pay_wx_api_key"], true)
  95. r, err := local_wxpay.TradeAppPay(client, params["subject"], params["ord_id"], params["amount"], params["notify_url"])
  96. return r, err
  97. }
  98. // 微信H5支付v2
  99. func WxH5Pay(params map[string]string) (map[string]string, error) {
  100. client := local_wxpay.NewClient(params["pay_wx_appid"], params["pay_wx_mch_id"], params["pay_wx_api_key"], true)
  101. r, err := local_wxpay.TradeH5Pay(client, params["subject"], params["ord_id"], params["amount"], params["notify_url"])
  102. return r, err
  103. }
  104. func WxPcPay(params map[string]string) (map[string]string, error) {
  105. client := local_wxpay.NewClient(params["pay_wx_appid"], params["pay_wx_mch_id"], params["pay_wx_api_key"], true)
  106. r, err := local_wxpay.TradePcPay(client, params["subject"], params["ord_id"], params["amount"], params["notify_url"], params["product_id"])
  107. return r, err
  108. }
  109. // 微信小程序v2
  110. func WxMiniProgPay(params map[string]string) (map[string]string, error) {
  111. client := local_wxpay.NewClient(params["pay_wx_appid"], params["pay_wx_mch_id"], params["pay_wx_api_key"], true)
  112. if params["third_party_wechat_openid"] == "" {
  113. return nil, errors.New("请先授权微信")
  114. }
  115. r, err := local_wxpay.TradeMiniProgPay(client, params["subject"], params["ord_id"], params["amount"], params["notify_url"], params["third_party_wechat_openid"])
  116. return r, err
  117. }
  118. // 微信JSAPI支付
  119. func WxAppJSAPIPay(params map[string]string) (map[string]string, error) {
  120. client := local_wxpay.NewClient(params["pay_wx_appid"], params["pay_wx_mch_id"], params["pay_wx_api_key"], true)
  121. if params["third_party_wechat_openid"] == "" {
  122. return nil, errors.New("请先授权微信")
  123. }
  124. r, err := local_wxpay.TradeJSAPIPay(client, params["subject"], params["ord_id"], params["amount"], params["notify_url"], params["third_party_wechat_openid"])
  125. return r, err
  126. }