From 57f9c516d5c14ff9ac9ebd5fc09eafea8a35b871 Mon Sep 17 00:00:00 2001 From: Kwok He Chu <> Date: Thu, 13 Jul 2023 09:20:50 +0200 Subject: [PATCH] Cleanup --- .../Models/HotelPaymentModel.cs | 22 +--- .../Repositories/HotelPaymentRepository.cs | 7 - .../AuthorisationAdjustmentService.cs | 120 ------------------ 3 files changed, 3 insertions(+), 146 deletions(-) delete mode 100644 authorisation-adjustment-example/Services/AuthorisationAdjustmentService.cs diff --git a/authorisation-adjustment-example/Models/HotelPaymentModel.cs b/authorisation-adjustment-example/Models/HotelPaymentModel.cs index eb3c7393..77011ea8 100644 --- a/authorisation-adjustment-example/Models/HotelPaymentModel.cs +++ b/authorisation-adjustment-example/Models/HotelPaymentModel.cs @@ -1,9 +1,9 @@ using System; -using System.Text; +using System.Collections.Generic; namespace adyen_dotnet_authorisation_adjustment_example.Models { - public class BookingPaymentModel + public class HotelPaymentModel { // PspReference provided by Adyen. public string PspReference { get; set; } @@ -19,27 +19,11 @@ public class BookingPaymentModel public string Currency { get; set; } // Result. - public string ResultCode { get; set; } // ResultCode == Authorised. + public string ResultCode { get; set; } public string RefusalReason { get; set; } // Populated when resultcode is not authorised. // Payment method used. public string PaymentMethodBrand { get; set; } // PaymentMethod brand (e.g. `mc`). public string PaymentMethodType { get; set; } // PaymentMethod type (e.g. `scheme`). - - // Override for outputting purposes. - public override string ToString() - { - var sb = new StringBuilder(); - sb.AppendLine($"{nameof(PspReference)}: {PspReference}"); - sb.AppendLine($"{nameof(Reference)}: {Reference}"); - sb.AppendLine($"{nameof(DateTime)}: {DateTime}"); - sb.AppendLine($"{nameof(Amount)}: {Amount}"); - sb.AppendLine($"{nameof(Currency)}: {Currency}"); - sb.AppendLine($"{nameof(ResultCode)}: {ResultCode}"); - sb.AppendLine($"{nameof(RefusalReason)}: {RefusalReason}"); - sb.AppendLine($"{nameof(PaymentMethodBrand)}: {PaymentMethodBrand}"); - sb.AppendLine($"{nameof(PaymentMethodType)}: {PaymentMethodType}"); - return sb.ToString(); - } } } \ No newline at end of file diff --git a/authorisation-adjustment-example/Repositories/HotelPaymentRepository.cs b/authorisation-adjustment-example/Repositories/HotelPaymentRepository.cs index 984b73d5..15650b76 100644 --- a/authorisation-adjustment-example/Repositories/HotelPaymentRepository.cs +++ b/authorisation-adjustment-example/Repositories/HotelPaymentRepository.cs @@ -9,8 +9,6 @@ namespace adyen_dotnet_authorisation_adjustment_example.Repositories public interface IHotelPaymentRepository { ConcurrentDictionary HotelPayments { get; } - - bool Remove(string pspReference); bool Upsert(HotelPaymentModel hotelPayment); } @@ -24,11 +22,6 @@ public HotelPaymentRepository() HotelPayments = new ConcurrentDictionary(); } - public bool Remove(string pspReference) - { - return HotelPayments.TryRemove(pspReference, out var _); - } - public bool Upsert(HotelPaymentModel hotelPayment) { return HotelPayments.TryAdd(hotelPayment.PspReference, hotelPayment); diff --git a/authorisation-adjustment-example/Services/AuthorisationAdjustmentService.cs b/authorisation-adjustment-example/Services/AuthorisationAdjustmentService.cs deleted file mode 100644 index f9b98ac1..00000000 --- a/authorisation-adjustment-example/Services/AuthorisationAdjustmentService.cs +++ /dev/null @@ -1,120 +0,0 @@ -using Adyen.HttpClient; -using Adyen.Model.Checkout; -using Adyen.Service.Checkout; -using adyen_dotnet_authorisation_adjustment_example.Models; -using adyen_dotnet_authorisation_adjustment_example.Options; -using adyen_dotnet_authorisation_adjustment_example.Repositories; -using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Options; -using System.Collections.Generic; -using System.Threading.Tasks; -using System.Threading; - -namespace adyen_dotnet_authorisation_adjustment_example.Services -{ - public class AuthorisationAdjustmentService - { - private readonly IHotelPaymentRepository _repository; - private readonly IModificationsService _modificationsService; - private readonly string _merchantAccount; - - public AuthorisationAdjustmentService(IHotelPaymentRepository repository, IModificationsService modificationsService, IOptions options) - { - _repository = repository; - _merchantAccount = options.Value.ADYEN_MERCHANT_ACCOUNT; - _modificationsService = modificationsService; - } - - public async Task IncrementalAdjustment(string reference, string pspReference, int amount, CancellationToken cancellationToken = default) - { - // TODO: handle incremental, decremental, extension. - try - { - var request = new CreatePaymentAmountUpdateRequest() - { - MerchantAccount = _merchantAccount, // Required - Amount = new Amount() { Value = amount, Currency = "EUR" }, - Reference = reference, - IndustryUsage = CreatePaymentAmountUpdateRequest.IndustryUsageEnum.DelayedCharge, - }; - - var response = await _modificationsService.UpdateAuthorisedAmountAsync(pspReference, request, cancellationToken: cancellationToken); - return response; - } - catch (HttpClientException e) - { - //ViewBag.Message = $"Incremental adjustment failed for PSPReference {pspReference}. See error logs for the exception."; - //ViewBag.Img = "failed"; - throw; - } - //try - //{ - // PaymentResponse result = await _checkoutClient.MakePaymentAsync(ShopperReference.Value, recurringDetailReference, cancellationToken); - - // switch (result.ResultCode) - // { - // // Handle other payment response cases here. - // case PaymentResponse.ResultCodeEnum.Authorised: - // ViewBag.Message = $"Successfully authorised a payment with RecurringDetailReference: {recurringDetailReference}."; - // ViewBag.Img = "success"; - // break; - // default: - // ViewBag.Message = $"Payment failed for RecurringDetailReference {recurringDetailReference}. See error logs for the message."; - // ViewBag.Img = "failed"; - // break; - // } - //} - //catch (HttpClientException) - //{ - // ViewBag.Message = $"Payment failed for RecurringDetailReference {recurringDetailReference}. See error logs for the exception."; - // ViewBag.Img = "failed"; - //} - } - - [Route("admin/capture/{reference}/{pspReference}/{amount}")] // TODO: Move amount/reference into its own storage, it should not be passed on as parameter. - public async Task Capture(string reference, string pspReference, int amount, CancellationToken cancellationToken = default) - { - try - { - var request = new CreatePaymentCaptureRequest() - { - MerchantAccount = _merchantAccount, // Required. - Amount = new Amount() { Value = amount, Currency = "EUR" }, // Required. - Reference = reference, - }; - - var response = await _modificationsService.CaptureAuthorisedPaymentAsync(pspReference, request, cancellationToken: cancellationToken); - return response; // Note that the response will have a DIFFERENT PSPReference compared to the initial preauth - } - catch (HttpClientException e) - { - //ViewBag.Message = $"Incremental adjustment failed for PSPReference {pspReference}. See error logs for the exception."; - //ViewBag.Img = "failed"; - throw; - } - } - - [Route("admin/cancel-pre-authorisation/{pspReference}/{reference}")] // Move reference - public async Task CancelPreAuthorisation(string pspReference, string reference, CancellationToken cancellationToken = default) - { - - try - { - var request = new CreatePaymentCancelRequest() - { - MerchantAccount = _merchantAccount, // Required. - Reference = reference, - }; - - var response = await _modificationsService.CancelAuthorisedPaymentByPspReferenceAsync(pspReference, request, cancellationToken: cancellationToken); - return response; - } - catch (HttpClientException e) - { - //ViewBag.Message = $"Incremental adjustment failed for PSPReference {pspReference}. See error logs for the exception."; - //ViewBag.Img = "failed"; - throw; - } - } - } -}