1 min read
Checkout Service
CheckoutService is found in Poq.Shopify.Services
namespace. Checkout service uses only Shopify Storefront API to store data and can be used for both feedless and feed implementation of BFC. It requires several dependencies to be used in your backend. All required dependencies should be registered using IoC:
services.AddScoped<ShopifyClientConfiguration, ShopifyClientConfiguration>();services.AddScoped<IShopifyCheckoutClient, ShopifyCheckoutClient>();services.AddScoped<IShopifyCartClient, ShopifyCartClient>();services.AddScoped<IAccountService, AccountService>();services.AddScoped<ICheckoutResponseModifier, CheckoutResponseModifier>();services.AddScoped<IShopifyMultipassTokenGenerator, ShopifyMultipassTokenGenerator>();services.AddScoped<ICheckoutService, CheckoutService>();
Explanation of services
ShopifyClientConfiguration
is part of Shopify SDK. It should be registered in IoC with correct settings.IShopifyCheckoutClient
is part of Shopify SDK. It should be registered in IoC with all required dependencies.IShopifyCartClient
is part of Shopify SDK. It should be registered in IoC with all required dependencies.IAccountService
is available as part of Shopify SDK.ICheckoutResponseModifier
with implementationCheckoutResponseModifier
is responsible of creation response object returned fromcheckout/start
.IShopifyMultipassTokenGenerator
with implementationShopifyMultipassTokenGenerator
This service is used only when MultipassToken is provided in configuration. It generates new access token to transfer shopify checkout.ICheckoutService
with implementationCheckoutService
Checkout Service transfers Cart information from Poq Cart Api to Shopify. Service functionality and order items mapping can be modified by providing custom implementation ofICheckoutResponseModifier
.
Here in an example of overriding default handling in ICheckoutResponseModifier
.
services.AddScoped<ICheckoutResponseModifier, MyCheckoutResponseModifier>();
public class MyCheckoutResponseModifier : ICheckoutResponseModifier{ Task<StartCartTransferResponse> CreateCheckoutResponseAsync(ShopifyCheckout shopifyCheckout, OrderInStartCartTransferResponse order) { var response = new StartCartTransferResponse { Url = shopifyCheckout.Url, Method = "GET", Order = order, }; return Task.FromResult(response); }
public OrderInStartCartTransferResponse CreateOrderResponse(CartWithProductsResponse cart, string channel, string currency) { var orderResponse = new OrderInStartCartTransferResponse { Channel = channel, Currency = currency, Reference = Guid.NewGuid().ToString(), Subtotal = cart.Total.Now };
foreach (var cartItem in cart.CartItems) { var oderResponseItem = new OrderItemInStartCartTransferResponse { CartItemId = cartItem.Id, ExternalID = cartItem.ListingId, Quantity = cartItem.Quantity, Price = cartItem.Price.Was ?? cartItem.Price.Now, Sku = cartItem.VariantId, SpecialPrice = cartItem.Price.Now, ProductTitle = cartItem.Title };
orderResponse.Items.Add(oderResponseItem); }
return orderResponse; }
public Task<List<ShopifyCheckoutLineItem>> CreateShopifyCheckoutItemsAsync(CartWithProductsResponse cart) { var shopifyItems = new List<ShopifyCheckoutLineItem>();
foreach (var item in cart.CartItems) { shopifyItems.Add(new ShopifyCheckoutLineItem { Quantity = item.Quantity, VariantId = Convert.ToBase64String( Encoding.UTF8.GetBytes($"gid://shopify/ProductVariant/{item.VariantId}")) }); }
return Task.FromResult(shopifyItems); }}
Methods
StartAsync
used to handlecheckout/start
requests by manually providing list of cart items. Method uses Shopify multipass token if provided to sign in logged in users into Shopify web page. Creates Shopify checkout. This method usage is discouraged useStartCartCheckoutAsync
when possible.StartCartCheckoutAsync
used to handlecheckout/start
requests by providing shopify cart id. Method uses Shopify multipass token if provided to sign in logged in users into Shopify web page. Uses shopify cart and all discounts and items already set in the cart will be handled by shopify.