Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/Examples/V2/EventNotificationHandlerEndpoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// File copied from our code generator; changes here will be overwritten.
namespace Examples.V2
{
#pragma warning disable SA1101 // Prefix local calls with this

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

Check failure on line 9 in src/Examples/V2/EventNotificationHandlerEndpoint.cs

View workflow job for this annotation

GitHub Actions / Build examples

The type or namespace name 'AspNetCore' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)

Check failure on line 9 in src/Examples/V2/EventNotificationHandlerEndpoint.cs

View workflow job for this annotation

GitHub Actions / Build examples

The type or namespace name 'AspNetCore' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)
using Stripe;
using Stripe.Events;

/// <summary>
/// receive and process event notifications (AKA thin events) like "v1.billing.meter.no_meter_found" using EventNotificationHandler.
///
/// In this example, we:
/// - write a fallback callback to handle unrecognized event notifications
/// - create a StripeClient called client
/// - Initialize an EventNotificationHandler with the client, webhook secret, and fallback callback
/// - register a specific handler for the "v1.billing.meter.no_meter_found" event notification type
/// - use handler.handle() to process the received notification webhook body.
/// </summary>
[Route("api/[controller]")]

Check failure on line 23 in src/Examples/V2/EventNotificationHandlerEndpoint.cs

View workflow job for this annotation

GitHub Actions / Build examples

The type or namespace name 'Route' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 23 in src/Examples/V2/EventNotificationHandlerEndpoint.cs

View workflow job for this annotation

GitHub Actions / Build examples

The type or namespace name 'RouteAttribute' could not be found (are you missing a using directive or an assembly reference?)
[ApiController]

Check failure on line 24 in src/Examples/V2/EventNotificationHandlerEndpoint.cs

View workflow job for this annotation

GitHub Actions / Build examples

The type or namespace name 'ApiController' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 24 in src/Examples/V2/EventNotificationHandlerEndpoint.cs

View workflow job for this annotation

GitHub Actions / Build examples

The type or namespace name 'ApiControllerAttribute' could not be found (are you missing a using directive or an assembly reference?)
public class EventNotificationHandlerEndpoint : ControllerBase

Check failure on line 25 in src/Examples/V2/EventNotificationHandlerEndpoint.cs

View workflow job for this annotation

GitHub Actions / Build examples

