2022-05-27 15:40:30 -07:00
|
|
|
using Core.Entities;
|
|
|
|
using Core.Entities.OrderAggregate;
|
|
|
|
using Core.Interfaces;
|
2022-05-31 11:38:23 -07:00
|
|
|
using Core.Specifications;
|
2022-05-27 15:40:30 -07:00
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
using Stripe;
|
2022-05-31 11:38:23 -07:00
|
|
|
using Order = Core.Entities.OrderAggregate.Order;
|
2022-05-27 15:40:30 -07:00
|
|
|
using Product = Core.Entities.Product;
|
|
|
|
|
|
|
|
namespace Infrastructure.Services
|
|
|
|
{
|
|
|
|
public class PaymentService : IPaymentService
|
|
|
|
{
|
|
|
|
private readonly IBasketRepository _basketRepository;
|
|
|
|
private readonly IUnitOfWork _unitOfWork;
|
|
|
|
private readonly IConfiguration _config;
|
|
|
|
public PaymentService(IBasketRepository basketRepository, IUnitOfWork unitOfWork, IConfiguration config)
|
|
|
|
{
|
|
|
|
_config = config;
|
|
|
|
_unitOfWork = unitOfWork;
|
|
|
|
_basketRepository = basketRepository;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<CustomerBasket> CreateOrUpdatePaymentIntent(string basketId)
|
|
|
|
{
|
|
|
|
StripeConfiguration.ApiKey = _config["StripeSettings:SecretKey"];
|
|
|
|
var basket = await _basketRepository.GetBasketAsync(basketId);
|
2022-05-30 13:25:27 -07:00
|
|
|
if(basket == null) return null;
|
2022-05-27 15:40:30 -07:00
|
|
|
var shippingPrice = 0m;
|
|
|
|
|
|
|
|
if(basket.DeliveryMethodId.HasValue){
|
|
|
|
var deliveryMethod = await _unitOfWork.Repository<DeliveryMethod>().GetByIdAsync((int)basket.DeliveryMethodId);
|
|
|
|
shippingPrice = deliveryMethod.Price;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (var item in basket.Items)
|
|
|
|
{
|
|
|
|
var productItem = await _unitOfWork.Repository<Product>().GetByIdAsync(item.Id);
|
|
|
|
if(item.Price != productItem.Price){
|
|
|
|
item.Price = productItem.Price;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var service = new PaymentIntentService();
|
|
|
|
PaymentIntent intent;
|
|
|
|
if(string.IsNullOrEmpty(basket.PaymentItentId))
|
|
|
|
{
|
|
|
|
var options = new PaymentIntentCreateOptions
|
|
|
|
{
|
|
|
|
Amount = (long) basket.Items.Sum(i => i.Quantity * (i.Price * 100)) + (long) shippingPrice * 100,
|
|
|
|
Currency = "usd",
|
|
|
|
PaymentMethodTypes = new List<string>{"card"}
|
|
|
|
};
|
|
|
|
intent = await service.CreateAsync(options);
|
|
|
|
basket.PaymentItentId = intent.Id;
|
|
|
|
basket.ClientSecret = intent.ClientSecret;
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
var options = new PaymentIntentUpdateOptions
|
|
|
|
{
|
|
|
|
Amount = (long) basket.Items.Sum(i => i.Quantity * (i.Price * 100)) + (long) shippingPrice * 100,
|
|
|
|
};
|
|
|
|
await service.UpdateAsync(basket.PaymentItentId, options);
|
|
|
|
}
|
|
|
|
await _basketRepository.UpdateBasketAsync(basket);
|
|
|
|
return basket;
|
|
|
|
}
|
2022-05-31 11:38:23 -07:00
|
|
|
|
|
|
|
public async Task<Core.Entities.OrderAggregate.Order> UpdateOrderPaymentFailed(string paymentIntentId)
|
|
|
|
{
|
|
|
|
var spec = new OrderByPaymentIntentIdSpecification(paymentIntentId);
|
|
|
|
var order = await _unitOfWork.Repository<Order>().GetEntityWithSpec(spec);
|
|
|
|
|
|
|
|
if(order == null) return null;
|
|
|
|
|
|
|
|
order.Status = OrderStatus.PaymentFailed;
|
|
|
|
await _unitOfWork.Complete();
|
|
|
|
return order;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<Core.Entities.OrderAggregate.Order> UpdateOrderPaymentSucceeeded(string paymentIntentId)
|
|
|
|
{
|
|
|
|
var spec = new OrderByPaymentIntentIdSpecification(paymentIntentId);
|
|
|
|
var order = await _unitOfWork.Repository<Order>().GetEntityWithSpec(spec);
|
|
|
|
|
|
|
|
if(order == null) return null;
|
|
|
|
|
|
|
|
order.Status = OrderStatus.PaymentReceived;
|
|
|
|
_unitOfWork.Repository<Order>().Update(order);
|
|
|
|
await _unitOfWork.Complete();
|
|
|
|
return order;
|
|
|
|
}
|
2022-05-27 15:40:30 -07:00
|
|
|
}
|
|
|
|
}
|