首页

微信支付V3 Java jsApi 扫码支付

java

2020-7-26

package com.demo;

import java.util.SortedMap;
import java.util.TreeMap;

import com.utils.GetWxOrderno;
import com.utils.RequestHandler;
import com.utils.Sha1Util;
import com.utils.TenpayUtil;

/**
 * @author ex_yangxiaoyi
 * 
 */
public class Demo {
	//微信支付商户开通后 微信会提供appid和appsecret和商户号partner
	private static String appid = "";
	private static String appsecret = "";
	private static String partner = "";
	//这个参数partnerkey是在商户后台配置的一个32位的key,微信商户平台-账户设置-安全设置-api安全
	private static String partnerkey = "";
	//openId 是微信用户针对公众号的标识,授权的部分这里不解释
	private static String openId = "";
	//微信支付成功后通知地址 必须要求80端口并且地址不能带参数
	private static String notifyurl = "";																	 // Key

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		//微信支付jsApi
		WxPayDto tpWxPay = new WxPayDto();
		tpWxPay.setOpenId(openId);
		tpWxPay.setBody("商品信息");
		tpWxPay.setOrderId(getNonceStr());
		tpWxPay.setSpbillCreateIp("127.0.0.1");
		tpWxPay.setTotalFee("0.01");
	    getPackage(tpWxPay);
	    
