File size: 1,990 Bytes
b9c7f0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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);
            }
        }
    }
}