-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathStripeController.cs
More file actions
118 lines (108 loc) · 4.85 KB
/
StripeController.cs
File metadata and controls
118 lines (108 loc) · 4.85 KB
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
using System;
using System.Globalization;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using SimplCommerce.Infrastructure.Data;
using SimplCommerce.Infrastructure.Helpers;
using SimplCommerce.Module.Checkouts.Services;
using SimplCommerce.Module.Core.Extensions;
using SimplCommerce.Module.Core.Services;
using SimplCommerce.Module.Orders.Models;
using SimplCommerce.Module.Orders.Services;
using SimplCommerce.Module.Payments.Models;
using SimplCommerce.Module.PaymentStripe.Areas.PaymentStripe.ViewModels;
using SimplCommerce.Module.PaymentStripe.Models;
using SimplCommerce.Module.ShoppingCart.Services;
using Stripe;
namespace SimplCommerce.Module.PaymentStripe.Areas.PaymentStripe.Controllers
{
[Area("PaymentStripe")]
[ApiExplorerSettings(IgnoreApi = true)]
public class StripeController : Controller
{
private readonly ICheckoutService _checkoutService;
private readonly IOrderService _orderService;
private readonly IWorkContext _workContext;
private readonly IRepositoryWithTypedId<PaymentProvider, string> _paymentProviderRepository;
private readonly IRepository<Payment> _paymentRepository;
private readonly ICurrencyService _currencyService;
public StripeController(
ICheckoutService checkoutService,
IOrderService orderService,
IWorkContext workContext,
IRepositoryWithTypedId<PaymentProvider, string> paymentProviderRepository,
IRepository<Payment> paymentRepository,
ICurrencyService currencyService)
{
_checkoutService = checkoutService;
_orderService = orderService;
_workContext = workContext;
_paymentProviderRepository = paymentProviderRepository;
_paymentRepository = paymentRepository;
_currencyService = currencyService;
}
public async Task<IActionResult> Charge(string stripeEmail, string stripeToken)
{
var stripeProvider = await _paymentProviderRepository.Query().FirstOrDefaultAsync(x => x.Id == PaymentProviderHelper.StripeProviderId);
var stripeSetting = JsonConvert.DeserializeObject<StripeConfigForm>(stripeProvider.AdditionalSettings);
StripeConfiguration.ApiKey = stripeSetting.PrivateKey;
var stripeChargeService = new ChargeService();
var currentUser = await _workContext.GetCurrentUser();
//TODO: pass checkout Id here
var cart = await _checkoutService.GetCheckoutDetails(Guid.Empty);
if (cart == null)
{
return NotFound();
}
var checkoutId = Guid.NewGuid();
var orderCreationResult = await _orderService.CreateOrder(checkoutId, "Stripe", 0, OrderStatus.PendingPayment);
if(!orderCreationResult.Success)
{
TempData["Error"] = orderCreationResult.Error;
return Redirect("~/checkout/payment");
}
var order = orderCreationResult.Value;
var zeroDecimalOrderAmount = order.OrderTotal;
if(!CurrencyHelper.IsZeroDecimalCurrencies(_currencyService.CurrencyCulture))
{
zeroDecimalOrderAmount = zeroDecimalOrderAmount * 100;
}
var regionInfo = new RegionInfo(_currencyService.CurrencyCulture.LCID);
var payment= new Payment()
{
OrderId = order.Id,
Amount = order.OrderTotal,
PaymentMethod = "Stripe",
CreatedOn = DateTimeOffset.UtcNow
};
try
{
var charge = stripeChargeService.Create(new ChargeCreateOptions
{
Amount = (int)zeroDecimalOrderAmount,
Description = "Sample Charge",
Currency = regionInfo.ISOCurrencySymbol,
Source = stripeToken
});
payment.GatewayTransactionId = charge.Id;
payment.Status = PaymentStatus.Succeeded;
order.OrderStatus = OrderStatus.PaymentReceived;
_paymentRepository.Add(payment);
await _paymentRepository.SaveChangesAsync();
return Redirect($"~/checkout/success?orderId={order.Id}");
}
catch(StripeException ex)
{
payment.Status = PaymentStatus.Failed;
payment.FailureMessage = ex.StripeError.Message;
order.OrderStatus = OrderStatus.PaymentFailed;
_paymentRepository.Add(payment);
await _paymentRepository.SaveChangesAsync();
TempData["Error"] = ex.StripeError.Message;
return Redirect($"~/checkout/error?orderId={order.Id}");
}
}
}
}