• 前端微信支付代码(公众号支付)


    一.场景 (完成代码见附录)

            其他商户跳转到我们支付页面,传入参数openId,fromTradeType,订单号,流水号,借由我们页面进行微信支付。具体在小程序后台如何配置,见微信官方文档。

    二.包引入部分

    import _ from "lodash";

    引入lodash主要使用防抖功能

     三.控制微信支付按钮显示隐藏的代码

    1. this.showWxPay = this.isWeChat() && tmp.subOpenId;
    2. isWeChat() {
    3. let ua = window.navigator.userAgent.toLowerCase();
    4. if (ua.match(/MicroMessenger/i) == "micromessenger") {
    5. return true; //是微信内部
    6. } else {
    7. return false;
    8. }
    9. },

    满足1.是在微信内核进入。2.传入了openId

    四.点击微信支付后的流程代码

    1. wxPay: _.debounce(function () {
    2. let url = ""; //接口获取后端timeStamp,nonceStr等微信支付拉起的参数
    3. let data = this.wxParams;
    4. if (!this.passJudge(data)) { //对进入的数据进行判读,必须每一个数据都是存在的
    5. //对参数做一次校验
    6. return;
    7. }
    8. this.$axios
    9. .post({
    10. url,
    11. data,
    12. headers: { "Content-Type": "application/json" },
    13. })
    14. .then((res) => {
    15. this.$emit(
    16. "getPayResult",
    17. );
    18. let judge =
    19. res.hasOwnProperty("data") &&
    20. res.data.hasOwnProperty("ERRCODE") &&
    21. res.data.ERRCODE == "000000" &&
    22. res.status == "SUCCESS";
    23. if (judge) {
    24. let _data = res.data;
    25. this.weChatParameter = { //记录拉起支付的参数
    26. //微信搭桥需要的数据
    27. appid: _data.appId || "",
    28. timeStamp: _data.timeStamp || "",
    29. nonceStr: _data.nonceStr || "",
    30. package: _data.package || "",
    31. signType: _data.signType || "",
    32. paySign: _data.paySign || "",
    33. };
    34. this.weixinPay(); //微信内置对象判断
    35. } else {
    36. this.$message({
    37. message: res.retMsg || "提交订单失败",
    38. type: "warning",
    39. });
    40. }
    41. })
    42. .catch((error) => {
    43. this.$message({
    44. message: "提交订单失败",
    45. type: "warning",
    46. });
    47. });
    48. }, 240),

    其中,passJudge函数为:

    1. passJudge(data) {
    2. if (!data) {
    3. this.$message.error("缺少请求参数");
    4. return false;
    5. }
    6. if (!data.reOrderNo) {
    7. this.$message.error("缺失参数:reOrderNo");
    8. return false;
    9. }
    10. if (!data.tranNo) {
    11. this.$message.error("缺失参数:tranNo");
    12. return false;
    13. }
    14. if (!data.subOpenId) {
    15. this.$message.error("缺失参数:subOpenId");
    16. return false;
    17. }
    18. if (!data.fromTradeType) {
    19. this.$message.error("缺失参数:fromTradeType");
    20. return false;
    21. }
    22. return true;
    23. },

    五.进行微信搭桥,进行环境处理 

    1. let that = this;
    2. if (typeof WeixinJSBridge == "undefined") {
    3. if (document.addEventListener) {
    4. document.addEventListener(
    5. "WeixinJSBridgeReady",
    6. that.onBridgeReady(),
    7. false
    8. );
    9. } else if (document.attachEvent) {
    10. document.attachEvent("WeixinJSBridgeReady", that.onBridgeReady());
    11. }
    12. } else {
    13. that.onBridgeReady();
    14. }

     六.环境准备好后,拉起支付

    1. onBridgeReady() {
    2. let that = this;
    3. let timestamp = Math.round(this.weChatParameter.timeStamp).toString();
    4. window.WeixinJSBridge.invoke(
    5. "getBrandWCPayRequest",
    6. {
    7. appId: that.weChatParameter.appid, //公众号名称,由商户传入
    8. timeStamp: timestamp, //时间戳,自1970年以来的秒数
    9. nonceStr: that.weChatParameter.nonceStr, //随机串
    10. package: that.weChatParameter.package,
    11. signType: that.weChatParameter.signType, //微信签名方式:
    12. paySign: that.weChatParameter.paySign, //微信签名
    13. // jsApiList: ["chooseWXPay"],
    14. },
    15. function (res) {
    16. that.$emit(
    17. "getPayResult",
    18. `拉起支付返回结果为:${JSON.stringify(res)}`
    19. );
    20. // 使用以上方式判断前端返回,微信团队郑重提示:res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。
    21. if (res.err_msg == "get_brand_wcpay_request:ok") {
    22. //支付成功
    23. that.$emit("toSuccess");
    24. } else if (res.err_msg == "get_brand_wcpay_request:cancel") {
    25. //取消支付
    26. } else {
    27. //支付失败
    28. that.$emit("toFail");
    29. }
    30. }
    31. );
    32. },

    七.附录:完整代码

    1. <script>
    2. import _ from "lodash";
    3. import Bus from "@/utils/bus.js";
    4. export default {
    5. data() {
    6. return {
    7. weChatParameter: "",
    8. };
    9. },
    10. props: {
    11. wxParams: {
    12. type: Object,
    13. default: {},
    14. },
    15. },
    16. created() {},
    17. mounted() {
    18. Bus.$on("getDetail", (amount, settlementNo, reconciliationTime) => {
    19. this.payAmount = amount;
    20. this.amount = amount;
    21. this.settlementNo = settlementNo;
    22. this.reconciliationTime = reconciliationTime;
    23. });
    24. },
    25. components: {},
    26. computed: {},
    27. watch: {},
    28. methods: {
    29. wxPay: _.debounce(function () {
    30. let url = "";
    31. let data = this.wxParams;
    32. this.$emit("changeWay", 3);
    33. if (!this.passJudge(data)) {
    34. //对参数做一次校验
    35. return;
    36. }
    37. this.$axios
    38. .post({
    39. url,
    40. data,
    41. headers: { "Content-Type": "application/json" },
    42. })
    43. .then((res) => {
    44. this.$emit(
    45. "getPayResult",
    46. "进入/cashier/PublicPay,数据为:" + JSON.stringify(res)
    47. );
    48. let judge =
    49. res.hasOwnProperty("data") &&
    50. res.data.hasOwnProperty("ERRCODE") &&
    51. res.data.ERRCODE == "000000" &&
    52. res.status == "SUCCESS";
    53. if (judge) {
    54. let _data = res.data;
    55. this.weChatParameter = {
    56. //微信搭桥需要的数据
    57. appid: _data.appId || "",
    58. timeStamp: _data.timeStamp || "",
    59. nonceStr: _data.nonceStr || "",
    60. package: _data.package || "",
    61. signType: _data.signType || "",
    62. paySign: _data.paySign || "",
    63. };
    64. this.weixinPay(); //微信内置对象判断
    65. } else {
    66. this.$message({
    67. message: res.retMsg || "提交订单失败",
    68. type: "warning",
    69. });
    70. }
    71. })
    72. .catch((error) => {
    73. this.$message({
    74. message: "提交订单失败",
    75. type: "warning",
    76. });
    77. });
    78. }, 240),
    79. weixinPay() {
    80. let that = this;
    81. if (typeof WeixinJSBridge == "undefined") {
    82. if (document.addEventListener) {
    83. document.addEventListener(
    84. "WeixinJSBridgeReady",
    85. that.onBridgeReady(),
    86. false
    87. );
    88. } else if (document.attachEvent) {
    89. document.attachEvent("WeixinJSBridgeReady", that.onBridgeReady());
    90. }
    91. } else {
    92. that.onBridgeReady();
    93. }
    94. },
    95. //微信内置浏览器类
    96. onBridgeReady() {
    97. let that = this;
    98. let timestamp = Math.round(this.weChatParameter.timeStamp).toString();
    99. this.$emit(
    100. "getPayResult",
    101. "进入onBridgeReady,数据为:" + JSON.stringify(that.weChatParameter)
    102. );
    103. window.WeixinJSBridge.invoke(
    104. "getBrandWCPayRequest",
    105. {
    106. appId: that.weChatParameter.appid, //公众号名称,由商户传入
    107. timeStamp: timestamp, //时间戳,自1970年以来的秒数
    108. nonceStr: that.weChatParameter.nonceStr, //随机串
    109. package: that.weChatParameter.package,
    110. signType: that.weChatParameter.signType, //微信签名方式:
    111. paySign: that.weChatParameter.paySign, //微信签名
    112. // jsApiList: ["chooseWXPay"],
    113. },
    114. function (res) {
    115. that.$emit(
    116. "getPayResult",
    117. `拉起支付返回结果为:${JSON.stringify(res)}`
    118. );
    119. // 使用以上方式判断前端返回,微信团队郑重提示:res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。
    120. if (res.err_msg == "get_brand_wcpay_request:ok") {
    121. //支付成功
    122. that.$emit("toSuccess");
    123. } else if (res.err_msg == "get_brand_wcpay_request:cancel") {
    124. //取消支付
    125. } else {
    126. //支付失败
    127. that.$emit("toFail");
    128. }
    129. }
    130. );
    131. },
    132. passJudge(data) {
    133. if (!data) {
    134. this.$message.error("缺少请求参数");
    135. return false;
    136. }
    137. if (!data.reOrderNo) {
    138. this.$message.error("缺失参数:reOrderNo");
    139. return false;
    140. }
    141. if (!data.tranNo) {
    142. this.$message.error("缺失参数:tranNo");
    143. return false;
    144. }
    145. if (!data.subOpenId) {
    146. this.$message.error("缺失参数:subOpenId");
    147. return false;
    148. }
    149. if (!data.fromTradeType) {
    150. this.$message.error("缺失参数:fromTradeType");
    151. return false;
    152. }
    153. return true;
    154. },
    155. },
    156. };
    157. script>
    158. <style lang="scss" scoped>
    159. .check-box {
    160. padding: 0px 10px;
    161. height: 28px;
    162. display: flex;
    163. justify-content: center;
    164. align-items: center;
    165. border: 1px solid rgb(220, 220, 220);
    166. margin-left: 10px;
    167. cursor: pointer;
    168. .check-img {
    169. width: 18px;
    170. height: 18px;
    171. }
    172. .check-word {
    173. margin-left: 5px;
    174. font-size: 14px;
    175. }
    176. }
    177. style>

  • 相关阅读:
    动物静态HTML网页作业作品 大学生野生动物保护网页设计制作成品 简单DIV CSS布局网站
    Spring Boot中实现发送文本、带附件和HTML邮件
    【Vue】Provide,Inject,模版 Ref 的用法
    作业 增删改查 三个功能
    C++如何在main函数开始之前(或结束之后)执行一段逻辑?
    Node.js基础知识点
    Singleton Pattern 单例模式简介与 C# 示例【创建型】【设计模式来了】
    io多路复用:select、poll和epoll
    SCT52240STDR,SCT52240MTER,SCT52240QSTDR,SCT52240QMTER,栅极驱动器
    uniapp 之 本地存储
  • 原文地址:https://blog.csdn.net/qq_36566924/article/details/127687420