Skip to content
Open
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
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<VersionPrefix>10.7.1</VersionPrefix>
<VersionPrefix>10.12.1</VersionPrefix>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<Title>Stripe Checkout 2.0</Title>
<Description>Stripe Checkout handler</Description>
Expand Down Expand Up @@ -34,9 +34,9 @@
<EmbeddedResource Include="Updates\Post_inline.cshtml" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Dynamicweb" Version="10.7.3" />
<PackageReference Include="Dynamicweb.Core" Version="10.7.3" />
<PackageReference Include="Dynamicweb.Ecommerce" Version="10.7.3" />
<PackageReference Include="Dynamicweb" Version="10.12.4" />
<PackageReference Include="Dynamicweb.Core" Version="10.12.4" />
<PackageReference Include="Dynamicweb.Ecommerce" Version="10.12.4" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>
Expand Down
3 changes: 3 additions & 0 deletions src/Models/Customer/Customer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,7 @@ internal sealed class Customer

[DataMember(Name = "livemode")]
public bool Livemode { get; set; }

[DataMember(Name = "deleted")]
public bool Deleted { get; set; }
}
17 changes: 12 additions & 5 deletions src/Service/StripeService.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using Dynamicweb.Core;
using Dynamicweb.Configuration;
using Dynamicweb.Core;
using Dynamicweb.Ecommerce.CheckoutHandlers.StripeCheckout.Models;
using Dynamicweb.Ecommerce.CheckoutHandlers.StripeCheckout.Models.Customer;
using Dynamicweb.Ecommerce.CheckoutHandlers.StripeCheckout.Models.Error;
using Dynamicweb.Ecommerce.CheckoutHandlers.StripeCheckout.Models.PaymentIntent;
using Dynamicweb.Ecommerce.CheckoutHandlers.StripeCheckout.Models.Refund;
using Dynamicweb.Ecommerce.CheckoutHandlers.StripeCheckout.Models.Customer;
using Dynamicweb.Ecommerce.Orders;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -513,21 +514,27 @@ public Session GetSession(string sessionId)

private void SetShippingParameters(Order order, Dictionary<string, object> parameters, string shippingParameter)
{
if (string.IsNullOrWhiteSpace(GetAddress()) && SystemConfiguration.Instance.GetBoolean("/Globalsettings/Ecom/Cart/CopyCustomerFieldsToDelivery"))
Services.Carts.CopyCustomerFieldsToDelivery(order);

//name is required parameter, so we should fill it by something
string recipientName = !string.IsNullOrWhiteSpace(order.DeliveryName) ? order.DeliveryName : order.DeliveryMiddleName;
if (string.IsNullOrWhiteSpace(shippingParameter))
recipientName = "Recipient name";
if (string.IsNullOrWhiteSpace(recipientName))
recipientName = "Recipient";

parameters[$"{shippingParameter}[name]"] = recipientName;
parameters[$"{shippingParameter}[phone]"] = order.DeliveryPhone;

string addressParameter = $"{shippingParameter}[address]";

//line 1 is required parameter
parameters[$"{addressParameter}[line1]"] = !string.IsNullOrWhiteSpace(order.DeliveryAddress) ? order.DeliveryAddress : order.DeliveryAddress2;
parameters[$"{addressParameter}[line1]"] = GetAddress();
parameters[$"{addressParameter}[line2]"] = order.DeliveryAddress2;
parameters[$"{addressParameter}[state]"] = order.DeliveryRegion;
parameters[$"{addressParameter}[city]"] = order.DeliveryCity;
parameters[$"{addressParameter}[country]"] = order.DeliveryCountryCode;
parameters[$"{addressParameter}[postal_code]"] = order.DeliveryZip;

string GetAddress() => !string.IsNullOrWhiteSpace(order.DeliveryAddress) ? order.DeliveryAddress : order.DeliveryAddress2;
}
}
41 changes: 32 additions & 9 deletions src/StripeCheckout.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Dynamicweb.Core;
using Dynamicweb.Ecommerce.Cart;
using Dynamicweb.Ecommerce.CheckoutHandlers.StripeCheckout.Models;
using Dynamicweb.Ecommerce.CheckoutHandlers.StripeCheckout.Models.Customer;
using Dynamicweb.Ecommerce.CheckoutHandlers.StripeCheckout.Models.PaymentIntent;
using Dynamicweb.Ecommerce.CheckoutHandlers.StripeCheckout.Models.PaymentMethod;
using Dynamicweb.Ecommerce.CheckoutHandlers.StripeCheckout.Models.Refund;
Expand Down Expand Up @@ -76,7 +77,7 @@

[AddInParameter("Capture now"), AddInParameterEditor(typeof(YesNoParameterEditor), "infoText=Auto-captures a payment when it is authorized. Please note that it is illegal in some countries to capture payment before shipping any physical goods.;")]
public bool CaptureNow { get; set; }

