|
|
package com.yohoufo.order.service.pay.alipay;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yohobuy.ufo.model.order.bo.OrderInfo;
|
|
|
import com.yohoufo.common.utils.DateUtil;
|
|
|
import com.yohoufo.common.utils.RSAUtils;
|
|
|
import com.yohoufo.order.config.AlipayConfig;
|
|
|
import com.yohoufo.order.model.PayQueryBo;
|
|
|
import com.yohoufo.order.model.PayRefundBo;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.net.URLEncoder;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
|
@Service
|
|
|
public class AlipayCrossBorderService extends AlipayServiceAbstract {
|
|
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger("alipayLogger");
|
|
|
|
|
|
@Override
|
|
|
protected String getMd5Key() {
|
|
|
return "";
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected String getRsaAlipubKey() {
|
|
|
return AlipayConfig.CROSS_BORDER_RSA_PUBLIC;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected String getRsaPrivateKey() {
|
|
|
return AlipayConfig.CROSS_BORDER_RSA_PRIVATE;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected String getAppId() {
|
|
|
return "";
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected String getPartnerId() {
|
|
|
return "2088121119526910";
|
|
|
}
|
|
|
|
|
|
private String getSellerId(){
|
|
|
return "zfbgj@yoho.cn";
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* OpenApi退款
|
|
|
* @param refundBo
|
|
|
* @return
|
|
|
*/
|
|
|
public PayRefundBo refundOpenApi(PayRefundBo refundBo) {
|
|
|
logger.info("enter AliPayRefunder to refund tradeNo {}, amount {}", refundBo.getRefundOrderCode(), refundBo.getAmount());
|
|
|
Map<String, String> refundParams = buildOpenApiRefundParams(refundBo.getPayOrderCode(), refundBo.getRefundOrderCode(), refundBo.getAmount());
|
|
|
String respTxt = sendOpenApiRequest(String.valueOf(refundBo.getPayOrderCode()), refundParams, AlipayConfig.OPENAPI_URL);
|
|
|
PayRefundBo bo = refundOpenApiConvert(respTxt, refundBo);
|
|
|
logger.info("exit AliPayRefunder refund, refundStatus: {}, refundMsg", bo.getRefundStatus(), bo.getRefundMsg());
|
|
|
return bo;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* openapi退款参数
|
|
|
* @param tradeNo
|
|
|
* @param refundNo
|
|
|
* @param amount
|
|
|
* @return
|
|
|
*/
|
|
|
private Map<String, String> buildOpenApiRefundParams(String tradeNo, String refundNo, double amount) {
|
|
|
Map<String, String> params = new HashMap<String, String>();
|
|
|
|
|
|
params.put("service", "forex_refund");
|
|
|
params.put("partner", getPartnerId());
|
|
|
params.put("_input_charset", AlipayConfig.input_charset);
|
|
|
params.put("sign_type", "RSA");
|
|
|
|
|
|
params.put("out_return_no",refundNo);
|
|
|
params.put("out_trade_no", tradeNo);
|
|
|
params.put("return_rmb_amount", String.valueOf(amount));
|
|
|
params.put("currency", "HKD");
|
|
|
|
|
|
|
|
|
|
|
|
params.put("app_id", getAppId());
|
|
|
params.put("method", "alipay.trade.refund");
|
|
|
params.put("charset", AlipayConfig.input_charset);
|
|
|
params.put("sign_type", "RSA");
|
|
|
params.put("timestamp", DateUtil.getCurrentTime());
|
|
|
params.put("version", "1.0");
|
|
|
|
|
|
JSONObject bizJson = new JSONObject();
|
|
|
bizJson.put("out_trade_no", tradeNo);
|
|
|
bizJson.put("refund_amount", amount);
|
|
|
bizJson.put("out_request_no", refundNo);
|
|
|
params.put("biz_content", bizJson.toJSONString());
|
|
|
|
|
|
String preSignStr = getOpenApiSignString(params);
|
|
|
params.put("sign", RSAUtils.sign(preSignStr, getRsaPrivateKey(), AlipayConfig.input_charset));
|
|
|
|
|
|
return params;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 查询支付结果
|
|
|
* @param tradeNo
|
|
|
* @param orderCreateTime
|
|
|
* @return
|
|
|
*/
|
|
|
public PayQueryBo payQuery(String tradeNo, int orderCreateTime) {
|
|
|
Map<String, String> queryParams = buildOpenApiQueryParams(tradeNo);
|
|
|
String respTxt = sendOpenApiRequest(tradeNo, queryParams, AlipayConfig.CROSS_BORDER_OPENAPI_URL);
|
|
|
QueryBo queryBo = JSON.parseObject(JSONObject.toJSONString(JSON.parseObject(respTxt).getJSONObject("alipay_trade_query_response")), QueryBo.class);
|
|
|
|
|
|
// 查询结果转换成 共通的对象
|
|
|
PayQueryBo payQueryBo = new PayQueryBo();
|
|
|
|
|
|
if ("10000".equals(queryBo.getCode())){
|
|
|
// 订单支付时的商户号
|
|
|
payQueryBo.setPayOrderCode(queryBo.getOut_trade_no());
|
|
|
payQueryBo.setTradeNo(queryBo.getTrade_no());
|
|
|
|
|
|
if ("TRADE_SUCCESS".equals(queryBo.getTrade_status())) {
|
|
|
payQueryBo.setPayStatus(true);
|
|
|
|
|
|
}else if("TRADE_CLOSED".equals(queryBo.getTrade_status())){
|
|
|
payQueryBo.setRefundStatus(true);
|
|
|
}
|
|
|
|
|
|
payQueryBo.setAmount(Double.parseDouble(queryBo.getTotal_amount()));
|
|
|
payQueryBo.setBuyerId(queryBo.getBuyer_user_id());
|
|
|
}
|
|
|
|
|
|
return payQueryBo;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
* openapi支付查询
|
|
|
* @param orderCode
|
|
|
* @return
|
|
|
*/
|
|
|
private Map<String, String> buildOpenApiQueryParams(String orderCode) {
|
|
|
Map<String, String> params = new HashMap<String, String>();
|
|
|
params.put("service", "single_trade_query");
|
|
|
params.put("partner", getPartnerId());
|
|
|
params.put("_input_charset", AlipayConfig.input_charset);
|
|
|
params.put("sign_type", "RSA");
|
|
|
params.put("out_trade_no", orderCode);
|
|
|
|
|
|
String preSignStr = getOpenApiSignString(params);
|
|
|
params.put("sign", RSAUtils.sign(preSignStr, getRsaPrivateKey(), AlipayConfig.input_charset));
|
|
|
return params;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 预支付接口
|
|
|
* @param orderInfo
|
|
|
* @return
|
|
|
*/
|
|
|
public JSONObject prepayRequest(OrderInfo orderInfo) {
|
|
|
logger.info("cross border orderInfo is {}", orderInfo);
|
|
|
|
|
|
String orderCode = String.valueOf(orderInfo.getOrderCode());
|
|
|
Map<String, String> queryParams = buildOpenApiPayParams(orderCode, orderInfo.getAmount(),orderInfo.getPayExpireTime());
|
|
|
|
|
|
StringBuilder res = new StringBuilder();
|
|
|
for (String key : queryParams.keySet()){
|
|
|
res.append(key);
|
|
|
res.append("=");
|
|
|
res.append(queryParams.get(key));
|
|
|
res.append("&");
|
|
|
}
|
|
|
|
|
|
JSONObject rr = new JSONObject();
|
|
|
rr.put("payParams", res.toString().substring(0, res.toString().length()-1));
|
|
|
return rr;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
* app支付结果
|
|
|
* @param tradeNo
|
|
|
* @param amount
|
|
|
* @param payExpireTime
|
|
|
* @return
|
|
|
*/
|
|
|
private Map<String, String> buildOpenApiPayParams(String tradeNo, BigDecimal amount, int payExpireTime) {
|
|
|
Map<String, String> params = new HashMap<String, String>();
|
|
|
|
|
|
params.put("service", "mobile.securitypay.pay");
|
|
|
params.put("partner", getPartnerId());
|
|
|
params.put("_input_charset", AlipayConfig.input_charset);
|
|
|
params.put("sign_type", "RSA");
|
|
|
params.put("notify_url", getNotifyURL());
|
|
|
|
|
|
params.put("out_trade_no", tradeNo);
|
|
|
String subject = "ufoOrder-" + tradeNo;
|
|
|
params.put("subject", subject);
|
|
|
params.put("payment_type", "1");
|
|
|
params.put("seller_id", getSellerId());
|
|
|
params.put("rmb_fee", amount.toPlainString());
|
|
|
params.put("currency", "HKD"); // Hong Kong Dollar
|
|
|
params.put("forex_biz", "FP");
|
|
|
params.put("it_b_pay", payExpireTime+"m");
|
|
|
params.put("refer_url", getNotifyURL()); // 下载商家应用的信息
|
|
|
params.put("product_code", "NEW_WAP_OVERSEAS_SELLER");
|
|
|
|
|
|
JSONObject trade_information = new JSONObject();
|
|
|
params.put("trade_information", trade_information.toJSONString());
|
|
|
|
|
|
trade_information.put("business_type", 4);
|
|
|
trade_information.put("goods_info", "xxdd^1");
|
|
|
trade_information.put("total_quantity",1);
|
|
|
|
|
|
|
|
|
String preSignStr = getOpenApiSignString(params);
|
|
|
String sign = RSAUtils.sign(preSignStr, getRsaPrivateKey(), AlipayConfig.input_charset);
|
|
|
params.put("sign", URLEncoder.encode(sign));
|
|
|
|
|
|
return params;
|
|
|
}
|
|
|
} |
...
|
...
|
|