Skip to content

Commit

Permalink
Added details view
Browse files Browse the repository at this point in the history
Parse webhooks
TODO: Explain extend (and the options in the admin panel)
Forgot to push
  • Loading branch information
Kwok He Chu committed Jul 20, 2023
1 parent f625089 commit dc0f03a
Show file tree
Hide file tree
Showing 22 changed files with 620 additions and 329 deletions.
129 changes: 67 additions & 62 deletions authorisation-adjustment-example/Controllers/AdminController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -34,73 +34,79 @@ public IActionResult Index()
{
List<HotelPaymentModel> hotelPayments = new List<HotelPaymentModel>();

// We fetch all hotel payments (regardless whether its authorised or not) that we have stored in our (local) repository and show it.
foreach (KeyValuePair<string, HotelPaymentModel> kvp in _repository.HotelPayments)
// We fetch the latest payment update. // TODO, do not filter by datetime
foreach (var kvp in _repository.HotelPayments)
{
hotelPayments.Add(kvp.Value);
hotelPayments.Add(_repository.FindLatestHotelPaymentByReference(kvp.Key));
}

hotelPayments.Add(new HotelPaymentModel()
{
Amount = 1234,
Currency = "EUR",
DateTime = DateTime.UtcNow,
PaymentMethodBrand = "scheme",
PaymentMethodType = "mc",
PspReference = "psp-1",
Reference = "ref-1",
ResultCode = "Authorised"
});

hotelPayments.Add(new HotelPaymentModel()
{
Amount = 5678,
Currency = "EUR",
DateTime = DateTime.UtcNow,
PaymentMethodBrand = "scheme",
PaymentMethodType = "visa",
PspReference = "psp-2",
Reference = "ref-2",
ResultCode = "Authorised"
});

hotelPayments.Add(new HotelPaymentModel()
{
Amount = 9876,
Currency = "EUR",
DateTime = DateTime.UtcNow,
PaymentMethodBrand = "scheme",
PaymentMethodType = "disc",
PspReference = "psp-3",
Reference = "ref-3",
ResultCode = "CAPTURE",
RefusalReason = "aaa"
});


hotelPayments.Add(new HotelPaymentModel()
{
DateTime = DateTime.UtcNow,
PaymentMethodBrand = "scheme",
PaymentMethodType = "mc",
PspReference = "psp-4",
Reference = "ref-4",
ResultCode = "CAPTURE",
RefusalReason = "aaa"
});
//hotelPayments.Add(new HotelPaymentModel()
//{
// Amount = 1234,
// Currency = "EUR",
// DateTime = System.DateTime.UtcNow,
// PaymentMethodBrand = "mc",
// PspReference = "psp-1",
// Reference = "ref-1",
// ResultCode = "Authorised"
//});

//hotelPayments.Add(new HotelPaymentModel()
//{
// Amount = 5678,
// Currency = "EUR",
// DateTime = System.DateTime.UtcNow,
// PaymentMethodBrand = "visa",
// PspReference = "psp-2",
// Reference = "ref-2",
// ResultCode = "AUTHORISATION_ADJUSTED",
// OriginalReference = "original-psp-1"
//});

//hotelPayments.Add(new HotelPaymentModel()
//{
// Amount = 9876,
// Currency = "EUR",
// DateTime = System.DateTime.UtcNow,
// PaymentMethodBrand = "visa",
// PspReference = "psp-3",
// Reference = "ref-3",
// ResultCode = "CAPTURE",
// RefusalReason = "aaa",
// OriginalReference = "original-psp-1"
//});


//hotelPayments.Add(new HotelPaymentModel()
//{
// DateTime = System.DateTime.UtcNow,
// PaymentMethodBrand = "mc",
// PspReference = "psp-4",
// Reference = "ref-4",
// ResultCode = "CAPTURE",
// RefusalReason = "aaa"
//});
ViewBag.HotelPayments = hotelPayments;
return View();
}

[HttpGet("admin/result/{status}/{pspReference}")]
public IActionResult Result(string pspReference, string status, [FromQuery(Name = "reason")] string refusalReason)
[Route("admin/details/{reference}")]
public IActionResult Details(string reference)
{
// We fetch all hotel payments (regardless whether its authorised/refused) that we have stored in our local repository and show it.
ViewBag.HotelPayments = _repository.FindByReference(reference).OrderBy(x=>x.DateTime).ToList();
return View();
}

