支付宝h5 pc策略实现
This commit is contained in:
parent
8195512ba8
commit
4652e75ad7
@ -1,11 +1,24 @@
|
|||||||
package net.jieyuu.component;
|
package net.jieyuu.component;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import com.alipay.api.AlipayApiException;
|
||||||
|
import com.alipay.api.request.AlipayTradePagePayRequest;
|
||||||
|
import com.alipay.api.request.AlipayTradeWapPayRequest;
|
||||||
|
import com.alipay.api.response.AlipayTradePagePayResponse;
|
||||||
|
import com.alipay.api.response.AlipayTradeWapPayResponse;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import net.jieyuu.config.AlipayConfig;
|
||||||
import net.jieyuu.config.PayUrlConfig;
|
import net.jieyuu.config.PayUrlConfig;
|
||||||
|
import net.jieyuu.enums.BizCodeEnum;
|
||||||
|
import net.jieyuu.enums.ClientType;
|
||||||
|
import net.jieyuu.exception.BizException;
|
||||||
import net.jieyuu.vo.PayInfoVO;
|
import net.jieyuu.vo.PayInfoVO;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
@ -16,7 +29,68 @@ public class AlipayStrategy implements PayStrategy {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String unifiedorder(PayInfoVO payInfoVO) {
|
public String unifiedorder(PayInfoVO payInfoVO) {
|
||||||
return PayStrategy.super.unifiedorder(payInfoVO);
|
HashMap<String, String> content = new HashMap<>();
|
||||||
|
//商户订单号,64个字符以内、可包含字母、数字、下划线;需保证在商户端不重复
|
||||||
|
String no = UUID.randomUUID().toString();
|
||||||
|
log.info("订单号:{}", no);
|
||||||
|
content.put("out_trade_no", no);
|
||||||
|
content.put("product_code", "FAST_INSTANT_TRADE_PAY");
|
||||||
|
//订单总金额,单位为元,精确到小数点后两位
|
||||||
|
content.put("total_amount", payInfoVO.getPayFee().toString());
|
||||||
|
//商品标题/交易标题/订单标题/订单关键字等。 注意:不可使用特殊字符,如 /,=,& 等。
|
||||||
|
content.put("subject", payInfoVO.getTitle());
|
||||||
|
//商品描述,可空
|
||||||
|
content.put("body", payInfoVO.getDescription());
|
||||||
|
|
||||||
|
|
||||||
|
double timeout = Math.floor(payInfoVO.getOrderPayTimeoutMills() / (1000 * 60));
|
||||||
|
// 前端也需要判断订单是否需要关闭 快要关闭则不给二次支付
|
||||||
|
if (timeout < 1) {
|
||||||
|
throw new BizException(BizCodeEnum.PAY_ORDER_PAY_TIMEOUT);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 该笔订单允许的最晚付款时间,逾期将关闭交易。取值范围:1m~15d。m-分钟,h-小时,d-天,1c-当天(1c-当天的情况下,无论交易何时创建,都在0点关闭)。 该参数数值不接受小数点, 如 1.5h,可转换为 90m。
|
||||||
|
content.put("timeout_express", Double.valueOf(timeout) + "m");
|
||||||
|
|
||||||
|
String clientType = payInfoVO.getClientType();
|
||||||
|
String form = "";
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (ClientType.H5.name().equalsIgnoreCase(clientType)) {
|
||||||
|
// h5手机网页支付
|
||||||
|
AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
|
||||||
|
request.setBizContent(JSON.toJSONString(content));
|
||||||
|
request.setNotifyUrl(payUrlConfig.getAlipayCallbackUrl());
|
||||||
|
request.setReturnUrl(payUrlConfig.getAlipaySuccessReturnUrl());
|
||||||
|
|
||||||
|
AlipayTradeWapPayResponse alipayResponse = AlipayConfig.getInstance().pageExecute(request);
|
||||||
|
|
||||||
|
log.info("日志响应:{}", alipayResponse);
|
||||||
|
if (alipayResponse.isSuccess()) {
|
||||||
|
form = alipayResponse.getBody();
|
||||||
|
} else {
|
||||||
|
log.error("支付宝构建h5表单失败:request={},payInfo={}", alipayResponse, payInfoVO);
|
||||||
|
}
|
||||||
|
} else if (ClientType.PC.name().equalsIgnoreCase(clientType)) {
|
||||||
|
// PC支付
|
||||||
|
AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
|
||||||
|
request.setBizContent(JSON.toJSONString(content));
|
||||||
|
request.setNotifyUrl(payUrlConfig.getAlipayCallbackUrl());
|
||||||
|
request.setReturnUrl(payUrlConfig.getAlipaySuccessReturnUrl());
|
||||||
|
AlipayTradePagePayResponse alipayResponse = AlipayConfig.getInstance().pageExecute(request);
|
||||||
|
|
||||||
|
log.info("日志响应:{}", alipayResponse);
|
||||||
|
if (alipayResponse.isSuccess()) {
|
||||||
|
form = alipayResponse.getBody();
|
||||||
|
} else {
|
||||||
|
log.error("支付宝构建PC表单失败:alipayResponse={},payInfo={}", alipayResponse, payInfoVO);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (AlipayApiException e) {
|
||||||
|
log.error("支付宝构建表单异常:payInfo={},异常={}", payInfoVO, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return form;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
Reference in New Issue
Block a user