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

Updatate wpf sample #8

Merged
merged 7 commits into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
✨ update wpf sample app to preview all 3 payment samples(links, check…
…outs, and resource)
  • Loading branch information
russkyc committed Dec 2, 2023
commit fd7bf7cbecc50f31f4bc25dc941c41e058e284ba
39 changes: 29 additions & 10 deletions wpf-sample/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,36 @@
Name="AmountTextBox"
Width="220"
Placeholder="Amount (up to 2 decimal places)" />
<moderncontrols:ModernButton
Width="100"
Margin="0,12,0,0"
Padding="12,7"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Click="OnPay"
Content="Pay" />
<StackPanel Width="220">
<moderncontrols:ModernButton
Margin="0,12,0,0"
Padding="12,7"
VerticalAlignment="Center"
Click="OnPayLink"
Content="Pay (Links)" />
<moderncontrols:ModernButton
Margin="0,12,0,0"
Padding="12,7"
VerticalAlignment="Center"
Click="OnPayCheckout"
Content="Pay (Checkout)" />
<moderncontrols:ModernButton
Margin="0,12,0,0"
Padding="12,7"
VerticalAlignment="Center"
Click="OnPayGcash"
Content="Pay (GCash)" />
<moderncontrols:ModernButton
Margin="0,12,0,0"
Padding="12,7"
VerticalAlignment="Center"
Click="OnPayGrabPay"
Content="Pay (GrabPay)" />
</StackPanel>
<TextBlock
Name="StatusBlock"
Margin="0,16,0,0"
TextAlignment="Center" />
Margin="16"
TextAlignment="Center"
TextWrapping="Wrap" />
</StackPanel>
</moderncontrols:ModernWindow>
237 changes: 236 additions & 1 deletion wpf-sample/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
using System.Windows;
using DotNetEnv;
using Paymongo.Sharp;
using Paymongo.Sharp.Checkouts.Entities;
using Paymongo.Sharp.Core.Entities;
using Paymongo.Sharp.Core.Enums;
using Paymongo.Sharp.Links.Entities;
using Paymongo.Sharp.Sources.Entities;

namespace WpfSample
{
Expand All @@ -20,7 +23,7 @@ public MainWindow()
Env.TraversePath().Load();
}

private async void OnPay(object sender, RoutedEventArgs e)
private async void OnPayLink(object sender, RoutedEventArgs e)
{
var isDouble = decimal.TryParse(AmountTextBox.Text, out decimal doubleAmount);

Expand Down Expand Up @@ -79,6 +82,238 @@ private async void OnPay(object sender, RoutedEventArgs e)

}

private async void OnPayCheckout(object sender, RoutedEventArgs e)
{
var isDouble = decimal.TryParse(AmountTextBox.Text, out decimal doubleAmount);

if (!isDouble)
{
return;
}

if (doubleAmount < 100)
{
return;
}

var amount = ConvertToInt(doubleAmount);

AmountTextBox.Text = string.Empty;
StatusBlock.Text = string.Empty;

var secretKey = Env.GetString("SECRET_KEY");
var client = new PaymongoClient(secretKey);

Checkout checkout = new Checkout()
{
Description = "Test Checkout",
ReferenceNumber = "9282321A",
LineItems = new []
{
new LineItem
{
Name = "Give You Up",
Images = new []
{
"https://i.insider.com/602ee9ced3ad27001837f2ac?width=750&format=jpeg"
},
Quantity = 1,
Currency = Currency.Php,
Amount = amount
}
},
PaymentMethodTypes = new[]
{
PaymentMethod.GCash,
PaymentMethod.Card,
PaymentMethod.Paymaya,
PaymentMethod.BillEase,
PaymentMethod.Dob,
PaymentMethod.GrabPay,
PaymentMethod.DobUbp
}
};

Checkout checkoutResult = await client.Checkouts.CreateCheckoutAsync(checkout);
var paymentWindow = new PaymentWindow(checkoutResult.CheckoutUrl);

paymentWindow.Show();

while (true)
{
var paymentStatus = await client.Checkouts.RetrieveCheckoutAsync(checkoutResult.Id);

if (!paymentStatus.Payments!.Any())
{
continue;
}

var payment = paymentStatus.Payments!.First();

if (payment.Status != PaymentStatus.Paid)
{
continue;
}

StatusBlock.Text = $"Paid by {payment.Billing!.Name} on {payment.PaidAt} using {payment.Source!["type"]}";

paymentWindow.Close();

break;
}

}

