支付模块
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

308 lines
10 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. // TradeMiniProgPay is 微信小程序支付 ☑️
  123. func TradeMiniProgPay(client *wechat.Client, subject, orderID, amount, notifyUrl, openid string) (map[string]string, error) {
  124. if len(subject) > 127 {
  125. tmpSubject := []rune(subject)
  126. subject = string(tmpSubject[0:44])
  127. }
  128. // 初始化 BodyMap
  129. bm := make(gopay.BodyMap)
  130. bm.Set("nonce_str", util.GetRandomString(32)).
  131. Set("body", subject).
  132. Set("openid", openid).
  133. Set("out_trade_no", orderID).
  134. Set("total_fee", amount).
  135. Set("spbill_create_ip", "127.0.0.1").
  136. Set("notify_url", notifyUrl).
  137. Set("trade_type", wechat.TradeType_Mini).
  138. Set("sign_type", wechat.SignType_MD5)
  139. // 预下单
  140. wxRsp, err := client.UnifiedOrder(bm)
  141. if err != nil {
  142. _ = zhios_pay_logx.Warn(err)
  143. return nil, err
  144. }
  145. fmt.Println(wxRsp)
  146. timeStamp := strconv.FormatInt(time.Now().Unix(), 10)
  147. packages := "prepay_id=" + wxRsp.PrepayId
  148. paySign := wechat.GetMiniPaySign(client.AppId, wxRsp.NonceStr, packages, wechat.SignType_MD5, timeStamp, client.ApiKey)
  149. res := map[string]string{
  150. "appId": client.AppId,
  151. "paySign": paySign,
  152. "signType": wechat.SignType_MD5,
  153. "package": packages,
  154. "nonceStr": wxRsp.NonceStr,
  155. "timeStamp": timeStamp,
  156. }
  157. return res, nil
  158. }
  159. // TradeAppPayV3 is 微信APP支付v3
  160. func TradeAppPayV3(client *v3.ClientV3, subject, orderID, amount, notifyUrl string) (map[string]string, error) {
  161. // 初始化 BodyMap
  162. amountNew := zhios_pay_utils.AnyToFloat64(amount) * 100
  163. bm := make(gopay.BodyMap)
  164. bm.Set("nonce_str", util.GetRandomString(32)).
  165. Set("body", subject).
  166. Set("out_trade_no", orderID).
  167. Set("total_fee", amountNew).
  168. Set("spbill_create_ip", "127.0.0.1").
  169. Set("notify_url", notifyUrl).
  170. Set("trade_type", wechat.TradeType_App).
  171. Set("sign_type", wechat.SignType_MD5)
  172. /*.Set("openid", "o0Df70H2Q0fY8JXh1aFPIRyOBgu8")*/
  173. //// 预下单
  174. //wxRsp, err := v3.UnifiedOrder(bm)
  175. //if err != nil {
  176. // _ = zhios_pay_logx.Warn(err)
  177. // return nil, err
  178. //}
  179. //_, err = wechat.VerifySign(client.ApiKey, wechat.SignType_MD5, wxRsp)
  180. //if err != nil {
  181. // _ = zhios_pay_logx.Warn(err)
  182. // return nil, err
  183. //}
  184. ////if !ok {
  185. //// return nil, errors.New("验签失败")
  186. ////}
  187. //timeStamp := strconv.FormatInt(time.Now().Unix(), 10)
  188. //paySign := wechat.GetAppPaySign(client.AppId, client.MchId, wxRsp.NonceStr, wxRsp.PrepayId, wechat.SignType_MD5, timeStamp, client.ApiKey)
  189. //res := map[string]string{
  190. // "appid": client.AppId,
  191. // "partnerid": client.MchId,
  192. // "prepayid": wxRsp.PrepayId,
  193. // "sign": paySign,
  194. // "package": "Sign=WXPay",
  195. // "noncestr": wxRsp.NonceStr,
  196. // "timestamp": timeStamp,
  197. //}
  198. //return res, nil
  199. return nil, nil
  200. }
  201. //// TradeJSAPIPay is 微信JSAPI支付
  202. func TradeJSAPIPay(client *wechat.Client, subject, orderID, amount, notifyUrl, openid string) (map[string]string, error) {
  203. // 初始化 BodyMap
  204. bm := make(gopay.BodyMap)
  205. bm.Set("nonce_str", util.GetRandomString(32)).
  206. Set("body", subject).
  207. Set("out_trade_no", orderID).
  208. Set("total_fee", amount).
  209. Set("spbill_create_ip", "121.196.29.49").
  210. Set("notify_url", notifyUrl).
  211. Set("trade_type", wechat.TradeType_JsApi).
  212. Set("sign_type", wechat.SignType_MD5).
  213. Set("openid", openid).
  214. SetBodyMap("scene_info", func(bm gopay.BodyMap) {
  215. bm.SetBodyMap("h5_info", func(bm gopay.BodyMap) {
  216. bm.Set("type", "Wap")
  217. bm.Set("wap_url", "https://www.fumm.cc")
  218. bm.Set("wap_name", "zyos")
  219. })
  220. })
  221. // 预下单
  222. wxRsp, err := client.UnifiedOrder(bm)
  223. if err != nil {
  224. _ = zhios_pay_logx.Warn(err)
  225. return nil, err
  226. }
  227. _, err = wechat.VerifySign(client.ApiKey, wechat.SignType_MD5, wxRsp)
  228. if err != nil {
  229. _ = zhios_pay_logx.Warn(err)
  230. return nil, err
  231. }
  232. //if !ok {
  233. // return nil, errors.New("验签失败")
  234. //}
  235. timeStamp := strconv.FormatInt(time.Now().Unix(), 10)
  236. //paySign := wechat.GetAppPaySign(client.AppId, client.MchId, wxRsp.NonceStr, wxRsp.PrepayId, wechat.SignType_MD5, timeStamp, client.ApiKey)
  237. packages := "prepay_id=" + wxRsp.PrepayId
  238. paySign := wechat.GetJsapiPaySign(client.AppId, wxRsp.NonceStr, packages, wechat.SignType_MD5, timeStamp, client.ApiKey)
  239. zhios_pay_logx.Info("wxRsp.PrepayId:" + wxRsp.PrepayId)
  240. zhios_pay_logx.Info("wxRsp.PrepayId:" + wxRsp.PrepayId)
  241. zhios_pay_logx.Info("wxRsp.PrepayId:" + openid)
  242. res := map[string]string{
  243. "appid": client.AppId,
  244. "partnerid": client.MchId,
  245. "prepayid": wxRsp.PrepayId,
  246. "sign": paySign,
  247. "package": "prepay_id=" + wxRsp.PrepayId,
  248. "noncestr": wxRsp.NonceStr,
  249. "timestamp": timeStamp,
  250. }
  251. return res, nil
  252. }
  253. // TradeH5PayV3 is 微信H5支付v3
  254. func TradeH5PayV3(client *wechat.Client, subject, orderID, amount, notifyUrl string) (string, error) {
  255. // 初始化 BodyMap
  256. bm := make(gopay.BodyMap)
  257. bm.Set("nonce_str", util.GetRandomString(32)).
  258. Set("body", subject).
  259. Set("out_trade_no", orderID).
  260. Set("total_fee", amount).
  261. Set("spbill_create_ip", "127.0.0.1").
  262. Set("notify_url", notifyUrl).
  263. Set("trade_type", wechat.TradeType_App).
  264. Set("device_info", "WEB").
  265. Set("sign_type", wechat.SignType_MD5).
  266. SetBodyMap("scene_info", func(bm gopay.BodyMap) {
  267. bm.SetBodyMap("h5_info", func(bm gopay.BodyMap) {
  268. bm.Set("type", "Wap")
  269. bm.Set("wap_url", "https://www.fumm.cc")
  270. bm.Set("wap_name", "H5测试支付")
  271. })
  272. }) /*.Set("openid", "o0Df70H2Q0fY8JXh1aFPIRyOBgu8")*/
  273. // 预下单
  274. wxRsp, err := client.UnifiedOrder(bm)
  275. if err != nil {
  276. _ = zhios_pay_logx.Warn(err)
  277. return "", err
  278. }
  279. // ====APP支付 paySign====
  280. timeStamp := strconv.FormatInt(time.Now().Unix(), 10)
  281. // 获取APP支付的 paySign
  282. // 注意:package 参数因为是固定值,无需开发者再传入
  283. // appId:AppID
  284. // partnerid:partnerid
  285. // nonceStr:随机字符串
  286. // prepayId:统一下单成功后得到的值
  287. // signType:签名方式,务必与统一下单时用的签名方式一致
  288. // timeStamp:时间
  289. // apiKey:API秘钥值
  290. paySign := wechat.GetAppPaySign(client.AppId, client.MchId, wxRsp.NonceStr, wxRsp.PrepayId, wechat.SignType_MD5, timeStamp, client.ApiKey)
  291. return paySign, nil
  292. }
  293. // TradeMiniProgPayV3 is 微信小程序支付v3
  294. func TradeMiniProgPayV3(client *v3.ClientV3, subject, orderID, amount, notifyUrl string) (string, error) {
  295. return "", nil
  296. }