mirror of
https://gitee.com/egzosn/pay-java-parent.git
synced 2026-06-19 22:17:38 +08:00
1. 撤销功能实现
2.代码相关优化,整理
This commit is contained in:
@@ -167,6 +167,12 @@
|
||||
Map result = service.close("支付宝单号", "我方系统单号");
|
||||
|
||||
```
|
||||
#### 交易撤销接口
|
||||
```java
|
||||
|
||||
Map result = service.cancel("支付宝单号", "我方系统单号");
|
||||
|
||||
```
|
||||
|
||||
|
||||
#### 申请退款接口
|
||||
|
||||
@@ -4,7 +4,6 @@ import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.egzosn.pay.ali.bean.AliTransactionType;
|
||||
import com.egzosn.pay.common.api.BasePayService;
|
||||
import com.egzosn.pay.common.api.Callback;
|
||||
import com.egzosn.pay.common.bean.*;
|
||||
import com.egzosn.pay.common.bean.result.PayException;
|
||||
import com.egzosn.pay.common.exception.PayErrorException;
|
||||
@@ -20,30 +19,37 @@ import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 支付宝支付服务
|
||||
* @author egan
|
||||
* 支付宝支付服务
|
||||
*
|
||||
* email egzosn@gmail.com
|
||||
* date 2017-2-22 20:09
|
||||
* @author egan
|
||||
* <p>
|
||||
* email egzosn@gmail.com
|
||||
* date 2017-2-22 20:09
|
||||
*/
|
||||
public class AliPayService extends BasePayService<AliPayConfigStorage> {
|
||||
|
||||
|
||||
/**
|
||||
* 正式测试环境
|
||||
*/
|
||||
private final static String httpsReqUrl = "https://openapi.alipay.com/gateway.do";
|
||||
private static final String HTTPS_REQ_URL = "https://openapi.alipay.com/gateway.do";
|
||||
/**
|
||||
* 沙箱测试环境账号
|
||||
*/
|
||||
private final static String devReqUrl = "https://openapi.alipaydev.com/gateway.do";
|
||||
|
||||
private static final String DEV_REQ_URL = "https://openapi.alipaydev.com/gateway.do";
|
||||
|
||||
public static final String SIGN = "sign";
|
||||
|
||||
public static final String SUCCESS_CODE = "10000";
|
||||
|
||||
public static final String CODE = "code";
|
||||
|
||||
/**
|
||||
* 获取对应的请求地址
|
||||
*
|
||||
* @return 请求地址
|
||||
*/
|
||||
public String getReqUrl(){
|
||||
return payConfigStorage.isTest() ? devReqUrl : httpsReqUrl;
|
||||
public String getReqUrl() {
|
||||
return payConfigStorage.isTest() ? DEV_REQ_URL : HTTPS_REQ_URL;
|
||||
}
|
||||
|
||||
|
||||
@@ -69,47 +75,43 @@ public class AliPayService extends BasePayService<AliPayConfigStorage> {
|
||||
@Override
|
||||
public boolean verify(Map<String, Object> params) {
|
||||
|
||||
|
||||
if (params.get("sign") == null) {
|
||||
if (params.get(SIGN) == null) {
|
||||
LOG.debug("支付宝支付异常:params:" + params);
|
||||
return false;
|
||||
}
|
||||
|
||||
return signVerify(params, (String) params.get("sign")) && verifySource( (String)params.get("notify_id"));
|
||||
return signVerify(params, (String) params.get(SIGN)) && verifySource((String) params.get("notify_id"));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据反馈回来的信息,生成签名结果
|
||||
*
|
||||
* @param params 通知返回来的参数数组
|
||||
* @param sign 比对的签名结果
|
||||
* @param sign 比对的签名结果
|
||||
* @return 生成的签名结果
|
||||
*/
|
||||
@Override
|
||||
public boolean signVerify(Map<String, Object> params, String sign) {
|
||||
|
||||
if (params instanceof JSONObject){
|
||||
for (String key : params.keySet()){
|
||||
if ("sign".equals(key)){
|
||||
if (params instanceof JSONObject) {
|
||||
for (Map.Entry<String, Object> entry : params.entrySet()) {
|
||||
if (SIGN.equals(entry.getKey())) {
|
||||
continue;
|
||||
}
|
||||
TreeMap response = new TreeMap((Map) params.get(key));
|
||||
TreeMap<String, Object> response = new TreeMap((Map<String, Object> )entry.getValue());
|
||||
LinkedHashMap<Object, Object> linkedHashMap = new LinkedHashMap<>();
|
||||
linkedHashMap.put("code", response.remove("code") );
|
||||
linkedHashMap.put("msg", response.remove("msg") );
|
||||
linkedHashMap.put(CODE, response.remove(CODE));
|
||||
linkedHashMap.put("msg", response.remove("msg"));
|
||||
linkedHashMap.putAll(response);
|
||||
return SignUtils.valueOf(payConfigStorage.getSignType()).verify(JSON.toJSONString(linkedHashMap), sign, payConfigStorage.getKeyPublic(), payConfigStorage.getInputCharset());
|
||||
return SignUtils.valueOf(payConfigStorage.getSignType()).verify(JSON.toJSONString(linkedHashMap), sign, payConfigStorage.getKeyPublic(), payConfigStorage.getInputCharset());
|
||||
}
|
||||
}
|
||||
|
||||
return SignUtils.valueOf(payConfigStorage.getSignType()).verify(params, sign, payConfigStorage.getKeyPublic(), payConfigStorage.getInputCharset());
|
||||
return SignUtils.valueOf(payConfigStorage.getSignType()).verify(params, sign, payConfigStorage.getKeyPublic(), payConfigStorage.getInputCharset());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 校验数据来源
|
||||
*
|
||||
@@ -118,21 +120,21 @@ public class AliPayService extends BasePayService<AliPayConfigStorage> {
|
||||
*/
|
||||
@Override
|
||||
public boolean verifySource(String id) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 生成并设置签名
|
||||
* 生成并设置签名
|
||||
*
|
||||
* @param parameters 请求参数
|
||||
* @return 请求参数
|
||||
*/
|
||||
private Map<String, Object> setSign(Map<String, Object> parameters){
|
||||
private Map<String, Object> setSign(Map<String, Object> parameters) {
|
||||
parameters.put("sign_type", payConfigStorage.getSignType());
|
||||
String sign = createSign( SignUtils.parameterText(parameters, "&", "sign"), payConfigStorage.getInputCharset());
|
||||
String sign = createSign(SignUtils.parameterText(parameters, "&", SIGN), payConfigStorage.getInputCharset());
|
||||
|
||||
parameters.put("sign", sign);
|
||||
parameters.put(SIGN, sign);
|
||||
return parameters;
|
||||
}
|
||||
|
||||
@@ -151,7 +153,7 @@ public class AliPayService extends BasePayService<AliPayConfigStorage> {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
/**
|
||||
* 支付宝创建订单信息
|
||||
* create the order info
|
||||
*
|
||||
@@ -159,7 +161,7 @@ public class AliPayService extends BasePayService<AliPayConfigStorage> {
|
||||
* @return 返回支付宝预下单信息
|
||||
* @see PayOrder 支付订单信息
|
||||
*/
|
||||
private Map<String, Object> getOrder(PayOrder order) {
|
||||
private Map<String, Object> getOrder(PayOrder order) {
|
||||
|
||||
|
||||
Map<String, Object> orderInfo = getPublicParameters(order.getTransactionType());
|
||||
@@ -174,7 +176,8 @@ public class AliPayService extends BasePayService<AliPayConfigStorage> {
|
||||
bizContent.put("subject", order.getSubject());
|
||||
bizContent.put("out_trade_no", order.getOutTradeNo());
|
||||
bizContent.put("total_amount", order.getPrice().setScale(2, BigDecimal.ROUND_HALF_UP).toString());
|
||||
switch ((AliTransactionType)order.getTransactionType()){
|
||||
switch ((AliTransactionType) order.getTransactionType()) {
|
||||
case PAGE:
|
||||
case DIRECT:
|
||||
bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");
|
||||
orderInfo.put("return_url", payConfigStorage.getReturnUrl());
|
||||
@@ -194,8 +197,8 @@ public class AliPayService extends BasePayService<AliPayConfigStorage> {
|
||||
bizContent.put("product_code", "QUICK_MSECURITY_PAY");
|
||||
}
|
||||
}
|
||||
if (null != order.getExpirationTime()){
|
||||
bizContent.put("timeout_express", ((order.getExpirationTime().getTime() - System.currentTimeMillis())/1000/60 + "m"));
|
||||
if (null != order.getExpirationTime()) {
|
||||
bizContent.put("timeout_express", ((order.getExpirationTime().getTime() - System.currentTimeMillis()) / 1000 / 60 + "m"));
|
||||
}
|
||||
orderInfo.put("biz_content", JSON.toJSONString(bizContent));
|
||||
return orderInfo;
|
||||
@@ -203,10 +206,11 @@ public class AliPayService extends BasePayService<AliPayConfigStorage> {
|
||||
|
||||
/**
|
||||
* 获取公共请求参数
|
||||
*
|
||||
* @param transactionType 交易类型
|
||||
* @return 放回公共请求参数
|
||||
*/
|
||||
private Map<String, Object> getPublicParameters(TransactionType transactionType ){
|
||||
private Map<String, Object> getPublicParameters(TransactionType transactionType) {
|
||||
Map<String, Object> orderInfo = new TreeMap<>();
|
||||
orderInfo.put("app_id", payConfigStorage.getAppid());
|
||||
orderInfo.put("method", transactionType.getMethod());
|
||||
@@ -215,14 +219,14 @@ public class AliPayService extends BasePayService<AliPayConfigStorage> {
|
||||
df.setTimeZone(TimeZone.getTimeZone("GMT+8"));
|
||||
orderInfo.put("timestamp", df.format(new Date()));
|
||||
orderInfo.put("version", "1.0");
|
||||
return orderInfo;
|
||||
return orderInfo;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 获取输出消息,用户返回给支付端
|
||||
* @param code 状态
|
||||
*
|
||||
* @param code 状态
|
||||
* @param message 消息
|
||||
* @return 返回输出消息
|
||||
*/
|
||||
@@ -234,6 +238,7 @@ public class AliPayService extends BasePayService<AliPayConfigStorage> {
|
||||
/**
|
||||
* 获取成功输出消息,用户返回给支付端
|
||||
* 主要用于拦截器中返回
|
||||
*
|
||||
* @param payMessage 支付回调消息
|
||||
* @return 返回输出消息
|
||||
*/
|
||||
@@ -243,19 +248,18 @@ public class AliPayService extends BasePayService<AliPayConfigStorage> {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param orderInfo 发起支付的订单信息
|
||||
* @param method 请求方式 "post" "get",
|
||||
* @return 获取输出消息,用户返回给支付端, 针对于web端
|
||||
* @return 获取输出消息,用户返回给支付端, 针对于web端
|
||||
*/
|
||||
@Override
|
||||
public String buildRequest(Map<String, Object> orderInfo, MethodType method) {
|
||||
StringBuffer formHtml = new StringBuffer();
|
||||
formHtml.append("<form id=\"_alipaysubmit_\" name=\"alipaysubmit\" action=\"");
|
||||
String biz_content = (String)orderInfo.remove("biz_content");
|
||||
String bizContent = (String) orderInfo.remove("biz_content");
|
||||
formHtml.append(getReqUrl()).append("?").append(UriVariables.getMapToParameters(orderInfo))
|
||||
.append("\" method=\"").append(method.name().toLowerCase()).append("\">");
|
||||
formHtml.append("<input type=\"hidden\" name=\"biz_content\" value=\'" + biz_content + "\'/>");
|
||||
.append("\" method=\"").append(method.name().toLowerCase()).append("\">");
|
||||
formHtml.append("<input type=\"hidden\" name=\"biz_content\" value=\'" + bizContent + "\'/>");
|
||||
formHtml.append("</form>");
|
||||
formHtml.append("<script>document.forms['_alipaysubmit_'].submit();</script>");
|
||||
|
||||
@@ -264,6 +268,7 @@ public class AliPayService extends BasePayService<AliPayConfigStorage> {
|
||||
|
||||
/**
|
||||
* 生成二维码支付
|
||||
*
|
||||
* @param order 发起支付的订单信息
|
||||
* @return 返回图片信息,支付时需要的
|
||||
*/
|
||||
@@ -276,15 +281,16 @@ public class AliPayService extends BasePayService<AliPayConfigStorage> {
|
||||
//预订单
|
||||
JSONObject result = getHttpRequestTemplate().postForObject(getReqUrl() + "?" + UriVariables.getMapToParameters(orderInfo), null, JSONObject.class);
|
||||
JSONObject response = result.getJSONObject("alipay_trade_precreate_response");
|
||||
if ("10000".equals(response.getString("code"))){
|
||||
return MatrixToImageWriter.writeInfoToJpgBuff( response.getString("qr_code"));
|
||||
if (SUCCESS_CODE.equals(response.getString(CODE))) {
|
||||
return MatrixToImageWriter.writeInfoToJpgBuff(response.getString("qr_code"));
|
||||
}
|
||||
throw new PayErrorException(new PayException(response.getString("code"), response.getString("msg"), result.toJSONString()));
|
||||
throw new PayErrorException(new PayException(response.getString(CODE), response.getString("msg"), result.toJSONString()));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* pos主动扫码付款(条码付)
|
||||
*
|
||||
* @param order 发起支付的订单信息
|
||||
* @return 支付结果
|
||||
*/
|
||||
@@ -294,15 +300,16 @@ public class AliPayService extends BasePayService<AliPayConfigStorage> {
|
||||
//预订单
|
||||
JSONObject result = getHttpRequestTemplate().postForObject(getReqUrl() + "?" + UriVariables.getMapToParameters(orderInfo), null, JSONObject.class);
|
||||
JSONObject response = result.getJSONObject("alipay_trade_pay_response");
|
||||
if (!"10000".equals(response.getString("code"))){
|
||||
LOG.info("收款失败");
|
||||
if (!SUCCESS_CODE.equals(response.getString(CODE))) {
|
||||
LOG.info("收款失败");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 交易查询接口
|
||||
* @param tradeNo 支付平台订单号
|
||||
* 交易查询接口
|
||||
*
|
||||
* @param tradeNo 支付平台订单号
|
||||
* @param outTradeNo 商户单号
|
||||
* @return 返回查询回来的结果集,支付方原值返回
|
||||
*/
|
||||
@@ -313,7 +320,6 @@ public class AliPayService extends BasePayService<AliPayConfigStorage> {
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 交易关闭接口
|
||||
*
|
||||
@@ -323,18 +329,34 @@ public class AliPayService extends BasePayService<AliPayConfigStorage> {
|
||||
*/
|
||||
@Override
|
||||
public Map<String, Object> close(String tradeNo, String outTradeNo) {
|
||||
return secondaryInterface(tradeNo, outTradeNo, AliTransactionType.CLOSE);
|
||||
return secondaryInterface(tradeNo, outTradeNo, AliTransactionType.CLOSE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 支付交易返回失败或支付系统超时,调用该接口撤销交易。
|
||||
* 如果此订单用户支付失败,支付宝系统会将此订单关闭;如果用户支付成功,支付宝系统会将此订单资金退还给用户。
|
||||
* 注意:只有发生支付系统超时或者支付结果未知时可调用撤销,其他正常支付的单如需实现相同功能请调用申请退款API。
|
||||
* 提交支付交易后调用【查询订单API】,没有明确的支付结果再调用【撤销订单API】。
|
||||
*
|
||||
* @param tradeNo 支付平台订单号
|
||||
* @param outTradeNo 商户单号
|
||||
* @return 返回支付方交易撤销后的结果
|
||||
*/
|
||||
@Override
|
||||
public Map<String, Object> cancel(String tradeNo, String outTradeNo) {
|
||||
return secondaryInterface(tradeNo, outTradeNo, AliTransactionType.CANCEL);
|
||||
}
|
||||
|
||||
/**
|
||||
* 申请退款接口
|
||||
* 废弃
|
||||
* @param tradeNo 支付平台订单号
|
||||
* @param outTradeNo 商户单号
|
||||
*
|
||||
* @param tradeNo 支付平台订单号
|
||||
* @param outTradeNo 商户单号
|
||||
* @param refundAmount 退款金额
|
||||
* @param totalAmount 总金额
|
||||
* @param totalAmount 总金额
|
||||
* @return 返回支付方申请退款后的结果
|
||||
* @see #refund(RefundOrder, Callback)
|
||||
* @see #refund(RefundOrder, com.egzosn.pay.common.api.Callback)
|
||||
*/
|
||||
@Deprecated
|
||||
@Override
|
||||
@@ -346,7 +368,7 @@ public class AliPayService extends BasePayService<AliPayConfigStorage> {
|
||||
/**
|
||||
* 申请退款接口
|
||||
*
|
||||
* @param refundOrder 退款订单信息
|
||||
* @param refundOrder 退款订单信息
|
||||
* @return 返回支付方申请退款后的结果
|
||||
*/
|
||||
@Override
|
||||
@@ -355,7 +377,7 @@ public class AliPayService extends BasePayService<AliPayConfigStorage> {
|
||||
Map<String, Object> parameters = getPublicParameters(AliTransactionType.REFUND);
|
||||
|
||||
Map<String, Object> bizContent = getBizContent(refundOrder.getTradeNo(), refundOrder.getOutTradeNo(), null);
|
||||
if (!StringUtils.isEmpty(refundOrder.getRefundNo())){
|
||||
if (!StringUtils.isEmpty(refundOrder.getRefundNo())) {
|
||||
bizContent.put("out_request_no", refundOrder.getRefundNo());
|
||||
}
|
||||
bizContent.put("refund_amount", refundOrder.getRefundAmount().setScale(2, BigDecimal.ROUND_HALF_UP));
|
||||
@@ -363,7 +385,7 @@ public class AliPayService extends BasePayService<AliPayConfigStorage> {
|
||||
parameters.put("biz_content", JSON.toJSONString(bizContent));
|
||||
//设置签名
|
||||
setSign(parameters);
|
||||
return requestTemplate.getForObject(getReqUrl() + "?" + UriVariables.getMapToParameters(parameters), JSONObject.class);
|
||||
return requestTemplate.getForObject(getReqUrl() + "?" + UriVariables.getMapToParameters(parameters), JSONObject.class);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -381,30 +403,31 @@ public class AliPayService extends BasePayService<AliPayConfigStorage> {
|
||||
/**
|
||||
* 查询退款
|
||||
*
|
||||
* @param refundOrder 退款订单单号信息
|
||||
* @param refundOrder 退款订单单号信息
|
||||
* @return 返回支付方查询退款后的结果
|
||||
*/
|
||||
@Override
|
||||
public Map<String, Object> refundquery(RefundOrder refundOrder){
|
||||
public Map<String, Object> refundquery(RefundOrder refundOrder) {
|
||||
|
||||
//获取公共参数
|
||||
Map<String, Object> parameters = getPublicParameters(AliTransactionType.REFUNDQUERY);
|
||||
|
||||
Map<String, Object> bizContent = getBizContent(refundOrder.getTradeNo(), refundOrder.getOutTradeNo(), null);
|
||||
if (!StringUtils.isEmpty(refundOrder.getRefundNo())){
|
||||
if (!StringUtils.isEmpty(refundOrder.getRefundNo())) {
|
||||
bizContent.put("out_request_no", refundOrder.getRefundNo());
|
||||
}
|
||||
//设置请求参数的集合
|
||||
parameters.put("biz_content", JSON.toJSONString(bizContent));
|
||||
parameters.put("biz_content", JSON.toJSONString(bizContent));
|
||||
|
||||
//设置签名
|
||||
setSign(parameters);
|
||||
return requestTemplate.getForObject(getReqUrl() + "?" + UriVariables.getMapToParameters(parameters), JSONObject.class);
|
||||
return requestTemplate.getForObject(getReqUrl() + "?" + UriVariables.getMapToParameters(parameters), JSONObject.class);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 目前只支持日账单
|
||||
*
|
||||
* @param billDate 账单类型,商户通过接口或商户经开放平台授权后其所属服务商通过接口可以获取以下账单类型:trade、signcustomer;trade指商户基于支付宝交易收单的业务账单;signcustomer是指基于商户支付宝余额收入及支出等资金变动的帐务账单;
|
||||
* @param billType 账单时间:日账单格式为yyyy-MM-dd,月账单格式为yyyy-MM。
|
||||
* @return 返回支付方下载对账单的结果
|
||||
@@ -428,24 +451,22 @@ public class AliPayService extends BasePayService<AliPayConfigStorage> {
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param tradeNoOrBillDate 支付平台订单号或者账单类型, 具体请
|
||||
* 类型为{@link String }或者 {@link Date },类型须强制限制,类型不对应则抛出异常{@link PayErrorException}
|
||||
* @param outTradeNoBillType 商户单号或者 账单类型
|
||||
* @param transactionType 交易类型
|
||||
* @param tradeNoOrBillDate 支付平台订单号或者账单类型, 具体请
|
||||
* 类型为{@link String }或者 {@link Date },类型须强制限制,类型不对应则抛出异常{@link PayErrorException}
|
||||
* @param outTradeNoBillType 商户单号或者 账单类型
|
||||
* @param transactionType 交易类型
|
||||
* @return 返回支付方对应接口的结果
|
||||
*/
|
||||
@Override
|
||||
public Map<String, Object> secondaryInterface(Object tradeNoOrBillDate, String outTradeNoBillType, TransactionType transactionType){
|
||||
public Map<String, Object> secondaryInterface(Object tradeNoOrBillDate, String outTradeNoBillType, TransactionType transactionType) {
|
||||
|
||||
if (transactionType == AliTransactionType.REFUND){
|
||||
if (transactionType == AliTransactionType.REFUND) {
|
||||
throw new PayErrorException(new PayException("failure", "通用接口不支持:" + transactionType));
|
||||
}
|
||||
|
||||
if (transactionType == AliTransactionType.DOWNLOADBILL){
|
||||
if (tradeNoOrBillDate instanceof Date){
|
||||
if (transactionType == AliTransactionType.DOWNLOADBILL) {
|
||||
if (tradeNoOrBillDate instanceof Date) {
|
||||
return downloadbill((Date) tradeNoOrBillDate, outTradeNoBillType);
|
||||
}
|
||||
throw new PayErrorException(new PayException("failure", "非法类型异常:" + tradeNoOrBillDate.getClass()));
|
||||
@@ -457,14 +478,13 @@ public class AliPayService extends BasePayService<AliPayConfigStorage> {
|
||||
parameters.put("biz_content", getContentToJson(tradeNoOrBillDate.toString(), outTradeNoBillType));
|
||||
//设置签名
|
||||
setSign(parameters);
|
||||
return requestTemplate.getForObject(getReqUrl() + "?" + UriVariables.getMapToParameters(parameters), JSONObject.class);
|
||||
return requestTemplate.getForObject(getReqUrl() + "?" + UriVariables.getMapToParameters(parameters), JSONObject.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转账
|
||||
*
|
||||
* @param order 转账订单
|
||||
*
|
||||
* @return 对应的转账结果
|
||||
*/
|
||||
@Override
|
||||
@@ -476,7 +496,7 @@ public class AliPayService extends BasePayService<AliPayConfigStorage> {
|
||||
bizContent.put("out_biz_no", order.getOutNo());
|
||||
//默认 支付宝登录号,支持邮箱和手机号格式。
|
||||
bizContent.put("payee_type", "ALIPAY_LOGONID");
|
||||
if (null != order.getTransferType()){
|
||||
if (null != order.getTransferType()) {
|
||||
bizContent.put("payee_type", order.getTransferType().getType());
|
||||
}
|
||||
bizContent.put("payee_account", order.getPayeeAccount());
|
||||
@@ -496,7 +516,6 @@ public class AliPayService extends BasePayService<AliPayConfigStorage> {
|
||||
*
|
||||
* @param outNo 商户转账订单号
|
||||
* @param tradeNo 支付平台转账订单号
|
||||
*
|
||||
* @return 对应的转账订单
|
||||
*/
|
||||
@Override
|
||||
@@ -505,9 +524,9 @@ public class AliPayService extends BasePayService<AliPayConfigStorage> {
|
||||
Map<String, Object> parameters = getPublicParameters(AliTransactionType.TRANS_QUERY);
|
||||
|
||||
Map<String, Object> bizContent = new TreeMap<String, Object>();
|
||||
if (StringUtils.isEmpty(outNo)){
|
||||
if (StringUtils.isEmpty(outNo)) {
|
||||
bizContent.put("order_id", tradeNo);
|
||||
}else {
|
||||
} else {
|
||||
bizContent.put("out_biz_no", outNo);
|
||||
}
|
||||
//设置请求参数的集合
|
||||
@@ -519,32 +538,34 @@ public class AliPayService extends BasePayService<AliPayConfigStorage> {
|
||||
|
||||
|
||||
/**
|
||||
* 获取biz_content。请求参数的集合 不包含下载账单
|
||||
* @param tradeNo 支付平台订单号
|
||||
* 获取biz_content。请求参数的集合 不包含下载账单
|
||||
*
|
||||
* @param tradeNo 支付平台订单号
|
||||
* @param outTradeNo 商户单号
|
||||
* @param bizContent 请求参数的集合
|
||||
* @param bizContent 请求参数的集合
|
||||
* @return 请求参数的集合 不包含下载账单
|
||||
*/
|
||||
private Map<String, Object> getBizContent(String tradeNo, String outTradeNo, Map<String, Object> bizContent){
|
||||
if (null == bizContent){
|
||||
bizContent = new TreeMap<>();
|
||||
private Map<String, Object> getBizContent(String tradeNo, String outTradeNo, Map<String, Object> bizContent) {
|
||||
if (null == bizContent) {
|
||||
bizContent = new TreeMap<>();
|
||||
}
|
||||
if (!StringUtils.isEmpty(outTradeNo)){
|
||||
if (!StringUtils.isEmpty(outTradeNo)) {
|
||||
bizContent.put("out_trade_no", outTradeNo);
|
||||
}
|
||||
if (!StringUtils.isEmpty(tradeNo)){
|
||||
if (!StringUtils.isEmpty(tradeNo)) {
|
||||
bizContent.put("trade_no", tradeNo);
|
||||
}
|
||||
return bizContent;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取biz_content。不包含下载账单
|
||||
* @param tradeNo 支付平台订单号
|
||||
* 获取biz_content。不包含下载账单
|
||||
*
|
||||
* @param tradeNo 支付平台订单号
|
||||
* @param outTradeNo 商户单号
|
||||
* @return 获取biz_content。不包含下载账单
|
||||
* @return 获取biz_content。不包含下载账单
|
||||
*/
|
||||
private String getContentToJson(String tradeNo, String outTradeNo){
|
||||
private String getContentToJson(String tradeNo, String outTradeNo) {
|
||||
|
||||
return JSON.toJSONString(getBizContent(tradeNo, outTradeNo, null));
|
||||
}
|
||||
|
||||
@@ -20,8 +20,14 @@ import com.egzosn.pay.common.bean.TransactionType;
|
||||
public enum AliTransactionType implements TransactionType {
|
||||
/**
|
||||
* 即时到帐
|
||||
* 过时的名称,请换至 {@link #PAGE}
|
||||
*/
|
||||
@Deprecated
|
||||
DIRECT("alipay.trade.page.pay"),
|
||||
/**
|
||||
* 网页支付
|
||||
*/
|
||||
PAGE("alipay.trade.page.pay"),
|
||||
/**
|
||||
* APP支付
|
||||
*/
|
||||
@@ -29,12 +35,12 @@ public enum AliTransactionType implements TransactionType {
|
||||
/**
|
||||
* 手机网站支付
|
||||
*/
|
||||
WAP("alipay.trade.wap.pay")
|
||||
WAP("alipay.trade.wap.pay"),
|
||||
|
||||
/**
|
||||
* 扫码付
|
||||
*/
|
||||
,SWEEPPAY("alipay.trade.precreate"),
|
||||
SWEEPPAY("alipay.trade.precreate"),
|
||||
/**
|
||||
* 条码付
|
||||
*/
|
||||
@@ -42,17 +48,21 @@ public enum AliTransactionType implements TransactionType {
|
||||
/**
|
||||
* 声波付
|
||||
*/
|
||||
WAVE_CODE("alipay.trade.pay")
|
||||
WAVE_CODE("alipay.trade.pay"),
|
||||
//交易辅助接口
|
||||
|
||||
/**
|
||||
* 交易订单查询
|
||||
*/
|
||||
,QUERY("alipay.trade.query"),
|
||||
QUERY("alipay.trade.query"),
|
||||
/**
|
||||
* 交易订单关闭
|
||||
*/
|
||||
CLOSE("alipay.trade.close"),
|
||||
/**
|
||||
* 交易订单撤销
|
||||
*/
|
||||
CANCEL("alipay.trade.cancel "),
|
||||
/**
|
||||
* 退款
|
||||
*/
|
||||
@@ -79,7 +89,7 @@ public enum AliTransactionType implements TransactionType {
|
||||
|
||||
private String method;
|
||||
|
||||
private AliTransactionType(String method) {
|
||||
AliTransactionType(String method) {
|
||||
this.method = method;
|
||||
}
|
||||
|
||||
@@ -88,7 +98,7 @@ public enum AliTransactionType implements TransactionType {
|
||||
return this.name();
|
||||
}
|
||||
|
||||
/* *
|
||||
/**
|
||||
* 获取接口名称
|
||||
* @return 接口名称
|
||||
*/
|
||||
|
||||
@@ -21,6 +21,8 @@ import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
import static com.egzosn.pay.ali.api.AliPayService.SIGN;
|
||||
|
||||
/**
|
||||
* 支付宝支付服务
|
||||
* @author egan
|
||||
@@ -37,8 +39,11 @@ public class AliPayService extends BasePayService<AliPayConfigStorage> {
|
||||
|
||||
private static final String HTTPS_REQ_URL = "https://mapi.alipay.com/gateway.do";
|
||||
private static final String QUERY_REQ_URL = "https://openapi.alipay.com/gateway.do";
|
||||
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
|
||||
{
|
||||
public static final String NOTIFY_ID = "notify_id";
|
||||
|
||||
public static final DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
|
||||
|
||||
static {
|
||||
df.setTimeZone(TimeZone.getTimeZone("GMT+8"));
|
||||
}
|
||||
public AliPayService(AliPayConfigStorage payConfigStorage) {
|
||||
@@ -63,13 +68,13 @@ public class AliPayService extends BasePayService<AliPayConfigStorage> {
|
||||
@Override
|
||||
public boolean verify(Map<String, Object> params) {
|
||||
|
||||
if (params.get("sign") == null || params.get("notify_id") == null) {
|
||||
if (params.get(SIGN) == null || params.get(NOTIFY_ID) == null) {
|
||||
LOG.debug("支付宝支付异常:params:" + params);
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
return signVerify(params, (String) params.get("sign")) && verifySource((String) params.get("notify_id"));
|
||||
return signVerify(params, (String) params.get(SIGN)) && verifySource((String) params.get(NOTIFY_ID));
|
||||
} catch (PayErrorException e) {
|
||||
LOG.error(e);
|
||||
}
|
||||
@@ -107,8 +112,8 @@ public class AliPayService extends BasePayService<AliPayConfigStorage> {
|
||||
*/
|
||||
private Map<String, Object> setSign(Map<String, Object> parameters){
|
||||
parameters.put("sign_type", payConfigStorage.getSignType());
|
||||
String sign = createSign(SignUtils.parameterText(parameters, "&", "sign", "appId"), payConfigStorage.getInputCharset());
|
||||
parameters.put("sign", sign);
|
||||
String sign = createSign(SignUtils.parameterText(parameters, "&", SIGN, "appId"), payConfigStorage.getInputCharset());
|
||||
parameters.put(SIGN, sign);
|
||||
|
||||
return parameters;
|
||||
}
|
||||
@@ -160,7 +165,7 @@ public class AliPayService extends BasePayService<AliPayConfigStorage> {
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
orderInfo.put("sign", sign);
|
||||
orderInfo.put(SIGN, sign);
|
||||
orderInfo.put("sign_type", payConfigStorage.getSignType());
|
||||
return orderInfo;
|
||||
}
|
||||
@@ -220,9 +225,6 @@ public class AliPayService extends BasePayService<AliPayConfigStorage> {
|
||||
orderInfo.put("it_b_pay", "30m");
|
||||
// 支付宝处理完请求后,当前页面跳转到商户指定页面的路径,可空
|
||||
orderInfo.put("return_url", payConfigStorage.getReturnUrl());
|
||||
// 调用银行卡支付,需配置此参数,参与签名, 固定值 (需要签约《无线银行卡快捷支付》才能使用)
|
||||
// if (order.getTransactionType().getType())
|
||||
// orderInfo.put("paymethod","expressGateway");
|
||||
|
||||
return orderInfo;
|
||||
}
|
||||
@@ -273,12 +275,12 @@ public class AliPayService extends BasePayService<AliPayConfigStorage> {
|
||||
.append( "\" method=\"")
|
||||
.append( method.name().toLowerCase()) .append( "\">");
|
||||
|
||||
for (String key: orderInfo.keySet()) {
|
||||
Object o = orderInfo.get(key);
|
||||
if (null == o ||"null".equals(o) || "".equals(o) ){
|
||||
for (Map.Entry<String, Object> entry : orderInfo.entrySet()) {
|
||||
Object o = entry.getValue();
|
||||
if (null == o || "null".equals(o) || "".equals(o)) {
|
||||
continue;
|
||||
}
|
||||
formHtml.append("<input type=\"hidden\" name=\"" + key + "\" value=\"" + orderInfo.get(key) + "\"/>");
|
||||
formHtml.append("<input type=\"hidden\" name=\"" + entry.getKey() + "\" value=\"" + o + "\"/>");
|
||||
}
|
||||
|
||||
|
||||
@@ -340,6 +342,22 @@ public class AliPayService extends BasePayService<AliPayConfigStorage> {
|
||||
return secondaryInterface(tradeNo, outTradeNo, AliTransactionType.CLOSE);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 支付交易返回失败或支付系统超时,调用该接口撤销交易。
|
||||
* 如果此订单用户支付失败,支付宝系统会将此订单关闭;如果用户支付成功,支付宝系统会将此订单资金退还给用户。
|
||||
* 注意:只有发生支付系统超时或者支付结果未知时可调用撤销,其他正常支付的单如需实现相同功能请调用申请退款API。
|
||||
* 提交支付交易后调用【查询订单API】,没有明确的支付结果再调用【撤销订单API】。
|
||||
*
|
||||
* @param tradeNo 支付平台订单号
|
||||
* @param outTradeNo 商户单号
|
||||
* @return 返回支付方交易撤销后的结果
|
||||
*/
|
||||
@Override
|
||||
public Map<String, Object> cancel(String tradeNo, String outTradeNo) {
|
||||
return secondaryInterface(tradeNo, outTradeNo, AliTransactionType.CANCEL);
|
||||
}
|
||||
|
||||
/**
|
||||
* 申请退款接口
|
||||
* 废弃
|
||||
|
||||
@@ -34,7 +34,7 @@ public enum AliTransactionType implements TransactionType {
|
||||
*/
|
||||
WAP("alipay.wap.create.direct.pay.by.user"),
|
||||
|
||||
//交易辅助接口
|
||||
//交易辅助接口,以下属于新版接口
|
||||
|
||||
/**
|
||||
* 交易订单查询
|
||||
@@ -44,6 +44,10 @@ public enum AliTransactionType implements TransactionType {
|
||||
* 交易订单关闭
|
||||
*/
|
||||
CLOSE("alipay.trade.close"),
|
||||
/**
|
||||
* 交易订单撤销
|
||||
*/
|
||||
CANCEL("alipay.trade.cancel "),
|
||||
/**
|
||||
* 退款
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user