package com.dsh.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.CertAlipayRequest;
|
import com.alipay.api.DefaultAlipayClient;
|
import com.alipay.api.domain.AlipayTradeAppPayModel;
|
import com.alipay.api.msg.AlipayMsgClient;
|
import com.alipay.api.msg.MsgHandler;
|
import com.alipay.api.request.*;
|
import com.alipay.api.response.*;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.dsh.course.util.UUIDUtil;
|
import com.dsh.guns.modular.system.controller.util.HttpClientUtil;
|
import com.dsh.guns.modular.system.controller.util.MD5AndKL;
|
import com.dsh.guns.modular.system.model.OperatorAuthAlipay;
|
import com.dsh.guns.modular.system.model.OperatorUser;
|
import com.dsh.guns.modular.system.service.IOperatorAuthService;
|
import com.dsh.guns.modular.system.service.IOperatorUserService;
|
import org.apache.commons.lang3.StringUtils;
|
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.http.HttpHeaders;
|
import org.springframework.http.MediaType;
|
import org.springframework.stereotype.Component;
|
|
import javax.crypto.Mac;
|
import javax.crypto.spec.SecretKeySpec;
|
import javax.servlet.http.HttpServletRequest;
|
import java.io.*;
|
import java.util.*;
|
|
import static com.dsh.guns.modular.system.controller.util.MD5AndKL.byteArrayToHexString;
|
|
/**
|
* 第三方支付工具类
|
*/
|
@Component
|
public class PayMoneyUtil {
|
private String aliAppid = "2021004105665036";//支付宝appid
|
|
private String appPrivateKey = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCi5i9nW/hGLJ3A06cZxTQdviFC7THpdSihoTYGLr9q006hu0V26ecBMY/o4w5bvIX0Ok/yofmZsVcCJpAPvbXL/uqVrIjnRRxXiaeBFThlxoBUTdunvbUSDYfzlEhJr5NvUKI6H6lz2niXlQGx4qy8Hau4ccWit9kM8jwUvsBVQoFgJA+xrjMvooA7YLopQtpOD+UJr5thApTSf1xrnr1W12yolTLEH15JmNV372cqXrYUuqnY0QsaPtxeqJUGAOcGdVLllQ7easEznP8DFBvDdHATcmp2SHNQDUEWN6MCVPbMgY06NQVqAXxqjTAYSVh+6TRu6bofPmpYC3TZB003AgMBAAECggEBAJAcR2+PA3NBYUYHeFrqBRMS8uX8ZR19kjZ7IgoSLTFaQsP9opRylPSPXhrPVBKAE5leRQAHn4MCSlESwHvMfxo7KFjFTFAc6dffZZpipYQUOc9bGampwJh58/3e/pyBgVMG6J23CPf/HJQtNFSkjd/V9+ayb/9l2dUEL3bC0fAZ/dbx8HsxdLw8wn3fLlWLj68hOMqa2deCZe3JdSVsPbeWqkh56FFsMLug0Nd+Ar4TgRl9/jnhXF0JWiD0LmPUYLhboY7EfUBzN4w1iYbDi1P+3zvoOYsiVKAXox9GMhQ2VzOO2UcSTuizSza2e98mGpabl/GpKmCz+RDFjtkX6eECgYEA2MyCij65eO3aGIm3FUe93DULRBYTfX8qJQSJq2WOWA3mmQlEW6L3O2B5/lG2h+8WmN6iLEs9eHpgycGYp7vAqgrANEn16ACVcuyx0scFtrZfZ+kmHMzFfiUWxJjVYk/6YngsGVBLdw6ueM42C8TTP67X9tU5TdVGoGWuqEj4W98CgYEAwFqwprXOch5Pqk/RPbb49r0Ou03K/UbciWnWWKzUhFFNS8MdlQPoDvQZbMwHLeWsa2VhaKITK3x5biLQb3U+0GLOn6lTvEyrEUH+ucREyLgVYTRAvwBPtnvlrzpyxPk2HnslQjju8WrvvLLBMKWUjlTrTOzhaHT21gz3pHMiOakCgYEAhLmfaXdBITGshb054sNLDtdCkGpbgEcrzAHdLps769iGxkYQHXHFngpQZUwtTUcoNGqIKknd1jZFrv7gsD+XkgKG7PwimehRlkwmCX5ilxtLiVgJRzRt6+5U5AMVD90a0tHzXYP0z2yjj73fBJF5KtGl0a10KZxaYrQdm1UhB00CgYBZZgzx/k9rtHC8LAqIj1CYhHejT92G53c6Gkl3vyOqN4sgKhfGmSEySfrDGPRBPZxr8ZtbIPCd5mUdberH0osWGMYFaJI1UsCy7aQwvGpniz7MhZeN7dweaOjwDs8mgtjHQ96mL4XGCDhR0BZ/wIURvZ/6iaGdhbbu9unlsWj3uQKBgQCmZYdsbbZkd3ev6f8rwyvMz+DrCQyYpY44cegBYuJgrZiQnL2fJioeN7ixX0UM48SfwsZEIrzshP/LGAwnc2MdjxKUl4jLN8SEe0NAjXOnz9Zaw740+aOmLpXcLWdP4uM2gIhWsvW1tEkQZCXmm7c9s/RsU8Pmzv+YL3+fSijOzA==";//支付宝开发者应用私钥
|
|
private String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAouYvZ1v4RiydwNOnGcU0Hb4hQu0x6XUooaE2Bi6/atNOobtFdunnATGP6OMOW7yF9DpP8qH5mbFXAiaQD721y/7qlayI50UcV4mngRU4ZcaAVE3bp721Eg2H85RISa+Tb1CiOh+pc9p4l5UBseKsvB2ruHHForfZDPI8FL7AVUKBYCQPsa4zL6KAO2C6KULaTg/lCa+bYQKU0n9ca569VtdsqJUyxB9eSZjVd+9nKl62FLqp2NELGj7cXqiVBgDnBnVS5ZUO3mrBM5z/AxQbw3RwE3JqdkhzUA1BFjejAlT2zIGNOjUFagF8ao0wGElYfuk0bum6Hz5qWAt02QdNNwIDAQAB";//支付宝应用公钥
|
|
private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmu8n/4yTHWbn7VOrNc9OsLtDL1bEQ8gC1dHkj8Wy5z0mkaOsjJRIG/28ze12M0V8jdCKuuDr5Z1OPKiqf+XO3ypguEh+mYUVMBM/cZodDFQfTY1TKLWjvQCuaqlA+QUTCK6f7T7stsgyQ1o9Jj0rXZDz6PM4QHSTzjrLIBaeqM5WIBvH+fy/X+QG5Utd+/UT0kc0JyvuKhZ65yVUd/C9VcwJJAPliRsAQNrqYterwAJ9zvw9tF11wj9W0XgJ8Ccu4x3gR1vrlLRJJo/OA97RmxPQ+5hSacWQZCUd1dwiBq+YCrKVHGTj14izRHXrLc0yBlRXo7tBOIqcy3IsvKVthQIDAQAB";//支付宝支付公钥
|
|
private String smid = "2088330203191220";//平台支付宝商户号...
|
|
private String appid = "wx41d32f362ba0f911";//微信appid
|
|
private String appSecret = "cf0ebf950f5926a69041a0e2bbe20f3e";
|
|
private String mchId = "1501481761";//微信商户号
|
|
private String key = "6f5e0c2dcabfa9c27b5da5836a362fef";//微信商户号
|
|
private String callbackPath = "https://online.daowepark.com:443/account";//支付回调网关地址
|
|
private String app_cert_path = "C:/cert/alipay/user/app_cert_path.crt";//应用公钥证书路径
|
|
private String alipay_cert_path = "C:/cert/alipay/user/alipay_cert_path.crt";//支付宝公钥证书文件路径
|
|
private String alipay_root_cert_path = "C:/cert/alipay/user/alipay_root_cert_path.crt";//支付宝CA根证书文件路径
|
|
private String certPath = "/usr/playpai/cert/weixin/apiclient_cert.p12";//微信证书
|
String signType = "RSA2" ;
|
String serverHost = "openchannel.alipay.com" ;
|
|
@Autowired
|
private IOperatorAuthService operatorAuthService;
|
@Autowired
|
private IOperatorUserService operatorUserService;
|
|
/**
|
* 添加分账接收方
|
* @param number 商户号或者openId
|
* @param name 商户全称或者个人姓名
|
* @return
|
*/
|
public ResultUtil addReceiver(String number,String name) throws Exception {
|
Map<String, Object> map = new HashMap<>();
|
map.put("mch_id", mchId);
|
map.put("appid", appid);
|
String nonce_str = UUIDUtil.getRandomCode(16);
|
map.put("nonce_str", nonce_str);
|
// 将这个字符串使用json格式拼接起来
|
Map<String, Object> body = new HashMap<>();
|
body.put("type", "MERCHANT_ID");
|
body.put("account", number);
|
body.put("name", name);
|
body.put("relation_type", "PARTNER");
|
JSONObject jsonObject = new JSONObject(body);
|
String jsonString = jsonObject.toString();
|
map.put("receiver",jsonString );
|
String s = this.weixinSignature(map);
|
map.put("sign", s);
|
String url = "https://api.mch.weixin.qq.com/pay/profitsharingaddreceiver";
|
//设置请求头
|
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 = null;
|
try {
|
body1 = HttpClientUtil.pushHttpRequsetXml(url, xmlString.toString(), new HashMap<>()).getData();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
//将结果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)) {
|
return ResultUtil.success();
|
} 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 order 微信订单号
|
* @return
|
*/
|
public ResultUtil fenzhang(String order) throws Exception {
|
Map<String, Object> map = new HashMap<>();
|
map.put("mch_id", mchId);
|
map.put("appid", appid);
|
String nonce_str = UUIDUtil.getRandomCode(16);
|
map.put("nonce_str", nonce_str);
|
map.put("transaction_id", order);
|
// 将这个字符串使用json格式拼接起来
|
Map<String, Object> body = new HashMap<>();
|
body.put("type", "MERCHANT_ID");
|
// todo 分账接收方 计算分账金额
|
body.put("account", "1233");
|
body.put("amount", "0");
|
body.put("description", "订单分账");
|
JSONObject jsonObject = new JSONObject(body);
|
String jsonString = jsonObject.toString();
|
map.put("receiver",jsonString);
|
String s = this.weixinSignature(map);
|
map.put("sign", s);
|
String url = "https://api.mch.weixin.qq.com/secapi/pay/profitsharing";
|
//设置请求头
|
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 = null;
|
try {
|
body1 = HttpClientUtil.pushHttpRequsetXml(url, xmlString.toString(), new HashMap<>()).getData();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
//将结果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)) {
|
return ResultUtil.success();
|
} 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 number 商户号或者openId
|
* @param name 商户全称或者个人姓名
|
* @return
|
*/
|
public ResultUtil deleteReceiver(String number,String name) throws Exception {
|
Map<String, Object> map = new HashMap<>();
|
map.put("mch_id", mchId);
|
map.put("appid", appid);
|
String nonce_str = UUIDUtil.getRandomCode(16);
|
map.put("nonce_str", nonce_str);
|
// 将这个字符串使用json格式拼接起来
|
Map<String, Object> body = new HashMap<>();
|
body.put("type", "MERCHANT_ID");
|
body.put("account", number);
|
body.put("name", name);
|
body.put("relation_type", "PARTNER");
|
JSONObject jsonObject = new JSONObject(body);
|
String jsonString = jsonObject.toString();
|
map.put("receiver",jsonString );
|
String s = this.weixinSignature(map);
|
map.put("sign", s);
|
String url = "https://api.mch.weixin.qq.com/pay/profitsharingaddreceiver";
|
//设置请求头
|
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 = null;
|
try {
|
body1 = HttpClientUtil.pushHttpRequsetXml(url, xmlString.toString(), new HashMap<>()).getData();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
//将结果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)) {
|
return ResultUtil.success();
|
} 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 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 = sha256_HMAC(sb.toString(), key).toUpperCase();
|
return sign;
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
/**
|
* sha256_HMAC加密
|
* @param message 消息
|
* @param secret 秘钥
|
* @return 加密后字符串
|
*/
|
public String sha256_HMAC(String message, String secret) {
|
String hash = "";
|
try {
|
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
|
SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
|
sha256_HMAC.init(secret_key);
|
byte[] bytes = sha256_HMAC.doFinal(message.getBytes());
|
hash = byteArrayToHexString(bytes);
|
} catch (Exception e) {
|
System.out.println("Error HmacSHA256 ===========" + e.getMessage());
|
}
|
return hash;
|
}
|
|
/**
|
* 处理直付通审核通过和拒绝消息
|
*/
|
public void getMessage() {
|
System.err.println("进入");
|
final AlipayMsgClient alipayMsgClient = AlipayMsgClient.getInstance(aliAppid);
|
try {
|
alipayMsgClient.setConnector(serverHost);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
alipayMsgClient.setSecurityConfig(signType, appPrivateKey, alipay_public_key);
|
try {
|
alipayMsgClient.connect();
|
} catch (InterruptedException e) {
|
e.printStackTrace();
|
}
|
alipayMsgClient.setMessageHandler( new MsgHandler() {
|
/**
|
* 客户端接收到消息后回调此方法
|
* @param msgApi 接收到的消息的消息api名
|
* @param msgId 接收到的消息的消息id
|
* @param bizContent 接收到的消息的内容,json格式
|
*/
|
public void onMessage (String msgApi, String msgId, String bizContent) {
|
// 直付通进件审核通过
|
if (StringUtils.equals(msgApi,"ant.merchant.expand.indirect.zft.passed")){
|
// 修改运营商状态 将返回的商户号填入运营商
|
System.out.println( "receive message. msgApi:" + msgApi + " msgId:" + msgId + " bizContent:" + bizContent);
|
JSONObject json = JSONObject.parseObject(bizContent);
|
// 商户号
|
String smid = json.getString("smid");
|
// 审核备注信息
|
String memo = json.getString("memo");
|
// 订单id
|
String orderId = json.getString("order_id");
|
// 通过订单id查询
|
OperatorAuthAlipay auth = operatorAuthService.getOne(new QueryWrapper<OperatorAuthAlipay>()
|
.eq("orderNo",orderId));
|
auth.setSmid(smid);
|
auth.setAuditState(2);
|
auth.setRefuseReason(memo);
|
operatorAuthService.updateById(auth);
|
Integer operatorId = auth.getOperatorId();
|
OperatorUser operatorId1 = operatorUserService.getOne(new QueryWrapper<OperatorUser>()
|
.eq("operatorId", operatorId));
|
operatorId1.setAlipayNum(smid);
|
operatorId1.setAlipayAudit(2);
|
operatorUserService.updateById(operatorId1);
|
}
|
// 直付通进件审核失败
|
if (StringUtils.equals(msgApi,"ant.merchant.expand.indirect.zft.rejected")){
|
JSONObject json = JSONObject.parseObject(bizContent);
|
// 商户号
|
String orderId = json.getString("order_id");
|
// 官方拒绝理由
|
String reason = json.getString("reason");
|
// 通过订单id查询
|
OperatorAuthAlipay auth = operatorAuthService.getOne(new QueryWrapper<OperatorAuthAlipay>()
|
.eq("orderNo",orderId));
|
auth.setAuditState(3);
|
auth.setRefuseReason(reason);
|
operatorAuthService.updateById(auth);
|
Integer operatorId = auth.getOperatorId();
|
OperatorUser operatorId1 = operatorUserService.getOne(new QueryWrapper<OperatorUser>()
|
.eq("operatorId", operatorId));
|
operatorId1.setAlipayAudit(3);
|
operatorUserService.updateById(operatorId1);
|
}
|
}
|
});
|
}
|
/**
|
* 支付宝支付
|
*/
|
public ResultUtil alipay(String body, String subject, String passbackParams, String outTradeNo, String amount, String notifyUrl) {
|
// //构造client
|
// CertAlipayRequest certAlipayRequest = new CertAlipayRequest ();
|
// //设置网关地址
|
// certAlipayRequest.setServerUrl("https://openapi.alipay.com/gateway.do");
|
// //设置应用Id
|
// certAlipayRequest.setAppId(aliAppid);
|
// //设置应用私钥
|
// certAlipayRequest.setPrivateKey(appPrivateKey);
|
// //设置请求格式,固定值json
|
// certAlipayRequest.setFormat("json");
|
// //设置字符集
|
// certAlipayRequest.setCharset("UTF-8");
|
// //设置签名类型
|
// certAlipayRequest.setSignType("RSA2");
|
// //设置应用公钥证书路径
|
// certAlipayRequest.setCertPath(app_cert_path);
|
// //设置支付宝公钥证书路径
|
// certAlipayRequest.setAlipayPublicCertPath(alipay_cert_path);
|
// //设置支付宝根证书路径
|
// certAlipayRequest.setRootCertPath(alipay_root_cert_path);
|
// //构造client
|
// AlipayClient alipayClient = null;
|
// try {
|
// alipayClient = new DefaultAlipayClient(certAlipayRequest);
|
// } catch (AlipayApiException e) {
|
// e.printStackTrace();
|
// }
|
// //实例化具体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);
|
// model.setSubject (subject);
|
// model.setOutTradeNo (outTradeNo);
|
// model.setTimeoutExpress ("30m" );
|
// model.setTotalAmount (amount);
|
// model.setProductCode ( "QUICK_MSECURITY_PAY" );
|
// model.setPassbackParams(passbackParams);//自定义参数
|
// 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();
|
// }
|
|
|
//实例化客户端
|
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", aliAppid, appPrivateKey, "json", "UTF-8", alipay_public_key, "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");
|
model.setPassbackParams(passbackParams);//自定义参数
|
|
|
//分账
|
// Integer coursePackagePayments = coursePackageClient.queryByCode(outTradeNo);
|
// Integer paymentCompetitions = competitionsClient.queryByCode(outTradeNo);
|
// Integer siteBookings = siteClient.queryByCode(outTradeNo);
|
// List<Integer> stores = new ArrayList<>();
|
// stores.add(coursePackagePayments);
|
// stores.add(paymentCompetitions);
|
// stores.add(siteBookings);
|
//
|
// OperatorUser operatorUser = siteClient.queryOperator(stores);
|
//
|
// String alipayProportion = operatorUser.getAlipayProportion();
|
// String alipayNum = operatorUser.getAlipayNum();
|
//
|
// ExtendParams extendParams = new ExtendParams();
|
//// extendParams.setSysServiceProviderId("YOUR_SERVICE_PROVIDER_ID");
|
// model.setExtendParams(extendParams);
|
//
|
// RoyaltyInfo royaltyInfo = new RoyaltyInfo();
|
//// royaltyInfo.setRoyaltyType("transfer");
|
//
|
//
|
// RoyaltyDetailInfos royaltyDetailInfo1 = new RoyaltyDetailInfos();
|
// royaltyDetailInfo1.setTransOutType("userId");
|
// royaltyDetailInfo1.setTransOut(aliAppid);
|
// royaltyDetailInfo1.setTransInType("loginName");
|
// royaltyDetailInfo1.setTransIn("18398968484");
|
//
|
// royaltyDetailInfo1.setDesc("分账描述1");
|
// royaltyDetailInfo1.setAmountPercentage(alipayProportion);
|
// List<RoyaltyDetailInfos> royaltyDetailInfos = new ArrayList<>();
|
//
|
//
|
//
|
// royaltyInfo.setRoyaltyDetailInfos(royaltyDetailInfos);
|
// model.setRoyaltyInfo(royaltyInfo);
|
// System.err.println("=================="+royaltyInfo);
|
//
|
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());
|
map.put("returnId", passbackParams);
|
System.out.println(map);//就是orderString 可以直接给客户端请求,无需再做处理。
|
return ResultUtil.success(map);
|
} catch (AlipayApiException e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
|
|
|
/**
|
* 支付宝查询订单支付状态
|
*
|
* @param out_trade_no
|
* @return
|
* @throws Exception
|
*/
|
public AlipayTradeQueryResponse 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);
|
return response;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
* 获取请求内容
|
*
|
* @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();
|
}
|
|
|
|
|
|
|
|
|
|
|
/**
|
* 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;
|
}
|
}
|