Skip to content

Commit

Permalink
Add PayPal Webhooks & Remove Payment Database
Browse files Browse the repository at this point in the history
Configured PaymentService to work with webhooks.
Removed all unnecessary api endpoints from PaymentService.
Removed payment storage in PaymentService.
Removed cache PaymentService.
Updated ApiGateway config to handle webhook's call.
  • Loading branch information
mqsrr committed Aug 20, 2024
1 parent 6b6dc42 commit 5015719
Show file tree
Hide file tree
Showing 42 changed files with 163 additions and 1,270 deletions.
6 changes: 1 addition & 5 deletions CoffeeSpace.AClient/Helpers/ApiEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public static class ApiEndpoints

public static class Authentication
{
private const string Base = "/api/auth";
private const string Base = "/auth";

public const string Register = $"{Base}/register";
public const string Login = $"{Base}/login";
Expand All @@ -19,14 +19,12 @@ public static class Products
private const string Base = "/products";

public const string GetAll = Base;
public const string GetById = $"{Base}/{{id}}";
}

public static class Buyer
{
private const string Base = "/buyers";

public const string GetById = $"{Base}/{{id}}";
public const string GetByEmail = $"{Base}/{{email}}";
}

Expand All @@ -36,7 +34,5 @@ public static class Orders

public const string Create = Base;
public const string GetAll = Base;
public const string Get = $"{Base}/{{id}}";
public const string Delete = $"{Base}/{{id}}";
}
}

This file was deleted.

This file was deleted.

3 changes: 1 addition & 2 deletions CoffeeSpace.AClient/Services/HubConnectionService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using CoffeeSpace.AClient.Helpers;
using CoffeeSpace.AClient.Models;
using CoffeeSpace.AClient.Services.Abstractions;
using Microsoft.AspNetCore.SignalR.Client;
Expand All @@ -18,7 +17,7 @@ internal sealed class HubConnectionService : IHubConnectionService, IAsyncDispos
public HubConnectionService()
{
_hubConnection = new HubConnectionBuilder()
.WithUrl($"{ApiEndpoints.BaseAddress}/ordering-hub")
.WithUrl($"http://localhost:5245/ordering-hub")
.WithAutomaticReconnect()
.Build();
}
Expand Down
56 changes: 33 additions & 23 deletions CoffeeSpace.AClient/ViewModels/CartWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Threading;
using CoffeeSpace.AClient.Contracts.Requests.Orders;
using CoffeeSpace.AClient.Mappers;
using CoffeeSpace.AClient.Messages.Commands;
using CoffeeSpace.AClient.Models;
using CoffeeSpace.AClient.RefitClients;
using CoffeeSpace.AClient.Services;
using CoffeeSpace.AClient.Services.Abstractions;
using CoffeeSpace.AClient.Views;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Mediator;
using Microsoft.Extensions.DependencyInjection;
using SukiUI.Controls;

Expand All @@ -23,27 +22,27 @@ namespace CoffeeSpace.AClient.ViewModels;
public sealed partial class CartWindowViewModel : ViewModelBase
{
private readonly IOrderingWebApi _orderingWebApi;
private readonly ISender _sender;
private readonly IHubConnectionService _hubConnectionService;
private readonly OrderHistoryWindowViewModel _historyWindowViewModel;

[ObservableProperty]
private ObservableCollection<Product> _cartProducts;

[ObservableProperty]
private CreateAddressRequest _address = new CreateAddressRequest();

public CartWindowViewModel(IOrderingWebApi orderingWebApi, ISender sender, IHubConnectionService hubConnectionService)
public CartWindowViewModel(IOrderingWebApi orderingWebApi, IHubConnectionService hubConnectionService, OrderHistoryWindowViewModel historyWindowViewModel)
{
_orderingWebApi = orderingWebApi;
_sender = sender;
_hubConnectionService = hubConnectionService;

_historyWindowViewModel = historyWindowViewModel;

CartProducts = new ObservableCollection<Product>();
}

public CartWindowViewModel()
public CartWindowViewModel(OrderHistoryWindowViewModel historyWindowViewModel)
{
_sender = null!;
_historyWindowViewModel = historyWindowViewModel;
_orderingWebApi = null!;
_hubConnectionService = null!;

Expand All @@ -57,23 +56,30 @@ public async Task InitializeAsync(CancellationToken cancellationToken)
await _hubConnectionService.StartConnectionAsync(StaticStorage.Buyer!.Id, cancellationToken);
}

_hubConnectionService.OnOrderCreated(_ =>
Dispatcher.UIThread.Post(async () =>
await SukiHost.ShowToast("Order Has Been Created", "You will receive payment request in a few seconds.")));
_hubConnectionService.OnOrderCreated(order =>
Dispatcher.UIThread.Post(async () =>
{
_historyWindowViewModel.Orders.Add(order);
ClearCartValues();
await SukiHost.ShowToast("Order Has Been Created", "You will receive payment request in a few seconds.");
}));

_hubConnectionService.OnOrderPaymentPageInitialized((_, paymentUri) =>
{
Dispatcher.UIThread.Post(() =>
{
var paymentView = App.Services.GetRequiredService<PaymentView>();
paymentView.WebView.Url = new Uri(paymentUri);
paymentView.WebView.WebMessageReceived += (_, args) =>
paymentView.WebView.NavigationStarting += async (_, arg) =>
{
if (args.Source.Port == 8085)
if (arg.Url!.Port == 8085)
{
await SukiHost.ShowToast("Success", "Order has been paid!");
paymentView.Close();
}
};
paymentView.Show();
});
});
Expand All @@ -96,16 +102,6 @@ private async Task CreateOrder(CancellationToken cancellationToken)
return;
}