// <summary>
/// Gets or sets post mode indicates how user will be redirected to Stripe service
/// </summary>
Expand All @@ -85,7 +86,7 @@
{
get => PostMode.ToString();
set
{
{
PostMode = value switch
{
nameof(PostModes.Auto) => PostModes.Auto,
Expand Down Expand Up @@ -187,17 +188,32 @@
var cardSettings = new BasePaymentCardSettings(order);
string action = recurring ? "CompleteSetup" : "Complete";

PaymentCardToken savedCard = Services.PaymentCard.GetByUserId(order.CustomerAccessUserId).FirstOrDefault(card => !string.IsNullOrEmpty(card.Token));
string[] cardToken = savedCard?.Token?.Split('|');
PaymentCardToken savedCard = Services.PaymentCard.GetByUserId(order.CustomerAccessUserId)
.FirstOrDefault(card => order.PaymentMethodId.Equals(card.PaymentID, StringComparison.OrdinalIgnoreCase) && !string.IsNullOrEmpty(card.Token));
string[] cardToken = savedCard?.Token?.Split('|');
string customerId = cardToken?.ElementAtOrDefault(0);

var service = new StripeService(GetSecretKey());
string idempotencyKey = IdempotencyKeyHelper.GetKey(ApiCommand.CreateSession, MerchantName, order.Id);

if (!string.IsNullOrWhiteSpace(customerId))
{
try
{
if (service.GetCustomer(customerId) is not Customer customer || customer.Deleted)
customerId = null;
}
catch
{
customerId = null;
}
}

string idempotencyKey = IdempotencyKeyHelper.GetKey(ApiCommand.CreateSession, MerchantName, order.Id);
Session session = service.CreateSession(idempotencyKey, order, new()
{
AutomaticCapture = CaptureNow,
SavePaymentMethod = cardSettings.IsSaveNeeded && SaveCards,
CustomerId = cardToken?.ElementAtOrDefault(0),
CustomerId = customerId,
Language = Language,
Mode = recurring ? SessionMode.Setup : SessionMode.Payment,
EmbeddedForm = PostMode is PostModes.InlineTemplate,
Expand Down Expand Up @@ -467,12 +483,13 @@
private OutputResult UseSavedCardInternal(Order order, bool recurringOrderPayment)
{
PaymentCardToken savedCard = Services.PaymentCard.GetById(order.SavedCardId);
if (savedCard is null || order.CustomerAccessUserId != savedCard.UserID)
if (savedCard is null || order.CustomerAccessUserId != savedCard.UserID || !order.PaymentMethodId.Equals(savedCard.PaymentID, StringComparison.OrdinalIgnoreCase))
throw new PaymentCardTokenException("Token is incorrect.");

LogEvent(order, "Using saved card({0}) with id: {1}", savedCard.Identifier, savedCard.ID);

var cardTokenKey = CardTokenKey.Parse(savedCard.Token);
string errorMessage = null;
try
{
var service = new StripeService(GetSecretKey());
Expand All @@ -497,19 +514,25 @@
CustomerId = cardTokenKey.CustomerId,
PaymentMethodId = cardTokenKey.PaymentMethodId
});

if (setupIntent.Status is not PaymentIntentStatus.Succeeded)
throw new Exception("Something went wrong during setup intent creation using saved card data. Probably the payment method is outdated or wasn't configured for off payments. Try to create recurring order using new card data.");

return new RedirectOutputResult { RedirectUrl = $"{GetBaseUrl(order)}&Action=CompleteSetup&setup_intent={setupIntent.Id}&SaveCard=false" };
}
}
catch
catch (Exception ex)
{
errorMessage = ex.Message;
CheckoutDone(order);
}

if (!order.Complete)
throw new Exception("Called create charge, but order is not set complete.");
{
if (string.IsNullOrWhiteSpace(errorMessage))
errorMessage = "Something went wrong during payment operation. Probably the saved card token is outdated. Try to use another saved card or create new.";
throw new Exception(errorMessage);
}

return PassToCart(order);
}
Expand Down Expand Up @@ -916,7 +939,7 @@
/// <summary>
/// A temporary method to maintain previous behavior. Redirects to cart by Response.Redirect. Please remove it when the needed changes will be done.
/// </summary>
private void RedirectToCart(RedirectOutputResult redirectResult) => Context.Current.Response.Redirect(redirectResult.RedirectUrl, redirectResult.IsPermanent);

Check warning on line 942 in src/StripeCheckout.cs

View workflow job for this annotation

GitHub Actions / call-workflow / Build

'IResponse.Redirect(string, bool)' is obsolete: 'Do not use'

Check warning on line 942 in src/StripeCheckout.cs

View workflow job for this annotation

GitHub Actions / call-workflow / Build

'IResponse.Redirect(string, bool)' is obsolete: 'Do not use'

private string GetPropertyLabel(string propertyName)
{
Expand Down