-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separating and adding logic supporting stripe subscription
- Loading branch information
Showing
25 changed files
with
711 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/Modules/OrchardCore.Commerce.Payment.Stripe/Indexes/StripeSessionDataIndex.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using OrchardCore.Commerce.Payment.Stripe.Models; | ||
using YesSql.Indexes; | ||
|
||
namespace OrchardCore.Commerce.Payment.Stripe.Indexes; | ||
|
||
public class StripeSessionDataIndex : MapIndex | ||
{ | ||
public string UserId { get; set; } | ||
public string StripeCustomerId { get; set; } | ||
public string StripeSessionId { get; set; } | ||
public string StripeSessionUrl { get; set; } | ||
public string StripeInvoiceId { get; set; } | ||
public string SerializedAdditionalData { get; set; } | ||
} | ||
|
||
public class StripeSessionDataIndexProvider : IndexProvider<StripeSessionData> | ||
{ | ||
public override void Describe(DescribeContext<StripeSessionData> context) => | ||
context.For<StripeSessionDataIndex>() | ||
.Map(sessionData => new StripeSessionDataIndex | ||
{ | ||
UserId = sessionData.UserId, | ||
StripeCustomerId = sessionData.StripeCustomerId, | ||
StripeSessionId = sessionData.StripeSessionId, | ||
StripeSessionUrl = sessionData.StripeSessionUrl, | ||
StripeInvoiceId = sessionData.StripeInvoiceId, | ||
SerializedAdditionalData = sessionData.SerializedAdditionalData, | ||
}); | ||
} |
24 changes: 24 additions & 0 deletions
24
src/Modules/OrchardCore.Commerce.Payment.Stripe/Migrations/StripeSessionMigrations.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using OrchardCore.Commerce.Payment.Stripe.Indexes; | ||
using OrchardCore.Data.Migration; | ||
using System.Threading.Tasks; | ||
using YesSql.Sql; | ||
|
||
namespace OrchardCore.Commerce.Payment.Stripe.Migrations; | ||
|
||
public class StripeSessionMigrations : DataMigration | ||
{ | ||
public async Task<int> CreateAsync() | ||
{ | ||
await SchemaBuilder | ||
.CreateMapIndexTableAsync<StripeSessionDataIndex>(table => table | ||
.Column<string>(nameof(StripeSessionDataIndex.UserId)) | ||
.Column<string>(nameof(StripeSessionDataIndex.StripeCustomerId)) | ||
.Column<string>(nameof(StripeSessionDataIndex.StripeSessionId)) | ||
.Column<string>(nameof(StripeSessionDataIndex.StripeSessionUrl)) | ||
.Column<string>(nameof(StripeSessionDataIndex.StripeInvoiceId)) | ||
.Column<string>(nameof(StripeSessionDataIndex.SerializedAdditionalData)) | ||
); | ||
|
||
return 1; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/Modules/OrchardCore.Commerce.Payment.Stripe/Models/StripeCustomer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace OrchardCore.Commerce.Payment.Stripe.Models; | ||
|
||
public class StripeCustomer | ||
{ | ||
public string CustomerId { get; set; } | ||
} |
11 changes: 11 additions & 0 deletions
11
src/Modules/OrchardCore.Commerce.Payment.Stripe/Models/StripeSessionData.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace OrchardCore.Commerce.Payment.Stripe.Models; | ||
|
||
public class StripeSessionData | ||
{ | ||
public string UserId { get; set; } | ||
public string StripeCustomerId { get; set; } | ||
public string StripeSessionId { get; set; } | ||
public string StripeSessionUrl { get; set; } | ||
public string StripeInvoiceId { get; set; } | ||
public string SerializedAdditionalData { get; set; } | ||
} |
9 changes: 9 additions & 0 deletions
9
src/Modules/OrchardCore.Commerce.Payment.Stripe/Models/StripeSessionDataSave.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace OrchardCore.Commerce.Payment.Stripe.Models; | ||
|
||
public class StripeSessionDataSave | ||
{ | ||
public StripeSessionData StripeSessionData { get; set; } | ||
public IEnumerable<string> Errors { get; set; } | ||
} |
40 changes: 40 additions & 0 deletions
40
src/Modules/OrchardCore.Commerce.Payment.Stripe/Services/DefaultStripeWebhookEventHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using OrchardCore.Commerce.Payment.Stripe.Abstractions; | ||
using Stripe; | ||
using System.Threading.Tasks; | ||
using static Stripe.Events; | ||
|
||
namespace OrchardCore.Commerce.Payment.Stripe.Services; | ||
|
||
public class DefaultStripeWebhookEventHandler : IStripeWebhookEventHandler | ||
{ | ||
private readonly IStripePaymentService _stripePaymentService; | ||
|
||
public DefaultStripeWebhookEventHandler(IStripePaymentService stripePaymentService) => | ||
_stripePaymentService = stripePaymentService; | ||
|
||
public async Task ReceivedStripeEventAsync(Event stripeEvent) | ||
{ | ||
if (stripeEvent.Type == ChargeSucceeded) | ||
{ | ||
var charge = stripeEvent.Data.Object as Charge; | ||
if (charge?.PaymentIntentId is not { } paymentIntentId) | ||
{ | ||
return; | ||
} | ||
|
||
// If the charge is associated with a customer, it means it's a subscription payment in the current implementation. | ||
if (!string.IsNullOrEmpty(charge.CustomerId)) | ||
{ | ||
return; | ||
} | ||
|
||
var paymentIntent = await _stripePaymentService.GetPaymentIntentAsync(paymentIntentId); | ||
await _stripePaymentService.UpdateOrderToOrderedAsync(paymentIntent, shoppingCartId: null); | ||
} | ||
else if (stripeEvent.Type == PaymentIntentPaymentFailed) | ||
{ | ||
var paymentIntent = stripeEvent.Data.Object as PaymentIntent; | ||
await _stripePaymentService.UpdateOrderToPaymentFailedAsync(paymentIntent.Id); | ||
} | ||
} | ||
} |
Oops, something went wrong.