[Fact] // 9. async Task<string> GetPaymentId(string sessionId) public async void GetPaymentId_WithGivenSessionId_ShouldReturnPaymentId() { // Arrange var id = Guid.NewGuid().ToString(); var paymentId = Guid.NewGuid().ToString(); var toStripeAccountId = Guid.NewGuid().ToString(); var session = new StripeCheckoutSession { Id = id, PaymentId = paymentId, ToStripeAccountId = toStripeAccountId, }; await this.Context.StripeCheckoutSessions.AddAsync(session); await this.Context.SaveChangesAsync(); var service = new PaymentCommonService(this.Context, null, null, null); // Act var result = await service.GetPaymentId(session.Id); var expected = await this.Context.StripeCheckoutSessions .Where(s => s.Id == session.Id) .Select(s => s.PaymentId) .FirstOrDefaultAsync(); // Assert result.Should().Equals(expected); }
public async Task CreateCheckoutSessionAsync(string sessionId, string paymentId, string toStripeAccountId) { var session = new StripeCheckoutSession { Id = sessionId, PaymentId = paymentId, ToStripeAccountId = toStripeAccountId, }; await this.context.StripeCheckoutSessions.AddAsync(session); await this.context.SaveChangesAsync(); }
[Fact] // 7. async Task<bool> MarkPaymentAsCompletedAsync(Session session) public async void MarkPaymentAsCompletedAsync_WithGivenSession_() { // Arrange var country = CountryCreator.Create(); var city = CityCreator.Create(country.Id); var home1 = HomeCreator.CreateAny(city.Id); // rented var home2 = HomeCreator.CreateManagedHome(home1.Owner.Id, city.Id); // managed var tenant1 = UserCreator.Create("Debelin", "Dignibutov", "but4eto", "*****@*****.**"); int id1 = 1; var contract1 = ContractCreator.CreateRentalContract(id1); var rental1 = RentalCreator.Create(id1, country, city, tenant1, home1, contract1); var payment1 = PaymentCreator.CreateForTenant(home1.Owner, tenant1.Id, rental1.Id); var payment2 = PaymentCreator.CreateForManager(home1.Owner.Id, home2.Manager.Id, home2.Id); var id = Guid.NewGuid().ToString(); var toStripeAccountId = "acct_1GUy2RB3QW0Kx8nS"; var options = new SessionCreateOptions { PaymentMethodTypes = new List <string> { "card", }, LineItems = new List <SessionLineItemOptions> { new SessionLineItemOptions { Quantity = 1, Amount = (long)payment1.Amount * 100, Currency = CurrencyUSD, Name = $"Rent Payment for {DateTime.UtcNow.ToString("MMMM")}/ {DateTime.UtcNow.Year}", }, }, PaymentIntentData = new SessionPaymentIntentDataOptions { ApplicationFeeAmount = (long)((payment1.Amount * 0.01m) * 100), CaptureMethod = "automatic", Description = payment1.Id, TransferData = new SessionPaymentIntentTransferDataOptions { Destination = toStripeAccountId, }, }, SuccessUrl = "https://homy.azurewebsites.net/checkout/success?sessionId={CHECKOUT_SESSION_ID}", CancelUrl = "https://homy.azurewebsites.net/checkout/cancel", }; var service = new SessionService(); Session session = service.Create(options, new RequestOptions { ApiKey = HomyTestSecretKey, }); var sessionForDb = new StripeCheckoutSession { Id = session.Id, PaymentId = payment1.Id, ToStripeAccountId = toStripeAccountId, }; await this.Context.Countries.AddAsync(country); await this.Context.Cities.AddAsync(city); await this.Context.Homes.AddRangeAsync(home1, home2); await this.Context.Users.AddAsync(tenant1); await this.Context.Rentals.AddAsync(rental1); await this.Context.Payments.AddRangeAsync(payment1, payment2); await this.Context.Contracts.AddRangeAsync(contract1); await this.Context.StripeCheckoutSessions.AddAsync(sessionForDb); await this.Context.SaveChangesAsync(); var testService = new PaymentCommonService(this.Context, null, null, null); // Act var result = await testService.MarkPaymentAsCompletedAsync(session); bool expected = await this.Context.Payments.Where(p => p.Id == payment1.Id) .AnyAsync(p => p.Status == PaymentStatus.Complete); // Assert result.Should().Equals(expected); result.Should().BeTrue(); }