uniapp-支付(微信)

1. 支付的前期准备工作

需要后端和老板(部长/领导)提前获取到appid和商户号 申请之后才能调取支付接口

这里主要讲解前端需要做的工作

文件内打开支付的相关

2.发请求获取需要的参数

在订单详情页的时候 点击提交订单按钮 先发了一个创建订单的请求 返回的数据有订单的id

请求成功之后 携带订单id 跳转到支付页面

在支付页面内 点击微信支付后 先是用订单id发一个请求获取到需要的参数 (例如appid等6个参数)

请求成功之后 用这些获得的这些参数 发 wx.requestPayment 请求 需要6个参数

这个成功之后 模拟器就会出现一个支付二维码 手机微信扫码就可以支付

3.遇到的bug

第一种 某个值 不为 String 类型 或值为undefined

这种情况 先检查(控制台前后打印)是否把参数传入成功(可能没赋值成功)

确定有值传入了 再看数据类型是否对得上 像需要的参数之一 时间戳 需要的就是字符串类型

再检查属性名是否有大小写的问,uniapp的官方文档以及微信部分官方文档的示例代码大小写是有问题的

第二种 支付验证签名失败

先检查自己的 signType 是否和后端保持一致

再检查 paySign 的格式是否正确(二次签名转换格式,这个需要后端去处理)

检查后端处理的代码是V2还是V3版本 推荐使用V3

实在不行 就把微信开发者工具重启试一下

4.示例代码

<template>
	<view class="btn" @click="handleSubmit">确认支付</view>
</template>

<script>
	import { payCreateOrder } from '@/api/pay-order/pay-weixin.js'
	data() {
		return {
    	...xxx,
			orderId: '',
			orderInfo: {},//支付要用到的数据
		}
	},
	
	methods: {
    async handleSubmit () {
			// 调用接口支付
			const data = {
				orderType: '1',
				orderId: this.orderId,
				payType: 'wx_app',
			}
			await payCreateOrder(data) 
			.then((res) => {
			    console.log(res, '接口成功之后操作');
  				this.orderInfo = {
  					appId: res.data.data.appId,  // 应用ID(AppID)
  					package: res.data.data.package,        // 固定值
  					nonceStr: res.data.data.nonceStr, // 随机字符串
  					timeStamp: Math.floor(Date.now() / 1000).toString(),        // 时间戳(单位:秒)
  					signType: res.data.data.signType ,// 签名
  					paySign: res.data.data.paySign // 签名,这里用的 MD5 签名
  				}
  				console.log(this.orderInfo)
  				uni.getProvider({
				    service: 'payment',
				    success:(res) => {
				        console.log(res.provider)
				        if (~res.provider.indexOf('wxpay')) {
				            wx.requestPayment({
				                appId: this.orderInfo.appId, 
				                package:  this.orderInfo.package, 
				                nonceStr: this.orderInfo.nonceStr, 
				                timeStamp: this.orderInfo.timeStamp,
				                signType: this.orderInfo.signType,
				                paySign: this.orderInfo.paySign,
				                success: (res) => {
				                    // 此处调用接口跳转页面
                          	// go('/vacant_house/submit_successfully/submit_successfully?submitType=1');
				                    console.log("支付成功",  res);
				                },
				                fail: (err) =>  {
				                    console.log('支付失败:' + JSON.stringify(err));
				                    console.log(this.orderInfo);
				                }
				            });
				        }
				    }
				});
			  })
		},
 }
  
</script>

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
//微信充值 //支付接口测试 function balance(url, data) { uni.request({ url: cfg.originUrl + '/wx/mp/js_sig.do', data: { route: url }, method: 'GET', success: (res) => { jweixin.config({ debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来 appId: res.data.appId, // 必填,公众号的唯一标识 timestamp: res.data.timestamp, // 必填,生成签名的时间戳 nonceStr: res.data.nonceStr, // 必填,生成签名的随机串 signature: res.data.signature, // 必填,签名 jsApiList: ['chooseWXPay'] // 必填,需要使用的JS接口列表 }); jweixin.ready(function() { uni.request({ url: cfg.originUrl + '/wx/recharge/pay.do', method: 'POST', header: { 'Content-type': "application/x-www-form-urlencoded", }, data: JSON.stringify(data), success: function(res) { alert("下单成功"); alert(JSON.stringify(res)); alert(res.data.order_id); all.globalData.orderId = res.data.order_id; uni.setStorageSync('orderId', res.data.order_id); jweixin.chooseWXPay({ timestamp: res.data.payParams.timeStamp, // 支付签名时间戳 nonceStr: res.data.payParams.nonceStr, // 支付签名随机串 package: res.data.payParams.package, // 接口返回的prepay_id参数 signType: res.data.payParams.signType, // 签名方式 paySign: res.data.payParams.paySign, // 支付签名 success: function(e) { alert("支付成功"); alert(JSON.stringify(e)); // 支付成功后的回调函数 } }); } }) }); jweixin.error(function(res) { // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。 console.log("验证失败!") }); } }) }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值