mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-04-20 19:15:35 +08:00
update 支付宝小程序
This commit is contained in:
@@ -35,9 +35,14 @@ public class JsowellAdapayConfig extends AbstractAdapayConfig {
|
||||
@Value("${adapay.jsowell.wechatAppId}")
|
||||
private String wechatAppId;
|
||||
|
||||
@Value("${adapay.jsowell.alipayAppId}")
|
||||
private String alipayAppId;
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
// key为微信小程序id
|
||||
AdapayConfigFactory.register(wechatAppId, this);
|
||||
// key为支付宝小程序id
|
||||
AdapayConfigFactory.register(alipayAppId, this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,9 +84,9 @@ public class AdapayService {
|
||||
private MemberBasicInfoService memberBasicInfoService;
|
||||
|
||||
/**
|
||||
* 获取支付参数
|
||||
* 获取支付参数-微信支付
|
||||
*/
|
||||
public Map<String, Object> createPayment(PayOrderDTO dto) {
|
||||
public Map<String, Object> createPaymentForWechat(PayOrderDTO dto) {
|
||||
// log.info("===============使用汇付支付-获取支付参数");
|
||||
// 相同参数重复请求,返回同一个支付对象
|
||||
String redisKey = CacheConstants.ADAPAY_ORDER_PARAM + dto.getOrderCode();
|
||||
@@ -173,6 +173,97 @@ public class AdapayService {
|
||||
return resultMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取支付参数-支付宝支付
|
||||
*/
|
||||
public Map<String, Object> createPaymentForAlipay(PayOrderDTO dto) {
|
||||
// log.info("===============使用汇付支付-获取支付参数");
|
||||
// 相同参数重复请求,返回同一个支付对象
|
||||
String redisKey = CacheConstants.ADAPAY_ORDER_PARAM + dto.getOrderCode();
|
||||
Map<String, Object> resultMap = redisCache.getCacheObject(redisKey);
|
||||
if (resultMap != null) {
|
||||
// 表示已经获取到支付参数了,后续再有支付请求就拒绝
|
||||
return resultMap;
|
||||
}
|
||||
|
||||
// 获取支付配置
|
||||
AbstractAdapayConfig config = AdapayConfigFactory.getConfig(dto.getAlipayAppId());
|
||||
if (config == null) {
|
||||
throw new BusinessException(ReturnCodeEnum.CODE_ADAPAY_CONFIG_IS_NULL_ERROR);
|
||||
}
|
||||
|
||||
// 获取openId
|
||||
MemberBasicInfo memberBasicInfo = memberBasicInfoService.selectInfoByMemberId(dto.getMemberId());
|
||||
if (memberBasicInfo == null) {
|
||||
throw new BusinessException(ReturnCodeEnum.CODE_GET_OPEN_ID_BY_CODE_ERROR);
|
||||
}
|
||||
String openId = memberBasicInfo.getOpenId();
|
||||
|
||||
// 支付场景
|
||||
String type = dto.getType();
|
||||
|
||||
// 封装对象
|
||||
String amount = AdapayUtil.formatAmount(dto.getPayAmount()); // 用户支付金额
|
||||
String delayMode = pileMerchantInfoService.getDelayModeByWechatAppId(dto.getWechatAppId());
|
||||
String payMode = MerchantDelayModeEnum.getAdapayPayMode(delayMode);
|
||||
CreateAdaPaymentParam createAdaPaymentParam = new CreateAdaPaymentParam();
|
||||
// 请求订单号, 防止请求订单号重复,结尾拼接时间
|
||||
String orderNo = dto.getOrderCode() + "_" + DateUtils.dateTimeNow();
|
||||
createAdaPaymentParam.setOrder_no(orderNo);
|
||||
createAdaPaymentParam.setPay_amt(amount);
|
||||
createAdaPaymentParam.setApp_id(config.getAdapayAppId());
|
||||
|
||||
// 2024年6月13日11点55分,需要兼容支付宝小程序,首先判断请求来源,如为空默认微信小程序
|
||||
String payChannel = StringUtils.isNotBlank(dto.getRequestSource())
|
||||
? dto.getRequestSource()
|
||||
: AdapayPayChannelEnum.WX_LITE.getValue();
|
||||
createAdaPaymentParam.setPay_channel(payChannel);
|
||||
createAdaPaymentParam.setGoods_title(dto.getGoodsTitle());
|
||||
createAdaPaymentParam.setGoods_desc(dto.getGoodsDesc()); // 这个字段是微信支付凭证的商品名
|
||||
Map<String, String> map = Maps.newHashMap();
|
||||
map.put("type", type);
|
||||
map.put("orderCode", dto.getOrderCode());
|
||||
map.put("payMode", payMode);
|
||||
map.put("memberId", dto.getMemberId());
|
||||
createAdaPaymentParam.setDescription(JSON.toJSONString(map));
|
||||
// 异步通知地址,url为http/https路径,服务器POST回调,URL 上请勿附带参数
|
||||
createAdaPaymentParam.setNotify_url(ADAPAY_CALLBACK_URL);
|
||||
|
||||
// alipay_lite参数 buyer_id String(100) Y 买家的支付宝用户 id
|
||||
createAdaPaymentParam.setExpend(JSON.toJSONString(ImmutableMap.of("buyer_id", openId)));
|
||||
|
||||
// 延时分账
|
||||
if (StringUtils.isNotBlank(payMode)) {
|
||||
createAdaPaymentParam.setPay_mode(payMode);
|
||||
}
|
||||
try {
|
||||
Map<String, Object> response = Payment.create(BeanMap.create(createAdaPaymentParam), config.getWechatAppId());
|
||||
log.info("创建汇付支付参数:{}, response:{}", JSON.toJSONString(createAdaPaymentParam), JSON.toJSONString(response));
|
||||
if (response != null && !response.isEmpty()) {
|
||||
String status = (String) response.get("status");
|
||||
if (!StringUtils.equals(status, AdapayStatusEnum.SUCCEEDED.getValue())) {
|
||||
String error_msg = (String) response.get("error_msg");
|
||||
throw new BusinessException(ReturnCodeEnum.CODE_GET_WECHAT_PAY_PARAMETER_ERROR.getValue(), error_msg);
|
||||
}
|
||||
JSONObject expend = JSONObject.parseObject(response.get("expend").toString());
|
||||
JSONObject pay_info = expend.getJSONObject("pay_info");
|
||||
resultMap = JSONObject.parseObject(pay_info.toJSONString(), new TypeReference<Map<String, Object>>() {});
|
||||
}
|
||||
} catch (BaseAdaPayException e) {
|
||||
log.error("汇付-获取支付对象发生异常, orderCode:{}", dto.getOrderCode(), e);
|
||||
}
|
||||
|
||||
// 放缓存
|
||||
if (resultMap != null) {
|
||||
// 请求参数放入缓存,15分钟以内返回同一个支付参数
|
||||
redisCache.setCacheObject(redisKey, resultMap, 15, TimeUnit.MINUTES);
|
||||
|
||||
// 请求订单号放redis
|
||||
redisCache.setCacheObject(CacheConstants.ORDER_WECHAT_PAY_PARAMETERS + dto.getOrderCode(), orderNo, CacheConstants.cache_expire_time_10d);
|
||||
}
|
||||
return resultMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建结算账户
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user