Skip to content

Commit

Permalink
Fix/mobilepay webhook endpoint failing (#309)
Browse files Browse the repository at this point in the history
  • Loading branch information
TTA777 authored Dec 4, 2024
1 parent af08e5d commit 8e2f18e
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 17 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,3 @@ jobs:
name: Build infrastructure
uses: ./.github/workflows/infra-build.yml
secrets: inherit

sonarcloud:
name: SonarCloud
uses: ./.github/workflows/sonarcloud.yml
secrets: inherit
5 changes: 5 additions & 0 deletions .github/workflows/pullrequest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ jobs:
build:
uses: ./.github/workflows/build.yml
secrets: inherit

sonarcloud:
name: SonarCloud
uses: ./.github/workflows/sonarcloud.yml
secrets: inherit
19 changes: 19 additions & 0 deletions coffeecard/CoffeeCard.Common/Errors/BadRequestException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Runtime.Serialization;

namespace CoffeeCard.Common.Errors;

public class BadRequestException : ApiException
{
public BadRequestException(string message, int statusCode = 400) : base(message, statusCode)
{
}

public BadRequestException(Exception ex, int statusCode = 400) : base(ex, statusCode)
{
}

protected BadRequestException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
3 changes: 2 additions & 1 deletion coffeecard/CoffeeCard.Library/Services/EmailService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ public class EmailService : IEmailService
private readonly ILogger<EmailService> _logger;

public EmailService(IEmailSender emailSender, EnvironmentSettings environmentSettings,
IWebHostEnvironment env, IMapperService mapperService)
IWebHostEnvironment env, IMapperService mapperService, ILogger<EmailService> logger)
{
_emailSender = emailSender;
_environmentSettings = environmentSettings;
_env = env;
_mapperService = mapperService;
_logger = logger;
}

public async Task SendInvoiceAsync(UserDto user, PurchaseDto purchase)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public async Task<UserSearchResponse> SearchUsers(String search, int pageNum, in

if (totalUsers < skip)
{
throw new ArgumentException($"The value of {nameof(pageNum)} is outside of the range of total users");
throw new BadRequestException($"The value of {nameof(pageNum)} is outside of the range of total users");
}

var usersByPage = await query
Expand Down
8 changes: 4 additions & 4 deletions coffeecard/CoffeeCard.Library/Services/v2/PurchaseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public async Task<InitiatePurchaseResponse> InitiatePurchase(InitiatePurchaseReq
/// <param name="initiateRequest">Purchase Request</param>
/// <param name="product">Product</param>
/// <exception cref="IllegalUserOperationException">User is not entitled to purchase product</exception>
/// <exception cref="ArgumentException">PaymentType FreePurchase used for a non-free product</exception>
/// <exception cref="BadRequestException">PaymentType FreePurchase used for a non-free product</exception>
private void CheckUserIsAllowedToPurchaseProduct(User user, InitiatePurchaseRequest initiateRequest, ProductResponse product)
{
//Product does not belong to same userGroup as user
Expand All @@ -92,7 +92,7 @@ private void CheckUserIsAllowedToPurchaseProduct(User user, InitiatePurchaseRequ
_logger.LogWarning(
"User tried to issue paid product to themselves, User {UserId}, Product {ProductId}",
user.Id, product.Id);
throw new ArgumentException($"Product '{product.Name}' is not free");
throw new BadRequestException($"Product '{product.Name}' is not free");
}
}

Expand Down Expand Up @@ -125,7 +125,7 @@ private void CheckUserIsAllowedToPurchaseProduct(User user, InitiatePurchaseRequ
break;
default:
_logger.LogError("Payment Type {PaymentType} is not handled in PurchaseService", purchaseRequest.PaymentType);
throw new ArgumentException($"Payment Type '{purchaseRequest.PaymentType}' is not handled");
throw new BadRequestException($"Payment Type '{purchaseRequest.PaymentType}' is not handled");
}

var purchase = new Purchase
Expand Down Expand Up @@ -247,7 +247,7 @@ public async Task HandleMobilePayPaymentUpdate(MobilePayWebhook webhook)
_logger.LogError(
"Unknown EventType from Webhook request. Event Type: {EventType}, Purchase Id: {PurchaseId}, Transaction Id: {TransactionId}",
eventTypeLowerCase, purchase.Id, webhook.Data.Id);
throw new ArgumentException($"Event Type {eventTypeLowerCase} is not valid");
throw new BadRequestException($"Event Type {eventTypeLowerCase} is not valid");
}
}

Expand Down
3 changes: 2 additions & 1 deletion coffeecard/CoffeeCard.Library/Services/v2/WebhookService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ public class WebhookService : IWebhookService
private readonly ILogger<WebhookService> _logger;

public WebhookService(CoffeeCardContext context, IMobilePayWebhooksService mobilePayWebhooksService,
MobilePaySettingsV2 mobilePaySettings, IMemoryCache memoryCache)
MobilePaySettingsV2 mobilePaySettings, IMemoryCache memoryCache, ILogger<WebhookService> logger)
{
_context = context;
_mobilePayWebhooksService = mobilePayWebhooksService;
_mobilePaySettings = mobilePaySettings;
_memoryCache = memoryCache;
_logger = logger;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class PurchaseServiceTests
{
[Theory(DisplayName =
"InitiatePurchase.CheckUserIsAllowedToPurchaseProduct throws exceptions in several conditions")]
[InlineData(1, 1, typeof(ArgumentException))] // FreePurchase PaymentType fails when product has a price != 0
[InlineData(1, 1, typeof(BadRequestException))] // FreePurchase PaymentType fails when product has a price != 0
[InlineData(1, 2, typeof(IllegalUserOperationException))] // Product not in PUG
[InlineData(1, 3, typeof(EntityNotFoundException))] // Product not exists
public async Task InitiatePurchaseCheckUserIsAllowedToPurchaseProductThrowsExceptionsInSeveralConditions(
Expand Down
4 changes: 0 additions & 4 deletions coffeecard/CoffeeCard.WebApi/Helpers/ApiExceptionFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ public override void OnException(ExceptionContext context)
apiError = new ApiError("Unauthorized Access");
context.HttpContext.Response.StatusCode = StatusCodes.Status401Unauthorized;
break;
case ArgumentException exception:
apiError = new ApiError(exception.Message);
context.HttpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
break;
default:
{
Log.Error(context.Exception, "Unhandled exception caught");
Expand Down

0 comments on commit 8e2f18e

Please sign in to comment.