微信支付》统一下单接口调用

遇到第一次做微信支付的小伙伴可能会遇到很大的问题,第一感觉就是摸不着头脑,完全没思路
我先给你说一下微信支付的流程,当你看过微信的官方文档之后,需要的一些关键参数我想你已经了解了,主要是appid,mch_id,key为商户平台设置的密钥key。这些参数可是必不可少的哟。支付流程分以下几步:
1)获取用户openID(如果你无法获取,建议你先去看看怎么获取openID)
2)通过统一下单接口创建微信支付订单
3)设置微信支付成功后回调路径(主要是对支付成功后做的一系列操作,为了安全起见,可调用订单查询接口判断一下订单是否支付成功)
4)调起微信支付,可通过js_sdk调起
下面主要说一下统一下单接口,我就直接上代码了(注意:属性上边标识1为必填,0为非必填,更多规则你可参照  》》  点击打开链接   《《)

package com.jikebao.gds.plugin;

import java.security.MessageDigest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;

@SuppressWarnings("unused")
public class WxpayPlugin {
	
	public WxpayPlugin(){}
	/**
	 * 所有参数类型构造器
	 * @param device_info
	 * @param nonce_str
	 * @param sign
	 * @param sign_type
	 * @param body
	 * @param detail
	 * @param attach
	 * @param out_trade_no
	 * @param fee_type
	 * @param total_fee
	 * @param spbill_create_ip
	 * @param time_start
	 * @param time_expire
	 * @param goods_tag
	 * @param notify_url
	 * @param trade_type
	 * @param product_id
	 * @param limit_pay
	 * @param openid
	 */
	public WxpayPlugin(String device_info, String nonce_str, String sign, String sign_type, String body, String detail,
			String attach, String out_trade_no, String fee_type, String total_fee, String spbill_create_ip,
			String time_start, String time_expire, String goods_tag, String notify_url, String trade_type,
			String product_id, String limit_pay, String openid) {
		super();
		this.device_info = device_info;
		this.nonce_str = nonce_str;
		this.sign = sign;
		this.sign_type = sign_type;
		this.body = body;
		this.detail = detail;
		this.attach = attach;
		this.out_trade_no = out_trade_no;
		this.fee_type = fee_type;
		this.total_fee = total_fee;
		this.spbill_create_ip = spbill_create_ip;
		this.time_start = time_start;
		this.time_expire = time_expire;
		this.goods_tag = goods_tag;
		this.notify_url = notify_url;
		this.trade_type = trade_type;
		this.product_id = product_id;
		this.limit_pay = limit_pay;
		this.openid = openid;
	}




