package com.chw.payment.wechatpay.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import javax.servlet.http.HttpServletRequest;
import org.jdom.JDOMException;
import com.chw.payment.wechatpay.config.WechatConfig;
import com.chw.payment.wechatpay.config.WechatConfigApp;
import com.chw.system.common.Constant;
import com.chw.system.util.CaptchaUtils;
import com.chw.system.util.JsonUtil;
/**
* 微信支付专用类 支付方法
* @author Administrator
*
*/
public class WechatPay {
public static String wechatpay_key_appid="appid";
public static String wechatpay_key_mchid="mch_id";
public static String wechatpay_key_noncestr="nonce_str";
public static String wechatpay_key_body="body";
public static String wechatpay_key_outTradeNo="out_trade_no";
public static String wechatpay_key_totalFee="total_fee";
public static String wechatpay_key_spbillIp="spbill_create_ip";
public static String wechatpay_key_notyfyUrl="notify_url";
public static String wechatpay_key_tradeType="trade_type";
public static String wechatpay_key_refoundNo="out_refund_no";
public static String wechatpay_key_refoundFee="refund_fee";
public static String wechatpay_key_optUserId="op_user_id";
public static String wechatpay_key_returnCode="return_code";
public static String wechatpay_key_service="unifiedorder";
public static String wechatrefound_key_service="refound";
public static String wechatpay_key_attach="attach";
/**
* 微信支付 生成含有微信支付连接的二维码 URL
* https://pay.weixin.qq.com/guide/qrcode_payment.shtml
* @param orderPrice
* @param decript
* @param orderNo
* @return
* @throws Exception
*/
public static String weixinQRPay(String orderPrice,String decript,String orderNo,String attach) throws Exception {
// 账号信息
String appid = WechatConfig.appID; // appid
//String appsecret = PayConfigUtil.APP_SECRET; // appsecret
String mch_id = WechatConfig.mchID; // 商业号
String key = WechatConfig.key; // key
String currTime = WePayCommonUtil.getCurrTime();
String strTime = currTime.substring(8, currTime.length());
String strRandom = WePayCommonUtil.buildRandom(4) + "";
String nonce_str = strTime + strRandom;
// 获取发起电脑 ip
String spbill_create_ip = WechatConfig.ip;
// 回调接口
String notify_url = Constant.LONGPINGTAI_HOST+WechatConfig.notifyUrl;
String trade_type = "NATIVE";
SortedMap<Object,Object> packageParams = new TreeMap<Object,Object>();
packageParams.put(wechatpay_key_appid, appid);
packageParams.put(wechatpay_key_mchid, mch_id);
packageParams.put(wechatpay_key_noncestr, nonce_str);
packageParams.put(wechatpay_key_body, decript);
packageParams.put(wechatpay_key_outTradeNo, orderNo);
/******修改******王金涛******微信支付单位由元修改为分传递*************20180226***开始********** */
BigDecimal newOrderPrice = new BigDecimal(orderPrice).multiply(new BigDecimal(100)).setScale(0);
packageParams.put(wechatpay_key_totalFee, newOrderPrice.toString());
/******结束****** */
packageParams.put(wechatpay_key_spbillIp, spbill_create_ip);
packageParams.put(wechatpay_key_notyfyUrl, notify_url);
packageParams.put(wechatpay_key_tradeType, trade_type);
packageParams.put(wechatpay_key_attach, attach);
String sign = WePayCommonUtil.createSign("UTF-8", packageParams,key);
packageParams.put("sign", sign);
String requestXML = WePayCommonUtil.getRequestXml(packageParams);
System.out.println(requestXML);
String resXml = HttpUtil.postData(WechatConfig.UNIFIED_PAY, requestXML);
/*ChwLogUtil.getTradeLogger().info("微信扫码支付QR resXml:"+ resXml); */
Map map = XMLUtil.doXMLParse(resXml);
String urlCode="-1";
if("SUCCESS".equals((String) map.get("return_code"))){
//String return_code = (String) map.get("return_code");
//String prepay_id = (String) map.get("prepay_id");
//微信支付url生成
urlCode = (String) map.get("code_url");
/*ChwLogUtil.getTradeLogger().info("微信扫码支付QRURl:"+ urlCode+" 订单号:"+orderNo);*/
//二维码url地址 二维码在画面生成
//urlCode=WePayCommonUtil.QRfromGoogle(urlCode);
}
return urlCode;
}
/**
* 微信支付查询
*
* @param orderNo
* @return 详细说明参照:https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_2
*
* 返回值说明:
* return_code (SUCCESS/FAIL)
*
* 以下字段在return_code为SUCCESS的时候有返回
* appid
* mch_id
* nonce_str
* sign
* result_code
* trade_state
*
* 以下字段在return_code 、result_code、trade_state都为SUCCESS时有返回
* out_trade_no
* time_end 订单支付时间,格式为yyyyMMddHHmmss
* total_fee 订单总金额,单位为分
*
* @throws Exception
*/
public static Map wepayQuery(String orderNo)throws Exception{
// 账号信息
String appid = WechatConfig.appID; // appid
//String appsecret = PayConfigUtil.APP_SECRET; // appsecret
String mch_id = WechatConfig.mchID; // 商业号
String key = WechatConfig.key; // key
String currTime = WePayCommonUtil.getCurrTime();
String strTime = currTime.substring(8, currTime.length());
String strRandom = WePayCommonUtil.buildRandom(4) + "";
String nonce_str = strTime + strRandom;
SortedMap<Object,Object> packageParams = new TreeMap<Object,Object>();
packageParams.put(wechatpay_key_appid, appid);
packageParams.put(wechatpay_key_mchid, mch_id);
packageParams.put(wechatpay_key_noncestr, nonce_str);
packageParams.put(wechatpay_key_outTradeNo, orderNo);
String sign = WePayCommonUtil.createSign("UTF-8", packageParams,key);
packageParams.put("sign", sign);
String requestXML = WePayCommonUtil.getRequestXml(packageParams);
System.out.println(requestXML);
String resXml = HttpUtil.postData(WechatConfig.PAY_QUERY_API, requestXML);
System.out.println("微信退款返回参数:"+resXml);
return XMLUtil.doXMLParse(resXml);
}
/**
* 微信支付退款-----PC端退款 C端订单
*
* @param orderNo
* @return 详细说明参照:https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_4
*
* 返回值说明:
* return_code (SUCCESS/FAIL)
*
* 以下字段在return_code为SUCCESS的时候有返回
* appid
* mch_id
* nonce_str
* sign
* transaction_id 微信订单号
* refund_id 微信退款单号
* refund_fee 退款总金额,单位为分
* total_fee 订单总金额,单位为分
* out_refund_no
* out_trade_no
*
* @throws Exception
*/
public static Map wepayRefound4C(String orderNo,String refoundNo,long totalFee, long refoundFee)throws Exception{
// 账号信息
String appid = WechatConfigApp.appID; // appid
//String appsecret = PayConfigUtil.APP_SECRET; // appsecret
String mch_id = WechatConfigApp.mchID; // 商业号
String key = WechatConfigA
评论1