Skip to content

Commit

Permalink
Create generic overloads for CreatePayment and GetPayment
Browse files Browse the repository at this point in the history
  • Loading branch information
Viincenttt committed May 20, 2024
1 parent 27de4f7 commit 0733255
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 8 deletions.
28 changes: 28 additions & 0 deletions src/Mollie.Api/Client/Abstract/IPaymentClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

namespace Mollie.Api.Client.Abstract {
public interface IPaymentClient : IBaseMollieClient {
Task<TResponse> CreatePaymentAsync<TRequest, TResponse>(TRequest paymentRequest, bool includeQrCode = false)
where TRequest : PaymentRequest
where TResponse : PaymentResponse;

Task<PaymentResponse> CreatePaymentAsync(PaymentRequest paymentRequest, bool includeQrCode = false);

/// <summary>
Expand All @@ -32,6 +36,30 @@ Task<PaymentResponse> GetPaymentAsync(
bool embedRefunds = false,
bool embedChargebacks = false);

/// <summary>
/// Retrieve a single payment object by its payment identifier.
/// </summary>
/// <param name="paymentId">The payment's ID, for example tr_7UhSN1zuXS.</param>
/// <param name="testmode">Oauth - Optional – Set this to true to get a payment made in test mode. If you omit
/// this parameter, you can only retrieve live mode payments.</param>
/// <param name="includeQrCode">Include a QR code object. Only available for iDEAL, Bancontact and bank transfer
/// payments.</param>
/// <param name="includeRemainderDetails">Include the Payment method-specific response parameters of the
/// ‘remainder payment’ as well. This applies to gift card and voucher payments where only part of the payment
/// was completed with gift cards or vouchers, and the remainder was completed with a regular payment method.
/// </param>
/// <param name="embedRefunds">Include all refunds created for the payment.</param>
/// <param name="embedChargebacks"> Include all chargebacks issued for the payment.</param>
/// <returns></returns>
Task<T> GetPaymentAsync<T>(
string paymentId,
bool testmode = false,
bool includeQrCode = false,
bool includeRemainderDetails = false,
bool embedRefunds = false,
bool embedChargebacks = false)
where T : PaymentResponse;

/// <summary>
/// Some payment methods are cancellable for an amount of time, usually until the next day. Or as long as the payment status is open. Payments may be cancelled manually from the Dashboard, or automatically by using this endpoint.
/// </summary>
Expand Down
41 changes: 33 additions & 8 deletions src/Mollie.Api/Client/PaymentClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,37 @@

namespace Mollie.Api.Client {
public class PaymentClient : BaseMollieClient, IPaymentClient {

public PaymentClient(string apiKey, HttpClient? httpClient = null) : base(apiKey, httpClient) { }

public async Task<PaymentResponse> CreatePaymentAsync(PaymentRequest paymentRequest, bool includeQrCode = false) {
public async Task<TResponse> CreatePaymentAsync<TRequest, TResponse>(TRequest paymentRequest, bool includeQrCode = false)
where TRequest : PaymentRequest
where TResponse : PaymentResponse {
if (!string.IsNullOrWhiteSpace(paymentRequest.ProfileId) || paymentRequest.Testmode.HasValue || paymentRequest.ApplicationFee != null) {
ValidateApiKeyIsOauthAccesstoken();
}

var queryParameters = BuildQueryParameters(
includeQrCode: includeQrCode);

return await PostAsync<PaymentResponse>($"payments{queryParameters.ToQueryString()}", paymentRequest).ConfigureAwait(false);
return await PostAsync<TResponse>($"payments{queryParameters.ToQueryString()}", paymentRequest).ConfigureAwait(false);
}

public async Task<PaymentResponse> GetPaymentAsync(
public async Task<PaymentResponse> CreatePaymentAsync(PaymentRequest paymentRequest, bool includeQrCode = false) {
return await CreatePaymentAsync<PaymentRequest, PaymentResponse>(paymentRequest, includeQrCode)
.ConfigureAwait(false);
}

public async Task<T> GetPaymentAsync<T>(
string paymentId,
bool testmode = false,
bool includeQrCode = false,
bool includeRemainderDetails = false,
bool embedRefunds = false,
bool embedChargebacks = false) {
bool embedChargebacks = false)
where T : PaymentResponse {

if (testmode) {
ValidateApiKeyIsOauthAccesstoken();
if (testmode) {
ValidateApiKeyIsOauthAccesstoken();
}

ValidateRequiredUrlParameter(nameof(paymentId), paymentId);
Expand All @@ -46,7 +53,25 @@ public async Task<PaymentResponse> GetPaymentAsync(
embedRefunds: embedRefunds,
embedChargebacks: embedChargebacks
);
return await GetAsync<PaymentResponse>($"payments/{paymentId}{queryParameters.ToQueryString()}").ConfigureAwait(false);

return await GetAsync<T>($"payments/{paymentId}{queryParameters.ToQueryString()}").ConfigureAwait(false);
}

public async Task<PaymentResponse> GetPaymentAsync(
string paymentId,
bool testmode = false,
bool includeQrCode = false,
bool includeRemainderDetails = false,
bool embedRefunds = false,
bool embedChargebacks = false) {

return await GetPaymentAsync<PaymentResponse>(
paymentId,
testmode,
includeQrCode,
includeRemainderDetails,
embedRefunds,
embedChargebacks);
}

public async Task CancelPaymentAsync(string paymentId, bool testmode = false) {
Expand Down

0 comments on commit 0733255

Please sign in to comment.