Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supporting currency in payment and registration #46

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
xmlns:local="clr-namespace:Portalum.Zvt.ControlPanel.Dialogs"
mc:Ignorable="d"
Title="change by code"
Height="223"
Height="243"
Width="425"
ResizeMode="NoResize"
WindowStartupLocation="CenterOwner"
Expand All @@ -23,8 +23,8 @@
<CheckBox x:Name="CheckBoxSendIntermediateStatusInformation" IsChecked="True" Content="Send Intermediate Status information" HorizontalAlignment="Left" Margin="26,65,0,0" VerticalAlignment="Top" Checked="CheckBoxChanged" Unchecked="CheckBoxChanged" Foreground="White"/>
<CheckBox x:Name="CheckBoxAllowStartPaymentViaPaymentTerminal" Content="Allow start payment via Payment Terminal" HorizontalAlignment="Left" Margin="26,85,0,0" VerticalAlignment="Top" Checked="CheckBoxChanged" Unchecked="CheckBoxChanged" Foreground="White"/>
<CheckBox x:Name="CheckBoxAllowAdministrationViaPaymentTerminal" Content="Allow administration via Payment Terminal" HorizontalAlignment="Left" Margin="26,105,0,0" VerticalAlignment="Top" Checked="CheckBoxChanged" Unchecked="CheckBoxChanged" Foreground="White"/>
<Button Style="{StaticResource BaseButtonStyle}" x:Name="ButtonSave" Content="Save" Margin="10,155,10,0" VerticalAlignment="Top" Click="ButtonSave_Click" Height="29"/>
<Button Style="{StaticResource BaseButtonStyle}" x:Name="ButtonSave" Content="Save" Margin="10,174,10,0" VerticalAlignment="Top" Click="ButtonSave_Click" Height="29"/>
<CheckBox x:Name="CheckBoxActivateTlvSupport" Content="Activate TLV Support" HorizontalAlignment="Left" Margin="26,124,0,0" VerticalAlignment="Top" IsChecked="True" Foreground="White"/>

<ComboBox x:Name="Currency" HorizontalAlignment="Stretch" Margin="26,144,26,0" VerticalAlignment="Top"></ComboBox>
</Grid>
</Window>
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
using System.Windows;
using Portalum.Zvt.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows;

