ParcelCraft Dynamic Shipping Fix - Checkout Session

This document provides the corrected implementation for enabling ParcelCraft dynamic shipping rates in the Stripe Checkout session creation process.

Problem Overview

The create-checkout-session.ts file had two critical issues preventing ParcelCraft dynamic shipping from working correctly:

  1. A duplicate sessionParams declaration was accidentally placed inside the line items loop (around line 730)
  2. The shipping_options configuration was missing, which is required to enable ParcelCraft dynamic rate calculation

Solution

Step 1: Remove Duplicate sessionParams Declaration

Locate and remove the duplicate sessionParams declaration that was mistakenly placed inside the line items loop around line 730. This duplicate declaration was overwriting the correct configuration.

Step 2: Keep the Correct sessionParams Declaration

Ensure the correct sessionParams declaration remains in place. This should come after the following variables are defined:

  • metadataForSession
  • paymentIntentMetadata
  • shippingAddressCollection

Step 3: Add shipping_options Configuration

Add the shipping_options configuration to the sessionParams object. This should be placed after shipping_address_collection and before success_url.

Code Implementation

Add the following configuration to your sessionParams object:

[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

How It Works

The shipping_options configuration enables ParcelCraft dynamic shipping by:

  1. Setting up a placeholder shipping rate with amount: 0
  2. Using the display name 'Calculated at checkout' to inform customers
  3. Including metadata with parcelcraft: 'true' to trigger the ParcelCraft webhook integration
  4. Conditionally applying this configuration only when shippingRequired is true

Placement in sessionParams

The shipping_options configuration should be placed in the sessionParams object structure as follows:

[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

Testing

After implementing these changes, test the checkout flow to verify:

  • The checkout session creates successfully without errors
  • Shipping rates are calculated dynamically based on the customer's address
  • The ParcelCraft webhook is triggered correctly
  • Shipping costs are accurately calculated and displayed to customers

Important Notes

  • The currency is set to 'usd' - adjust this if your store uses a different currency
  • The initial amount is 0 because ParcelCraft will calculate the actual shipping cost dynamically
  • Ensure your ParcelCraft webhook is properly configured to handle the checkout.session.async_payment_succeeded event
  • The spread operator syntax (...) ensures this configuration is only added when shipping is required

Related Posts

Top Products

Explore our most requested billet components and performance upgrades, each engineered in-house and backed by track data.

Top Services

Pair your hardware upgrades with professional installation, calibration, and security options from the F.A.S. Motorsports team.