Micro-channel pay (PC scan code and pay H5 number of public pay)

In a letter recently to make micro payments, micro-channel pay relatively pit, the official actually only .NET, C #, PHP is actually no demo of the demo java. Then micro-channel payment is not required to provide test account directly with the public official number. First, let's introduce micro-channel scan code to pay it, micro-channel scan code in two ways, where the second method is unified under a single ( JSAPI ). In advance public micro-channel micro-channel payment application numbers background opened, the opening of specific rules detailed documentation see Click to open link

In fact, the amount of micro-channel pay quite simply nothing more than micro-channel assembled parameters sent to the request, points to note is that the payment is divided, there is no micro-channel synchronous callback address, only asynchronous callback address. In this case you can not pay the real-time feedback to the PC end results page. Online most synchronous approach is by polling the status of orders ajax, (can check the payment status of your service orders or to transfer micro-channel pay query interface micro letter to order status we returned) Both are, lz recommendations or to inquire micro letter Order inquiry service side of the interface. This will ease the pressure on our own server, but also can prevent malicious people to brush our interface. Jingdong also uses state of the timing of polling query orders they are to check once every 3s.

Attached below H5 PC scan and code the code which is paid in a single H5 and Pc Interface

package com.workit.pay.weixin.wxPay;

import java.util.Date;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.workit.comm.util.String.StringUtil;
import com.workit.pay.weixin.wxPay.constant.WeiXinPayConstant;
import com.workit.pay.weixin.wxPay.util.PayCommonUtil;
import com.workit.pay.weixin.wxPay.util.WeixinFormatXmlProcess;



/**   
* @Title: WxPayService.java
*.qqwx.wxPay
* @Description: TODO
*   
* @date 2017年5月3日 上午11:16:48
* @version V1.0   
*/
public class WxPayService {
	private static final Logger logger = LoggerFactory.getLogger(WxPayService.class);
	
	
	
	public static String getOrderNo(){
		return "DD"+String.valueOf(new Date().getTime());
	}
	
	/**
	 * 
	 * @Title: unifiedOrderPC
	 * @Description: 微信PC 扫码下单接口
	 * @param @param body
	 * @param @param orderNo
	 * @param @param totalFee
	 * @param @return
	 * @param @throws Exception    
	 * @return String
	 */
	public static String unifiedOrderPC(String body,String orderNo, String totalFee) throws Exception{
		return unifiedOrder(body,orderNo, totalFee,null,WeiXinPayConstant.TRADE_TYPE_PC,null);
	}
	
