.netCore
If issue related with ABP Framework
Latest
I'm trying to integrate with GoCardless to add Direct Debit Payments for memberships. They sent a webhook when payments are complete and I need to add the following to a controller to process that event.
[HttpPost]
[AllowAnonymous]
public async Task<StatusCodeResult> WebhookEvent()
{
var requestBody = HttpContext.Request.Body;
requestBody.Seek(0, System.IO.SeekOrigin.Begin);
var requestJson = new StreamReader(requestBody).ReadToEnd();
// We recommend storing your webhook endpoint secret in an environment variable
// for security
var secret = AppConsts.GoCardlesssSandBoxToken;
var signature = Request.Headers["Webhook-Signature"];
try
{
// Webhooks can contain many events. In a real implementation, you should handle
// the processing of each event asychronously to avoid timing out here.
// You could check whether you've processed the event before (by recording the event
// ID when you process it) and retrieve the associated resource to ensure that you have
// the most up-to-date information about it.
foreach (GoCardless.Resources.Event whevent in WebhookParser.Parse(requestJson, secret, signature))
{
// To keep this example simple, we're only handling Mandate events
if (whevent.ResourceType == GoCardless.Resources.EventResourceType.Mandates)
{
switch (whevent.Action)
{
case "created":
Console.WriteLine($"Mandate {whevent.Links.Mandate} has been created, yay!");
break;
case "cancelled":
Console.WriteLine($"Oh no, mandate {whevent.Links.Mandate} was cancelled!");
break;
default:
Console.WriteLine($"{whevent.Links.Mandate} has been {whevent.Action}");
break;
}
}
else
{
Console.WriteLine($"No method to handle {whevent.ResourceType} events yet");
}
}
return new StatusCodeResult((int)HttpStatusCode.OK);
}
catch (InvalidSignatureException e)
{
return new BadRequestResult();
}
}
I've added to the MVC controller but I keep getting 302 redirected to login page. I can't figure out why as I've added the [AllowAnonymous] Attribute. I've also tried the [abpAllowAnonymous] attribute with no success.
2 Answer(s)
-
0
Hi @GarethWright
Can you please add that ActionResults to one your controller and request them without login to application.
[AllowAnonymous] public async Task<IActionResult> Test1() { return Content("Test1"); } [AbpMvcAuthorize] public async Task<IActionResult> Test2() { return Content("Test2"); }
You should be able to get "Test1" response, but test2 should redirect you to login page. Can you please check that
-
0
I couldn't get it to work. Ended up creating a public facing unauthenticated controller to handle incoming webhooks.