• 微信小程序调起微信支付


    微信支付开发文档:wx.requestPayment(Object object) | 微信开放文档

    一、先有一个提交订单页面

    (1)wxml

    <view class="btn" bindtap="addOrder">{{btnText}}</view>

    二、接入支付流程

    (1)在点击提交订单的时候向后端传递商品信息、通过调取的接口返回 拿到wx.requestPayment接口所需要的参数 

    1. addOrder(){
    2. let that=this
    3. request.request('POST','接口地址',this.data.form).then(function(result){
    4. if(result.data){
    5. that.setData({
    6. orderNumber:result.data.orderNumber
    7. })
    8. //appid存在为付费订单,免费订单仅返回orderNumber
    9. if(result.data.appId){
    10. result.data.paySign=result.data.sign
    11. that.jsapiPay({
    12. ...result.data,
    13. });
    14. }else{
    15. //免费订单直接跳转到支付详情
    16. that.submitSuccess()
    17. }
    18. }
    19. },function(res){
    20. wx.showToast({
    21. title: res.msg,
    22. icon: 'none'
    23. })
    24. });
    25. },

    (2)wx.requestPayment 接收参数 调起支付

    1. // jsapi方式支付
    2. jsapiPay: function(param) {
    3. const that = this;
    4. console.log(param);
    5. wx.requestPayment({
    6. ...param,
    7. success (res) {
    8. console.log(res,'--success');
    9. // 支付成功
    10. that.submitSuccess()
    11. },
    12. fail (res) {
    13. console.log(res,'fail-----------');
    14. if (res.errMsg == 'requestPayment:fail cancel') {
    15. // 用户取消支付
    16. that.submitCancel()
    17. return;
    18. }
    19. that.submitFail()
    20. }
    21. })
    22. },

     (3)用户取消支付

    1. // 用户取消支付,返回进入的页面
    2. submitCancel: function() {
    3. try {
    4. wx.hideLoading()
    5. } catch(e){}
    6. setTimeout(() => {
    7. wx.showToast({
    8. title: "购买失败",
    9. // icon: 'success',
    10. icon: 'none',
    11. duration: 1000
    12. });
    13. }, 500)
    14. // setTimeout(() => {
    15. // // 返回确认订单页面
    16. // wx.navigateBack()
    17. // }, 1500);
    18. },

    (4)用户支付失败 

    1. // 支付失败
    2. submitFail: function(err) {
    3. const that = this;
    4. try {
    5. wx.hideLoading()
    6. } catch(e){}
    7. setTimeout(() => {
    8. wx.showToast({
    9. title: err || "订单支付失败,请重新支付",
    10. // icon: 'success',
    11. icon: 'none',
    12. duration: 1000
    13. });
    14. }, 500)
    15. that.setData({btnText : "重新支付"})
    16. },

    (5)支付成功 

    1. // 购买成功之后跳转到详情页
    2. submitSuccess: function() {
    3. const that = this;
    4. wx.redirectTo({
    5. url: `/pages/orderCenter/orderSuccess/orderSuccess?orderNumber=${that.data.orderNumber}`,
    6. })
    7. },

    三、js完整代码

    1. var request = require('../../../utils/request');
    2. import { detail} from "../../../utils/api/index";
    3. Page({
    4. /**
    5. * 页面的初始数据
    6. */
    7. data: {
    8. headerOpt:{
    9. showGoHome:false,
    10. title:"确认订单",
    11. },
    12. btnText:'提交订单',
    13. form:{},
    14. detailInfo:{},
    15. orderNumber:''//订单编号
    16. },
    17. addOrder(){
    18. let that=this
    19. request.request('POST','接口地址',this.data.form).then(function(result){
    20. if(result.data){
    21. that.setData({
    22. orderNumber:result.data.orderNumber
    23. })
    24. //appid存在为付费订单,免费订单仅返回orderNumber
    25. if(result.data.appId){
    26. that.jsapiPay({
    27. ...result.data,
    28. });
    29. }else{
    30. //免费订单直接跳转到支付详情
    31. that.submitSuccess()
    32. }
    33. }
    34. },function(res){
    35. wx.showToast({
    36. title: res.msg,
    37. icon: 'none'
    38. })
    39. });
    40. },
    41. // jsapi方式支付
    42. jsapiPay: function(param) {
    43. const that = this;
    44. console.log(param);
    45. wx.requestPayment({
    46. ...param,
    47. success (res) {
    48. // 支付成功
    49. that.submitSuccess()
    50. },
    51. fail (res) {
    52. if (res.errMsg == 'requestPayment:fail cancel') {
    53. // 用户取消支付
    54. that.submitCancel()
    55. return;
    56. }
    57. that.submitFail()
    58. }
    59. })
    60. },
    61. // 购买成功之后跳转到详情页
    62. submitSuccess: function() {
    63. const that = this;
    64. wx.redirectTo({
    65. url: '',
    66. })
    67. },
    68. // 用户取消支付,返回进入的页面
    69. submitCancel: function() {
    70. try {
    71. wx.hideLoading()
    72. } catch(e){}
    73. setTimeout(() => {
    74. wx.showToast({
    75. title: "购买失败",
    76. // icon: 'success',
    77. icon: 'none',
    78. duration: 1000
    79. });
    80. }, 500)
    81. // setTimeout(() => {
    82. // // 返回确认订单页面
    83. // wx.navigateBack()
    84. // }, 1500);
    85. },
    86. // 支付失败
    87. submitFail: function(err) {
    88. const that = this;
    89. try {
    90. wx.hideLoading()
    91. } catch(e){}
    92. setTimeout(() => {
    93. wx.showToast({
    94. title: err || "订单支付失败,请重新支付",
    95. // icon: 'success',
    96. icon: 'none',
    97. duration: 1000
    98. });
    99. }, 500)
    100. that.setData({btnText : "重新支付"})
    101. },
    102. //获取对应的支付信息
    103. getDetail(id){
    104. detail(id).then((res)=>{
    105. // console.log(res);
    106. if(res.data){
    107. this.setData({
    108. detailInfo:res.data
    109. })
    110. let price=this.data.form.type==1?res.data.twPrice:res.data.spPrice
    111. // 处理小数点
    112. this.dealNum(price);
    113. }
    114. })
    115. },
    116. dealNum: function(price) {
    117. let arr = (price + "").split(".");
    118. this.setData({
    119. 'detailInfo.current': arr[0],
    120. })
    121. if (arr[1]) {
    122. this.setData({
    123. 'detailInfo.point': arr[1],
    124. })
    125. }
    126. this.setData({
    127. 'detailInfo.current': arr[0],
    128. 'detailInfo.point': arr[1],
    129. })
    130. },
    131. /**
    132. * 生命周期函数--监听页面加载
    133. */
    134. onLoad(options) {
    135. if(options.form){
    136. let form=JSON.parse(decodeURIComponent(options.form))
    137. this.setData({
    138. form:form
    139. })
    140. this.getDetail(form.id)
    141. }
    142. },
    143. /**
    144. * 生命周期函数--监听页面初次渲染完成
    145. */
    146. onReady() {
    147. },
    148. /**
    149. * 生命周期函数--监听页面显示
    150. */
    151. onShow() {
    152. },
    153. /**
    154. * 生命周期函数--监听页面隐藏
    155. */
    156. onHide() {
    157. },
    158. /**
    159. * 生命周期函数--监听页面卸载
    160. */
    161. onUnload() {
    162. },
    163. /**
    164. * 页面相关事件处理函数--监听用户下拉动作
    165. */
    166. onPullDownRefresh() {
    167. },
    168. /**
    169. * 页面上拉触底事件的处理函数
    170. */
    171. onReachBottom() {
    172. },
    173. /**
    174. * 用户点击右上角分享
    175. */
    176. onShareAppMessage() {
    177. }
    178. })

  • 相关阅读:
    Flask 学习-35.restful-full 自定义错误内容 error_msg 使用
    轻量应用服务器使用总结
    HackTheBox-Starting Point--Tier 2---Base
    vue2.0 elementui 封装表单
    vulfocus——opensns命令执行(CNVD-2021-34590)
    read commited(RC) 与 repeatable read(RR)的异同
    【Http】大文件传输 | 与tcp的关系
    UE 虚幻引擎,项目结构
    no such file or directory, lstat ‘C:\Users\lanzuo\AppData\Roaming\npm‘
    Go读取文件n行的思路之旅
  • 原文地址:https://blog.csdn.net/m0_55969466/article/details/133911163