namespace Portalum.Zvt.ControlPanel.Dialogs
{
Expand All @@ -9,10 +14,19 @@ public partial class RegistrationConfigurationDialog : Window
{
public RegistrationConfig RegistrationConfig { get; private set; }

public IEnumerable<CurrencyCodeIso4217> CurrencyCodes => Enum.GetValues<CurrencyCodeIso4217>().AsEnumerable();

public RegistrationConfigurationDialog()
{
this.InitializeComponent();

this.Currency.ItemsSource = this.CurrencyCodes;
this.Currency.SelectedItem = CurrencyCodeIso4217.EUR;

this.UpdateRegistrationConfig();



}

private void UpdateRegistrationConfig()
Expand Down Expand Up @@ -42,14 +56,16 @@ private void UpdateRegistrationConfig()
return;
}


this.RegistrationConfig = new RegistrationConfig
{
AllowAdministrationViaPaymentTerminal = this.CheckBoxAllowAdministrationViaPaymentTerminal.IsChecked.Value,
AllowStartPaymentViaPaymentTerminal = this.CheckBoxAllowStartPaymentViaPaymentTerminal.IsChecked.Value,
ReceiptPrintoutForAdministrationFunctionsViaPaymentTerminal = this.CheckBoxReceiptPrintoutForAdministrationFunctionsViaPaymentTerminal.IsChecked.Value,
ReceiptPrintoutForPaymentFunctionsViaPaymentTerminal = this.CheckBoxReceiptPrintoutForPaymentFunctionsViaPaymentTerminal.IsChecked.Value,
SendIntermediateStatusInformation = this.CheckBoxSendIntermediateStatusInformation.IsChecked.Value,
ActivateTlvSupport = this.CheckBoxActivateTlvSupport.IsChecked.Value
ActivateTlvSupport = this.CheckBoxActivateTlvSupport.IsChecked.Value,
Currency = (CurrencyCodeIso4217)this.Currency.SelectedItem
};

this.SetTitle();
Expand Down
3 changes: 2 additions & 1 deletion src/Portalum.Zvt.ControlPanel/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@
</GroupBox>
<GroupBox Template="{StaticResource GroupBoxWithHeader}" Header="Payment" Foreground="White" Margin="0,162,0,0" HorizontalAlignment="Left" Width="307" Height="148" VerticalAlignment="Top">
<Grid>
<TextBox x:Name="TextBoxAmount" HorizontalAlignment="Left" Margin="10,22,0,0" Text="0" TextWrapping="Wrap" Width="111" TextAlignment="Right" FontSize="20" Height="29" VerticalAlignment="Top"/>
<TextBox x:Name="TextBoxAmount" HorizontalAlignment="Left" Margin="10,22,0,0" Text="0" TextWrapping="Wrap" Width="50" TextAlignment="Right" FontSize="20" Height="29" VerticalAlignment="Top"/>
<ComboBox x:Name="Currency" HorizontalAlignment="Left" Margin="67,22,0,0" Text="0" Width="52" FontSize="12" Height="29" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" VerticalAlignment="Top"></ComboBox>
<Button Style="{StaticResource BaseButtonStyle}" x:Name="ButtonPay" Content="Pay" HorizontalAlignment="Left" Margin="126,22,0,0" VerticalAlignment="Top" Click="ButtonPay_Click" Height="29" Width="80"/>
<Button Style="{StaticResource BaseButtonStyle}" x:Name="ButtonRefund" Content="Refund" HorizontalAlignment="Left" Margin="211,22,0,0" VerticalAlignment="Top" Click="ButtonRefund_Click" Width="80" Height="29"/>
<TextBox x:Name="TextBoxReceiptNumber" HorizontalAlignment="Left" Text="0000" TextWrapping="Wrap" VerticalAlignment="Top" Width="111" TextAlignment="Right" FontSize="20" Margin="10,69,0,0"/>
Expand Down
7 changes: 6 additions & 1 deletion src/Portalum.Zvt.ControlPanel/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -30,6 +31,7 @@ public partial class MainWindow : Window
private readonly StringBuilder _printLineCache;
private readonly DeviceConfiguration _deviceConfiguration;
private CancellationTokenSource _cancellationTokenSource;
public IEnumerable<CurrencyCodeIso4217> CurrencyCodes => Enum.GetValues<CurrencyCodeIso4217>().AsEnumerable();

public MainWindow(DeviceConfiguration deviceConfiguration)
{
Expand All @@ -46,6 +48,9 @@ public MainWindow(DeviceConfiguration deviceConfiguration)

this.TextBoxAmount.Text = $"{this.CreateRandomAmount()}";

this.Currency.ItemsSource = this.CurrencyCodes;
this.Currency.SelectedItem = CurrencyCodeIso4217.EUR;

CodePagesEncodingProvider.Instance.GetEncoding(437);
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

Expand Down Expand Up @@ -589,7 +594,7 @@ private async Task PaymentAsync(decimal amount)
this.AddCommandInfo("Payment/Authorization (06 01)");

this.ButtonPay.IsEnabled = false;
var commandResponse = await this._zvtClient?.PaymentAsync(amount, this._cancellationTokenSource.Token);
var commandResponse = await this._zvtClient?.PaymentAsync(amount, (CurrencyCodeIso4217)this.Currency.SelectedItem, this._cancellationTokenSource.Token);
this.ProcessCommandRespone(commandResponse);
this.ButtonPay.IsEnabled = true;
}
Expand Down
77 changes: 77 additions & 0 deletions src/Portalum.Zvt.UnitTest/ZvtClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -572,5 +572,82 @@ public async Task PaymentAsync_IssueOfGoods_RejectedCard_Successful()

Assert.AreEqual(CommandResponseState.Abort, commandResponse.State);
}

