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
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
For plugin configuration please check:<br/>
<br/>
1. Generate API keys from the Stripe Dashboard: <a href="https://stripe.com/docs/api/authentication" target="_blank">Click here.</a><br/>
2. Register webhook endpoints with Stripe <a href="https://stripe.com/docs/development/dashboard/register-webhook" target="_blank">Click here.</a><br/>
2. Register webhook endpoints with Stripe <a href="https://stripe.com/docs/development/dashboard/register-webhook" target="_blank">Click here.</a>
Send it the event <code>payment_intent.succeeded</code>; to sell recurring products, also <code>checkout.session.completed</code>, <code>invoice.paid</code>, <code>invoice.payment_failed</code> and <code>customer.subscription.deleted</code>.<br/>
3. Your Webhook URL: @webhookUrl <br/>
4. Fill in your credentials below.<br/>
5. Save the configuration.<br/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
For plugin configuration please check:<br/>
<br/>
1. Generate API keys from the Stripe Dashboard: <a href="https://stripe.com/docs/api/authentication" target="_blank">Click here.</a><br/>
2. Register webhook endpoints with Stripe <a href="https://stripe.com/docs/development/dashboard/register-webhook" target="_blank">Click here.</a><br/>
2. Register webhook endpoints with Stripe <a href="https://stripe.com/docs/development/dashboard/register-webhook" target="_blank">Click here.</a>
Send it the event <code>payment_intent.succeeded</code>; to sell recurring products, also <code>checkout.session.completed</code>, <code>invoice.paid</code>, <code>invoice.payment_failed</code> and <code>customer.subscription.deleted</code>.<br/>
3. Your Webhook URL: @ViewBag.WebhookUrl <br/>
4. Fill in your credentials below.<br/>
5. Save the configuration.<br/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ public interface IStripeCheckoutService
{
Task<string> CreateRedirectUrl(Order order);
Task<IList<string>> Refund(PaymentTransaction paymentTransaction, double? amount);
Task<IList<string>> CancelSubscription(RecurringPayment recurringPayment);
Task<bool> WebHookProcessPayment(string stripeSignature, string json);
}
260 changes: 245 additions & 15 deletions src/Plugins/Payments.StripeCheckout/Services/StripeCheckoutService.cs

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions src/Plugins/Payments.StripeCheckout/StripeCheckoutDefaults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@ public static class StripeCheckoutDefaults
public const string ConfigurationUrl = "/Admin/StripeCheckout/Configure";
public static string WebHook => "Plugin.Payments.StripeCheckout.WebHook";
public static string PaymentInfo => "Plugin.Payments.StripeCheckout.PaymentInfo";

/// <summary>
/// How long one call to Stripe may take: well below 10 s, as Stripe.net's own 80 s is too long for a webhook,
/// a checkout or a cancel to wait
/// </summary>
public static TimeSpan RequestTimeout => TimeSpan.FromSeconds(5);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace Payments.StripeCheckout;
public class StripeCheckoutPaymentProvider : IPaymentProvider
{
private readonly IOrderService _orderService;
private readonly IRecurringPaymentService _recurringPaymentService;
private readonly StripeCheckoutPaymentSettings _stripeCheckoutPaymentSettings;
private readonly IStripeCheckoutService _stripeCheckoutService;
private readonly IRequestTranslationService _translationService;
Expand All @@ -21,10 +22,12 @@ public class StripeCheckoutPaymentProvider : IPaymentProvider
public StripeCheckoutPaymentProvider(
IRequestTranslationService translationService,
IOrderService orderService,
IRecurringPaymentService recurringPaymentService,
StripeCheckoutPaymentSettings stripeCheckoutPaymentSettings, IStripeCheckoutService stripeCheckoutService)
{
_translationService = translationService;
_orderService = orderService;
_recurringPaymentService = recurringPaymentService;
_stripeCheckoutPaymentSettings = stripeCheckoutPaymentSettings;
_stripeCheckoutService = stripeCheckoutService;
}
Expand Down Expand Up @@ -173,7 +176,15 @@ public async Task<bool> CanRePostRedirectPayment(PaymentTransaction paymentTrans
if ((DateTime.UtcNow - paymentTransaction.CreatedOnUtc).TotalMinutes < 1)
return false;

return await Task.FromResult(true);
//the customer completed the checkout and Stripe runs the subscription, whose first invoice is still to be
//reported: paying again would start a second subscription charging every cycle twice
var order = await _orderService.GetOrderByGuid(paymentTransaction.OrderGuid);
if (order != null &&
await _recurringPaymentService.GetRecurringPaymentByInitialOrderId(order.Id) is { } recurringPayment &&
!string.IsNullOrEmpty(recurringPayment.ProviderReference))
return false;

return true;
}

/// <summary>
Expand Down Expand Up @@ -229,6 +240,27 @@ public async Task<bool> SupportVoid()
return await Task.FromResult(false);
}

