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

180 lines
7.9 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. "github.com/iGoogle-ink/gopay/wechat"
  10. "github.com/pkg/errors"
  11. )
  12. /**
  13. 自有支付 支付宝
  14. req 传参
  15. ord_id //订单号
  16. amount //总金额
  17. subject //描述
  18. order_type //订单类型
  19. notify_url //自有支付回调地址
  20. platform //平台 由前端header传来的
  21. pay_app_cert_sn //应用公钥证书 值
  22. pay_alipayrsa_public_key //支付宝公钥证书 值
  23. pay_ali_use_type //支付宝选择版本 0旧版 1新版
  24. private_key //支付宝商户私钥 数据库字段 旧版 pay_ali_private_key 新版 pay_ali_new_private_key
  25. app_id //支付宝appId 数据库字段 旧版 pay_ali_app_id 新版 pay_ali_new_app_id
  26. rsa //支付宝密钥长度 旧版使用 数据库字段 pay_ali_key_len_type
  27. pkcs //支付宝密钥格式 旧版使用 数据库字段 pay_ali_key_format_type
  28. page_url //支付宝wap支付成功跳转链接
  29. */
  30. func AlipayCofing(args map[string]string) *md.PayData {
  31. var paySet = &md.PayData{
  32. PayAppCertSn: args["pay_app_cert_sn"],
  33. PayAlipayrsaPublicKey: args["pay_alipayrsa_public_key"],
  34. PayAliUseType: args["pay_ali_use_type"],
  35. }
  36. return paySet
  37. }
  38. func AlipayApp(args map[string]string) (string, error) {
  39. paySet := AlipayCofing(args)
  40. if args["private_key"] == "" || args["app_id"] == "" {
  41. return "", errors.New("请在后台正确配置支付宝")
  42. }
  43. 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)
  44. if err != nil {
  45. fmt.Println("支付宝错误日志")
  46. fmt.Println(param)
  47. fmt.Println(err)
  48. return "", errors.New("支付宝订单创建失败")
  49. }
  50. return zhios_pay_utils.AnyToString(param), nil
  51. }
  52. func AlipayPc(args map[string]string) (string, error) {
  53. paySet := AlipayCofing(args)
  54. if args["private_key"] == "" || args["app_id"] == "" {
  55. return "", errors.New("请在后台正确配置支付宝")
  56. }
  57. 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"], args["page_url"], paySet)
  58. if err != nil {
  59. fmt.Println("支付宝错误日志")
  60. fmt.Println(param)
  61. fmt.Println(err)
  62. return "", errors.New("支付宝订单创建失败")
  63. }
  64. return zhios_pay_utils.AnyToString(param), nil
  65. }
  66. func AlipayWap(args map[string]string) (string, error) {
  67. paySet := AlipayCofing(args)
  68. if args["private_key"] == "" || args["app_id"] == "" {
  69. return "", errors.New("请在后台正确配置支付宝")
  70. }
  71. 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)
  72. if err != nil {
  73. fmt.Println("支付宝错误日志")
  74. fmt.Println(param)
  75. fmt.Println(err)
  76. return "", errors.New("支付宝订单创建失败")
  77. }
  78. return zhios_pay_utils.AnyToString(param), nil
  79. }
  80. func AlipayApplet(args map[string]string, isPrd bool) (string, error) {
  81. var url string
  82. if isPrd == true {
  83. url = md.ZHIOS_PAY_URL_PRD + "/alipay/Auth/payTradeCreate"
  84. } else {
  85. url = md.ZHIOS_PAY_URL_DEV + "/alipay/Auth/payTradeCreate"
  86. }
  87. config := map[string]string{
  88. "pay_ali_app_id": args["pay_ali_app_id"],
  89. "pay_ali_private_key": args["pay_ali_private_key"],
  90. "pay_ali_public_key": args["pay_ali_public_key"],
  91. }
  92. payParams := map[string]interface{}{
  93. "notify_url": args["notify_url"],
  94. "op_app_id": args["op_app_id"],
  95. "buyer_id": args["buyer_id"],
  96. "total_amount": args["amount"],
  97. "out_trade_no": args["out_trade_no"],
  98. "subject": args["subject"],
  99. "app_auth_token": args["app_auth_token"],
  100. "config": config,
  101. }
  102. fmt.Println(zhios_pay_utils.SerializeStr(payParams))
  103. bytes, err := zhios_pay_utils.CurlPost(url, zhios_pay_utils.SerializeStr(payParams), nil)
  104. if err != nil {
  105. return "", err
  106. }
  107. return string(bytes), nil
  108. }
  109. // 微信app支付v2
  110. func WxAppPay(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. r, err := local_wxpay.TradeAppPay(client, params["subject"], params["ord_id"], params["amount"], params["notify_url"])
  113. return r, err
  114. }
  115. // 微信H5支付v2
  116. func WxH5Pay(params map[string]string) (map[string]string, error) {
  117. client := local_wxpay.NewClient(params["pay_wx_appid"], params["pay_wx_mch_id"], params["pay_wx_api_key"], true)
  118. r, err := local_wxpay.TradeH5Pay(client, params["subject"], params["ord_id"], params["amount"], params["notify_url"])
  119. return r, err
  120. }
  121. func WxPcPay(params map[string]string) (map[string]string, error) {
  122. client := local_wxpay.NewClient(params["pay_wx_appid"], params["pay_wx_mch_id"], params["pay_wx_api_key"], true)
  123. r, err := local_wxpay.TradePcPay(client, params["subject"], params["ord_id"], params["amount"], params["notify_url"], params["product_id"])
  124. return r, err
  125. }
  126. // 微信小程序v2
  127. func WxMiniProgPay(params map[string]string) (map[string]string, error) {
  128. client := local_wxpay.NewClient(params["pay_wx_appid"], params["pay_wx_mch_id"], params["pay_wx_api_key"], true)
  129. if params["third_party_wechat_openid"] == "" {
  130. return nil, errors.New("请先授权微信")
  131. }
  132. r, err := local_wxpay.TradeMiniProgPay(client, params["subject"], params["ord_id"], params["amount"], params["notify_url"], params["third_party_wechat_openid"])
  133. return r, err
  134. }
  135. // 微信JSAPI支付
  136. func WxAppJSAPIPay(params map[string]string) (map[string]string, error) {
  137. client := local_wxpay.NewClient(params["pay_wx_appid"], params["pay_wx_mch_id"], params["pay_wx_api_key"], true)
  138. if params["third_party_wechat_openid"] == "" {
  139. return nil, errors.New("请先授权微信")
  140. }
  141. r, err := local_wxpay.TradeJSAPIPay(client, params["subject"], params["ord_id"], params["amount"], params["notify_url"], params["third_party_wechat_openid"])
  142. return r, err
  143. }
  144. func WxAppRefund(params map[string]string) (*wechat.RefundResponse, gopay.BodyMap, error) {
  145. client := local_wxpay.NewClient(params["pay_wx_appid"], params["pay_wx_mch_id"], params["pay_wx_api_key"], true)
  146. r, r1, err := local_wxpay.TradeAppRefund(client, params["subject"], params["transaction_id"], params["out_refund_no"], params["total_fee"], params["amount"], params["p12_str"], params["notify_url"])
  147. return r, r1, err
  148. }
  149. func WxAppRefundQuery(params map[string]string) (*wechat.QueryRefundResponse, gopay.BodyMap, error) {
  150. client := local_wxpay.NewClient(params["pay_wx_appid"], params["pay_wx_mch_id"], params["pay_wx_api_key"], true)
  151. r, r1, err := local_wxpay.TradeAppRefundQuery(client, params["transaction_id"])
  152. return r, r1, err
  153. }
  154. func WxShowQuery(params map[string]string) (*wechat.QueryOrderResponse, gopay.BodyMap, error) {
  155. client := local_wxpay.NewClient(params["pay_wx_appid"], params["pay_wx_mch_id"], params["pay_wx_api_key"], true)
  156. r, r1, err := local_wxpay.TradeShowQuery(client, params["transaction_id"])
  157. return r, r1, err
  158. }
  159. func WxShowQuery1(params map[string]string) (*wechat.QueryOrderResponse, gopay.BodyMap, error) {
  160. client := local_wxpay.NewClient(params["pay_wx_appid"], params["pay_wx_mch_id"], params["pay_wx_api_key"], true)
  161. r, r1, err := local_wxpay.TradeShowQuery1(client, params["transaction_id"])
  162. return r, r1, err
  163. }
  164. func TradeShowRefundQuery(params map[string]string) (*wechat.QueryRefundResponse, gopay.BodyMap, error) {
  165. client := local_wxpay.NewClient(params["pay_wx_appid"], params["pay_wx_mch_id"], params["pay_wx_api_key"], true)
  166. r, r1, err := local_wxpay.TradeShowRefundQuery(client, params["transaction_id"])
  167. return r, r1, err
  168. }