wx-pay.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * 微信小程序支付
  3. * 此处针对于微信小程序开发的支付插件
  4. * 第一次支付成功后会跳出订阅的消息 如果用户拒绝或同意都会跳转到支付成功页面
  5. * 如果点击订阅 会将状态写进缓存 之后不再提醒。
  6. *
  7. * @param {sn,price}
  8. */
  9. import { getWeChatMpMessage } from "@/api/message.js";
  10. import { initiatePay } from "@/api/trade";
  11. class LiLiWXPay {
  12. constructor(...payList) {
  13. this.data = payList[0];
  14. console.log(payList);
  15. // 调用支付
  16. this.pay = () => {
  17. uni.showLoading({
  18. title: "加载中",
  19. });
  20. let submitData = {
  21. sn: this.data.sn,
  22. orderType: this.data.orderType || "TRADE",
  23. clientType: "WECHAT_MP",
  24. };
  25. const paymentMethod = "WECHAT";
  26. const paymentClient = "MP";
  27. // 调用支付
  28. initiatePay(paymentMethod, paymentClient, submitData).then((res) => {
  29. let response = res.data.result;
  30. uni.hideLoading();
  31. uni.requestPayment({
  32. provider: "wxpay",
  33. appid: response.appid,
  34. timeStamp: response.timeStamp,
  35. nonceStr: response.nonceStr,
  36. package: response.package,
  37. signType: response.signType,
  38. paySign: response.paySign,
  39. success: (e) => {
  40. uni.showToast({
  41. icon: "none",
  42. title: "支付成功!",
  43. });
  44. sendMessage(payList[0].price);
  45. },
  46. fail: (e) => {
  47. this.exception = e;
  48. // 支付异常或支付失败之后跳转到订单页面
  49. uni.showModal({
  50. content: "支付失败,如果您已支付,请勿反复支付",
  51. showCancel: false,
  52. success: () => {
  53. uni.redirectTo({
  54. url: "/pages/order/myOrder?status=0",
  55. });
  56. },
  57. });
  58. },
  59. });
  60. });
  61. };
  62. }
  63. }
  64. function sendMessage(price) {
  65. //订阅消息
  66. getWeChatMpMessage().then((res) => {
  67. var message = res.data.result;
  68. var templateid = message.map((item) => item.code);
  69. uni.requestSubscribeMessage({
  70. tmplIds: templateid,
  71. success: (res) => {
  72. },
  73. fail: (res) => {
  74. console.log('fail', res)
  75. uni.showToast({
  76. icon: "none",
  77. title: "订阅消息失败",
  78. })
  79. },
  80. complete: (res) => {
  81. console.log('complete', res)
  82. /**
  83. * 已经支付成功
  84. */
  85. uni.redirectTo({
  86. url:
  87. "/pages/cart/payment/success?paymentMethod=WECHAT" +
  88. "&payPrice=" +
  89. price,
  90. });
  91. },
  92. });
  93. });
  94. }
  95. export default LiLiWXPay;