[HttpGet("admin/result/{status}/{reference}")]
public IActionResult Result(string reference, string status, [FromQuery(Name = "reason")] string refusalReason)
{
string msg;
string img;
switch (status)
{
case "received":
msg = $"Request received for {pspReference}.";
msg = $"Request received for Merchant Reference: {reference}. Wait a bit to receive the asynchronous webhook response.";
img = "success";
break;
default:
Expand All @@ -114,11 +120,10 @@ public IActionResult Result(string pspReference, string status, [FromQuery(Name
return View();
}


[HttpPost("admin/update-payment-amount")]
public async Task<ActionResult<PaymentAmountUpdateResource>> UpdatePaymentAmount([FromBody] UpdatePaymentAmountRequest request, CancellationToken cancellationToken = default)
{
var hotelPayment = _repository.GetByPspReference(request.PspReference);
var hotelPayment = _repository.FindLatestHotelPaymentByReference(request.Reference);

if (hotelPayment == null)
{
Expand All @@ -135,7 +140,7 @@ public async Task<ActionResult<PaymentAmountUpdateResource>> UpdatePaymentAmount
IndustryUsage = CreatePaymentAmountUpdateRequest.IndustryUsageEnum.DelayedCharge,
};

var response = await _modificationsService.UpdateAuthorisedAmountAsync(request.PspReference, createPaymentAmountUpdateRequest, cancellationToken: cancellationToken);
var response = await _modificationsService.UpdateAuthorisedAmountAsync(hotelPayment.GetOriginalPspReference(), createPaymentAmountUpdateRequest, cancellationToken: cancellationToken);
return Ok(response);
}
catch (HttpClientException e)
Expand All @@ -148,7 +153,7 @@ public async Task<ActionResult<PaymentAmountUpdateResource>> UpdatePaymentAmount
[HttpPost("admin/capture-payment")]
public async Task<ActionResult<PaymentCaptureResource>> CapturePayment([FromBody] CreateCapturePaymentRequest request, CancellationToken cancellationToken = default)
{
var hotelPayment = _repository.GetByPspReference(request.PspReference);
var hotelPayment = _repository.FindLatestHotelPaymentByReference(request.Reference);

if (hotelPayment == null)
{
Expand All @@ -164,7 +169,7 @@ public async Task<ActionResult<PaymentCaptureResource>> CapturePayment([FromBody
Reference = hotelPayment.Reference
};

var response = await _modificationsService.CaptureAuthorisedPaymentAsync(request.PspReference, createPaymentCaptureRequest, cancellationToken: cancellationToken);
var response = await _modificationsService.CaptureAuthorisedPaymentAsync(hotelPayment.GetOriginalPspReference(), createPaymentCaptureRequest, cancellationToken: cancellationToken);
return Ok(response); // Note that the response will have a different PSPReference compared to the initial preauthorisation.
}
catch (HttpClientException e)
Expand All @@ -177,7 +182,7 @@ public async Task<ActionResult<PaymentCaptureResource>> CapturePayment([FromBody
[HttpPost("admin/reversal-payment")]
public async Task<ActionResult<PaymentReversalResource>> ReversalPayment([FromBody] CreateReversalPaymentRequest request, CancellationToken cancellationToken = default)
{
var hotelPayment = _repository.GetByPspReference(request.PspReference);
var hotelPayment = _repository.FindLatestHotelPaymentByReference(request.Reference);

if (hotelPayment == null)
{
Expand All @@ -192,7 +197,7 @@ public async Task<ActionResult<PaymentReversalResource>> ReversalPayment([FromBo
Reference = hotelPayment.Reference
};

var response = await _modificationsService.RefundOrCancelPaymentAsync(request.PspReference, createPaymentReversalRequest, cancellationToken: cancellationToken);
var response = await _modificationsService.RefundOrCancelPaymentAsync(hotelPayment.GetOriginalPspReference(), createPaymentReversalRequest, cancellationToken: cancellationToken);
return Ok(response);
}
catch (HttpClientException e)
Expand Down
24 changes: 17 additions & 7 deletions authorisation-adjustment-example/Controllers/ApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public async Task<ActionResult<PaymentDetailsResponse>> SubmitAdditionalDetails(
}

[HttpGet("api/handleRedirect")]
public async Task<IActionResult> HandleShoppperRedirect(string payload = null, string redirectResult = null, CancellationToken cancellationToken = default)
public async Task<IActionResult> HandleRedirect(string payload = null, string redirectResult = null, CancellationToken cancellationToken = default)
{
var detailsRequest = new DetailsRequest();
if (!string.IsNullOrWhiteSpace(redirectResult))
Expand Down Expand Up @@ -143,7 +143,6 @@ public async Task<ActionResult<PaymentResponse>> PreAuthorisation(PaymentRequest
{
{ "allow3DS2", "true" }, // Required for the 3DS2 flow.
{ "authorisationType", "PreAuth" }, // Set `authorisationType` to `preAuth`.
{ "manualCapture", "true" }, // Set `manualCapture` to `true` so we do not finalize the payment until we capture it.
}
};

Expand All @@ -155,17 +154,28 @@ public async Task<ActionResult<PaymentResponse>> PreAuthorisation(PaymentRequest
var hotelPayment = new HotelPaymentModel()
{
PspReference = response.PspReference,
OriginalReference = null,
Reference = response.MerchantReference,
Amount = response.Amount.Value.HasValue ? response.Amount.Value : null,
Currency = response.Amount.Currency,
DateTime = DateTime.UtcNow,
Amount = response.Amount?.Value,
Currency = response.Amount?.Currency,
DateTime = DateTimeOffset.UtcNow,
ResultCode = response.ResultCode.ToString(),
RefusalReason = response.RefusalReason,
PaymentMethodBrand = response.PaymentMethod?.Brand,
PaymentMethodType = response.PaymentMethod?.Type
};

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

return Ok(response);
}
catch (Adyen.HttpClient.HttpClientException e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public IActionResult Preview(string id)
return View();
}

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

0 comments on commit dc0f03a

Please sign in to comment.