await _sender.Send(new CreateOrderHistoryCommand
{
Order = createdOrderResponse.Content!
}, cancellationToken);

CartProducts.Clear();

Address.City = string.Empty;
Address.Country = string.Empty;
Address.Street = string.Empty;
}

[RelayCommand]
Expand All @@ -116,4 +112,18 @@ private Task RemoveFromCart(Product product)

return Task.CompletedTask;
}

private void ClearCartValues()
{
CartProducts.Clear();

Address.Country = string.Empty;
Address.City = string.Empty;
Address.Street = string.Empty;

var cartView = App.Services.GetRequiredService<CartView>();
cartView.GetControl<TextBox>("CountryTextBox").Clear();
cartView.GetControl<TextBox>("CityTextBox").Clear();
cartView.GetControl<TextBox>("StreetTextBox").Clear();
}
}
6 changes: 0 additions & 6 deletions CoffeeSpace.AClient/ViewModels/FindMyCoffeeViewModel.cs

This file was deleted.

28 changes: 11 additions & 17 deletions CoffeeSpace.AClient/ViewModels/OrderHistoryWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Avalonia.Threading;
using CoffeeSpace.AClient.Models;
using CoffeeSpace.AClient.Services;
using CoffeeSpace.AClient.Services.Abstractions;
Expand Down Expand Up @@ -36,26 +37,19 @@ internal async Task InitializeAsync(CancellationToken cancellationToken)
await _hubConnectionService.StartConnectionAsync(StaticStorage.Buyer!.Id, cancellationToken);
}

_hubConnectionService.OnOrderCreated(order =>
{
bool isContains = Orders.Contains(order);
if (!isContains)
{
Orders.Add(order);
}
});

_hubConnectionService.OnOrderCreated(order => Orders.Add(order));
_hubConnectionService.OnOrderStatusUpdated((status, orderId) =>
{
var orderToUpdate = Orders.FirstOrDefault(order => order.Id == orderId);
if (orderToUpdate is null)
Dispatcher.UIThread.Post(() =>
{
return;
}
orderToUpdate.Status = status;
Orders.Remove(orderToUpdate);
Orders.Add(orderToUpdate);
var orderToUpdate = Orders.FirstOrDefault(order => order.Id == orderId);
if (orderToUpdate is null)
{
return;
}
orderToUpdate.Status = status;
});
});
}
}
11 changes: 7 additions & 4 deletions CoffeeSpace.AClient/Views/CartView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,18 @@
ColumnDefinitions="170,170, *"
RowDefinitions="Auto,Auto,Auto,Auto">
<TextBox Margin="5" Watermark="Country"
Text="{Binding Address.Country}" />
Text="{Binding Address.Country}"
x:Name="CountryTextBox"/>

<TextBox Grid.Row="0" Grid.Column="1"
Margin="5" Watermark="City"
Text="{Binding Address.City}" />
Text="{Binding Address.City}"
x:Name="CityTextBox"/>

<TextBox Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"
Margin="5" Watermark="Street"
Text="{Binding Address.Street}" />
Margin="5" Watermark="Street"
Text="{Binding Address.Street}"
x:Name="StreetTextBox"/>

