微信小程序支付——springboot——API

本文详细介绍了如何使用前端JavaScript通过uni.request进行微信支付接口调用,并结合后端Java实现签名验证及统一下单流程。在前端,利用uni.requestPayment发起支付请求,并处理支付成功或失败的回调。在后端,使用微信支付SDK生成预支付订单,并返回给前端关键参数进行支付操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前端js

uni.request({
	url: that.urlPath + 'workfolder/toWeChatRequestPay',
	data: {
		body: that.copyrightOpusOrderinfo.sku_name,
		out_trade_no: that.copyrightOpusOrderinfo.order_sn,
		total_fee: parseInt(that.copyrightOpusOrderinfo.real_price*100),
		openid: uni.getStorageSync('self_user').openid,
		attach: 'WeChat'
	},
	method: 'GET',
	header: {
		'content-type': 'application/json'
	},
	success(res){
		// console.log(res.data)
		// console.log('prepay_id=' + res.data.prepay_id)
		//前去支付
		uni.requestPayment({
			timeStamp: res.data.timeStamp,
			nonceStr: res.data.nonce_str,
			package: 'prepay_id=' + res.data.prepay_id,
			signType: 'MD5',
			paySign: res.data.signKeyPay,
			success(res){
				// console.log(res.data)
				//支付成功,回调修改状态
				//that.updateCopyrightOpusOrder()
			},
			fail(error){
				console.log(error.errMsg)
				if (res.errMsg === 'requestPayment: fail cancel') {
					uni.showToast({
						title: '取消支付',
						icon: 'none'
					})
				} else {
					uni.showToast({
						title: '请重新支付',
						icon: 'none'
					})
				}
			}
		})
	}
})
@RequestMapping(value = "/toWeChatRequestPay" , method = { RequestMethod.POST, RequestMethod.GET })
    @ResponseBody
    public Map<String, String> toWeChatRequestPay(String body, String out_trade_no, String openid, String total_fee, String attach) {
        InetAddress addr = null;
        try {
            addr = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        String spbill_create_ip = addr.getHostAddress();
        Map<String, String> auMap = new HashMap<>();
		auMap.put("appid", DataConfig.APPID);
		auMap.put("attach", attach);		
		auMap.put("body", body);	
		auMap.put("detail", "线上版权登记付款");
		auMap.put("mch_id", DataConfig.MCHID);//商户号
		auMap.put("nonce_str", WXPayUtil.generateNonceStr().toString());//随机数
		auMap.put("notify_url", "https://www.1ipr.top/yysipr_beta/workfolder");
		auMap.put("openid", openid);
		auMap.put("out_trade_no", out_trade_no);//商户订单号  Long.toString(System.currentTimeMillis())
		auMap.put("spbill_create_ip", spbill_create_ip);//订单生成的机器 IP
		auMap.put("total_fee", total_fee);//总金额
		auMap.put("trade_type", "JSAPI");
        try {
            String xmlParam = WXPayUtil.generateSignedXml(auMap, DataConfig.MCHID_KEY);
            // System.out.println(xmlParam);    
            HttpClientM client= new HttpClientM("https://api.mch.weixin.qq.com/pay/unifiedorder");
            client.setHttps(true);
            client.setXmlParam(xmlParam);
            client.post();
            String result = client.getContent();
            // System.out.println(result);    
            Document doc = DocumentHelper.parseText(result);//获取xml文件
            Element rootel = doc.getRootElement();//获取根目录
            // String prepay_id = rootel.elementText("prepay_id");
            auMap.put("prepay_id", rootel.elementText("prepay_id"));
            auMap.put("timeStamp", String.valueOf(Instant.now().getEpochSecond()));
            String signPay = "appId=" + auMap.get("appid") + "&nonceStr=" + auMap.get("nonce_str")
	            + "&package=prepay_id=" + rootel.elementText("prepay_id") + "&signType=" + "MD5"  + "&timeStamp=" + String.valueOf(Instant.now().getEpochSecond())
	            + "&key=" + DataConfig.MCHID_KEY;
            auMap.put("signKeyPay", new BigInteger(1, MessageDigest.getInstance("MD5").digest(signPay.getBytes())).toString(16).toUpperCase());
            // System.out.println(prepay_id);
            return auMap;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally{
            // sql = null;
        }
    }

WXPayUtil等微信sdk
微信支付java-sdk-v3.0.9

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值