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

327 lines
11 KiB

  1. package local_wxpay
  2. import (
  3. zhios_pay_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/utils"
  4. "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/utils/logx"
  5. "fmt"
  6. "github.com/iGoogle-ink/gopay"
  7. "github.com/iGoogle-ink/gopay/pkg/util"
  8. "github.com/iGoogle-ink/gopay/wechat"
  9. v3 "github.com/iGoogle-ink/gopay/wechat/v3"
  10. "strconv"
  11. "time"
  12. )
  13. func NewClient(appId, mchId, apiKey string, isProd bool) *wechat.Client {
  14. // 初始化微信客户端
  15. // appId:应用ID
  16. // mchId:商户ID
  17. // apiKey:API秘钥值
  18. // isProd:是否是正式环境
  19. client := wechat.NewClient(appId, mchId, apiKey, isProd)
  20. // 打开Debug开关,输出请求日志,默认关闭
  21. client.DebugSwitch = gopay.DebugOn
  22. // 设置国家:不设置默认 中国国内
  23. // wechat.China:中国国内
  24. // wechat.China2:中国国内备用
  25. // wechat.SoutheastAsia:东南亚
  26. // wechat.Other:其他国家
  27. client.SetCountry(wechat.China)
  28. // 添加微信证书 Path 路径
  29. // certFilePath:apiclient_cert.pem 路径
  30. // keyFilePath:apiclient_key.pem 路径
  31. // pkcs12FilePath:apiclient_cert.p12 路径
  32. // 返回err
  33. //client.AddCertFilePath()
  34. // 添加微信证书内容 Content
  35. // certFileContent:apiclient_cert.pem 内容
  36. // keyFileContent:apiclient_key.pem 内容
  37. // pkcs12FileContent:apiclient_cert.p12 内容
  38. // 返回err
  39. //client.AddCertFileContent()
  40. return client
  41. }
  42. // TradeAppPay is 微信APP支付
  43. func TradeAppPay(client *wechat.Client, subject, orderID, amount, notifyUrl string) (map[string]string, error) {
  44. // 初始化 BodyMap
  45. bm := make(gopay.BodyMap)
  46. bm.Set("nonce_str", util.GetRandomString(32)).
  47. Set("body", subject).
  48. Set("out_trade_no", orderID).
  49. Set("total_fee", amount).
  50. Set("spbill_create_ip", "127.0.0.1").
  51. Set("notify_url", notifyUrl).
  52. Set("trade_type", wechat.TradeType_App).
  53. Set("sign_type", wechat.SignType_MD5)
  54. /*.Set("openid", "o0Df70H2Q0fY8JXh1aFPIRyOBgu8")*/
  55. // 预下单
  56. wxRsp, err := client.UnifiedOrder(bm)
  57. if err != nil {
  58. _ = zhios_pay_logx.Warn(err)
  59. return nil, err
  60. }
  61. _, err = wechat.VerifySign(client.ApiKey, wechat.SignType_MD5, wxRsp)
  62. if err != nil {
  63. _ = zhios_pay_logx.Warn(err)
  64. return nil, err
  65. }
  66. //if !ok {
  67. // return nil, errors.New("验签失败")
  68. //}
  69. timeStamp := strconv.FormatInt(time.Now().Unix(), 10)
  70. paySign := wechat.GetAppPaySign(client.AppId, client.MchId, wxRsp.NonceStr, wxRsp.PrepayId, wechat.SignType_MD5, timeStamp, client.ApiKey)
  71. res := map[string]string{
  72. "appid": client.AppId,
  73. "partnerid": client.MchId,
  74. "prepayid": wxRsp.PrepayId,
  75. "sign": paySign,
  76. "package": "Sign=WXPay",
  77. "noncestr": wxRsp.NonceStr,
  78. "timestamp": timeStamp,
  79. }
  80. return res, nil
  81. }
  82. // TradeAppPay is 微信H5支付
  83. func TradeH5Pay(client *wechat.Client, subject, orderID, amount, notifyUrl string) (map[string]string, error) {
  84. // 初始化 BodyMap
  85. bm := make(gopay.BodyMap)
  86. bm.Set("nonce_str", util.GetRandomString(32)).
  87. Set("body", subject).
  88. Set("out_trade_no", orderID).
  89. Set("total_fee", amount).
  90. Set("spbill_create_ip", "121.196.29.49").
  91. Set("notify_url", notifyUrl).
  92. Set("trade_type", wechat.TradeType_H5).
  93. Set("sign_type", wechat.SignType_MD5).
  94. SetBodyMap("scene_info", func(bm gopay.BodyMap) {
  95. bm.SetBodyMap("h5_info", func(bm gopay.BodyMap) {
  96. bm.Set("type", "Wap")
  97. bm.Set("wap_url", "https://www.fumm.cc")
  98. bm.Set("wap_name", "zyos")
  99. })
  100. })
  101. /*.Set("openid", "o0Df70H2Q0fY8JXh1aFPIRyOBgu8")*/
  102. // 预下单
  103. wxRsp, err := client.UnifiedOrder(bm)
  104. if err != nil {
  105. _ = zhios_pay_logx.Warn(err)
  106. return nil, err
  107. }
  108. _, err = wechat.VerifySign(client.ApiKey, wechat.SignType_MD5, wxRsp)
  109. if err != nil {
  110. _ = zhios_pay_logx.Warn(err)
  111. return nil, err
  112. }
  113. timeStamp := strconv.FormatInt(time.Now().Unix(), 10)
  114. packages := "prepay_id=" + wxRsp.PrepayId
  115. paySign := wechat.GetH5PaySign(client.AppId, wxRsp.NonceStr, packages, wechat.SignType_MD5, timeStamp, client.ApiKey)
  116. fmt.Println("paySign===", paySign)
  117. r := map[string]string{
  118. "redirect_url": wxRsp.MwebUrl,
  119. }
  120. return r, nil
  121. }
  122. func TradePcPay(client *wechat.Client, subject, orderID, amount, notifyUrl, productId string) (map[string]string, error) {
  123. // 初始化 BodyMap
  124. bm := make(gopay.BodyMap)
  125. bm.Set("nonce_str", util.GetRandomString(32)).
  126. Set("body", subject).
  127. Set("out_trade_no", orderID).
  128. Set("total_fee", amount).
  129. Set("spbill_create_ip", "121.196.29.49").
  130. Set("notify_url", notifyUrl).
  131. Set("product_id", productId).
  132. Set("trade_type", wechat.TradeType_Native).
  133. Set("sign_type", wechat.SignType_MD5)
  134. // 预下单
  135. wxRsp, err := client.UnifiedOrder(bm)
  136. if err != nil {
  137. _ = zhios_pay_logx.Warn(err)
  138. return nil, err
  139. }
  140. r := map[string]string{
  141. "code_url": wxRsp.CodeUrl,
  142. }
  143. return r, nil
  144. }
  145. // TradeMiniProgPay is 微信小程序支付 ☑️
  146. func TradeMiniProgPay(client *wechat.Client, subject, orderID, amount, notifyUrl, openid string) (map[string]string, error) {
  147. // 初始化 BodyMap
  148. bm := make(gopay.BodyMap)
  149. bm.Set("nonce_str", util.GetRandomString(32)).
  150. Set("body", subject).
  151. Set("openid", openid).
  152. Set("out_trade_no", orderID).
  153. Set("total_fee", amount).
  154. Set("spbill_create_ip", "127.0.0.1").
  155. Set("notify_url", notifyUrl).
  156. Set("trade_type", wechat.TradeType_Mini).
  157. Set("sign_type", wechat.SignType_MD5)
  158. // 预下单
  159. wxRsp, err := client.UnifiedOrder(bm)
  160. if err != nil {
  161. _ = zhios_pay_logx.Warn(err)
  162. return nil, err
  163. }
  164. fmt.Println(wxRsp)
  165. timeStamp := strconv.FormatInt(time.Now().Unix(), 10)
  166. packages := "prepay_id=" + wxRsp.PrepayId
  167. paySign := wechat.GetMiniPaySign(client.AppId, wxRsp.NonceStr, packages, wechat.SignType_MD5, timeStamp, client.ApiKey)
  168. res := map[string]string{
  169. "appId": client.AppId,
  170. "paySign": paySign,
  171. "signType": wechat.SignType_MD5,
  172. "package": packages,
  173. "nonceStr": wxRsp.NonceStr,
  174. "timeStamp": timeStamp,
  175. }
  176. return res, nil
  177. }
  178. // TradeAppPayV3 is 微信APP支付v3
  179. func TradeAppPayV3(client *v3.ClientV3, subject, orderID, amount, notifyUrl string) (map[string]string, error) {
  180. // 初始化 BodyMap
  181. amountNew := zhios_pay_utils.AnyToFloat64(amount) * 100
  182. bm := make(gopay.BodyMap)
  183. bm.Set("nonce_str", util.GetRandomString(32)).
  184. Set("body", subject).
  185. Set("out_trade_no", orderID).
  186. Set("total_fee", amountNew).
  187. Set("spbill_create_ip", "127.0.0.1").
  188. Set("notify_url", notifyUrl).
  189. Set("trade_type", wechat.TradeType_App).
  190. Set("sign_type", wechat.SignType_MD5)
  191. /*.Set("openid", "o0Df70H2Q0fY8JXh1aFPIRyOBgu8")*/
  192. //// 预下单
  193. //wxRsp, err := v3.UnifiedOrder(bm)
  194. //if err != nil {
  195. // _ = zhios_pay_logx.Warn(err)
  196. // return nil, err
  197. //}
  198. //_, err = wechat.VerifySign(client.ApiKey, wechat.SignType_MD5, wxRsp)
  199. //if err != nil {
  200. // _ = zhios_pay_logx.Warn(err)
  201. // return nil, err
  202. //}
  203. ////if !ok {
  204. //// return nil, errors.New("验签失败")
  205. ////}
  206. //timeStamp := strconv.FormatInt(time.Now().Unix(), 10)
  207. //paySign := wechat.GetAppPaySign(client.AppId, client.MchId, wxRsp.NonceStr, wxRsp.PrepayId, wechat.SignType_MD5, timeStamp, client.ApiKey)
  208. //res := map[string]string{
  209. // "appid": client.AppId,
  210. // "partnerid": client.MchId,
  211. // "prepayid": wxRsp.PrepayId,
  212. // "sign": paySign,
  213. // "package": "Sign=WXPay",
  214. // "noncestr": wxRsp.NonceStr,
  215. // "timestamp": timeStamp,
  216. //}
  217. //return res, nil
  218. return nil, nil
  219. }
  220. //// TradeJSAPIPay is 微信JSAPI支付
  221. func TradeJSAPIPay(client *wechat.Client, subject, orderID, amount, notifyUrl, openid string) (map[string]string, error) {
  222. // 初始化 BodyMap
  223. bm := make(gopay.BodyMap)
  224. bm.Set("nonce_str", util.GetRandomString(32)).
  225. Set("body", subject).
  226. Set("out_trade_no", orderID).
  227. Set("total_fee", amount).
  228. Set("spbill_create_ip", "121.196.29.49").
  229. Set("notify_url", notifyUrl).
  230. Set("trade_type", wechat.TradeType_JsApi).
  231. Set("sign_type", wechat.SignType_MD5).
  232. Set("openid", openid).
  233. SetBodyMap("scene_info", func(bm gopay.BodyMap) {
  234. bm.SetBodyMap("h5_info", func(bm gopay.BodyMap) {
  235. bm.Set("type", "Wap")
  236. bm.Set("wap_url", "https://www.fumm.cc")
  237. bm.Set("wap_name", "zyos")
  238. })
  239. })
  240. // 预下单
  241. wxRsp, err := client.UnifiedOrder(bm)
  242. if err != nil {
  243. _ = zhios_pay_logx.Warn(err)
  244. return nil, err
  245. }
  246. _, err = wechat.VerifySign(client.ApiKey, wechat.SignType_MD5, wxRsp)
  247. if err != nil {
  248. _ = zhios_pay_logx.Warn(err)
  249. return nil, err
  250. }
  251. //if !ok {
  252. // return nil, errors.New("验签失败")
  253. //}
  254. timeStamp := strconv.FormatInt(time.Now().Unix(), 10)
  255. //paySign := wechat.GetAppPaySign(client.AppId, client.MchId, wxRsp.NonceStr, wxRsp.PrepayId, wechat.SignType_MD5, timeStamp, client.ApiKey)
  256. packages := "prepay_id=" + wxRsp.PrepayId
  257. paySign := wechat.GetJsapiPaySign(client.AppId, wxRsp.NonceStr, packages, wechat.SignType_MD5, timeStamp, client.ApiKey)
  258. zhios_pay_logx.Info("wxRsp.PrepayId:" + wxRsp.PrepayId)
  259. zhios_pay_logx.Info("wxRsp.PrepayId:" + wxRsp.PrepayId)
  260. zhios_pay_logx.Info("wxRsp.PrepayId:" + openid)
  261. res := map[string]string{
  262. "appid": client.AppId,
  263. "partnerid": client.MchId,
  264. "prepayid": wxRsp.PrepayId,
  265. "sign": paySign,
  266. "package": "prepay_id=" + wxRsp.PrepayId,
  267. "noncestr": wxRsp.NonceStr,
  268. "timestamp": timeStamp,
  269. }
  270. return res, nil
  271. }
  272. // TradeH5PayV3 is 微信H5支付v3
  273. func TradeH5PayV3(client *wechat.Client, subject, orderID, amount, notifyUrl string) (string, error) {
  274. // 初始化 BodyMap
  275. bm := make(gopay.BodyMap)
  276. bm.Set("nonce_str", util.GetRandomString(32)).
  277. Set("body", subject).
  278. Set("out_trade_no", orderID).
  279. Set("total_fee", amount).
  280. Set("spbill_create_ip", "127.0.0.1").
  281. Set("notify_url", notifyUrl).
  282. Set("trade_type", wechat.TradeType_App).
  283. Set("device_info", "WEB").
  284. Set("sign_type", wechat.SignType_MD5).
  285. SetBodyMap("scene_info", func(bm gopay.BodyMap) {
  286. bm.SetBodyMap("h5_info", func(bm gopay.BodyMap) {
  287. bm.Set("type", "Wap")
  288. bm.Set("wap_url", "https://www.fumm.cc")
  289. bm.Set("wap_name", "H5测试支付")
  290. })
  291. }) /*.Set("openid", "o0Df70H2Q0fY8JXh1aFPIRyOBgu8")*/
  292. // 预下单
  293. wxRsp, err := client.UnifiedOrder(bm)
  294. if err != nil {
  295. _ = zhios_pay_logx.Warn(err)
  296. return "", err
  297. }
  298. // ====APP支付 paySign====
  299. timeStamp := strconv.FormatInt(time.Now().Unix(), 10)
  300. // 获取APP支付的 paySign
  301. // 注意:package 参数因为是固定值,无需开发者再传入
  302. // appId:AppID
  303. // partnerid:partnerid
  304. // nonceStr:随机字符串
  305. // prepayId:统一下单成功后得到的值
  306. // signType:签名方式,务必与统一下单时用的签名方式一致
  307. // timeStamp:时间
  308. // apiKey:API秘钥值
  309. paySign := wechat.GetAppPaySign(client.AppId, client.MchId, wxRsp.NonceStr, wxRsp.PrepayId, wechat.SignType_MD5, timeStamp, client.ApiKey)
  310. return paySign, nil
  311. }
  312. // TradeMiniProgPayV3 is 微信小程序支付v3
  313. func TradeMiniProgPayV3(client *v3.ClientV3, subject, orderID, amount, notifyUrl string) (string, error) {
  314. return "", nil
  315. }