private async void OnPayGcash(object sender, RoutedEventArgs e)
{
var isDouble = decimal.TryParse(AmountTextBox.Text, out decimal doubleAmount);

if (!isDouble)
{
return;
}

if (doubleAmount < 100)
{
return;
}

var amount = ConvertToInt(doubleAmount);

AmountTextBox.Text = string.Empty;
StatusBlock.Text = string.Empty;

var secretKey = Env.GetString("SECRET_KEY");
var client = new PaymongoClient(secretKey);

// Arrange
Source source = new Source
{
Amount = amount,
Description = "New Gcash Payment",
Billing = new Billing
{
Name = "TestName",
Email = "test@paymongo.com",
Phone = "9063364572",
Address = new Address
{
Line1 = "TestAddress1",
Line2 = "TestAddress2",
PostalCode = "4506",
State = "TestState",
City = "TestCity",
Country = "PH"
}
},
Redirect = new Redirect
{
Success = "http://127.0.0.1",
Failed = "http://127.0.0.1"
},
Type = SourceType.GCash,
Currency = Currency.Php
};

// Act
var sourceResult = await client.Sources.CreateSourceAsync(source);
var paymentWindow = new PaymentWindow(sourceResult.Redirect!.CheckoutUrl);

paymentWindow.Show();

while (true)
{
var paymentStatus = await client.Sources.RetrieveSourceAsync(sourceResult.Id);

if (paymentStatus.Status != SourceStatus.Chargeable)
{
continue;
}

StatusBlock.Text = $"Chargeable on GCash by {paymentStatus.Billing!.Name} on {paymentStatus.UpdatedAt}";

paymentWindow.Close();

break;
}

}

private async void OnPayGrabPay(object sender, RoutedEventArgs e)
{
var isDouble = decimal.TryParse(AmountTextBox.Text, out decimal doubleAmount);

if (!isDouble)
{
return;
}

if (doubleAmount < 100)
{
return;
}

var amount = ConvertToInt(doubleAmount);

AmountTextBox.Text = string.Empty;
StatusBlock.Text = string.Empty;

var secretKey = Env.GetString("SECRET_KEY");
var client = new PaymongoClient(secretKey);

// Arrange
Source source = new Source
{
Amount = amount,
Description = "New GrabPay Payment",
Billing = new Billing
{
Name = "TestName",
Email = "test@paymongo.com",
Phone = "9063364572",
Address = new Address
{
Line1 = "TestAddress1",
Line2 = "TestAddress2",
PostalCode = "4506",
State = "TestState",
City = "TestCity",
Country = "PH"
}
},
Redirect = new Redirect
{
Success = "http://127.0.0.1",
Failed = "http://127.0.0.1"
},
Type = SourceType.GrabPay,
Currency = Currency.Php
};

// Act
var sourceResult = await client.Sources.CreateSourceAsync(source);
var paymentWindow = new PaymentWindow(sourceResult.Redirect!.CheckoutUrl);

paymentWindow.Show();

while (true)
{
var paymentStatus = await client.Sources.RetrieveSourceAsync(sourceResult.Id);

if (paymentStatus.Status != SourceStatus.Chargeable)
{
continue;
}

StatusBlock.Text = $"Chargeable on GrabPay by {paymentStatus.Billing!.Name} on {paymentStatus.UpdatedAt}";

paymentWindow.Close();

break;
}

}

int ConvertToInt(decimal decimalValue)
{
int decimalPlaces = BitConverter.GetBytes(decimal.GetBits(decimalValue)[3])[2];
Expand Down