[TestMethod]
public async Task PaymentAsync_WithCurrency_Successful()
{
var loggerZvtClient = LoggerHelper.GetLogger<ZvtClient>();
var mockDeviceCommunication = new Mock<IDeviceCommunication>();

StatusInformation receivedStatusInformation = null;

void statusInformationReceived(StatusInformation statusInformation)
{
receivedStatusInformation = statusInformation;
}

var dataSent = Array.Empty<byte>();
var dataSentCancellationTokenSource = new CancellationTokenSource();

mockDeviceCommunication
.Setup(c => c.SendAsync(It.IsAny<byte[]>(), It.IsAny<CancellationToken>()))
.ReturnsAsync((byte[] data, CancellationToken cancellationToken) =>
{
dataSent = data;
dataSentCancellationTokenSource?.Cancel();
return true;
});

var startAsyncCompletionCalled = false;
var clientConfig = new ZvtClientConfig
{
CommandCompletionTimeout = TimeSpan.FromSeconds(5)
};

var zvtClient = new ZvtClient(mockDeviceCommunication.Object, loggerZvtClient.Object, clientConfig);
zvtClient.StatusInformationReceived += statusInformationReceived;
zvtClient.CompletionStartReceived += (_) => startAsyncCompletionCalled = true;

var paymentTask = zvtClient.PaymentAsync(10, CurrencyCodeIso4217.USD);
await Task.Delay(500, dataSentCancellationTokenSource.Token).ContinueWith(_ => { });

dataSentCancellationTokenSource.Dispose();
dataSentCancellationTokenSource = new CancellationTokenSource();

// ensure the timeout is not set, when nothing is passed to PaymentAsync
CollectionAssert.AreEqual(new byte[] { 0x06, 0x01, 0xA, 0x04, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x49, 0x08, 0x40 }, dataSent, $"Invalid Payment command");

dataSent = Array.Empty<byte>();

var paymentTerminalPositiveCompletion = new byte[] { 0x80, 0x00, 0x00 };
mockDeviceCommunication.Raise(mock => mock.DataReceived += null, paymentTerminalPositiveCompletion);

var paymentTerminalStatusInformation = new byte[] { 0x04, 0x0F, 0x02, 0x27, 0x00 };
mockDeviceCommunication.Raise(mock => mock.DataReceived += null, paymentTerminalStatusInformation);
await Task.Delay(3000, dataSentCancellationTokenSource.Token).ContinueWith(_ => { });

Assert.IsNotNull(receivedStatusInformation, "No StatusInformation received");
receivedStatusInformation = null;

// check that the ECR answers immediately, as no issueGoodsCallback is set
var electronicCashRegisterCommandCompletionForStatusInformation = new byte[] { 0x80, 0x00, 0x00 };
CollectionAssert.AreEqual(electronicCashRegisterCommandCompletionForStatusInformation, dataSent, $"Collection is wrong {BitConverter.ToString(dataSent)}");

dataSentCancellationTokenSource.Dispose();
dataSentCancellationTokenSource = new CancellationTokenSource();

mockDeviceCommunication.Raise(mock => mock.DataReceived += null, new byte[] { 0x06, 0x0F, 0x00 });
await Task.Delay(3000, dataSentCancellationTokenSource.Token).ContinueWith(_ => { });

var commandResponse = await paymentTask;
Assert.IsFalse(startAsyncCompletionCalled, "CompletionStartReceived not called");

zvtClient.StatusInformationReceived -= statusInformationReceived;

zvtClient.Dispose();
dataSentCancellationTokenSource.Dispose();

Assert.AreEqual(CommandResponseState.Successful, commandResponse.State);
}
}
}
Loading