2022uniapp微信支付v3前端代码

//布局
<template>
	<view style="margin-top: 200rpx;">
	        <view class="uni-padding-wrap uni-common-mt">
			<button type="default" @click="kk">确认支付</button>
			 </view>
	    </view>
	    </view> 
		
</template>
//js代码
<script>
	export default {
		data() {
			return {
				code:'',//存用户code
				totaFell:'0.03',//支付金额 这里我是写死得
				data:''//存前端返回得data
		}
		},
		onLoad(){
		
		},
		methods: {
			
			kk(){
				uni.login({//1.向微信服务器请求 用户code
					provider:'weixin',
					success:res2=>{
					//获得code赋值给this.code
					this.code = res2.code;
					//获取成功后调用this.selBanner()请求后端服务器
					this.selBanner();		
					}
				})	
			},
			//2.把金额和code传给后端获取data
			async selBanner(){
				let res = await this.$myRequest({
					url:'/api/wx/user/pay',
					method:'POST',
					data:{	
						totaFell:this.totaFell,
						code:this.code
							}	
							});
				//3.请求后拿到data赋值给this.data
				this.data=res.data.data
				//4.调用函数uni.requestPayment请求微信调用支付
				  uni.requestPayment({
				      provider: 'wxpay',//微信支付就是'wxpay'
				      timeStamp: this.data.timeStamp,//时间戳
				      nonceStr: this.data.nonceStr,//就是你生成签名的时候那个随机字符串
				      package: this.data.prepay_id,//支付id号,我是后端拼接号了,如果没拼接需要加"prepay_id="
				      signType: this.data.signType,//v3不是MD5而是RSA
				      paySign: this.data.paySign,
				      success: function (res) {
				          console.log('success:' + JSON.stringify(res));
				      },
				      fail: function (err) {
				          console.log('fail:' + JSON.stringify(err));
				      }
				  });		 
			}

	}
	}
</script>

<style>
</style>

后端返回得数据

			 final String prepay_id = bodyAsJSON.getString("prepay_id");
            final String timeStamp = String.valueOf(System.currentTimeMillis());
            final String nonceStr = RandomStringGenerator.getRandomStringByLength(32);
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append(DD.getAppid() + "\n");
            stringBuffer.append(timeStamp + "\n");
            stringBuffer.append(nonceStr + "\n");
            stringBuffer.append("prepay_id="+prepay_id+"\n");
            Signature signature = Signature.getInstance("SHA256withRSA");
            signature.initSign(merchantPrivateKey);
            signature.update(stringBuffer.toString().getBytes("UTF-8"));
            byte[] signBytes = signature.sign();
            String paySign = Base64.encodeBytes(signBytes);
            JSONObject params = new JSONObject();
            params.put("appId", DD.getAppid());//v3不用appId"
            params.put("timeStamp", timeStamp);
            params.put("nonceStr", nonceStr);
            params.put("prepay_id", "prepay_id="+prepay_id);
            params.put("signType", "RSA");
            params.put("paySign", paySign);
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

虚构的乌托邦

如果可以请打赏鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值