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

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