Start implementing Strip
This commit is contained in:
parent
1bf08737d2
commit
9763ef9c25
23
API/Controllers/PaymentsController.cs
Normal file
23
API/Controllers/PaymentsController.cs
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
using Core.Entities;
|
||||||
|
using Core.Interfaces;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
namespace API.Controllers
|
||||||
|
{
|
||||||
|
public class PaymentsController : BaseApiController
|
||||||
|
{
|
||||||
|
private readonly IPaymentService _paymentService;
|
||||||
|
public PaymentsController(IPaymentService paymentService)
|
||||||
|
{
|
||||||
|
_paymentService = paymentService;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Authorize]
|
||||||
|
[HttpPost("{basketId}")]
|
||||||
|
public async Task<ActionResult<CustomerBasket>> CreateOrUpdatePaymentIntent(string basketId)
|
||||||
|
{
|
||||||
|
return await _paymentService.CreateOrUpdatePaymentIntent(basketId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -7,5 +7,8 @@ namespace API.Dtos
|
|||||||
[Required]
|
[Required]
|
||||||
public string Id { get; set; }
|
public string Id { get; set; }
|
||||||
public List<BasketItemDto> Items { get; set; }
|
public List<BasketItemDto> Items { get; set; }
|
||||||
|
public int? DeliveryMethodId { get; set; }
|
||||||
|
public string ClientSecret { get; set; }
|
||||||
|
public string PaymentItentId { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -14,6 +14,7 @@ namespace API.Extensions
|
|||||||
services.AddScoped<iProductRepository, ProductRepository>();
|
services.AddScoped<iProductRepository, ProductRepository>();
|
||||||
services.AddScoped<IBasketRepository, BasketRepository>();
|
services.AddScoped<IBasketRepository, BasketRepository>();
|
||||||
services.AddScoped<IOrderService, OrderService>();
|
services.AddScoped<IOrderService, OrderService>();
|
||||||
|
services.AddScoped<IPaymentService, PaymentService>();
|
||||||
services.AddScoped<IUnitOfWork, UnitOfWork>();
|
services.AddScoped<IUnitOfWork, UnitOfWork>();
|
||||||
services.AddScoped(typeof(IGenericRepository<>), (typeof(GenericRepository<>)));
|
services.AddScoped(typeof(IGenericRepository<>), (typeof(GenericRepository<>)));
|
||||||
services.Configure<ApiBehaviorOptions>(options =>
|
services.Configure<ApiBehaviorOptions>(options =>
|
||||||
|
@ -18,5 +18,8 @@ namespace Core.Entities
|
|||||||
|
|
||||||
public string Id { get; set; }
|
public string Id { get; set; }
|
||||||
public List<BasketItem> Items { get; set; } = new List<BasketItem>();
|
public List<BasketItem> Items { get; set; } = new List<BasketItem>();
|
||||||
|
public int? DeliveryMethodId { get; set; }
|
||||||
|
public string ClientSecret { get; set; }
|
||||||
|
public string PaymentItentId { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
9
Core/Interfaces/IPaymentService.cs
Normal file
9
Core/Interfaces/IPaymentService.cs
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
using Core.Entities;
|
||||||
|
|
||||||
|
namespace Core.Interfaces
|
||||||
|
{
|
||||||
|
public interface IPaymentService
|
||||||
|
{
|
||||||
|
Task<CustomerBasket> CreateOrUpdatePaymentIntent(string basketId);
|
||||||
|
}
|
||||||
|
}
|
@ -9,6 +9,7 @@
|
|||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.5" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.5" />
|
||||||
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.17.0" />
|
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.17.0" />
|
||||||
<PackageReference Include="StackExchange.Redis" Version="2.5.61" />
|
<PackageReference Include="StackExchange.Redis" Version="2.5.61" />
|
||||||
|
<PackageReference Include="Stripe.net" Version="39.115.0" />
|
||||||
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.17.0" />
|
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.17.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
66
Infrastructure/Services/PaymentService.cs
Normal file
66
Infrastructure/Services/PaymentService.cs
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
using Core.Entities;
|
||||||
|
using Core.Entities.OrderAggregate;
|
||||||
|
using Core.Interfaces;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Stripe;
|
||||||
|
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);
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user