	/**
	 * 必传参数构造器,同时添加到map集合
	 * @param nonce_str
	 * @param body
	 * @param out_trade_no
	 * @param total_fee
	 * @param spbill_create_ip
	 * @param notify_url
	 * @param trade_type
	 */
	public WxpayPlugin(String body, String out_trade_no, String total_fee, String spbill_create_ip,
			String notify_url, String trade_type,String openid) {
		this.nonce_str = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 32);
		this.body = body;
		this.out_trade_no = out_trade_no;
		this.total_fee = total_fee;
		this.spbill_create_ip = spbill_create_ip;
		this.notify_url = notify_url;
		this.trade_type = trade_type;
		this.openid=openid;
		data.put("nonce_str", this.nonce_str);
		data.put("body", this.body);
		data.put("out_trade_no", this.out_trade_no);
		data.put("total_fee", this.total_fee);
		data.put("spbill_create_ip", this.spbill_create_ip);
		data.put("notify_url", this.notify_url);
		data.put("trade_type", this.trade_type);
		data.put("openid", this.openid);
		data.put("appid", this.appid);
		data.put("mch_id", this.mch_id);
		
	}




	/**1
	 * 公众账号ID
	 */
	private final String appid="";
	/**1
	 * 商户号
	 */
	private final String mch_id="";
	/**0
	 * 设备号
	 */
	private String device_info;
	/**1
	 * 随机字符串
	 */
	private String nonce_str;
	/**1
	 * 签名
	 */
	private String sign;
	/**0
	 * 签名类型
	 */
	private String sign_type;
	/**1
	 * 商品描述
	 */
	private String body;
	/**0
	 * 商品详情(注意不能编译的字符,建议加上<![CDATA[   ]]> )
	 */
	private String detail;
	/**0
	 * 附加数据
	 */
	private String attach;
	/**1
	 * 商户订单号
	 */
	private String out_trade_no;
	/**0
	 * 标价币种
	 */
	private String fee_type;
	/**1
	 * total_fee
	 */
	private String total_fee;
	/**1
	 * 终端IP
	 */
	private String spbill_create_ip;
	/**0
	 * 交易起始时间
	 */
	private String time_start;
	/**0
	 * 交易结束时间
	 */
	private String time_expire;
	/**0
	 * 订单优惠标记
	 */
	private String goods_tag;
	/**1
	 * 通知地址
	 */
	private String notify_url;
	/**1
	 * 交易类型
	 */
	private String trade_type;
	/**0
	 * 商品ID
	 */
	private String product_id;
	/**0
	 * 指定支付方式
	 */
	private String limit_pay;
	/**0
	 * 用户标识(JSAPI支付必须传openid)
	 */
	private String openid;
	/**0
	 * key为商户平台设置的密钥key 
	 */
	private final String key="";
	/**
	 * 参数集合
	 */
	private Map<String, String> data=new HashMap<String, String>();
	
	/**
	 * 统一下单URL地址:https://api.mch.weixin.qq.com/pay/unifiedorder
	 */
	private final String createOrderUrl="https://api.mch.weixin.qq.com/pay/unifiedorder";
	
	/**
	 * 获取签名
	 * @return
	 * @throws Exception 
	 */
	private String getSign() throws Exception{
		StringBuilder sb = new StringBuilder();
		Set<String> datakey=data.keySet();
		int datakeysize=datakey.size();
		String[] datakeyarr=new String[datakeysize];
		datakey.toArray(datakeyarr);
		Arrays.sort(datakeyarr);
		for (int i = 0; i < datakeysize; i++) {
			if(null != data.get(datakeyarr[i]) && ""!=data.get(datakeyarr[i])){
				sb.append(datakeyarr[i]).append("=").append(data.get(datakeyarr[i])).append("&");
			}
			
		}
		sb.append("key=");
		sb.append(key);
		return encryptionMd5(sb.toString());
	}
	
	/**
	 * 对字符串进行MD5加密
	 * @param str 需要进行加密的字符串
	 * @return 加密后的字符串
	 * @throws Exception
	 */
	private String encryptionMd5(String str) throws Exception{
		java.security.MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] array = md.digest(str.getBytes("UTF-8"));
        StringBuilder sb = new StringBuilder();
        for (byte item : array) {
            sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString().toUpperCase();
	}
	
	/**
	 * 生成xml格式参数
	 * @return
	 * @throws Exception
	 */
	private String createXmlParam() throws Exception{
		sign=getSign();
		data.put("sign", sign);
		StringBuilder sb = new StringBuilder();
		sb.append("<xml>");
		for (String in : data.keySet()) {
			if(null != data.get(in) && ""!=data.get(in)){
				sb.append("<").append(in).append(">").append(data.get(in)).append("</").append(in).append(">");
			}
		}
		sb.append("</xml>");
		System.out.println(sb.toString());
		return sb.toString();
	}
	
	/**
	 * 调用统一下单接口
	 * @return
	 * @throws Exception
	 */
	public String createOrder() throws Exception {
		String str=null;
		HttpClient client = new HttpClient();
		PostMethod post = new PostMethod(createOrderUrl);
		post.setRequestEntity(new StringRequestEntity(createXmlParam(), "text/xml", "UTF-8"));
		client.executeMethod(post);
		byte[] by = post.getResponseBody();
		
		str=new String(by);
		 //释放连接
        post.releaseConnection();
        return str;
    }
}


package com.jikebao.gds.test;

import java.util.Date;

import com.jikebao.gds.plugin.WxpayPlugin;

public class ZhouTest {

	public static void main(String[] args) throws Exception {
		Long datestr=new Date().getTime();
		WxpayPlugin plugin=new WxpayPlugin("测试商品", String.valueOf(datestr), "1", "222.211.175.100", "http://www.zhou.red", "JSAPI","orD24uPL2bOpRqzo_rUv94pLkmEk");
		String str =plugin.createOrder();//接收微信返回的数据,并转换成字符串,未xml类型
		System.out.println(str);
	}
}

相关参数appid,mch_id,key需要你自己填进去

以上就是统一下单接口的应用,其他与微信支付相关的微信接口你也可以模仿着调用,不会太难!

最后,大家要是觉得好,请点赞,有问题请留言[email protected]

点赞