Klarna Docs - Shopify Plus: custom scripts

Shopify Plus: custom scripts

Shopify Plus merchants can use custom scripts to customize their payment methods(s) presentation in Shopify checkout, based on available cart data, as documented below.  Please note: Klarna is not responsible for this sample code; merchants should review & test this code in their store.  Shopify has example payment script code here.

Go to the Script Editor app and follow the instructions per: 

https://help.shopify.com/en/manual/apps/apps-by-shopify/script-editor/shopify-scripts#cart

Even though the name of Klarna appears differently in checkout with a different translation per language, as documented here, Script Editor custom code should reference the name of the payment method configured in the app's payment method extension, which for this integration, is "Klarna".

Each store should customize the code below to use the appropriate data for the store.

Shopify only allows a single script to be published for each script type [Line Items, Shipping, Payments]; the code examples below likely require modifications to be combined into a single script.

The code below was copied, and slightly modified match the payment method/gateway names, from Shopify’s example code for the “Reorder payment gateways” in the Script Editor app. Shopify published example code to reorder gateways is available here.

RUBY
desired_order = ["Klarna", "Shopify Payments", “PayPal”]
Output.payment_gateways = Input.payment_gateways.sort_by do |payment_gateway|
  desired_order.index(payment_gateway.name) || Float::INFINITY
end
RUBY
cart = Input.cart
available_gateways = Input.payment_gateways
if cart.presentment_currency != "USD"
  available_gateways = available_gateways.delete_if do |payment_gateway|
    payment_gateway.name == "Klarna"
  end
end
Output.payment_gateways = available_gateways

Shopify published example code to hide gateways for specific countries is available here.

RUBY
cart = Input.cart
available_gateways = Input.payment_gateways
if (cart.shipping_address)
  if cart.shipping_address.country_code != "US"
    available_gateways = available_gateways.delete_if do |payment_gateway|
      payment_gateway.name == "Klarna"
    end
  end
end
Output.payment_gateways = available_gateways

(Note: the Shopify API does not provide billing address cart data as parameters for payment gateway custom scripts since the billing address is entered by the customer on the same page where the payment gateway custom script is run.)

Shopify published example code to hide gateways for specific products is available here.

RUBY
available_gateways = Input.payment_gateways
cart = Input.cart
 
SKUS_TO_HIDE = ["SKU-1234", "..."]
 
cart.line_items.each do |item|
  item.variant.skus.each do |sku|
    if SKUS_TO_HIDE.include? sku
      available_gateways = available_gateways.delete_if do |payment_gateway|
        payment_gateway.name == "Klarna"
RUBY
available_gateways = Input.payment_gateways
cart = Input.cart
  
TAGS_TO_HIDE = ["hide-klarna", "..."]
  
cart.line_items.each do |item|
  item.variant.product.tags.each do |tag|
    if TAGS_TO_HIDE.include? tag
      available_gateways = available_gateways.delete_if do |payment_gateway|
        payment_gateway.name == "Klarna"