-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPaymentApplicationService.cs
85 lines (75 loc) · 3.03 KB
/
PaymentApplicationService.cs
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
using Stripe.Checkout;
using StripeModule.DomainManagers;
using StripeModule.DTOs;
using StripeModule.EventTransferObjects;
using StripeModule.Interfaces;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.EventBus.Distributed;
using Volo.Abp.Guids;
namespace StripeModule.ApplicationServices
{
public class PaymentApplicationService : ApplicationService, IPaymentApplicationService
{
private readonly IRepository<Payment.Payment, Guid> _repository;
private readonly IGuidGenerator _guidGenerator;
private readonly IDistributedEventBus _distributedEventBus;
public PaymentApplicationService(
IRepository<Payment.Payment, Guid> repository,
IGuidGenerator guidGenerator,
IDistributedEventBus distributedEventBus)
{
_repository = repository;
_guidGenerator = guidGenerator;
_distributedEventBus = distributedEventBus;
}
public async Task<PaymentDto> CreateAsync(string orderId, decimal amount, Currency currency)
{
var payment = new Payment.Payment(_guidGenerator.Create(), Convert.ToInt32(orderId), currency, amount);
var paymentEntity = await _repository.InsertAsync(payment);
await _repository.InsertAsync(paymentEntity);
await _distributedEventBus.PublishAsync(new PaymentCreatedEto
{
PaymentId = paymentEntity.Id,
ProductId = paymentEntity.OrderId
});
return ObjectMapper.Map<Payment.Payment, PaymentDto>(paymentEntity);
}
public async Task<string> GetCheckoutUrl(double price, string orderId, string domain, string currency = "usd")
{
var options = new SessionCreateOptions
{
LineItems = new List<SessionLineItemOptions>
{
new()
{
PriceData = new()
{
UnitAmount = Convert.ToInt16(price) * 100, // цена в центах (например, £49 = 4900)
Currency = currency,
ProductData = new()
{
Name = $"Order#{orderId??"111"}",
}
},
Quantity = 1,
},
},
Mode = "payment",
SuccessUrl = domain + $"/OrderComplete/{orderId}/{ConvertCurrencyToInt(currency)}/{price}",
CancelUrl = domain + "/OrderAbandoned"
};
var service = new SessionService();
var session = await service.CreateAsync(options);
return session.Url;
}
private int ConvertCurrencyToInt(string currency) => currency switch
{
"usd" => (int)Currency.USD,
_ => 0
};
}
}