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);

你可能感兴趣的:(第三方api工具使用,前端,微信,微信小程序)