广告平台(总站长使用)
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.

api.go 9.8 KiB

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