Skip to content

Commit

Permalink
Authorisation adjustment sample update (#96)
Browse files Browse the repository at this point in the history
* Separate the logic in the repository for inserting the PaymentModel and the PaymentDetailsModel (history)
* Rename Reference to MerchantReference for consistency
* Updated comments in model-classes to include a summary of each property.
* Updated admin panel view and CSS
* Removed "type" from the javascript file and the cshtml, which was unused since only "card" is supported
* Added current PaymentStatus
* Added logic in webhook controller to update the amount to the latest authorisation adjustment amount

---------

Co-authored-by: Kwok He Chu <>
  • Loading branch information
Kwok-he-Chu authored Aug 28, 2023
1 parent f69f19b commit 14139ef
Show file tree
Hide file tree
Showing 18 changed files with 508 additions and 333 deletions.
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

0 comments on commit 14139ef

Please sign in to comment.