Skip to content

Commit

Permalink
Added Livemode and Reason properties to ThinEvent (#2995)
Browse files Browse the repository at this point in the history
  • Loading branch information
jar-stripe authored Oct 3, 2024
1 parent f8b339e commit 29c695e
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Stripe.net/Infrastructure/Public/ThinEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace Stripe
{
using System;
using Newtonsoft.Json;
using Stripe.V2;

/// <summary>
/// A pushed thin event. Use this the Id with the <see cref="Stripe.V2.Core.EventService"/>
Expand All @@ -27,7 +28,19 @@ public class ThinEvent
[JsonProperty("created")]
public DateTime Created { get; internal set; }

/// <summary>
/// Livemode indicates if the event is from a production(true) or test(false) account.
/// </summary>
[JsonProperty("livemode")]
public bool Livemode { get; internal set; }

#nullable enable
/// <summary>
/// [Optional] Reason for the event.
/// </summary>
[JsonProperty("reason")]
public EventReason? Reason { get; internal set; }

/// <summary>
/// [Optional] Authentication context needed to fetch the event or related object.
/// </summary>
Expand Down
61 changes: 61 additions & 0 deletions src/StripeTests/Events/V2/EventTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class EventTest : BaseStripeTest
""object"": ""event"",
""type"": ""this.event.doesnt.exist"",
""created"": ""2022-02-15T00:27:45.330Z"",
""livemode"": true,
""related_object"": {
""id"": ""fa_123"",
""type"": ""financial_account"",
Expand All @@ -43,6 +44,7 @@ public class EventTest : BaseStripeTest
""object"": ""event"",
""type"": ""v1.billing.meter.no_meter_found"",
""created"": ""2022-02-15T00:27:45.330Z"",
""livemode"": true,
}";

private static string v2KnownEventPayload =
Expand All @@ -52,6 +54,7 @@ public class EventTest : BaseStripeTest
""type"": ""v1.billing.meter.error_report_triggered"",
""created"": ""2022-02-15T00:27:45.330Z"",
""context"": ""context 123"",
""livemode"": true,
""related_object"": {
""id"": ""me_123"",
""type"": ""billing.meter"",
Expand All @@ -67,6 +70,7 @@ public class EventTest : BaseStripeTest
""type"": ""v1.billing.meter.error_report_triggered"",
""created"": ""2022-02-15T00:27:45.330Z"",
""context"": ""context 123"",
""livemode"": true,
""related_object"": {
""id"": ""me_123"",
""type"": ""billing.meter"",
Expand Down Expand Up @@ -95,6 +99,31 @@ public class EventTest : BaseStripeTest
}
}";

private static string v2KnownEventLivemodeFalsePayload =
@"{
""id"": ""evt_234"",
""object"": ""event"",
""type"": ""v1.billing.meter.no_meter_found"",
""created"": ""2022-02-15T00:27:45.330Z"",
""livemode"": false,
}";

private static string v2KnownEventWithReasonPayload =
@"{
""id"": ""evt_234"",
""object"": ""event"",
""type"": ""v1.billing.meter.no_meter_found"",
""created"": ""2022-02-15T00:27:45.330Z"",
""livemode"": true,
""reason"": {
""type"": ""a.b.c"",
""request"": {
""id"": ""r_123"",
""idempotency_key"": ""key""
}
}
}";

private StripeClient stripeClient;

public EventTest(MockHttpClientFixture mockHttpClientFixture)
Expand Down Expand Up @@ -162,6 +191,7 @@ public void ParseThinEventWithoutRelatedObject()
Assert.Equal("evt_234", baseThinEvent.Id);
Assert.Equal("v1.billing.meter.no_meter_found", baseThinEvent.Type);
Assert.Equal(new DateTime(2022, 2, 15, 0, 27, 45, 330, DateTimeKind.Utc), baseThinEvent.Created);
Assert.True(baseThinEvent.Livemode);
Assert.Null(baseThinEvent.Context);
Assert.Null(baseThinEvent.RelatedObject);
}
Expand All @@ -174,6 +204,7 @@ public void ParseThinEventWithRelatedObject()
Assert.Equal("evt_234", baseThinEvent.Id);
Assert.Equal("v1.billing.meter.error_report_triggered", baseThinEvent.Type);
Assert.Equal(new DateTime(2022, 2, 15, 0, 27, 45, 330, DateTimeKind.Utc), baseThinEvent.Created);
Assert.True(baseThinEvent.Livemode);
Assert.Equal("context 123", baseThinEvent.Context);
Assert.NotNull(baseThinEvent.RelatedObject);
Assert.Equal("me_123", baseThinEvent.RelatedObject.Id);
Expand All @@ -193,6 +224,36 @@ public void ParseUnknownEvent()
Assert.Equal(this.stripeClient.Requestor, stripeEvent.Requestor);
}

[Fact]
public void ParseThinEventWithLivemodeFalse()
{
var baseThinEvent = this.stripeClient.ParseThinEvent(v2KnownEventLivemodeFalsePayload, GenerateSigHeader(v2KnownEventLivemodeFalsePayload), WebhookSecret);
Assert.NotNull(baseThinEvent);
Assert.Equal("evt_234", baseThinEvent.Id);
Assert.Equal("v1.billing.meter.no_meter_found", baseThinEvent.Type);
Assert.Equal(new DateTime(2022, 2, 15, 0, 27, 45, 330, DateTimeKind.Utc), baseThinEvent.Created);
Assert.False(baseThinEvent.Livemode);
Assert.Null(baseThinEvent.Context);
Assert.Null(baseThinEvent.RelatedObject);
}

[Fact]
public void ParseThinEventWithReason()
{
var baseThinEvent = this.stripeClient.ParseThinEvent(v2KnownEventWithReasonPayload, GenerateSigHeader(v2KnownEventWithReasonPayload), WebhookSecret);
Assert.NotNull(baseThinEvent);
Assert.Equal("evt_234", baseThinEvent.Id);
Assert.Equal("v1.billing.meter.no_meter_found", baseThinEvent.Type);
Assert.Equal(new DateTime(2022, 2, 15, 0, 27, 45, 330, DateTimeKind.Utc), baseThinEvent.Created);
Assert.True(baseThinEvent.Livemode);
Assert.Null(baseThinEvent.Context);
Assert.Null(baseThinEvent.RelatedObject);
Assert.NotNull(baseThinEvent.Reason);
Assert.Equal("a.b.c", baseThinEvent.Reason.Type);
Assert.Equal("r_123", baseThinEvent.Reason.Request.Id);
Assert.Equal("key", baseThinEvent.Reason.Request.IdempotencyKey);
}

[Fact]
public void ParseThinEventWithInvalidSignature()
{
Expand Down

0 comments on commit 29c695e

Please sign in to comment.