/// <summary>
/// Gets a value indicating whether recurring payments are supported: a recurring order is paid with a Stripe
/// subscription, whose invoices report every cycle
/// </summary>
public Task<bool> SupportRecurringPayment()
{
return Task.FromResult(true);
}

/// <summary>
/// Cancels the Stripe subscription of a recurring payment
/// </summary>
public async Task<CancelRecurringPaymentResult> CancelRecurringPayment(RecurringPayment recurringPayment)
{
var result = new CancelRecurringPaymentResult();
foreach (var error in await _stripeCheckoutService.CancelSubscription(recurringPayment))
result.AddError(error);

return result;
}

/// <summary>
/// Gets a payment method type
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Grand.Business.Core.Interfaces.Checkout.Orders;
using Grand.Business.Core.Utilities.Checkout;
using Grand.Domain.Orders;
using Grand.Domain.Payments;
using Grand.Web.Common.Localization;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand All @@ -23,8 +24,71 @@ public void Init()
_stripeCheckoutService = new Mock<IStripeCheckoutService>();
_stripeCheckoutService.Setup(s => s.Refund(It.IsAny<PaymentTransaction>(), It.IsAny<double?>()))
.ReturnsAsync(new List<string>());
_orderService = new Mock<IOrderService>();
_recurringPaymentService = new Mock<IRecurringPaymentService>();
_provider = new StripeCheckoutPaymentProvider(Mock.Of<IRequestTranslationService>(),
Mock.Of<IOrderService>(), new StripeCheckoutPaymentSettings(), _stripeCheckoutService.Object);
_orderService.Object, _recurringPaymentService.Object, new StripeCheckoutPaymentSettings(),
_stripeCheckoutService.Object);
}

private Mock<IOrderService> _orderService;
private Mock<IRecurringPaymentService> _recurringPaymentService;

[TestMethod]
public async Task SupportRecurringPayment_IsTrue()
{
Assert.IsTrue(await _provider.SupportRecurringPayment());
}

[TestMethod]
public async Task CancelRecurringPayment_CancelsTheSubscription()
{
var recurringPayment = new RecurringPayment { ProviderReference = "sub_1" };
_stripeCheckoutService.Setup(s => s.CancelSubscription(recurringPayment)).ReturnsAsync(new List<string>());

Assert.IsTrue((await _provider.CancelRecurringPayment(recurringPayment)).Success);
_stripeCheckoutService.Verify(s => s.CancelSubscription(recurringPayment), Times.Once);
}

[TestMethod]
public async Task CancelRecurringPayment_ServiceErrors_AreReturned()
{
_stripeCheckoutService.Setup(s => s.CancelSubscription(It.IsAny<RecurringPayment>()))
.ReturnsAsync(new List<string> { "No such subscription" });

var result = await _provider.CancelRecurringPayment(new RecurringPayment());

Assert.IsFalse(result.Success);
CollectionAssert.Contains(result.Errors.ToList(), "No such subscription");
}

private PaymentTransaction OldTransaction()
{
var orderGuid = Guid.NewGuid();
_orderService.Setup(x => x.GetOrderByGuid(orderGuid)).ReturnsAsync(new Order { Id = "o1" });
return new PaymentTransaction { OrderGuid = orderGuid, CreatedOnUtc = DateTime.UtcNow.AddMinutes(-5) };
}

[TestMethod]
public async Task CanRePostRedirectPayment_StandardOrPendingRecurringOrder_IsTrue()
{
var transaction = OldTransaction();
Assert.IsTrue(await _provider.CanRePostRedirectPayment(transaction));

_recurringPaymentService.Setup(x => x.GetRecurringPaymentByInitialOrderId("o1"))
.ReturnsAsync(new RecurringPayment());
Assert.IsTrue(await _provider.CanRePostRedirectPayment(transaction));
}

[TestMethod]
public async Task CanRePostRedirectPayment_SubscriptionStarted_IsFalse()
{
//paying again would start a second subscription
var transaction = OldTransaction();
_recurringPaymentService.Setup(x => x.GetRecurringPaymentByInitialOrderId("o1"))
.ReturnsAsync(new RecurringPayment { ProviderReference = "sub_1" });

Assert.IsFalse(await _provider.CanRePostRedirectPayment(transaction));
}

[TestMethod]
Expand Down
Loading
Loading