package com.stylefeng.guns.modular.system.util;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONObject;
|
import com.alipay.api.AlipayApiException;
|
import com.alipay.api.AlipayClient;
|
import com.alipay.api.DefaultAlipayClient;
|
import com.alipay.api.domain.AlipayTradeAppPayModel;
|
import com.alipay.api.internal.util.AlipaySignature;
|
import com.alipay.api.request.AlipayTradeAppPayRequest;
|
import com.alipay.api.request.AlipayTradePrecreateRequest;
|
import com.alipay.api.request.AlipayTradeQueryRequest;
|
import com.alipay.api.request.AlipayTradeRefundRequest;
|
import com.alipay.api.response.AlipayTradeAppPayResponse;
|
import com.alipay.api.response.AlipayTradePrecreateResponse;
|
import com.alipay.api.response.AlipayTradeQueryResponse;
|
import com.alipay.api.response.AlipayTradeRefundResponse;
|
import org.apache.commons.collections.map.HashedMap;
|
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
import org.dom4j.Document;
|
import org.dom4j.DocumentException;
|
import org.dom4j.Element;
|
import org.dom4j.io.SAXReader;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.MediaType;
|
import org.springframework.stereotype.Component;
|
|
import javax.crypto.BadPaddingException;
|
import javax.crypto.Cipher;
|
import javax.crypto.IllegalBlockSizeException;
|
import javax.crypto.NoSuchPaddingException;
|
import javax.crypto.spec.SecretKeySpec;
|
import javax.servlet.http.HttpServletRequest;
|
import java.io.*;
|
import java.math.BigDecimal;
|
import java.net.InetAddress;
|
import java.net.UnknownHostException;
|
import java.security.InvalidKeyException;
|
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchProviderException;
|
import java.security.Security;
|
import java.util.*;
|
|
/**
|
* 第三方支付工具类
|
*/
|
@Component
|
public class PayMoneyUtil {
|
|
@Value("${alipay.appid}")
|
private String aliAppid;//支付宝appid
|
|
@Value("${alipay.appPrivateKey}")
|
private String appPrivateKey;//支付宝开发者应用私钥
|
|
@Value("${alipay.alipayPublicKey}")
|
private String alipayPublicKey;//支付宝应用公钥
|
|
@Value("${alipay.alipay_public_key}")
|
private String alipay_public_key;//支付宝支付公钥
|
|
@Value("${wx.appid}")
|
private String appid;//微信appid
|
|
@Value("${wx.mchId}")
|
private String mchId;//微信商户号
|
|
@Value("${wx.key}")
|
private String key;//微信商户号
|
|
@Value("${callbackPath}")
|
private String callbackPath;//支付回调网关地址
|
|
@Autowired
|
private HttpClientUtil httpClientUtil;
|
|
private Map<String, JSONObject> order = new HashMap<>();//存储支付订单用于主动查询支付结果
|
|
|
/**
|
* 支付宝支付
|
*/
|
public ResultUtil alipay(String body, String subject, String outTradeNo, String amount, String notifyUrl){
|
//实例化客户端
|
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", aliAppid, appPrivateKey, "json", "UTF-8", alipayPublicKey, "RSA2");
|
//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
|
AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
|
//SDK已经封装掉了公共参数,这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
|
AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
|
model.setBody(body);//对一笔交易的具体描述信息。如果是多种商品,请将商品描述字符串累加传给body。
|
model.setSubject(subject);//商品的标题/交易标题/订单标题/订单关键字等。
|
model.setOutTradeNo(outTradeNo);//商户网站唯一订单号
|
model.setTimeoutExpress("30m");
|
model.setTotalAmount(amount);//付款金额
|
model.setProductCode("QUICK_MSECURITY_PAY");
|
request.setBizModel(model);
|
request.setNotifyUrl(callbackPath + notifyUrl);
|
try {
|
//这里和普通的接口调用不同,使用的是sdkExecute
|
AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
|
Map<String, String> map = new HashMap<>();
|
map.put("orderString", response.getBody());
|
System.out.println(map);//就是orderString 可以直接给客户端请求,无需再做处理。
|
return ResultUtil.success(map);
|
} catch (AlipayApiException e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
|
/**
|
* 支付宝扫码支付下单
|
* @param body
|
* @param subject
|
* @param outTradeNo
|
* @param amount
|
* @param notifyUrl
|
* @return
|
*/
|
public ResultUtil aliScanCodePay(String body, String subject, String outTradeNo, String amount, String notifyUrl){
|
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", aliAppid, appPrivateKey, "json", "UTF-8", alipay_public_key, "RSA2"); //获得初始化的AlipayClient
|
AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();//创建API对应的request类
|
request.setBizContent("{" +
|
" \"out_trade_no\":\"" + outTradeNo + "\"," +//商户订单号
|
" \"total_amount\":\"" + 1 + "\"," +
|
" \"subject\":\"" + subject + "\"," +
|
" \"notify_url\":\"" + callbackPath + notifyUrl + "\"," +
|
" \"body\":\"" + body + "\"," +
|
" \"store_id\":\"NJ_001\"," +
|
" \"timeout_express\":\"90m\"}");//订单允许的最晚付款时间
|
AlipayTradePrecreateResponse response = null;
|
try {
|
response = alipayClient.execute(request);
|
} catch (AlipayApiException e) {
|
e.printStackTrace();
|
}
|
JSONObject alipay_trade_precreate_response = JSON.parseObject(response.getBody()).getJSONObject("alipay_trade_precreate_response");
|
|
System.err.print(alipay_trade_precreate_response.getString("qr_code"));
|
return ResultUtil.success(alipay_trade_precreate_response.getString("qr_code"));
|
}
|
|
|
/**
|
* 支付成功后的回调处理逻辑
|
* @param request
|
*/
|
public Map<String, String> alipayCallback(HttpServletRequest request){
|
//获取支付宝POST过来反馈信息
|
Map<String,String> params = new HashMap<String,String>();
|
Map requestParams = request.getParameterMap();
|
for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) {
|
String name = (String) iter.next();
|
String[] values = (String[]) requestParams.get(name);
|
String valueStr = "";
|
for (int i = 0; i < values.length; i++) {
|
valueStr = (i == values.length - 1) ? valueStr + values[i]
|
: valueStr + values[i] + ",";
|
}
|
//乱码解决,这段代码在出现乱码时使用。
|
//valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
|
params.put(name, valueStr);
|
}
|
//切记alipaypublickey是支付宝的公钥,请去open.alipay.com对应应用下查看。
|
//boolean AlipaySignature.rsaCheckV1(Map<String, String> params, String publicKey, String charset, String sign_type)
|
/*try {
|
boolean flag = AlipaySignature.rsaCheckV1(params, alipayPublicKey, "UTF-8","RSA2");
|
if(flag){*/
|
Map<String, String> map = new HashMap<>();
|
String out_trade_no = params.get("out_trade_no");
|
String subject = params.get("subject");
|
String total_amount = params.get("total_amount");
|
String trade_no = params.get("trade_no");
|
String passback_params = params.get("passback_params");
|
map.put("out_trade_no", out_trade_no);//商家订单号
|
map.put("subject", subject);
|
map.put("total_amount", total_amount);
|
map.put("trade_no", trade_no);//支付宝交易号
|
return map;
|
/*}
|
|
} catch (AlipayApiException e) {
|
e.printStackTrace();
|
}
|
return null;*/
|
}
|
|
|
/**
|
* 支付宝查询订单支付状态
|
* @param out_trade_no
|
* @return
|
* @throws Exception
|
*/
|
public ResultUtil queryALIOrder(String out_trade_no) throws Exception{
|
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",aliAppid, appPrivateKey,"json","UTF-8", alipay_public_key,"RSA2");
|
AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
|
request.setBizContent("{" +
|
"\"out_trade_no\":" + out_trade_no +
|
" }");
|
AlipayTradeQueryResponse response = alipayClient.execute(request);
|
if(response.isSuccess()){
|
String tradeStatus = response.getTradeStatus();//交易状态:WAIT_BUYER_PAY(交易创建,等待买家付款)、TRADE_CLOSED(未付款交易超时关闭,或支付完成后全额退款)、TRADE_SUCCESS(交易支付成功)、TRADE_FINISHED(交易结束,不可退款)
|
return ResultUtil.success(tradeStatus);
|
} else {
|
return ResultUtil.error(response.getMsg());
|
}
|
}
|
|
|
|
/**
|
* 微信统一下单
|
* @param body 商品描述
|
* @param attach 附加数据
|
* @param out_trade_no 商户订单号
|
* @param total_fee 标价金额
|
* @param notify_url 通知地址
|
* @param tradeType 交易类型
|
* @return
|
*/
|
public ResultUtil weixinpay(String body, String attach, String out_trade_no, String total_fee, String notify_url, String tradeType,String openId) throws Exception{
|
int i = new BigDecimal(total_fee).multiply(new BigDecimal("100")).intValue();
|
String hostAddress = null;
|
try {
|
hostAddress = InetAddress.getLocalHost().getHostAddress();
|
} catch (UnknownHostException e) {
|
e.printStackTrace();
|
}
|
String nonce_str = UUIDUtil.getRandomCode(16);
|
Map<String, Object> map = new HashMap<>();
|
map.put("appid", appid);
|
map.put("mch_id", mchId);
|
map.put("nonce_str", nonce_str);
|
map.put("body", body);
|
map.put("out_trade_no", out_trade_no);//存储的订单code
|
map.put("total_fee", i);
|
map.put("notify_url", callbackPath + notify_url);
|
map.put("trade_type", tradeType);
|
map.put("openid", openId);
|
String s = this.weixinSignature(map);
|
map.put("sign", s);
|
System.out.println(map);
|
String url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
|
//设置请求头
|
HttpHeaders headers = new HttpHeaders();
|
headers.setContentType(MediaType.APPLICATION_XML);
|
StringBuffer xmlString = new StringBuffer();
|
Set<String> strings = map.keySet();
|
String[] keys = {};
|
keys = strings.toArray(keys);
|
Arrays.sort(keys);
|
xmlString.append("<xml>");
|
for(int l = 0; l < keys.length; l++){
|
xmlString.append("<" + keys[l] + ">" + map.get(keys[l]) + "</" + keys[l] + ">");
|
}
|
xmlString.append("</xml>");
|
|
Map<String, String> map1 = null;
|
HttpClientUtil httpClientUtil = new HttpClientUtil();
|
String body1 = httpClientUtil.pushHttpRequsetXml(url, xmlString.toString(), new HashMap<>());
|
//将结果xml解析成map
|
body1 = body1.replaceAll("<!\\[CDATA\\[","");
|
body1 = body1.replaceAll("]]>", "");
|
try {
|
map1 = this.xmlToMap(body1, "UTF-8");
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
} catch (DocumentException e) {
|
e.printStackTrace();
|
}
|
String return_code = map1.get("return_code");
|
if("SUCCESS".equals(return_code)){
|
String result_code = map1.get("result_code");
|
if("SUCCESS".equals(result_code)){
|
String type = map1.get("trade_type");
|
|
String prepay_id = map1.get("prepay_id");
|
//重新进行签名后返回给前端
|
Map<String, Object> map2 = new HashMap<>();
|
switch (type){
|
case "JSAPI":
|
map2.put("appId", appid);
|
map2.put("nonceStr", nonce_str);
|
map2.put("package", "prepay_id="+prepay_id);
|
map2.put("timeStamp", (new Date().getTime()+"").substring(0, 10));
|
map2.put("signType", "MD5");//签名类型
|
String s1 = this.weixinSignature(map2);
|
map2.put("sign", s1);
|
System.err.println(map2);
|
return ResultUtil.success(JSONObject.toJSONString(map2));
|
case "NATIVE":
|
String code_url = map1.get("code_url");
|
return ResultUtil.success(code_url);
|
case "APP":
|
map2.put("appid", appid);
|
map2.put("noncestr", nonce_str);
|
map2.put("package", "Sign=WXPay");
|
map2.put("partnerid", mchId);
|
map2.put("prepayid", prepay_id);
|
map2.put("timestamp", new Date().getTime() / 1000);
|
map2.put("sign", this.weixinSignature(map2));
|
System.err.println(map2);
|
return ResultUtil.success(map2);
|
}
|
return null;
|
}else{
|
System.err.println(map1.get("err_code_des"));
|
return ResultUtil.error(map1.get("err_code_des"));
|
}
|
}else{
|
System.err.println(map1.get("return_msg") + appid + "----" + mchId);
|
return ResultUtil.error(map1.get("return_msg"), new JSONObject());
|
}
|
}
|
|
|
|
|
|
/**
|
* 微信支付成功后的回调处理
|
* @param request
|
*/
|
public Map<String, String> weixinpayCallback(HttpServletRequest request){
|
try {
|
String param = this.getParam(request);
|
param = param.replaceAll("<!\\[CDATA\\[","");
|
param = param.replaceAll("]]>", "");
|
Map<String, String> map = this.xmlToMap(param, "UTF-8");
|
String return_code = map.get("return_code");
|
if("SUCCESS".equals(return_code)){
|
String result_code = map.get("result_code");
|
if("SUCCESS".equals(result_code)){
|
Map<String, String> map1 = new HashedMap();
|
map1.put("nonce_str", map.get("nonce_str"));
|
map1.put("out_trade_no", map.get("out_trade_no"));//存储的订单code
|
map1.put("attach", map.get("attach"));//存储订单id
|
map1.put("total_fee", map.get("total_fee"));
|
map1.put("transaction_id", map.get("transaction_id"));//微信支付订单号
|
String result = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
|
map1.put("result", result);
|
return map1;
|
}else{
|
System.err.println(map.get("err_code_des"));
|
}
|
}else{
|
System.err.println(map.get("return_msg"));
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
} catch (DocumentException e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
|
/**
|
* 微信扫码收款
|
* @param body 商品描述
|
* @param attach 附加数据
|
* @param nonce_str 随机字符串
|
* @param out_trade_no 商户订单号
|
* @param total_fee 订单金额
|
* @param auth_code 授权码 扫码支付授权码,设备读取用户微信中的条码或者二维码信息(注:用户付款码条形码规则:18位纯数字,以10、11、12、13、14、15开头)
|
* @return
|
*/
|
public ResultUtil wxScanQRCodePay(String body, String attach, String nonce_str, String out_trade_no, String total_fee, String auth_code){
|
int i = new BigDecimal(total_fee).multiply(new BigDecimal("100")).intValue();
|
String hostAddress = null;
|
try {
|
InetAddress address = InetAddress.getLocalHost();
|
hostAddress = address.getHostAddress();
|
} catch (UnknownHostException e) {
|
e.printStackTrace();
|
}
|
String randomCode = null;
|
try {
|
randomCode = UUIDUtil.getRandomCode(10);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
Map<String, Object> map = new HashMap<>();
|
map.put("appid", appid);
|
map.put("mch_id", mchId);
|
map.put("nonce_str", nonce_str);//存储的支付人员id,员工扫描二维码支付的时候存储的是收款员工id
|
map.put("body", body);
|
map.put("attach", attach);//存储的费用月份数据,员工扫描二维码支付的时候存储的是收费项id
|
map.put("out_trade_no", randomCode + "_" + out_trade_no);//存储的房间id
|
map.put("total_fee", i);
|
map.put("spbill_create_ip", hostAddress);
|
map.put("auth_code", auth_code);
|
String s = this.weixinSignature(map);
|
map.put("sign", s);
|
|
String url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
|
//设置请求头
|
HttpHeaders headers = new HttpHeaders();
|
headers.setContentType(MediaType.APPLICATION_XML);
|
StringBuffer xmlString = new StringBuffer();
|
Set<String> strings = map.keySet();
|
String[] keys = {};
|
keys = strings.toArray(keys);
|
Arrays.sort(keys);
|
xmlString.append("<xml>");
|
for(int l = 0; l < keys.length; l++){
|
xmlString.append("<" + keys[l] + ">" + map.get(keys[l]) + "</" + keys[l] + ">");
|
}
|
xmlString.append("</xml>");
|
|
Map<String, String> map1 = null;
|
String body1 = httpClientUtil.pushHttpRequsetXml(url, xmlString.toString(), new HashMap<>());
|
//将结果xml解析成map
|
body1 = body1.replaceAll("<!\\[CDATA\\[","");
|
body1 = body1.replaceAll("]]>", "");
|
try {
|
map1 = this.xmlToMap(body1, "UTF-8");
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
} catch (DocumentException e) {
|
e.printStackTrace();
|
}
|
String return_code = map1.get("return_code");
|
if("SUCCESS".equals(return_code)){
|
String result_code = map1.get("result_code");
|
if("SUCCESS".equals(result_code)){
|
String type = map1.get("trade_type");
|
switch (type){
|
case "JSAPI":
|
break;
|
case "NATIVE":
|
String code_url = map1.get("code_url");
|
return ResultUtil.success(code_url);
|
case "APP":
|
String prepay_id = map1.get("prepay_id");
|
//重新进行签名后返回给前端
|
Map<String, Object> map2 = new HashMap<>();
|
map2.put("appid", appid);
|
map2.put("noncestr", nonce_str);
|
map2.put("package", "Sign=WXPay");
|
map2.put("partnerid", mchId);
|
map2.put("prepayid", prepay_id);
|
map2.put("timestamp", new Date().getTime() + "");
|
String s1 = this.weixinSignature(map2);
|
|
map2.put("pac", "Sign=WXPay");
|
map2.put("sign", s1);
|
// System.err.println(map2);
|
return ResultUtil.success(map2);
|
}
|
return null;
|
}else{
|
// System.err.println(map1.get("err_code_des"));
|
return ResultUtil.error(map1.get("err_code_des"));
|
}
|
}else{
|
// System.err.println(map1.get("return_msg") + appid + "----" + mchId);
|
return ResultUtil.error(map1.get("return_msg"), new JSONObject());
|
}
|
}
|
|
|
/**
|
* 支付宝扫码收款
|
* @param data
|
* @return
|
*/
|
public Object aliScanQRCodePay(String data){
|
return null;
|
}
|
|
|
/**
|
* 微信退款申请
|
* @param transaction_id 微信订单号。微信生成的订单号,在支付通知中有返回
|
* @param out_refund_no 商户退款单号。商户系统内部的退款单号,商户系统内部唯一,只能是数字、大小写字母_-|*@ ,同一退款单号多次请求只退一笔。
|
* @param total_fee 订单金额。订单总金额,单位为分,只能为整数
|
* @param refund_fee 退款金额。退款总金额,订单总金额,单位为分,只能为整数
|
* @param notify_url 退款结果通知url。异步接收微信支付退款结果通知的回调地址,通知URL必须为外网可访问的url,不允许带参数 如果参数中传了notify_url,则商户平台上配置的回调地址将不会生效。
|
* @return
|
*/
|
public Map<String, String> wxRefund(String transaction_id, String out_refund_no, String total_fee, String refund_fee, String notify_url){
|
int tf = new BigDecimal(total_fee).multiply(new BigDecimal("100")).intValue();
|
int rf = new BigDecimal(refund_fee).multiply(new BigDecimal("100")).intValue();
|
String nonce_str = UUIDUtil.getRandomCode();
|
Map<String, Object> map = new HashMap<>();
|
map.put("appid", appid);
|
map.put("mch_id", mchId);
|
map.put("nonce_str", nonce_str);
|
map.put("transaction_id", transaction_id);
|
map.put("out_refund_no", out_refund_no);
|
map.put("total_fee", tf);
|
map.put("refund_fee", rf);
|
map.put("notify_url", callbackPath + notify_url);
|
String s = this.weixinSignature(map);
|
map.put("sign", s);
|
|
String url = "https://api.mch.weixin.qq.com/secapi/pay/refund";
|
//设置请求头
|
HttpHeaders headers = new HttpHeaders();
|
headers.setContentType(MediaType.APPLICATION_XML);
|
StringBuffer xmlString = new StringBuffer();
|
Set<String> strings = map.keySet();
|
String[] keys = {};
|
keys = strings.toArray(keys);
|
Arrays.sort(keys);
|
xmlString.append("<xml>");
|
for(int l = 0; l < keys.length; l++){
|
xmlString.append("<" + keys[l] + ">" + map.get(keys[l]) + "</" + keys[l] + ">");
|
}
|
xmlString.append("</xml>");
|
|
Map<String, String> map1 = null;
|
String body1 = httpClientUtil.pushHttpRequsetXml(url, xmlString.toString(), new HashMap<>());
|
//将结果xml解析成map
|
body1 = body1.replaceAll("<!\\[CDATA\\[","");
|
body1 = body1.replaceAll("]]>", "");
|
try {
|
map1 = this.xmlToMap(body1, "UTF-8");
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
} catch (DocumentException e) {
|
e.printStackTrace();
|
}
|
String return_code = map1.get("return_code");
|
Map<String, String> map2 = new HashMap<>();
|
if("SUCCESS".equals(return_code)){
|
String result_code = map1.get("result_code");
|
if("SUCCESS".equals(result_code)){
|
map2.put("return_code", result_code);
|
map2.put("refund_id", String.valueOf(map1.get("refund_id")));//微信退款订单号
|
map2.put("refund_fee", String.valueOf(map1.get("refund_fee")));//退款金额
|
return map2;
|
}else{
|
map2.put("return_code", result_code);
|
map2.put("return_msg", map1.get("err_code_des"));
|
return map2;
|
}
|
}else{
|
map2.put("return_code", return_code);
|
map2.put("return_msg", map1.get("return_msg"));
|
return map2;
|
}
|
}
|
|
|
/**
|
* 微信退款成功后的回调处理
|
* @param request
|
* @return
|
*/
|
public Map<String, String> wxRefundCallback(HttpServletRequest request){
|
try {
|
String param = this.getParam(request);
|
param = param.replaceAll("<!\\[CDATA\\[","");
|
param = param.replaceAll("]]>", "");
|
Map<String, String> map = this.xmlToMap(param, "UTF-8");
|
String return_code = map.get("return_code");
|
if("SUCCESS".equals(return_code)){
|
String req_info = map.get("req_info");//加密信息请用商户秘钥进行解密
|
String s = this.wxDecrypt(req_info);
|
s = s.replaceAll("<!\\[CDATA\\[","");
|
s = s.replaceAll("]]>", "");
|
map = this.xmlToMap(s, "UTF-8");
|
Map<String, String> map1 = new HashMap<>();
|
map1.put("refund_id", map.get("refund_id"));
|
map1.put("out_refund_no", map.get("out_refund_no"));
|
return map1;
|
}else{
|
// System.err.println(map.get("return_msg"));
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
} catch (DocumentException e) {
|
e.printStackTrace();
|
} catch (NoSuchAlgorithmException e) {
|
e.printStackTrace();
|
} catch (InvalidKeyException e) {
|
e.printStackTrace();
|
} catch (NoSuchPaddingException e) {
|
e.printStackTrace();
|
} catch (BadPaddingException e) {
|
e.printStackTrace();
|
} catch (NoSuchProviderException e) {
|
e.printStackTrace();
|
} catch (IllegalBlockSizeException e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
|
/**
|
* 支付宝退款
|
* @param trade_no 支付宝交易号
|
* @param refund_amount 退款金额
|
* @return
|
* @throws AlipayApiException
|
*/
|
public Map<String, String> aliRefund(String trade_no, String refund_amount) throws AlipayApiException {
|
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", aliAppid, appPrivateKey,"json","UTF-8", alipay_public_key,"RSA2");
|
AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
|
JSONObject jsonObject = new JSONObject();
|
jsonObject.put("trade_no", trade_no);
|
jsonObject.put("refund_amount", refund_amount);
|
request.setBizContent(jsonObject.toJSONString());
|
AlipayTradeRefundResponse response = alipayClient.execute(request);
|
Map<String, String> map = new HashMap<>();
|
if(response.isSuccess()){
|
System.out.println("调用成功");
|
String outTradeNo = response.getOutTradeNo();
|
map.put("code", response.getCode());//10000
|
map.put("trade_no", response.getTradeNo());//支付宝交易号
|
map.put("out_trade_no", outTradeNo);//商户订单号
|
} else {
|
System.out.println("调用失败");
|
map.put("code", response.getCode());
|
map.put("msg", response.getSubMsg());
|
}
|
return map;
|
}
|
|
|
/**
|
* 查询微信支付订单
|
* @return
|
* @throws Exception
|
*/
|
public ResultUtil queryWXOrder() throws Exception{
|
String url = "https://api.mch.weixin.qq.com/pay/orderquery";
|
String nonce_str = UUIDUtil.getRandomCode(16);
|
Map<String, Object> map = new HashMap<>();
|
map.put("appid", appid);
|
map.put("mch_id", mchId);
|
map.put("transaction_id", nonce_str);//微信订单号
|
map.put("nonce_str", nonce_str);//随机字符串
|
String s = this.weixinSignature(map);
|
map.put("sign", s);
|
|
//设置请求头
|
HttpHeaders headers = new HttpHeaders();
|
headers.setContentType(MediaType.APPLICATION_XML);
|
StringBuffer xmlString = new StringBuffer();
|
Set<String> strings = map.keySet();
|
String[] keys = {};
|
keys = strings.toArray(keys);
|
Arrays.sort(keys);
|
xmlString.append("<xml>");
|
for(int l = 0; l < keys.length; l++){
|
xmlString.append("<" + keys[l] + ">" + map.get(keys[l]) + "</" + keys[l] + ">");
|
}
|
xmlString.append("</xml>");
|
|
Map<String, String> map1 = null;
|
String body1 = httpClientUtil.pushHttpRequsetXml(url, xmlString.toString(), new HashMap<>());
|
//将结果xml解析成map
|
body1 = body1.replaceAll("<!\\[CDATA\\[","");
|
body1 = body1.replaceAll("]]>", "");
|
try {
|
map1 = this.xmlToMap(body1, "UTF-8");
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
} catch (DocumentException e) {
|
e.printStackTrace();
|
}
|
String return_code = map1.get("return_code");
|
if("SUCCESS".equals(return_code)){
|
String result_code = map1.get("result_code");
|
if("SUCCESS".equals(result_code)){
|
String type = map1.get("trade_type");
|
switch (type){
|
case "JSAPI":
|
break;
|
case "NATIVE":
|
String code_url = map1.get("code_url");
|
return ResultUtil.success(code_url);
|
case "APP":
|
String trade_state = map1.get("trade_state");
|
String time_end = map1.get("time_end");
|
Map<String, Object> map2 = new HashMap<>();
|
map2.put("trade_state", trade_state);//订单状态SUCCESS—支付成功,REFUND—转入退款,NOTPAY—未支付,CLOSED—已关闭,REVOKED—已撤销(刷卡支付),USERPAYING--用户支付中,PAYERROR--支付失败(其他原因,如银行返回失败)
|
map2.put("time_end", time_end);//订单支付时间,格式为yyyyMMddHHmmss,如2009年12月25日9点10分10秒表示为20091225091010。
|
return ResultUtil.success(map2);
|
}
|
return null;
|
}else{
|
System.err.println(map1.get("err_code_des"));
|
return ResultUtil.error(map1.get("err_code_des"));
|
}
|
}else{
|
System.err.println(map1.get("return_msg") + appid + "----" + mchId);
|
return ResultUtil.error(map1.get("return_msg"), new JSONObject());
|
}
|
}
|
|
|
|
/**
|
* 获取请求内容
|
* @param request
|
* @return
|
* @throws IOException
|
*/
|
private String getParam(HttpServletRequest request) throws IOException {
|
// 读取参数
|
InputStream inputStream;
|
StringBuilder sb = new StringBuilder();
|
inputStream = request.getInputStream();
|
String s;
|
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
|
while ((s = in.readLine()) != null) {
|
sb.append(s);
|
}
|
in.close();
|
inputStream.close();
|
return sb.toString();
|
}
|
|
|
/**
|
* 微信下单的签名算法
|
* @param map
|
* @return
|
*/
|
private String weixinSignature(Map<String, Object> map){
|
try {
|
Set<Map.Entry<String, Object>> entries = map.entrySet();
|
List<Map.Entry<String, Object>> infoIds = new ArrayList<Map.Entry<String, Object>>(entries);
|
// 对所有传入参数按照字段名的 ASCII 码从小到大排序(字典序)
|
Collections.sort(infoIds, new Comparator<Map.Entry<String, Object>>() {
|
public int compare(Map.Entry<String, Object> o1, Map.Entry<String, Object> o2) {
|
return (o1.getKey()).toString().compareTo(o2.getKey());
|
}
|
});
|
// 构造签名键值对的格式
|
StringBuilder sb = new StringBuilder();
|
for (Map.Entry<String, Object> item : infoIds) {
|
if (item.getKey() != null || item.getKey() != "") {
|
String key = item.getKey();
|
Object val = item.getValue();
|
if (!(val == "" || val == null)) {
|
sb.append(key + "=" + val + "&");
|
}
|
}
|
}
|
sb.append("key=" + key);
|
String sign = MD5AndKL.MD5Encode(sb.toString(), "UTF-8").toUpperCase(); //注:MD5签名方式
|
return sign;
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
|
/**
|
* 微信退款成功后的解密
|
* @param req_info
|
* @return
|
*/
|
private String wxDecrypt(String req_info) throws NoSuchPaddingException, NoSuchAlgorithmException, NoSuchProviderException,
|
InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
|
byte[] decode = Base64.getDecoder().decode(req_info);
|
String sign = MD5AndKL.MD5Encode(key, "UTF-8").toLowerCase();
|
if (Security.getProvider("BC") == null){
|
Security.addProvider(new BouncyCastleProvider());
|
}
|
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
|
SecretKeySpec secretKeySpec = new SecretKeySpec(sign.getBytes(), "AES");
|
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
|
return new String(cipher.doFinal(decode));
|
}
|
|
|
public static void main(String[] ages)throws Exception{
|
// PayMoneyUtil payMoneyUtil = new PayMoneyUtil();
|
// payMoneyUtil.weixinpay("测试", "123", "12.5", "");
|
PayMoneyUtil payMoneyUtil = new PayMoneyUtil();
|
payMoneyUtil.weixinpay("完成订单", "4861","4861_1_fei","172","/base/wxPayOrderTaxi","JSAPI","o5o4Q5Cwvoxa7m7DciaNZmZauFuM");
|
}
|
|
|
/**
|
* xml转map
|
* @param xml
|
* @param charset
|
* @return
|
* @throws UnsupportedEncodingException
|
* @throws DocumentException
|
*/
|
public static Map<String, String> xmlToMap(String xml, String charset) throws UnsupportedEncodingException, DocumentException {
|
|
Map<String, String> respMap = new HashMap<String, String>();
|
|
SAXReader reader = new SAXReader();
|
Document doc = reader.read(new ByteArrayInputStream(xml.getBytes(charset)));
|
Element root = doc.getRootElement();
|
xmlToMap(root, respMap);
|
return respMap;
|
}
|
|
public static Map<String, String> xmlToMap(Element tmpElement, Map<String, String> respMap){
|
if (tmpElement.isTextOnly()) {
|
respMap.put(tmpElement.getName(), tmpElement.getText());
|
return respMap;
|
}
|
|
@SuppressWarnings("unchecked")
|
Iterator<Element> eItor = tmpElement.elementIterator();
|
while (eItor.hasNext()) {
|
Element element = eItor.next();
|
xmlToMap(element, respMap);
|
}
|
return respMap;
|
}
|
}
|