mirror of https://github.com/elunez/eladmin
118 lines
5.1 KiB
Java
118 lines
5.1 KiB
Java
/*
|
|
* Copyright 2019-2025 Zheng Jie
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
package me.zhengjie.service.impl;
|
|
|
|
import com.alipay.api.AlipayClient;
|
|
import com.alipay.api.DefaultAlipayClient;
|
|
import com.alipay.api.request.AlipayTradePagePayRequest;
|
|
import com.alipay.api.request.AlipayTradeWapPayRequest;
|
|
import lombok.RequiredArgsConstructor;
|
|
import me.zhengjie.domain.vo.TradeVo;
|
|
import me.zhengjie.domain.AlipayConfig;
|
|
import me.zhengjie.exception.BadRequestException;
|
|
import me.zhengjie.repository.AliPayRepository;
|
|
import me.zhengjie.service.AliPayService;
|
|
import org.springframework.cache.annotation.CacheConfig;
|
|
import org.springframework.cache.annotation.CachePut;
|
|
import org.springframework.cache.annotation.Cacheable;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import java.util.Optional;
|
|
|
|
/**
|
|
* @author Zheng Jie
|
|
* @date 2018-12-31
|
|
*/
|
|
@Service
|
|
@RequiredArgsConstructor
|
|
@CacheConfig(cacheNames = "aliPay")
|
|
public class AliPayServiceImpl implements AliPayService {
|
|
|
|
private final AliPayRepository alipayRepository;
|
|
|
|
@Override
|
|
@Cacheable(key = "'config'")
|
|
public AlipayConfig find() {
|
|
Optional<AlipayConfig> alipayConfig = alipayRepository.findById(1L);
|
|
return alipayConfig.orElseGet(AlipayConfig::new);
|
|
}
|
|
|
|
@Override
|
|
@CachePut(key = "'config'")
|
|
@Transactional(rollbackFor = Exception.class)
|
|
public AlipayConfig config(AlipayConfig alipayConfig) {
|
|
alipayConfig.setId(1L);
|
|
return alipayRepository.save(alipayConfig);
|
|
}
|
|
|
|
@Override
|
|
public String toPayAsPc(AlipayConfig alipay, TradeVo trade) throws Exception {
|
|
|
|
if(alipay.getId() == null){
|
|
throw new BadRequestException("Please add the corresponding configuration first, then operate");
|
|
}
|
|
AlipayClient alipayClient = new DefaultAlipayClient(alipay.getGatewayUrl(), alipay.getAppId(), alipay.getPrivateKey(), alipay.getFormat(), alipay.getCharset(), alipay.getPublicKey(), alipay.getSignType());
|
|
|
|
// Create API request (desktop web version)
|
|
AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
|
|
// Return page and asynchronous notification address after order completion
|
|
request.setReturnUrl(alipay.getReturnUrl());
|
|
request.setNotifyUrl(alipay.getNotifyUrl());
|
|
// Fill order parameters
|
|
request.setBizContent("{" +
|
|
" \"out_trade_no\":\""+trade.getOutTradeNo()+"\"," +
|
|
" \"product_code\":\"FAST_INSTANT_TRADE_PAY\"," +
|
|
" \"total_amount\":"+trade.getTotalAmount()+"," +
|
|
" \"subject\":\""+trade.getSubject()+"\"," +
|
|
" \"body\":\""+trade.getBody()+"\"," +
|
|
" \"extend_params\":{" +
|
|
" \"sys_service_provider_id\":\""+alipay.getSysServiceProviderId()+"\"" +
|
|
" }"+
|
|
" }");//Fill business parameters
|
|
// Call SDK to generate form, can get URL through GET method
|
|
return alipayClient.pageExecute(request, "GET").getBody();
|
|
}
|
|
|
|
@Override
|
|
public String toPayAsWeb(AlipayConfig alipay, TradeVo trade) throws Exception {
|
|
if(alipay.getId() == null){
|
|
throw new BadRequestException("Please add the corresponding configuration first, then operate");
|
|
}
|
|
AlipayClient alipayClient = new DefaultAlipayClient(alipay.getGatewayUrl(), alipay.getAppId(), alipay.getPrivateKey(), alipay.getFormat(), alipay.getCharset(), alipay.getPublicKey(), alipay.getSignType());
|
|
|
|
double money = Double.parseDouble(trade.getTotalAmount());
|
|
double maxMoney = 5000;
|
|
if(money <= 0 || money >= maxMoney){
|
|
throw new BadRequestException("Test amount too large");
|
|
}
|
|
// Create API request (mobile web version)
|
|
AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
|
|
request.setReturnUrl(alipay.getReturnUrl());
|
|
request.setNotifyUrl(alipay.getNotifyUrl());
|
|
request.setBizContent("{" +
|
|
" \"out_trade_no\":\""+trade.getOutTradeNo()+"\"," +
|
|
" \"product_code\":\"FAST_INSTANT_TRADE_PAY\"," +
|
|
" \"total_amount\":"+trade.getTotalAmount()+"," +
|
|
" \"subject\":\""+trade.getSubject()+"\"," +
|
|
" \"body\":\""+trade.getBody()+"\"," +
|
|
" \"extend_params\":{" +
|
|
" \"sys_service_provider_id\":\""+alipay.getSysServiceProviderId()+"\"" +
|
|
" }"+
|
|
" }");
|
|
return alipayClient.pageExecute(request, "GET").getBody();
|
|
}
|
|
}
|