java微信公众号支付相关说明

一. 微信支付配置

如下图,找到微信支付“开发配置”:



  • http://htyundai.com/jfinal-weixin-demo/pay/对应后台处理微信支付逻辑的路径,前台js主要代码如下:

  • //微信支付
     $("#li_wepay").click(function(){
          var expense = 10;
          var url = contextPath + "/pay";
      	  $.post(url,
      			{	expense : expense,
      			},function(result){
      			  WeixinJSBridge.invoke('getBrandWCPayRequest',
      					{
      				 	 "appId":result.appId,
      				 	  "timeStamp":result.timeStamp,
      				 	  "nonceStr":result.nonceStr, //随机串     
      			          "package":result.package,     
      			          "signType":result.signType,         //微信签名方式:     
      			          "paySign":result.paySign //微信签名 
      			  		},
      					function(res){
      			  			//getProes(res);
      			  			//支付成功或失败前台判断
      						if(res.err_msg=='get_brand_wcpay_request:ok'){
      						     window.location.href = contextPath+"/pay_finish.jsp";
      						}   	  
      					});
      		  },'json');
     	});
    个人觉得如果条件允许,不必使用测试授权目录和测试白名单,因为以后移植的时候可能导致错误。
  • 值得注意的是,由于android机微信开发,错误提示得非常不明显,笔者建议直接获取支付返回的“res”对象,将里面的属性全部弹出,就知道哪里出错了!

  • 二.  微信支付主要后台代码
  • 2.1 微信支付后台主接口
  • 调用微信支付方法:
  • /**
    	 * 微信支付配置
    	 * @param money
    	 * @param orderid
    	 * @return
    	 */
    	public String handleWePay(String money,String orderid) {
    		//微信支付jsApi
    		WxPayDto tpWxPay = new WxPayDto();
    		tpWxPay.setOpenId(CookieUtils.getCookieByName(getRequest(), "openid"));
    		tpWxPay.setBody("商品信息");
    		//订单号
    		tpWxPay.setOrderId(orderid);
    		tpWxPay.setSpbillCreateIp("127.0.0.1");
    		//TODO 暂时设为1分方便测试 money
    		tpWxPay.setTotalFee("0.01");
    		//tpWxPay.setTotalFee(money);
    	   return "{"+PayUtils.getPackage(tpWxPay)+"}";
    	}
    封装的WxPayDto类:
  • package com.cbj.entity;
    
    
    public class WxPayDto {
    
    	private String orderId;//订单号
    	private String totalFee;//金额
    	private String spbillCreateIp;//订单生成的机器 IP
    	private String notifyUrl;//这里notify_url是 支付完成后微信发给该链接信息,可以判断会员是否支付成功,改变订单状态等
    	private String body;// 商品描述根据情况修改
    	private String openId;//微信用户对一个公众号唯一
    	
    	/**
    	 * @return the orderId
    	 */
    	public String getOrderId() {
    		return orderId;
    	}
    	/**
    	 * @param orderId the orderId to set
    	 */
    	public void setOrderId(String orderId) {
    		this.orderId = orderId;
    	}
    	/**
    	 * @return the totalFee
    	 */
    	public String getTotalFee() {
    		return totalFee;
    	}
    	/**
    	 * @param totalFee the totalFee to set
    	 */
    	public void setTotalFee(String totalFee) {
    		this.totalFee = totalFee;
    	}
    	/**
    	 * @return the spbillCreateIp
    	 */
    	public String getSpbillCreateIp() {
    		return spbillCreateIp;
    	}
    	/**
    	 * @param spbillCreateIp the spbillCreateIp to set
    	 */
    	public void setSpbillCreateIp(String spbillCreateIp) {
    		this.spbillCreateIp = spbillCreateIp;
    	}
    	/**
    	 * @return the notifyUrl
    	 */
    	public String getNotifyUrl() {
    		return notifyUrl;
    	}
    	/**
    	 * @param notifyUrl the notifyUrl to set
    	 */
    	public void setNotifyUrl(String notifyUrl) {
    		this.notifyUrl = notifyUrl;
    	}
    	/**
    	 * @return the body
    	 */
    	public String getBody() {
    		return body;
    	}
    	/**
    	 * @param body the body to set
    	 */
    	public void setBody(String body) {
    		this.body = body;
    	}
    	/**
    	 * @return the openId
    	 */
    	public String getOpenId() {
    		return openId;
    	}
    	/**
    	 * @param openId the openId to set
    	 */
    	public void setOpenId(String openId) {
    		this.openId = openId;
    	}
    	
    }
    
    PayUtils中的getPackage方法:
  • /**
    	 * 获取请求预支付id报文
    	 * @return
    	 */
    	@SuppressWarnings("static-access")
    	public static String getPackage(WxPayDto tpWxPayDto) {
    		
    		String openId = tpWxPayDto.getOpenId();
    		// 1 参数
    		// 订单号
    		String orderId = tpWxPayDto.getOrderId();
    		// 附加数据 原样返回
    		String attach = "";
    		// 总金额以分为单位,不带小数点
    		String totalFee = getMoney(tpWxPayDto.getTotalFee());
    		
    		// 订单生成的机器 IP
    		String spbill_create_ip = tpWxPayDto.getSpbillCreateIp();
    		// 这里notify_url是 支付完成后微信发给该链接信息,可以判断会员是否支付成功,改变订单状态等。
    		String trade_type = "JSAPI";
    
    		// ---必须参数
    		// 商户号
    		String mch_id = PropKit.get("mch_id");
    		// 随机字符串
    		String nonce_str = SignUtil.getNonceStr();
    
    		// 商品描述根据情况修改
    		String body = tpWxPayDto.getBody();
    
    		// 商户订单号
    		String out_trade_no = orderId;
    		String appid = PropKit.get("appId");
    		SortedMap<String, String> packageParams = new TreeMap<String, String>();
    		packageParams.put("appid", appid);
    		packageParams.put("mch_id", mch_id);
    		packageParams.put("nonce_str", nonce_str);
    		packageParams.put("body", body);
    		packageParams.put("attach", attach);
    		packageParams.put("out_trade_no", out_trade_no);
    
    		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, PropKit.get("appSecret"), PropKit.get("paternerKey"));
    
    		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;
    	}
  • 转换金额的getMoney方法:
  • 	/**
    	 * 元转换成分
    	 * @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();  
            int amLong = 0;  
            if(index == -1){  
                amLong = Integer.valueOf(currency+"00");  
            }else if(length - index >= 3){  
                amLong = Integer.valueOf((currency.substring(0, index+3)).replace(".", ""));  
            }else if(length - index == 2){  
                amLong = Integer.valueOf((currency.substring(0, index+2)).replace(".", "")+0);  
            }else{  
                amLong = Integer.valueOf((currency.substring(0, index+1)).replace(".", "")+"00");  
            }  
            System.out.println("预支付金额:"+amLong);
            return amLong+""; 
    	}
    未完待续。。。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值