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

Authorisation adjustment sample update #96

Merged
merged 12 commits into from
Aug 28, 2023
42 changes: 18 additions & 24 deletions authorisation-adjustment-example/Controllers/AdminController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -32,21 +31,16 @@ public AdminController(IPaymentRepository repository, IModificationsService modi
[Route("admin")]
public IActionResult Index()
{
List<PaymentModel> payments = new List<PaymentModel>();
foreach (var kvp in _repository.Payments)
{
payments.Add(_repository.FindLatestPaymentByReference(kvp.Key));
}
ViewBag.Payments = payments;
ViewBag.Payments = _repository.Payments.Values.ToList();
return View();
}

[Route("admin/details/{reference}")]
public IActionResult Details(string reference)
{
// We fetch all payments (regardless whether its authorised/refused) that we have stored in our local repository and show it.
ViewBag.Payments = _repository.FindByReference(reference)
.OrderBy(x=> x.DateTime)
ViewBag.PaymentsHistory = _repository.GetPayment(reference)?
.PaymentsHistory?
.OrderBy(paymentDetails => paymentDetails.DateTime)?
.ToList();
return View();
}
Expand Down Expand Up @@ -76,9 +70,9 @@ public IActionResult Result(string reference, string status, [FromQuery(Name = "
[HttpPost("admin/update-payment-amount")]
public async Task<ActionResult<PaymentAmountUpdateResponse>> UpdatePaymentAmount([FromBody] UpdatePaymentAmountRequest request, CancellationToken cancellationToken = default)
{
PaymentModel payment = _repository.FindLatestPaymentByReference(request.Reference);
PaymentModel preauthorisedPayment = _repository.GetPayment(request.Reference);

if (payment == null)
if (preauthorisedPayment == null)
{
return NotFound();
}
Expand All @@ -88,12 +82,12 @@ public async Task<ActionResult<PaymentAmountUpdateResponse>> UpdatePaymentAmount
var paymentAmountUpdateRequest = new PaymentAmountUpdateRequest()
{
MerchantAccount = _merchantAccount, // Required
Amount = new Amount() { Value = request.Amount, Currency = payment.Currency },
Reference = payment.Reference,
Amount = new Amount() { Value = request.Amount, Currency = preauthorisedPayment.Currency },
Reference = preauthorisedPayment.MerchantReference,
IndustryUsage = PaymentAmountUpdateRequest.IndustryUsageEnum.DelayedCharge,
};

var response = await _modificationsService.UpdateAuthorisedAmountAsync(payment.GetOriginalPspReference(), paymentAmountUpdateRequest, cancellationToken: cancellationToken);
var response = await _modificationsService.UpdateAuthorisedAmountAsync(preauthorisedPayment.PspReference, paymentAmountUpdateRequest, cancellationToken: cancellationToken);
return Ok(response);
}
catch (HttpClientException e)
Expand All @@ -106,9 +100,9 @@ public async Task<ActionResult<PaymentAmountUpdateResponse>> UpdatePaymentAmount
[HttpPost("admin/capture-payment")]
public async Task<ActionResult<PaymentCaptureResponse>> CapturePayment([FromBody] CreateCapturePaymentRequest request, CancellationToken cancellationToken = default)
{
PaymentModel payment = _repository.FindLatestPaymentByReference(request.Reference);
PaymentModel preauthorisedPayment = _repository.GetPayment(request.Reference);

if (payment == null)
if (preauthorisedPayment == null)
{
return NotFound();
}
Expand All @@ -118,11 +112,11 @@ public async Task<ActionResult<PaymentCaptureResponse>> CapturePayment([FromBody
var paymentCaptureRequest = new PaymentCaptureRequest()
{
MerchantAccount = _merchantAccount, // Required.
Amount = new Amount() { Value = payment.Amount, Currency = payment.Currency }, // Required.
Reference = payment.Reference
Amount = new Amount() { Value = preauthorisedPayment.Amount, Currency = preauthorisedPayment.Currency }, // Required.
Reference = preauthorisedPayment.MerchantReference
};

var response = await _modificationsService.CaptureAuthorisedPaymentAsync(payment.GetOriginalPspReference(), paymentCaptureRequest, cancellationToken: cancellationToken);
var response = await _modificationsService.CaptureAuthorisedPaymentAsync(preauthorisedPayment.PspReference, paymentCaptureRequest, cancellationToken: cancellationToken);
return Ok(response); // Note that the response will have a different PSPReference compared to the initial pre-authorisation.
}
catch (HttpClientException e)
Expand All @@ -135,9 +129,9 @@ public async Task<ActionResult<PaymentCaptureResponse>> CapturePayment([FromBody
[HttpPost("admin/reversal-payment")]
public async Task<ActionResult<PaymentReversalResponse>> ReversalPayment([FromBody] CreateReversalPaymentRequest request, CancellationToken cancellationToken = default)
{
PaymentModel payment = _repository.FindLatestPaymentByReference(request.Reference);
PaymentModel preauthorisedPayment = _repository.GetPayment(request.Reference);

if (payment == null)
if (preauthorisedPayment == null)
{
return NotFound();
}
Expand All @@ -147,10 +141,10 @@ public async Task<ActionResult<PaymentReversalResponse>> ReversalPayment([FromBo
var paymentReversalRequest = new PaymentReversalRequest()
{
MerchantAccount = _merchantAccount, // Required.
Reference = payment.Reference
Reference = preauthorisedPayment.MerchantReference
};

var response = await _modificationsService.RefundOrCancelPaymentAsync(payment.GetOriginalPspReference(), paymentReversalRequest, cancellationToken: cancellationToken);
var response = await _modificationsService.RefundOrCancelPaymentAsync(preauthorisedPayment.PspReference, paymentReversalRequest, cancellationToken: cancellationToken);
return Ok(response);
}
catch (HttpClientException e)
Expand Down
28 changes: 3 additions & 25 deletions authorisation-adjustment-example/Controllers/ApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,31 +150,9 @@ public async Task<ActionResult<PaymentResponse>> PreAuthorisation(PaymentRequest
{
var response = await _paymentsService.PaymentsAsync(paymentRequest, cancellationToken: cancellationToken);
_logger.LogInformation($"Response for Payments:\n{response}\n");

var payment = new PaymentModel()
{
PspReference = response.PspReference,
OriginalReference = null,
Reference = response.MerchantReference,
Amount = response.Amount?.Value,
Currency = response.Amount?.Currency,
DateTime = DateTimeOffset.UtcNow,
ResultCode = response.ResultCode.ToString(),
RefusalReason = response.RefusalReason,
PaymentMethodBrand = response.PaymentMethod?.Brand,
};

if (!_repository.Payments.TryGetValue(payment.Reference, out var list))
{
// Reference does not exist, let's add it.
_repository.Payments.TryAdd(
payment.Reference, /// Key: Reference.
new List<PaymentModel>() {
{
payment /// Value: <see cref="PaymentModel"/>.
}
});
}

// Insert our pre-authorised payment.
_repository.InsertPayment(response);

return Ok(response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ public IActionResult Preview(string id)
return View();
}

[Route("booking/{id}")]
public IActionResult Booking(string id)
[Route("booking")]
public IActionResult Booking()
{
ViewBag.PaymentMethod = id;
ViewBag.ClientKey = _clientKey;
return View();
}
Expand Down
Loading
Loading