File size: 5,995 Bytes
e4e48f1
6db0915
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4e48f1
 
6db0915
 
 
 
 
 
 
 
e4e48f1
 
 
6db0915
 
 
 
 
 
 
 
e4e48f1
 
6db0915
 
 
 
 
 
e4e48f1
6db0915
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4e48f1
6db0915
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4e48f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6db0915
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using ECommerce.Model.Entities;
using ECommerce.Model.Repositories;
using ECommerce.Presenter.Presenters;
using ECommerce.Presenter.ViewModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using System.Text.Json;

namespace Ecommerce_web_project.Controllers;

public class CheckoutController : Controller
{
    private readonly CheckoutPresenter _checkoutPresenter;
    private readonly CartPresenter _cartPresenter;
    private readonly OrderPresenter _orderPresenter;
    private readonly ICustomerRepository _customerRepo;
    private readonly UserManager<IdentityUser> _userManager;
    private readonly IEmailSender _emailSender;
    private readonly IConfiguration _config;
    private readonly IRepository<Cart> _cartRepo;
    private readonly IRepository<CartItem> _cartItemRepo;

    public CheckoutController(
        CheckoutPresenter checkoutPresenter,
        CartPresenter cartPresenter,
        OrderPresenter orderPresenter,
        ICustomerRepository customerRepo,
        UserManager<IdentityUser> userManager,
        IEmailSender emailSender,
        IConfiguration config,
        IRepository<Cart> cartRepo,
        IRepository<CartItem> cartItemRepo)
    {
        _checkoutPresenter = checkoutPresenter;
        _cartPresenter = cartPresenter;
        _orderPresenter = orderPresenter;
        _customerRepo = customerRepo;
        _userManager = userManager;
        _emailSender = emailSender;
        _config = config;
        _cartRepo = cartRepo;
        _cartItemRepo = cartItemRepo;
    }

    [HttpGet]
    [Authorize]
    public async Task<IActionResult> Index()
    {
        var sessionItems = await GetCartAsync();
        if (sessionItems.Count == 0)
            return RedirectToAction("Index", "Cart");

        string? customerId = null;
        if (User.Identity?.IsAuthenticated == true)
        {
            var user = await _userManager.GetUserAsync(User);
            if (user != null)
            {
                var customer = await _customerRepo.GetByUserIdAsync(user.Id);
                if (customer != null)
                    customerId = customer.Id.ToString();
            }
        }

        var model = await _checkoutPresenter.PrepareCheckoutAsync(customerId, sessionItems);
        ViewBag.StripePublishableKey = _config["Stripe:PublishableKey"];
        return View(model);
    }

    [HttpPost]
    [Authorize]
    public async Task<IActionResult> PlaceOrder(string street, string city, string state, string zipCode, string country, string paymentIntentId)
    {
        var sessionItems = await GetCartAsync();
        if (sessionItems.Count == 0)
            return RedirectToAction("Index", "Cart");

        string? customerId = null;
        if (User.Identity?.IsAuthenticated == true)
        {
            var user = await _userManager.GetUserAsync(User);
            if (user != null)
            {
                var customer = await _customerRepo.GetByUserIdAsync(user.Id);
                if (customer != null)
                    customerId = customer.Id.ToString();
            }
        }

        var request = new CheckoutRequest
        {
            Street = street,
            City = city,
            State = state,
            ZipCode = zipCode,
            Country = country
        };

        var order = await _checkoutPresenter.PlaceOrderAsync(customerId, sessionItems, request, paymentIntentId);
        if (order == null)
        {
            TempData["Error"] = "Failed to place order.";
            return RedirectToAction("Index");
        }

        HttpContext.Session.Remove("Cart");

        var userEmail = User.Identity?.IsAuthenticated == true
            ? (await _userManager.GetUserAsync(User))?.Email
            : null;
        if (userEmail != null)
        {
            var itemsHtml = string.Join("", order.Items.Select(i =>
                $"<tr><td>{i.ProductName}</td><td>{i.Quantity}</td><td>${i.Subtotal:F2}</td></tr>"));
            var body = $"""
                <h2>Order Confirmed — {order.OrderNumber}</h2>
                <p>Thank you for your purchase!</p>
                <table border="1" cellpadding="8" cellspacing="0" style="border-collapse:collapse;width:100%">
                    <thead><tr><th>Item</th><th>Qty</th><th>Total</th></tr></thead>
                    <tbody>{itemsHtml}</tbody>
                </table>
                <p><strong>Total: ${order.TotalAmount:F2}</strong></p>
                <p>Your order will be processed shortly.</p>
                """;
            try { await _emailSender.SendEmailAsync(userEmail, $"Order Confirmed — {order.OrderNumber}", body); }
            catch { }
        }

        return RedirectToAction("Confirmation", new { id = order.Id });
    }

    [HttpGet]
    public async Task<IActionResult> Confirmation(Guid id)
    {
        var order = await _orderPresenter.GetOrderByIdAsync(id);
        if (order == null) return NotFound();
        return View(order);
    }

    private async Task<List<CartItemSession>> GetCartAsync()
    {
        if (User.Identity?.IsAuthenticated == true)
        {
            var user = await _userManager.GetUserAsync(User);
            if (user != null)
            {
                var customer = await _customerRepo.GetByUserIdAsync(user.Id);
                if (customer != null)
                {
                    var dbCart = (await _cartRepo.FindAsync(c => c.CustomerId == customer.Id)).FirstOrDefault();
                    if (dbCart != null)
                    {
                        var dbItems = await _cartItemRepo.FindAsync(i => i.CartId == dbCart.Id);
                        return dbItems.Select(i => new CartItemSession { ProductId = i.ProductId, Quantity = i.Quantity }).ToList();
                    }
                }
            }
        }
        return JsonSerializer.Deserialize<List<CartItemSession>>(HttpContext.Session.GetString("Cart") ?? "[]") ?? new();
    }
}