微信扫码支付Demo(java)


第一步

要扫码,必须要先有码。生成二维码demo

String url = "weixin://wxpay/bizpayurl?sign="+er.getSign()+"&appid="+er.getAppid()+
			"&mch_id="+er.getMch_id()+"&product_id="+er.getProduct_id()+"&time_stamp="+er.getTime_stamp()+"&nonce_str="+er.getNonce_str();

我相信以上几个参数,看过API的应该都清楚。这样就生成了二维码


第二步

设置扫码后微信通知你的URL

微信扫码支付Demo(java)_第1张图片



第三步

创建编写回调URL servlet

package com.yuwei.nativepay.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.yuwei.nativepay.main.UnifiedOrderBean;
import com.yuwei.nativepay.sign.Signature;
import com.yuwei.nativepay.sign.XMLParser;
import com.yuwei.nativepay.util.Configure;
import com.yuwei.nativepay.util.HttpRequest;
import com.yuwei.nativepay.util.RequestUtil;
import com.yuwei.nativepay.util.Util;

public class NativeCallBackServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 3358943773018523786L;

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		
		PrintWriter pw = resp.getWriter();
		String retStr = new String(Util.readInput(req.getInputStream()),"utf-8");
		
		//扫码返回XML demo
//		
//		
//		
//		
//		
//		
//		
//		
		
		
		
		try {
			Map map = XMLParser.getMapFromXML(retStr);
			
			//outTradeNo 订单号
			UnifiedOrderBean order = new UnifiedOrderBean(map.get("appid").toString(), map.get("mch_id").toString(), "测试1", RequestUtil.getRandomStringByLength(32), 
					null, "Ipad mini  16G  白色", "Ipad mini  16G  白色", "java demo", new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()), "CNY",
					1, "122.224.40.204", new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()), new SimpleDateFormat("yyyyMMddHHmmss").format(new Date(new Date().getTime()+1800000)), 
					"WXG", "http://????/wx_nativepay/native", "NATIVE", map.get("product_id").toString(), "no_credit", map.get("openid").toString());
			
			
			order.setSign(Signature.getSign(order));

			HttpRequest http = new HttpRequest();
			String retOrder = http.sendPost("https://api.mch.weixin.qq.com/pay/unifiedorder", order);
			
			//同意订单接口返回demo
//			
//			
//			
//			
//			
//			
//			
//			
//			
//			
//			此处的code_url模式二里面可以直接展示给用户进行扫码支付,有效期2小时
//			
			
			Map mapOrder = XMLParser.getMapFromXML(retOrder);
			
			
			//返回return xml
			Map retNative = new HashMap();
			retNative.put("return_code", mapOrder.get("return_code"));
			retNative.put("return_msg", mapOrder.get("return_msg"));
			retNative.put("appid", mapOrder.get("appid"));
			retNative.put("mch_id", mapOrder.get("mch_id"));
			retNative.put("nonce_str", mapOrder.get("nonce_str"));
			retNative.put("prepay_id", mapOrder.get("prepay_id"));
			retNative.put("result_code", mapOrder.get("result_code"));
			retNative.put("sign", Signature.getSign(retNative));
			
			
			pw.print(XMLParser.getXMLFromMap(retNative));
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			pw.flush();
			pw.close();
		}
		
		
	}
	
	
}


一切OK,到此结束



你可能感兴趣的:(微信)