wxpay.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. function onBridgeReady(params) {
  2. //记录要回跳的url
  3. window.WeixinJSBridge.invoke(
  4. 'getBrandWCPayRequest', {
  5. 'appId': params.appId, // 公众号名称,由商户传入
  6. 'timeStamp': params.timeStamp, // 时间戳,自1970年以来的秒数
  7. 'nonceStr': params.nonceStr, // 随机串
  8. 'package': params.package,
  9. 'signType': params.signType, // 微信签名方式:
  10. 'paySign': params.paySign // 微信签名
  11. },
  12. (res) => {
  13. //alert(JSON.stringify(res)); // 支付取消会执行 支付成功不会
  14. if (res.err_msg == 'get_brand_wcpay_request:ok') {
  15. //alert("支付成功") // 不会执行
  16. window.location.href = params.url
  17. } else if (res.err_msg == "get_brand_wcpay_request:cancel") {
  18. //alert("支付取消") // 会执行
  19. } else {
  20. //alert("支付失败")
  21. }
  22. }
  23. )
  24. }
  25. export const wxPayJs = (params) => {
  26. if (!params.url) {
  27. params.url = window.location.href.split("#")[0] + "/#/";
  28. }
  29. if (typeof window.WeixinJSBridge === 'undefined') {
  30. if (document.addEventListener) {
  31. document.addEventListener('WeixinJSBridgeReady', function() {
  32. onBridgeReady(params)
  33. }, false)
  34. } else if (document.attachEvent) {
  35. document.attachEvent('WeixinJSBridgeReady', function() {
  36. onBridgeReady(params)
  37. })
  38. document.attachEvent('onWeixinJSBridgeReady', function() {
  39. onBridgeReady(params)
  40. })
  41. }
  42. } else {
  43. onBridgeReady(params)
  44. }
  45. }