The type or namespace name 'ControllerBase' could not be found (are you missing a using directive or an assembly reference?)
{
private readonly StripeClient client;
private readonly StripeEventNotificationHandler handler;

public EventNotificationHandlerEndpoint()
{
client = new StripeClient(Environment.GetEnvironmentVariable("STRIPE_API_KEY"));
handler = client.NotificationHandler(Environment.GetEnvironmentVariable("WEBHOOK_SECRET") ?? string.Empty, FallbackCallback);

// register handlers
handler.V1BillingMeterErrorReportTriggered += HandleBillingMeterErrorReportTriggeredEventNotification;

Check warning on line 36 in src/Examples/V2/EventNotificationHandlerEndpoint.cs

View workflow job for this annotation

GitHub Actions / Build examples

Nullability of reference types in type of parameter 'sender' of 'void EventNotificationHandlerEndpoint.HandleBillingMeterErrorReportTriggeredEventNotification(object sender, StripeEventNotificationEventArgs<V1BillingMeterErrorReportTriggeredEventNotification> e)' doesn't match the target delegate 'EventHandler<StripeEventNotificationEventArgs<V1BillingMeterErrorReportTriggeredEventNotification>>' (possibly because of nullability attributes).

Check warning on line 36 in src/Examples/V2/EventNotificationHandlerEndpoint.cs

View workflow job for this annotation

GitHub Actions / Build examples

Nullability of reference types in type of parameter 'sender' of 'void EventNotificationHandlerEndpoint.HandleBillingMeterErrorReportTriggeredEventNotification(object sender, StripeEventNotificationEventArgs<V1BillingMeterErrorReportTriggeredEventNotification> e)' doesn't match the target delegate 'EventHandler<StripeEventNotificationEventArgs<V1BillingMeterErrorReportTriggeredEventNotification>>' (possibly because of nullability attributes).
}

private void HandleBillingMeterErrorReportTriggeredEventNotification(object sender, Stripe.StripeEventNotificationEventArgs<Stripe.Events.V1BillingMeterErrorReportTriggeredEventNotification> e)
{
var meter = e.EventNotification.FetchRelatedObject();
Console.WriteLine($"Meter {meter.DisplayName} had an error");
}

private void FallbackCallback(object sender, Stripe.StripeUnhandledEventNotificationEventArgs e)
{
Console.WriteLine($"Received unhandled event notification type: {e.EventNotification.Type}");
}

[HttpPost]

Check failure on line 50 in src/Examples/V2/EventNotificationHandlerEndpoint.cs

View workflow job for this annotation

GitHub Actions / Build examples

The type or namespace name 'HttpPost' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 50 in src/Examples/V2/EventNotificationHandlerEndpoint.cs

View workflow job for this annotation

GitHub Actions / Build examples

The type or namespace name 'HttpPostAttribute' could not be found (are you missing a using directive or an assembly reference?)
public async Task<IActionResult> Index()

Check failure on line 51 in src/Examples/V2/EventNotificationHandlerEndpoint.cs

View workflow job for this annotation

GitHub Actions / Build examples

The type or namespace name 'IActionResult' could not be found (are you missing a using directive or an assembly reference?)
{
var json = await new StreamReader(HttpContext.Request.Body).ReadToEndAsync();
handler.Handle(json, Request.Headers["Stripe-Signature"]);
return Ok();
}
}
}
3 changes: 2 additions & 1 deletion src/Stripe.net/Entities/V2/Core/EventNotification.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// File copied from our code generator; changes here will be overwritten.
namespace Stripe.V2.Core
{
using System;
Expand Down Expand Up @@ -133,4 +134,4 @@ protected virtual async Task<T> FetchRelatedObjectAsync<T>(EventNotificationRela
return this.Client.Deserialize<T>(res.Content);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// File copied from our code generator; changes here will be overwritten.
namespace Stripe.V2.Core
{
using Newtonsoft.Json;
Expand All @@ -22,4 +23,4 @@ public class EventNotificationReason
[STJS.JsonPropertyName("request")]
public EventNotificationReasonRequest Request { get; set; }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// File copied from our code generator; changes here will be overwritten.
namespace Stripe.V2.Core
{
using Newtonsoft.Json;
Expand All @@ -22,4 +23,4 @@ public class EventNotificationReasonRequest
[STJS.JsonPropertyName("idempotency_key")]
public string IdempotencyKey { get; set; }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// File copied from our code generator; changes here will be overwritten.
#nullable disable

namespace Stripe.V2.Core
Expand All @@ -22,4 +23,4 @@ public class EventNotificationRelatedObject
[STJS.JsonInclude]
public string Url { get; internal set; }
}
}
}
3 changes: 2 additions & 1 deletion src/Stripe.net/Events/UnknownEventNotification.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// File copied from our code generator; changes here will be overwritten.
namespace Stripe.Events
{
using System;
Expand Down Expand Up @@ -51,4 +52,4 @@ public Task<StripeEntity> FetchRelatedObjectAsync()
return this.FetchRelatedObjectAsync<StripeEntity>(this.RelatedObject);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// File copied from our code generator; changes here will be overwritten.
namespace Stripe.Infrastructure
{
using System;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// File copied from our code generator; changes here will be overwritten.
namespace Stripe.Infrastructure
{
using System;
Expand Down
7 changes: 7 additions & 0 deletions src/Stripe.net/Infrastructure/Public/LiveApiRequestor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ public LiveApiRequestor(StripeClientOptions options, List<string> defaultUsage =
/// <value>The <see cref="IHttpClient"/> used to send HTTP requests.</value>
public override IHttpClient HttpClient { get; }

/// <summary>Gets the current StripeContext for this requestor; Used in unit tests.</summary>
/// <value>The current StripeContext.</value>
internal StripeContext CurrentStripeContext
{
get => this.clientOptions.StripeContext;
}

/// <summary>Sends a request to Stripe's API as an asynchronous operation.</summary>
/// <typeparam name="T">Type of the Stripe entity returned by the API.</typeparam>
/// <param name="baseAddress">The base address of the request.</param>
Expand Down
11 changes: 11 additions & 0 deletions src/Stripe.net/Infrastructure/Public/StripeClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,17 @@ public EventNotification ParseEventNotification(
return EventNotification.FromJson(json, this);
}

/// <summary>
/// Create a new StripeEventNotificationHandler bound to this StripeClient.
/// </summary>
/// <param name="webhookSecret">The secret for this event destination, e.g. "wh_sec_...".</param>
/// <param name="fallbackCallback">The function to call when handing an event for whom there's no callback registered.</param>
/// <returns></returns>
public StripeEventNotificationHandler NotificationHandler(string webhookSecret, Action<object, StripeUnhandledEventNotificationEventArgs> fallbackCallback)
{
return new StripeEventNotificationHandler(this, webhookSecret, fallbackCallback);
}

internal JsonSerializerSettings GetJsonSerializationSettings()
{
return this.jsonSerializerSettings;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// File copied from our code generator; changes here will be overwritten.
namespace Stripe
{
using System;

/// <summary>
/// EventArgs for Stripe webhook event notifications.
/// Contains the strongly-typed EventNotification and the StripeClient instance.
/// </summary>
/// <typeparam name="TEventNotification">The type of EventNotification.</typeparam>
public class StripeEventNotificationEventArgs<TEventNotification> : EventArgs
where TEventNotification : V2.Core.EventNotification
{
/// <summary>
/// Initializes a new instance of the <see cref="StripeEventNotificationEventArgs{TEventNotification}"/> class.
/// </summary>
/// <param name="eventNotification">The event notification instance.</param>
/// <param name="client">The StripeClient instance.</param>
public StripeEventNotificationEventArgs(TEventNotification eventNotification, StripeClient client)
{
if (eventNotification == null)
{
throw new ArgumentNullException(nameof(eventNotification));
}

this.EventNotification = eventNotification;

if (client == null)
{
throw new ArgumentNullException(nameof(client));
}

this.Client = client;
}

/// <summary>
/// Gets the event notification instance.
/// </summary>
public TEventNotification EventNotification { get; }

/// <summary>
/// Gets the StripeClient instance that can be used to make API requests.
/// </summary>
public StripeClient Client { get; }
}
}
Loading
Loading