	/**
	 * 
	 * @Title: unifiedOrderH5
	 * @Description: 微信H5 下单接口
	 * @param @param body 商品描述
	 * @param @param orderNo
	 * @param @param totalFee
	 * @param @param openId
	 * @param @param ip
	 * @param @return
	 * @param @throws Exception    
	 * @return SortedMap<Object,Object>
	 */
	public static SortedMap<Object, Object> unifiedOrderH5(String body,String orderNo, String totalFee,String openId,String ip) throws Exception{
		String prepay_id =  unifiedOrder(body,orderNo, totalFee,openId,WeiXinPayConstant.TRADE_TYPE_H5,ip);
		if(StringUtil.isNotEmpty(prepay_id)){
			SortedMap<Object, Object> payMap = new TreeMap<Object, Object>();
	        payMap.put("appId",WeiXinPayConstant.APPID);  
	        payMap.put("timeStamp",PayCommonUtil.createTimestamp());  
	        payMap.put("nonceStr",PayCommonUtil.CreateNoncestr());  
	        payMap.put("signType","MD5");  
	        payMap.put("package", "prepay_id="+prepay_id);  
	        String paySign = PayCommonUtil.createSign("UTF-8",payMap,WeiXinPayConstant.API_KEY);
	        payMap.put("paySign", paySign); 
	        payMap.put("pg", prepay_id);  
	        return payMap;
		}else{
			return null;
		}
  
	}
	/**
	 * 
	 * @Title: unifiedOrder
	 * @Description: 微信支付扫码和H5支付统一下单接口
	 * @param @param body 商品描述
	 * @param @param orderNo 订单编号
	 * @param @param totalFee 总金额单位分
	 * @param @param openId H5必传的参数
	 * @param @param payType 支付终端
	 * @param @param ip 请求IP
	 * @param @return
	 * @param @throws Exception    
	 * @return String 
	 */
	public static String unifiedOrder(String body,String orderNo, String totalFee,String openId,String payType,String ip) throws Exception{
		SortedMap<Object, Object> parameters = new TreeMap<Object, Object>();
		String tradeType = "";
		String spbill_create_ip = "";
		if(WeiXinPayConstant.TRADE_TYPE_H5.equals(payType)){//H5支付
			tradeType = WeiXinPayConstant.TRADE_TYPE_H5;
			spbill_create_ip = ip;
			parameters.put("openid", openId);//用户标识 openid 否 String(128) oUpF8uMuAJO_M2pxb1Q9zNjWeS6o trade_type=JSAPI时(即公众号支付),此参数必传,此参数为微信用户在商户对应appid下的唯一标识。openid如何获取,可参考【获取openid】。企业号请使用【企业号OAuth2.0接口】获取企业号内成员userid,再调用【企业号userid转openid接口】进行转换 
		}else{
			spbill_create_ip = "10.20.20.64";
			tradeType = WeiXinPayConstant.TRADE_TYPE_PC;//PC扫码支付
			parameters.put("product_id",orderNo);
		}
		parameters.put("appid", WeiXinPayConstant.APPID);//公众账号ID appid 是 String(32) wxd678efh567hg6787 微信支付分配的公众账号ID(企业号corpid即为此appId) 
		parameters.put("body", body);//商品描述 body 是 String(128) 腾讯充值中心-QQ会员充值 商品简单描述,该字段请按照规范传递,具体请见参数规定
		parameters.put("mch_id", WeiXinPayConstant.MCH_ID);//商户号 mch_id 是 String(32) 1230000109 微信支付分配的商户号 
		parameters.put("nonce_str",PayCommonUtil.CreateNoncestr());//随机字符串 nonce_str 是 String(32) 5K8264ILTKCH16CQ2502SI8ZNMTM67VS 随机字符串,长度要求在32位以内。推荐随机数生成算法 
		parameters.put("out_trade_no",orderNo);//商户订单号 out_trade_no 是 String(32) 20150806125346 商户系统内部订单号,要求32个字符内,只能是数字、大小写字母_-|*@ ,且在同一个商户号下唯一。详见商户订单号 
		parameters.put("spbill_create_ip",spbill_create_ip);//终端IP spbill_create_ip 是 String(16) 123.12.12.123 APP和网页支付提交用户端ip,Native支付填调用微信支付API的机器IP。 
		parameters.put("total_fee", totalFee);//标价金额 total_fee 是 Int 88 订单总金额,单位为分,详见支付金额 
		parameters.put("notify_url",WeiXinPayConstant.NOTIFY_URL);//通知地址 notify_url 是 String(256) http://www.weixin.qq.com/wxpay/pay.php 异步接收微信支付结果通知的回调地址,通知url必须为外网可访问的url,不能携带参数。 
		parameters.put("trade_type",tradeType);//交易类型 trade_type 是 String(16) JSAPI 取值如下:JSAPI,NATIVE,APP等,说明详见参数规定 
		String sign = PayCommonUtil.createSign("UTF-8",parameters,WeiXinPayConstant.API_KEY);//签名 sign 是 String(32) C380BEC2BFD727A4B6845133519F3AD6 通过签名算法计算得出的签名值,详见签名生成算法 
		parameters.put("sign", sign);
		String requestXML = PayCommonUtil.getRequestXml(parameters); //把参数打包转成xml格式
		String result =PayCommonUtil.httpsRequest(WeiXinPayConstant.WEIXIN_PAY_URL,"POST",requestXML);//向微信发送统一下单请求
		Map<String, Object> mapResult = WeixinFormatXmlProcess.parseXml1(result); //微信响应的结果
		String resultCode =(String)mapResult.get("return_code");
		logger.info(mapResult.toString());
		if(WeiXinPayConstant.WX_PAY_SUCCESS_STATUS.equals(resultCode)){
			if(WeiXinPayConstant.TRADE_TYPE_H5.equals(payType)){//H5支付
				return (String)mapResult.get("prepay_id");
			}else{
				return (String)mapResult.get("code_url");
			}
		}else{
			return null;
		}
	}
	
}


Published 55 original articles · won praise 31 · views 80000 +

Guess you like

Origin blog.csdn.net/zengfanwei1990/article/details/71727932