<controls:BusyArea x:Name="BusyArea" IsBusy="{Binding CreateOrderCommand.IsRunning}"
Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2">
Expand Down
4 changes: 0 additions & 4 deletions CoffeeSpace.AClient/Views/CartView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Avalonia.Controls;
using CoffeeSpace.AClient.ViewModels;
using Microsoft.Extensions.DependencyInjection;
using SukiUI.Controls;

namespace CoffeeSpace.AClient.Views;

Expand All @@ -15,9 +14,6 @@ public CartView()
InitializeComponent();
_viewModel = App.Services.GetRequiredService<CartWindowViewModel>();
DataContext = _viewModel;

var paymentView = App.Services.GetRequiredService<PaymentView>();
paymentView.Closed += async (_, _) => await SukiHost.ShowToast("Success", "Order has been paid!");
}

protected override async void OnInitialized()
Expand Down
7 changes: 1 addition & 6 deletions CoffeeSpace.AClient/Views/DashboardView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
xmlns:vm="using:CoffeeSpace.AClient.ViewModels"
xmlns:converters="clr-namespace:CoffeeSpace.AClient.Converters"
xmlns:controls="clr-namespace:SukiUI.Controls;assembly=SukiUI"
xmlns:theme="clr-namespace:SukiUI.Theme;assembly=SukiUI"
mc:Ignorable="d" d:DesignWidth="1280" d:DesignHeight="650"
x:DataType="vm:DashboardWindowViewModel"
x:Class="CoffeeSpace.AClient.Views.DashboardView"
Expand All @@ -26,11 +25,7 @@
ColumnDefinitions="Auto,*" Margin="10">
<Label Grid.Row="0" Grid.Column="0" Padding="20"
Content="{Binding Buyer.Name, StringFormat='Welcome, {0}!'}" FontSize="40" FontWeight="SemiBold" />

<TextBox Grid.Row="1" Grid.Column="0"
Margin="20 10 0 0" UseFloatingWatermark="True" Watermark="Search Coffee"
theme:TextBoxExtensions.AddDeleteButton="True" Width="300" HorizontalAlignment="Left"/>


<ScrollViewer Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" Margin="10 20 0 0">
<ItemsControl ItemsSource="{Binding Products}">
<ItemsControl.ItemsPanel>
Expand Down
4 changes: 2 additions & 2 deletions CoffeeSpace.AClient/Views/DashboardView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public sealed partial class DashboardView : UserControl
{
private readonly DashboardWindowViewModel _viewModel;
private readonly ProductDetailsView _productDetailsView;

public DashboardView()
{
InitializeComponent();
Expand All @@ -31,7 +31,7 @@ private void OpenProductDetailsView(object? sender, TappedEventArgs e)
{
var glassCard = sender as GlassCard;
var selectedProduct = glassCard!.DataContext as Product;

_productDetailsView.SetNewProduct(selectedProduct!);
SukiHost.ShowDialog(_productDetailsView, allowBackgroundClose: true);
}
Expand Down
28 changes: 3 additions & 25 deletions CoffeeSpace.ApiGateway/ocelot.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,28 +70,6 @@
"TimeoutValue": 6500
}
},
{
"UpstreamPathTemplate": "/paged-products",
"UpstreamHttpMethod": [ "GET" ],

"DownstreamPathTemplate": "/api/paged-products",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "products-api",
"Port": 8082
}
],
"AuthenticationOptions": {
"AuthenticationProviderKey": "Bearer",
"AllowedScopes": []
},
"QoSOptions": {
"ExceptionsAllowedBeforeBreaking": 3,
"DurationOfBreak": 1000,
"TimeoutValue": 6500
}
},
{
"UpstreamPathTemplate": "/products/{id:guid}",
"UpstreamHttpMethod": [ "GET", "DELETE", "PUT" ],
Expand Down Expand Up @@ -292,10 +270,10 @@
}
},
{
"UpstreamPathTemplate": "/application-orders/{orderId:guid}",
"UpstreamHttpMethod": [ "GET" ],
"UpstreamPathTemplate": "/",
"UpstreamHttpMethod": [ "POST" ],

"DownstreamPathTemplate": "/api/application-orders/{orderId:guid}",
"DownstreamPathTemplate": "/",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
Expand Down
Loading

0 comments on commit 5015719

Please sign in to comment.