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

Add PaymentLink API example #386

Draft
wants to merge 2 commits into
base: development
Choose a base branch
from
Draft
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
@@ -0,0 +1,23 @@
using System.ComponentModel.DataAnnotations;
using Mollie.Api.Models;
using Mollie.WebApplication.Blazor.Framework.Validators;

namespace Mollie.WebApplication.Blazor.Models.PaymentLink;

public class CreatePaymentLinkModel {
[Required]
[Range(0.01, 1000, ErrorMessage = "Please enter an amount between 0.01 and 1000")]
[DecimalPlaces(2)]
public required decimal Amount { get; set; }

[Required]
[StaticStringList(typeof(Currency))]
public required string Currency { get; set; }

[Required]
public required string Description { get; set; }

[Required]
[Url]
public string? RedirectUrl { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
@page "/paymentlink/create"

@using Mollie.Api.Client
@using Mollie.Api.Models.PaymentLink.Request
@using Mollie.WebApplication.Blazor.Models.PaymentLink

@inject IPaymentLinkClient PaymentLinkClient
@inject NavigationManager NavigationManager

<h3>Create new payment link</h3>

<ApiExceptionDisplay Exception="_apiException"></ApiExceptionDisplay>

<EditForm Model="_model" OnValidSubmit="@OnSave" class="col-md-6">
<DataAnnotationsValidator />
<ValidationSummary />

<div class="form-group">
<label for="amount">Amount</label>
<InputNumber
id="amount"
class="form-control"
@bind-Value="_model.Amount">
</InputNumber>
</div>

<div class="form-group">
<label for="currency">Currency</label>
<InputSelect
id="currency"
class="form-control"
@bind-Value="_model.Currency">
@foreach (string currency in StaticStringListBuilder.GetStaticStringList(typeof(Currency))) {
<option value="@currency">@currency</option>
}
</InputSelect>
</div>

<div class="form-group">
<label for="redirect-url">Redirect url</label>
<InputText
id="redirect-url"
class="form-control"
@bind-Value="_model.RedirectUrl">
</InputText>
</div>

<div class="form-group">
<label for="description">Description</label>
<InputText
id="description"
class="form-control"
@bind-Value="_model.Description">
</InputText>
</div>

<input type="submit" name="Save" value="Save" class="btn btn-primary mt-2"/>
</EditForm>

@code {
private MollieApiException? _apiException;

private CreatePaymentLinkModel _model = new() {
Amount = 10.00m,
Currency = "EUR",
RedirectUrl = "https://www.mollie.com/",
Description = "A payment from the example application"
};

private async Task OnSave() {
try {
_apiException = null;

await PaymentLinkClient.CreatePaymentLinkAsync(new PaymentLinkRequest {
Amount = new Amount(_model.Currency, _model.Amount),
RedirectUrl = _model.RedirectUrl,
Description = _model.Description
});

NavigationManager.NavigateTo("/paymentlink/overview");
}
catch (MollieApiException e) {
_apiException = e;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
@page "/paymentlink/overview"
@using Mollie.Api.Models.List.Response
@using Mollie.Api.Models.PaymentLink.Response

@inject IPaymentLinkClient PaymentLinkClient

<h3>Payment links</h3>

@if (_paymentLinks == null) {
<p>Loading...</p>
}
else {
<div class="clearfix">
<a href="/paymentlink/create" class="btn btn-primary float-right">Create new payment link</a>
</div>

<table class="table table-striped">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Date created</th>
<th scope="col">Amount</th>
<th scope="col">Archived</th>
<th scope="col">Paid at</th>
<th scope="col">Description</th>
<th scope="col">Actions</th>
</tr>
</thead>
<tbody>
@foreach (PaymentLinkResponse paymentLink in _paymentLinks.Items) {
<tr>
<td>@paymentLink.Id</td>
<td>@paymentLink.CreatedAt</td>
<td>@(paymentLink.Amount != null ? paymentLink.Amount : "No amount specified")</td>
<td>@(paymentLink.Archived ? "Yes" : "No")</td>
<td>@paymentLink.PaidAt</td>
<td>Description</td>
<td>
@if (paymentLink.Archived == false && paymentLink.PaidAt == null)
{
<a href="@paymentLink.Links.PaymentLink.Href" class="btn btn-outline-secondary" target="_blank">Pay</a>
}
</td>
</tr>
}
</tbody>
</table>

<OverviewNavigation
Previous="_paymentLinks.Links.Previous"
Next="_paymentLinks.Links.Next">
</OverviewNavigation>
}

@code {
[Parameter]
[SupplyParameterFromQuery]
public string? Url { get; set; }

private ListResponse<PaymentLinkResponse>? _paymentLinks;

protected override async Task OnParametersSetAsync() {
await LoadData();
}

private async Task LoadData() {
if (string.IsNullOrEmpty(Url)) {
_paymentLinks = await PaymentLinkClient.GetPaymentLinkListAsync();
}
else {
_paymentLinks = await PaymentLinkClient.GetPaymentLinkListAsync(new UrlObjectLink<ListResponse<PaymentLinkResponse>> {
Href = Url,
Type = "application/json"
});
}
}
}
7 changes: 7 additions & 0 deletions samples/Mollie.WebApplication.Blazor/Shared/NavMenu.razor
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,19 @@
<span class="oi oi-home" aria-hidden="true"></span> Home
</NavLink>
</div>

<div class="nav-item px-3">
<NavLink class="nav-link" href="payment/overview">
<span class="oi oi-euro" aria-hidden="true"></span> Payments
</NavLink>
</div>

<div class="nav-item px-3">
<NavLink class="nav-link" href="paymentlink/overview">
<span class="oi oi-euro" aria-hidden="true"></span> Payment links
</NavLink>
</div>

<div class="nav-item px-3">
<NavLink class="nav-link" href="order/overview">
<span class="oi oi-cart" aria-hidden="true"></span> Orders
Expand Down
Loading