	    //扫码支付
	    WxPayDto tpWxPay1 = new WxPayDto();
	    tpWxPay1.setBody("商品信息");
	    tpWxPay1.setOrderId(getNonceStr());
	    tpWxPay1.setSpbillCreateIp("127.0.0.1");
	    tpWxPay1.setTotalFee("0.01");
		getCodeurl(tpWxPay1);
	}
	
	/**
	 * 获取微信扫码支付二维码连接
	 */
	public static String getCodeurl(WxPayDto tpWxPayDto){
		
		// 子商户号
		String sub_mch_id = tpWxPayDto.getSubMchId();
		
		// 订单号
		String orderId = tpWxPayDto.getOrderId();
		
		// 附加数据 原样返回
		String attach = "";
		
		// 总金额以分为单位,不带小数点
		String totalFee = getMoney(tpWxPayDto.getTotalFee());
		
		// 订单生成的机器 IP
		String spbill_create_ip = tpWxPayDto.getSpbillCreateIp();
		
		// 这里notify_url是 支付完成后微信发给该链接信息,可以判断会员是否支付成功,改变订单状态等。
		String notify_url = notifyurl;
		String trade_type = "NATIVE";

		// 商户号
		String mch_id = partner;
		// 随机字符串
		String nonce_str = getNonceStr();

		// 商品描述根据情况修改
		String body = tpWxPayDto.getBody();

		// 商户订单号
		String out_trade_no = orderId;

		SortedMap<String, String> packageParams = new TreeMap<String, String>();
		packageParams.put("appid", appid);
		packageParams.put("mch_id", mch_id);
		packageParams.put("sub_mch_id", sub_mch_id);
		packageParams.put("nonce_str", nonce_str);
		packageParams.put("body", body);
		packageParams.put("attach", attach);
		packageParams.put("out_trade_no", out_trade_no);

		// 这里写的金额为1 分到时修改
		packageParams.put("total_fee", totalFee);
		packageParams.put("spbill_create_ip", spbill_create_ip);
		packageParams.put("notify_url", notify_url);

		packageParams.put("trade_type", trade_type);

		RequestHandler reqHandler = new RequestHandler(null, null);
		reqHandler.init(appid, appsecret, partnerkey);

		String sign = reqHandler.createSign(packageParams);
		
		String xml = "<xml>"   "<appid>"   appid   "</appid>"   "<mch_id>"
				  mch_id   "</mch_id>"   "<sub_mch_id>"   sub_mch_id
				  "</sub_mch_id>"   "<nonce_str>"   nonce_str
				  "</nonce_str>"   "<sign>"   sign   "</sign>"
				  "<body><![CDATA["   body   "]]></body>" 
				  "<out_trade_no>"   out_trade_no
				  "</out_trade_no>"   "<attach>"   attach   "</attach>"
				  "<total_fee>"   totalFee   "</total_fee>"
				  "<spbill_create_ip>"   spbill_create_ip
				  "</spbill_create_ip>"   "<notify_url>"   notify_url
				  "</notify_url>"   "<trade_type>"   trade_type
				  "</trade_type>"   "</xml>";
		String code_url = "";
		String createOrderURL = "https://api.mch.weixin.qq.com/pay/unifiedorder";
		
		code_url = new GetWxOrderno().getCodeUrl(createOrderURL, xml);
		System.out.println("code_url----------------" code_url);
		
		return code_url;
	}

	/**
	 * 获取请求预支付id报文
	 * @return
	 */
	@SuppressWarnings("static-access")
	public static String getPackage(WxPayDto tpWxPayDto) {
		
		String openId = tpWxPayDto.getOpenId();
		// 子商户号
		String sub_mch_id = tpWxPayDto.getSubMchId();
		// 订单号
		String orderId = tpWxPayDto.getOrderId();
		// 附加数据 原样返回
		String attach = "";
		// 总金额以分为单位,不带小数点
		String totalFee = getMoney(tpWxPayDto.getTotalFee());
		
		// 订单生成的机器 IP
		String spbill_create_ip = tpWxPayDto.getSpbillCreateIp();
		// 这里notify_url是 支付完成后微信发给该链接信息,可以判断会员是否支付成功,改变订单状态等。
		String notify_url = notifyurl;
		String trade_type = "JSAPI";

		// ---必须参数
		// 商户号
		String mch_id = partner;
		// 随机字符串
		String nonce_str = getNonceStr();

		// 商品描述根据情况修改
		String body = tpWxPayDto.getBody();

		// 商户订单号
		String out_trade_no = orderId;

		SortedMap<String, String> packageParams = new TreeMap<String, String>();
		packageParams.put("appid", appid);
		packageParams.put("mch_id", mch_id);
		packageParams.put("sub_mch_id", sub_mch_id);
		packageParams.put("nonce_str", nonce_str);
		packageParams.put("body", body);
		packageParams.put("attach", attach);
		packageParams.put("out_trade_no", out_trade_no);

		// 这里写的金额为1 分到时修改
		packageParams.put("total_fee", totalFee);
		packageParams.put("spbill_create_ip", spbill_create_ip);
		packageParams.put("notify_url", notify_url);

		packageParams.put("trade_type", trade_type);
		packageParams.put("openid", openId);

		RequestHandler reqHandler = new RequestHandler(null, null);
		reqHandler.init(appid, appsecret, partnerkey);

		String sign = reqHandler.createSign(packageParams);
		String xml = "<xml>"   "<appid>"   appid   "</appid>"   "<mch_id>"
				  mch_id   "</mch_id>"   "<nonce_str>"   nonce_str
				  "</nonce_str>"   "<sign>"   sign   "</sign>"
				  "<body><![CDATA["   body   "]]></body>" 
				  "<out_trade_no>"   out_trade_no
				  "</out_trade_no>"   "<attach>"   attach   "</attach>"
				  "<total_fee>"   totalFee   "</total_fee>"
				  "<spbill_create_ip>"   spbill_create_ip
				  "</spbill_create_ip>"   "<notify_url>"   notify_url
				  "</notify_url>"   "<trade_type>"   trade_type
				  "</trade_type>"   "<openid>"   openId   "</openid>"
				  "</xml>";
		String prepay_id = "";
		String createOrderURL = "https://api.mch.weixin.qq.com/pay/unifiedorder";
		
		
		prepay_id = new GetWxOrderno().getPayNo(createOrderURL, xml);

		System.out.println("获取到的预支付ID:"   prepay_id);
		
		
		//获取prepay_id后,拼接最后请求支付所需要的package
		
		SortedMap<String, String> finalpackage = new TreeMap<String, String>();
		String timestamp = Sha1Util.getTimeStamp();
		String packages = "prepay_id=" prepay_id;
		finalpackage.put("appId", appid);  
		finalpackage.put("timeStamp", timestamp);  
		finalpackage.put("nonceStr", nonce_str);  
		finalpackage.put("package", packages);  
		finalpackage.put("signType", "MD5");
		//要签名
		String finalsign = reqHandler.createSign(finalpackage);
		
		String finaPackage = "\"appId\":\""   appid   "\",\"timeStamp\":\""   timestamp
		  "\",\"nonceStr\":\""   nonce_str   "\",\"package\":\""
		  packages   "\",\"signType\" : \"MD5"   "\",\"paySign\":\""
		  finalsign   "\"";

		System.out.println("V3 jsApi package:" finaPackage);
		return finaPackage;
	}

	/**
	 * 获取随机字符串
	 * @return
	 */
	public static String getNonceStr() {
		// 随机数
		String currTime = TenpayUtil.getCurrTime();
		// 8位日期
		String strTime = currTime.substring(8, currTime.length());
		// 四位随机数
		String strRandom = TenpayUtil.buildRandom(4)   "";
		// 10位序列号,可以自行调整。
		return strTime   strRandom;
	}

	/**
	 * 元转换成分
	 * @param money
	 * @return
	 */
	public static String getMoney(String amount) {
		if(amount==null){
			return "";
		}
		// 金额转化为分为单位
		String currency =  amount.replaceAll("\\$|\\¥|\\,", "");  //处理包含, ¥ 或者$的金额  
        int index = currency.indexOf(".");  
        int length = currency.length();  
        Long amLong = 0l;  
        if(index == -1){  
            amLong = Long.valueOf(currency "00");  
        }else if(length - index >= 3){  
            amLong = Long.valueOf((currency.substring(0, index 3)).replace(".", ""));  
        }else if(length - index == 2){  
            amLong = Long.valueOf((currency.substring(0, index 2)).replace(".", "") 0);  
        }else{  
            amLong = Long.valueOf((currency.substring(0, index 1)).replace(".", "") "00");  
        }  
        return amLong.toString(); 
	}
}
资源下载此资源下载价格为3D币(VIP免费),请先
资源文件列表
weChatpay/.classpath , 529
weChatpay/.mymetadata , 310
weChatpay/.project , 1757
weChatpay/.settings/.jsdtscope , 500
weChatpay/.settings/org.eclipse.core.resources.prefs , 88
weChatpay/.settings/org.eclipse.jdt.core.prefs , 395
weChatpay/.settings/org.eclipse.wst.common.component , 539
weChatpay/.settings/org.eclipse.wst.common.project.facet.core.xml , 252
weChatpay/.settings/org.eclipse.wst.jsdt.ui.superType.container , 49
weChatpay/.settings/org.eclipse.wst.jsdt.ui.superType.name , 6
weChatpay/src/com/demo/Demo.java , 9077
weChatpay/src/com/demo/Notify.java , 4235
weChatpay/src/com/demo/WxPayDto.java , 2076
weChatpay/src/com/demo/WxPayResult.java , 4258
weChatpay/src/com/utils/GetWxOrderno.java , 4413
weChatpay/src/com/utils/http/HttpClientConnectionManager.java , 2022
weChatpay/src/com/utils/http/HttpConnect.java , 1810
weChatpay/src/com/utils/http/HttpRequest.java , 3498
weChatpay/src/com/utils/http/HttpResponse.java , 1773
weChatpay/src/com/utils/http/HttpResultType.java , 699
weChatpay/src/com/utils/http/MySSLSocketFactory.java , 1224
weChatpay/src/com/utils/http/TrustAnyTrustManager.java , 584
weChatpay/src/com/utils/MD5Util.java , 1153
weChatpay/src/com/utils/RequestHandler.java , 6448
weChatpay/src/com/utils/Sha1Util.java , 2090
weChatpay/src/com/utils/TenpayUtil.java , 3624
weChatpay/WebRoot/META-INF/MANIFEST.MF , 36
weChatpay/WebRoot/page/jquery.min.js , 57254
weChatpay/WebRoot/page/jsapi_demo.html , 1168
weChatpay/WebRoot/page/native_demo.html , 827
weChatpay/WebRoot/page/qrcode.js , 22454
weChatpay/WebRoot/readme.txt , 400
weChatpay/WebRoot/WEB-INF/classes/com/demo/Demo.class , 6906
weChatpay/WebRoot/WEB-INF/classes/com/demo/Notify.class , 4769
weChatpay/WebRoot/WEB-INF/classes/com/demo/WxPayDto.class , 1693
weChatpay/WebRoot/WEB-INF/classes/com/demo/WxPayResult.class , 3512
weChatpay/WebRoot/WEB-INF/classes/com/utils/GetWxOrderno.class , 5054
weChatpay/WebRoot/WEB-INF/classes/com/utils/http/HttpClientConnectionManager.class , 2721
weChatpay/WebRoot/WEB-INF/classes/com/utils/http/HttpConnect.class , 2941
weChatpay/WebRoot/WEB-INF/classes/com/utils/http/HttpRequest.class , 2654
weChatpay/WebRoot/WEB-INF/classes/com/utils/http/HttpResponse.class , 1372
weChatpay/WebRoot/WEB-INF/classes/com/utils/http/HttpResultType.class , 1033
weChatpay/WebRoot/WEB-INF/classes/com/utils/http/MySSLSocketFactory.class , 1603
weChatpay/WebRoot/WEB-INF/classes/com/utils/http/TrustAnyTrustManager.class , 889
weChatpay/WebRoot/WEB-INF/classes/com/utils/MD5Util.class , 2075
weChatpay/WebRoot/WEB-INF/classes/com/utils/RequestHandler.class , 5929
weChatpay/WebRoot/WEB-INF/classes/com/utils/Sha1Util.class , 3272
weChatpay/WebRoot/WEB-INF/classes/com/utils/TenpayUtil.class , 3308
weChatpay/WebRoot/WEB-INF/lib/commons-beanutils-1.8.0.jar , 231320
weChatpay/WebRoot/WEB-INF/lib/commons-cli-1.0.jar , 30117
weChatpay/WebRoot/WEB-INF/lib/commons-codec-1.7.jar , 259600
weChatpay/WebRoot/WEB-INF/lib/commons-collections-3.2.1.jar , 575389
weChatpay/WebRoot/WEB-INF/lib/commons-dbcp-20030825.184428.jar , 94636
weChatpay/WebRoot/WEB-INF/lib/commons-httpclient-3.1.jar , 305001
weChatpay/WebRoot/WEB-INF/lib/commons-lang-2.6.jar , 284220
weChatpay/WebRoot/WEB-INF/lib/commons-logging-1.1.3.jar , 62050
weChatpay/WebRoot/WEB-INF/lib/commons-pool-1.6.jar , 111119
weChatpay/WebRoot/WEB-INF/lib/ezmorph-1.0.6.jar , 86487
weChatpay/WebRoot/WEB-INF/lib/httpclient-4.2.5.jar , 433368
weChatpay/WebRoot/WEB-INF/lib/httpcore-4.2.4.jar , 227275
weChatpay/WebRoot/WEB-INF/lib/jdom-1.0.jar , 153253
weChatpay/WebRoot/WEB-INF/lib/json-lib-2.4-jdk15.jar , 159123
weChatpay/WebRoot/WEB-INF/lib/servlet-api.jar , 132931
weChatpay/WebRoot/WEB-INF/web.xml , 462
没有账号? 忘记密码?

社交账号快速登录