Spaces:
Running
Running
| using FlowAPI.Application.Interfaces; | |
| using FlowAPI.Domain.Entities; | |
| using Microsoft.Extensions.Configuration; | |
| namespace FlowAPI.Application.Services | |
| { | |
| public class PaymentService : IPaymentService | |
| { | |
| private readonly IUserRepository _userRepo; | |
| private readonly IConfiguration _config; | |
| public PaymentService(IUserRepository userRepo, IConfiguration config) | |
| { | |
| _userRepo = userRepo; | |
| _config = config; | |
| } | |
| public async Task<string> CreateCheckoutSessionAsync(Guid userId) | |
| { | |
| // Implementation with Stripe.net: | |
| // var options = new SessionCreateOptions { ... }; | |
| // var service = new SessionService(); | |
| // var session = await service.CreateAsync(options); | |
| // return session.Url; | |
| return "https://checkout.stripe.com/pay/cs_test_placeholder_" + userId; | |
| } | |
| public async Task<bool> HandleWebhookAsync(string payload, string signature) | |
| { | |
| try | |
| { | |
| // In real app, we'd verify the signature: | |
| // var stripeEvent = EventUtility.ConstructEvent(payload, signature, _config["Stripe:WebhookSecret"]); | |
| // if (stripeEvent.Type == Events.CheckoutSessionCompleted) | |
| // { | |
| // var session = stripeEvent.Data.Object as Session; | |
| // var userId = Guid.Parse(session.ClientReferenceId); | |
| // await UpgradeUserToPremium(userId); | |
| // } | |
| return true; | |
| } | |
| catch | |
| { | |
| return false; | |
| } | |
| } | |
| private async Task UpgradeUserToPremium(Guid userId) | |
| { | |
| var user = await _userRepo.GetByIdAsync(userId); | |
| if (user != null) | |
| { | |
| user.IsPremium = true; | |
| await _userRepo.UpdateAsync(user); | |
| } | |
| } | |
| } | |
| } | |