100 lines
3.7 KiB
C#
100 lines
3.7 KiB
C#
using Apimanager_backend.Data;
|
|
using Apimanager_backend.Exceptions;
|
|
using Apimanager_backend.Models;
|
|
using AutoMapper;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Apimanager_backend.Services
|
|
{
|
|
public class PaymentConfigService : IPaymentConfigService
|
|
{
|
|
private ApiContext _context;
|
|
private ILogger<PaymentConfigService> _logger;
|
|
private IMapper _mapper;
|
|
public PaymentConfigService(ApiContext context,ILogger<PaymentConfigService> logger,IMapper mapper)
|
|
{
|
|
_context = context;
|
|
_logger = logger;
|
|
_mapper = mapper;
|
|
}
|
|
|
|
public async Task<bool> AddPaymentAsync(PaymentConfig payment)
|
|
{
|
|
bool res = await _context.paymentConfigs.AnyAsync(x => x.PayType == payment.PayType && x.Method == payment.Method);
|
|
if (res)
|
|
{
|
|
throw new BaseException(1006,"该支付方式已存在");
|
|
}
|
|
_context.paymentConfigs.Add(payment);
|
|
await _context.SaveChangesAsync();
|
|
return true;
|
|
}
|
|
|
|
public async Task<List<PaymentConfig>> GetAllPaymentAdminAsync()
|
|
{
|
|
return await _context.paymentConfigs.ToListAsync();
|
|
}
|
|
|
|
public async Task<List<PaymentConfigDto>> GetAllPaymentAsync()
|
|
{
|
|
var data = await _context.paymentConfigs.Where(x => x.IsEnabled).ToListAsync();
|
|
return _mapper.Map<List<PaymentConfigDto>>(data);
|
|
}
|
|
|
|
public Task<PaymentConfig> GetPaymentAdminAsync(PayType payType, PaymentType paymentType)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public async Task<PaymentConfig> GetPaymentConfigInfoByIdAsync(int id)
|
|
{
|
|
var payment = await _context.paymentConfigs.SingleOrDefaultAsync(x => x.Id == id);
|
|
if(payment is null)
|
|
{
|
|
throw new BaseException(1004,"未找到支付接口");
|
|
}
|
|
return payment;
|
|
}
|
|
|
|
public async Task<PaymentConfig> GetPaymentConfigInfoByTypeAsync(string payType)
|
|
{
|
|
if(!Enum.TryParse(payType, true, out PaymentType parsedPayType))
|
|
{
|
|
throw new BaseException(4001, "未配置该支付方式的通道");
|
|
}
|
|
var data = await _context.paymentConfigs.SingleOrDefaultAsync(x => x.Method == parsedPayType);
|
|
if(data == null)
|
|
{
|
|
throw new BaseException(4001, "未配置该支付方式的通道");
|
|
}
|
|
return data;
|
|
}
|
|
|
|
public async Task<bool> UpdatePaymentAsync(PaymentConfig payment)
|
|
{
|
|
var paymentConfig = await _context.paymentConfigs.FirstOrDefaultAsync(x => x.Id == payment.Id);
|
|
if(paymentConfig == null)
|
|
{
|
|
_context.paymentConfigs.Add(payment);
|
|
}
|
|
else
|
|
{
|
|
paymentConfig.AppId = payment.AppId;
|
|
paymentConfig.SecretKey = payment.SecretKey;
|
|
paymentConfig.PublicKey = payment.PublicKey;
|
|
paymentConfig.IsEnabled = payment.IsEnabled;
|
|
paymentConfig.Url = payment.Url;
|
|
paymentConfig.Description = payment.Description;
|
|
paymentConfig.ExtraSettingsJson = payment.ExtraSettingsJson;
|
|
paymentConfig.GatewayUrl = payment.GatewayUrl;
|
|
paymentConfig.PayType = payment.PayType;
|
|
paymentConfig.Method = payment.Method;
|
|
paymentConfig.NotifyUrl = payment.NotifyUrl;
|
|
_context.paymentConfigs.Update(paymentConfig);
|
|
}
|
|
await _context.SaveChangesAsync();
|
|
return true;
|
|
}
|
|
}
|
|
}
|