diff --git a/.gitignore b/.gitignore index 75a537172a2..7c5074fb114 100644 --- a/.gitignore +++ b/.gitignore @@ -41,3 +41,5 @@ storybook-static/ .qodo .qodo + +.qodo diff --git a/platform/canvas-packages/internal_pkgs/spree/package.json b/platform/canvas-packages/internal_pkgs/spree/package.json index bd4ffc9fd4a..43a36825d8e 100644 --- a/platform/canvas-packages/internal_pkgs/spree/package.json +++ b/platform/canvas-packages/internal_pkgs/spree/package.json @@ -1,6 +1,6 @@ { "name": "commerce-spree", - "version": "0.1.4", + "version": "0.2.1", "description": "Plasmic registration calls for spree commerce provider", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/platform/canvas-packages/internal_pkgs/spree/src/checkout/use-checkout.tsx b/platform/canvas-packages/internal_pkgs/spree/src/checkout/use-checkout.tsx index b8a605440a1..51037d0fd07 100644 --- a/platform/canvas-packages/internal_pkgs/spree/src/checkout/use-checkout.tsx +++ b/platform/canvas-packages/internal_pkgs/spree/src/checkout/use-checkout.tsx @@ -4,6 +4,8 @@ import getCart from '../utils/get-cart' import normalizeCart from '../utils/normalizations/normalize-cart' import { useMemo } from 'react' import { GetCheckoutHook } from '../commerce/types/checkout' +import getShippingRates from '../utils/get-shipping-rates' +import getPaymentMethods from '../utils/get-payment-methods' export default useCheckout as UseCheckout @@ -24,13 +26,24 @@ export const handler: SWRHook = { ) const spreeCartResponse = await getCart(fetch) const cart = normalizeCart(spreeCartResponse, spreeCartResponse.data) + let shippingRates = null + let paymentMethods = null + if (cart.shippingAddress) { + shippingRates = await getShippingRates(fetch) + paymentMethods = await getPaymentMethods(fetch) + } return { - hasPayment: false, - hasShipping: false, + hasPayment: cart.payments.length > 0, + hasShipping: cart.shipments.length > 0, addressId: null, - payments: [], + payments: cart.payments, cardId: null, lineItems: cart.lineItems, + billingAddress: cart.billingAddress, + shippingAddress: cart.shippingAddress, + shipments: cart.shipments, + shippingRates, + paymentMethods, } }, useHook: ({ useData }) => { diff --git a/platform/canvas-packages/internal_pkgs/spree/src/checkout/use-submit-checkout.tsx b/platform/canvas-packages/internal_pkgs/spree/src/checkout/use-submit-checkout.tsx index c9e7579784b..31bf22443d0 100644 --- a/platform/canvas-packages/internal_pkgs/spree/src/checkout/use-submit-checkout.tsx +++ b/platform/canvas-packages/internal_pkgs/spree/src/checkout/use-submit-checkout.tsx @@ -38,37 +38,44 @@ export const handler: MutationHook = { async (input) => { const { email, - special_instructions, - billing_address, - shipping_address, + specialInstructions, + billingAddress, + shippingAddress, payments, shipments, - onSuccessAction, + shippingMethodId, + paymentMethodId, + action, } = input if ( !email && - !special_instructions && - !billing_address && - !shipping_address && + !specialInstructions && + !billingAddress && + !shippingAddress && !payments && + !shippingMethodId && + !paymentMethodId && !shipments ) { throw new ValidationError({ message: 'email or special_instructions or billing_address or' + - ' shipping_address or payments or shipments needs to be provided.', + ' shipping_address or payments or shipments or shippingMethodId or paymentMethodId' + + ' needs to be provided.', }) } const data = await fetch({ input: { email, - special_instructions, - billing_address, - shipping_address, + specialInstructions, + billingAddress, + shippingAddress, payments, shipments, - onSuccessAction, + shippingMethodId, + paymentMethodId, + action, }, }) diff --git a/platform/canvas-packages/internal_pkgs/spree/src/commerce/types/checkout.ts b/platform/canvas-packages/internal_pkgs/spree/src/commerce/types/checkout.ts index 70d6bf2dafb..c68b19ff0dc 100644 --- a/platform/canvas-packages/internal_pkgs/spree/src/commerce/types/checkout.ts +++ b/platform/canvas-packages/internal_pkgs/spree/src/commerce/types/checkout.ts @@ -1,6 +1,7 @@ import type { AddressFields } from './customer/address' -import type { Card, CardFields } from './customer/card' +import type { CardFields } from './customer/card' import type { LineItem } from './cart' +import { AddressAttr, PaymentAttr, ShipmentAttr } from '../../types' export interface Checkout { /** @@ -15,10 +16,6 @@ export interface Checkout { * The unique identifier for the address that the customer has selected for shipping. */ addressId: string - /** - * The list of payment cards that the customer has available. - */ - payments?: Card[] /** * The unique identifier of the card that the customer has selected for payment. */ @@ -27,6 +24,12 @@ export interface Checkout { * List of items in the checkout. */ lineItems?: LineItem[] + shippingAddress?: AddressAttr + billingAddress?: AddressAttr + payments?: PaymentAttr[] + shipments?: ShipmentAttr[] + paymentMethods?: PaymentMethod[] + shippingRates?: ShippingRate[] } export interface Payment { @@ -38,11 +41,46 @@ export interface Shipment { selectedShippingRateId: string } +export interface OrderShipment { + id: string + number: string + finalPrice: string + displayFinalPrice: string + state: string + shippedAt: Date + trackingUrl: string + free: boolean + shippingRates: ShippingRate[] +} + +export interface ShippingRate { + id: string + name: string + selected: boolean + finalPrice: string + displayFinalPrice: string + cost: string + displayCost: string + taxAmount: string + displayTaxAmount: string + shippingMethodId: string + free: boolean +} + +export interface PaymentMethod { + id: string + type: string + name: string + description: string + publicMetadata?: object + preferences?: object +} + export interface CheckoutBody { /** * The email assigned to this cart. */ - email: string + email?: string /** * The unique identifier for the cart. */ @@ -56,22 +94,30 @@ export interface CheckoutBody { * The billing Address information. * @see AddressFields */ - billing_address?: AddressFields + billingAddress?: AddressFields /** * The shipping Address information. * @see AddressFields */ - shipping_address?: AddressFields + shippingAddress?: AddressFields /** * The special instructions for the order. */ - special_instructions?: string + specialInstructions?: string /** * The list of payments. */ payments?: Payment[] shipments?: Shipment[] - onSuccessAction?: 'orderNext' | 'advance' | 'complete' | null + shippingMethodId?: string + paymentMethodId?: string + action: + | 'orderUpdate' + | 'orderNext' + | 'advance' + | 'complete' + | 'selectShippingMethod' + | 'addPayment' } export type CheckoutTypes = { diff --git a/platform/canvas-packages/internal_pkgs/spree/src/commerce/types/customer/address.ts b/platform/canvas-packages/internal_pkgs/spree/src/commerce/types/customer/address.ts index 4626a423c18..4bc098049f0 100644 --- a/platform/canvas-packages/internal_pkgs/spree/src/commerce/types/customer/address.ts +++ b/platform/canvas-packages/internal_pkgs/spree/src/commerce/types/customer/address.ts @@ -18,43 +18,43 @@ export interface AddressFields { /** * The customer's first name. */ - firstName: string + firstName?: string /** * The customer's last name. */ - lastName: string + lastName?: string /** * Company name. */ - company: string + company?: string /** * The customer's billing address street number. */ - streetNumber: string + streetNumber?: string /** * The customer's billing address apartment number. */ - apartments: string + apartments?: string /** * The customer's billing address zip code. */ - zipCode: string + zipCode?: string /** * The customer's billing address city. */ - city: string + city?: string /** * The customer's billing address state. */ - state: string + state?: string /** * The customer's billing address country. */ - country: string + country?: string /** * The customer's phone number. */ - phone: string + phone?: string } /** diff --git a/platform/canvas-packages/internal_pkgs/spree/src/registerCheckoutProvider.tsx b/platform/canvas-packages/internal_pkgs/spree/src/registerCheckoutProvider.tsx index df0f770535b..a5c91d40d1a 100644 --- a/platform/canvas-packages/internal_pkgs/spree/src/registerCheckoutProvider.tsx +++ b/platform/canvas-packages/internal_pkgs/spree/src/registerCheckoutProvider.tsx @@ -33,12 +33,20 @@ export const checkoutProviderMeta: ComponentMeta< interface CheckoutActions extends GlobalActionDict { submitCheckout: ( email: string, - special_instructions: string, - billing_address: AddressFields, - shipping_address: AddressFields, + specialInstructions: string, + billingAddress: AddressFields, + shippingAddress: AddressFields, payments: Payment[], shipments: Shipment[], - onSuccessAction: 'orderNext' | 'advance' | 'complete' | null + shippingMethodId: string, + paymentMethodId: string, + action: + | 'orderUpdate' + | 'orderNext' + | 'advance' + | 'complete' + | 'selectShippingMethod' + | 'addPayment' ) => void } @@ -52,21 +60,31 @@ export function CheckoutActionsProvider( () => ({ submitCheckout( email: string, - special_instructions: string, - billing_address: AddressFields, - shipping_address: AddressFields, + specialInstructions: string, + billingAddress: AddressFields, + shippingAddress: AddressFields, payments: Payment[], shipments: Shipment[], - onSuccessAction: 'orderNext' | 'advance' | 'complete' | null + shippingMethodId: string, + paymentMethodId: string, + action: + | 'orderUpdate' + | 'orderNext' + | 'advance' + | 'complete' + | 'selectShippingMethod' + | 'addPayment' ) { submitCheckout({ email, - special_instructions, - billing_address, - shipping_address, + specialInstructions, + billingAddress, + shippingAddress, payments, shipments, - onSuccessAction, + shippingMethodId, + paymentMethodId, + action, }) }, }), @@ -172,12 +190,12 @@ export const globalActionsRegistrations: Record< type: 'string', }, { - name: 'special_instructions', + name: 'specialInstructions', displayName: 'Special instructions', type: 'string', }, { - name: 'billing_address', + name: 'billingAddress', displayName: 'Billing address', type: { type: 'object', @@ -185,7 +203,7 @@ export const globalActionsRegistrations: Record< }, }, { - name: 'shipping_address', + name: 'shippingAddress', displayName: 'Shipping address', type: { type: 'object', @@ -203,16 +221,28 @@ export const globalActionsRegistrations: Record< type: 'object', }, { - name: 'onSuccessAction', - displayName: 'On success action', + name: 'shippingMethodId', + displayName: 'Shipping method ID', + type: 'string', + }, + { + name: 'paymentMethodId', + displayName: 'Payment method ID', + type: 'string', + }, + { + name: 'action', + displayName: 'Action', type: { type: 'choice', multiSelect: false, options: [ + { value: 'orderUpdate', label: 'Update checkout' }, { value: 'orderNext', label: 'Next' }, { value: 'advance', label: 'Advance' }, { value: 'complete', label: 'Complete' }, - { value: 'null', label: 'None' }, + { value: 'selectShippingMethod', label: 'Select shipping method' }, + { value: 'addPayment', label: 'Add payment' }, ], }, }, diff --git a/platform/canvas-packages/internal_pkgs/spree/src/types/cart.ts b/platform/canvas-packages/internal_pkgs/spree/src/types/cart.ts index 27494e7a3d7..85fd0771dd5 100644 --- a/platform/canvas-packages/internal_pkgs/spree/src/types/cart.ts +++ b/platform/canvas-packages/internal_pkgs/spree/src/types/cart.ts @@ -4,11 +4,10 @@ */ import type { Image, Measurement } from './common' import { CartType as Core } from '@plasmicpkgs/commerce' - - +import { AddressFields } from '../commerce/types/customer/address' export type SaleorCart = {} -export type LineItem = Core.LineItem; +export type LineItem = Core.LineItem /** * Extend core cart types @@ -17,6 +16,10 @@ export type LineItem = Core.LineItem; export type Cart = Core.Cart & { lineItems: Core.LineItem[] url?: string + shippingAddress?: AddressFields + billingAddress?: AddressFields + payments?: Payment[] + shipments?: Shipment[] } export type CartTypes = Core.CartTypes @@ -93,7 +96,6 @@ export interface ProductVariant { depth?: Measurement } - export interface SelectedOption { /** * The unique identifier for the option. @@ -107,4 +109,62 @@ export interface SelectedOption { * The product option’s value, such as "Red" or "XL". */ value: string -} \ No newline at end of file +} + +export interface Shipment { + tracking?: string + number?: string + cost?: string + shippedAt?: any + state?: string + createdAt?: string + updatedAt?: string + adjustmentTotal?: string + additionalTaxTotal?: string + promoTotal?: string + includedTaxTotal?: string + preTaxAmount?: string + taxableAdjustmentTotal?: string + nonTaxableAdjustmentTotal?: string + displayDiscountedCost?: string + displayItemCost?: string + displayAmount?: string + displayFinalPrice?: string + displayCost?: string + trackingUrl?: any +} + +export interface Payment { + amount?: number + sourceType?: string + state?: string + responseCode?: string + avsResponse?: string + createdAt?: string + updatedAt?: string + number?: string + cvvResponseCode?: string + cvvResponseMessage?: string + displayAmount?: string +} + +export interface ShippingRate { + name?: string + selected?: boolean + finalPrice?: string + displayFinalPrice?: string + cost?: string + displayCost?: string + taxAmount?: string + displayTaxAmount?: string + shippingMethodId?: string + free?: boolean +} + +export interface PaymentMethod { + type?: string + name?: string + description?: string + publicMetadata?: object + preferences?: object +} diff --git a/platform/canvas-packages/internal_pkgs/spree/src/types/index.ts b/platform/canvas-packages/internal_pkgs/spree/src/types/index.ts index 49ab76ceee1..5eeddb98b95 100644 --- a/platform/canvas-packages/internal_pkgs/spree/src/types/index.ts +++ b/platform/canvas-packages/internal_pkgs/spree/src/types/index.ts @@ -156,3 +156,90 @@ export interface CommerceAPIFetchOptions { variables?: Variables preview?: boolean } + +export interface AddressAttr extends JsonApiDocument { + attributes: { + country_id: string + state_id: string + state_name: string + address1: string + address2: string + city: string + zipcode: string + phone: string + alternative_phone: string + firstname: string + lastname: string + label: string + company: string + user_id: string + public_metadata?: { + [key: string]: string + } + private_metadata?: { + [key: string]: string + } + } +} + +export interface ShipmentAttr extends JsonApiDocument { + attributes: { + tracking: string + number: string + cost: string + shipped_at: any + state: string + created_at: string + updated_at: string + adjustment_total: string + additional_tax_total: string + promo_total: string + included_tax_total: string + pre_tax_amount: string + taxable_adjustment_total: string + non_taxable_adjustment_total: string + display_discounted_cost: string + display_item_cost: string + display_amount: string + display_final_price: string + display_cost: string + tracking_url: any + public_metadata?: { + [key: string]: string + } + private_metadata?: { + [key: string]: string + } + } +} + +export interface PaymentAttr extends JsonApiDocument { + attributes: { + amount: string + source_type: string + state: string + response_code: string + avs_response: string + created_at: string + updated_at: string + number: string + cvv_response_code: string + cvv_response_message: string + display_amount: string + } +} + +export interface ShippingRateAttr extends JsonApiDocument { + attributes: { + name: string + selected: boolean + final_price: string + display_final_price: string + cost: string + display_cost: string + tax_amount: string + display_tax_amount: string + shipping_method_id: string + free: boolean + } +} diff --git a/platform/canvas-packages/internal_pkgs/spree/src/utils/get-cart.ts b/platform/canvas-packages/internal_pkgs/spree/src/utils/get-cart.ts index d48650f2747..5db0bb7f32d 100644 --- a/platform/canvas-packages/internal_pkgs/spree/src/utils/get-cart.ts +++ b/platform/canvas-packages/internal_pkgs/spree/src/utils/get-cart.ts @@ -40,6 +40,10 @@ const getCart = async ( 'line_items.variant.images', 'line_items.variant.option_values', 'line_items.variant.product.option_types', + 'billing_address', + 'shipping_address', + 'payments', + 'shipments', ].join(','), image_transformation: { quality: imagesQuality, diff --git a/platform/canvas-packages/internal_pkgs/spree/src/utils/get-payment-methods.ts b/platform/canvas-packages/internal_pkgs/spree/src/utils/get-payment-methods.ts new file mode 100644 index 00000000000..6731720e3f1 --- /dev/null +++ b/platform/canvas-packages/internal_pkgs/spree/src/utils/get-payment-methods.ts @@ -0,0 +1,37 @@ +import type { GraphQLFetcherResult } from '../types' +import { HookFetcherContext } from '@plasmicpkgs/commerce' +import type { IToken } from '@spree/storefront-api-v2-sdk/types/interfaces/Token' +import ensureIToken from './tokens/ensure-itoken' +import { IPaymentMethods } from '@spree/storefront-api-v2-sdk/types/interfaces/PaymentMethod' +import { PaymentMethod } from '../commerce/types/checkout' + +const getPaymentMethods = async ( + fetch: HookFetcherContext<{ + data: any + }>['fetch'] +): Promise => { + const token: IToken | undefined = ensureIToken() + + if (!token) { + return null + } else { + const { data: spreeSuccessResponse } = await fetch< + GraphQLFetcherResult + >({ + variables: { + methodPath: 'checkout.paymentMethods', + arguments: [token, {}], + }, + }) + return spreeSuccessResponse.data.map((paymentMethod): PaymentMethod => { + return { + id: paymentMethod.id, + type: paymentMethod.attributes.type, + name: paymentMethod.attributes.name, + description: paymentMethod.attributes.description, + } + }) + } +} + +export default getPaymentMethods diff --git a/platform/canvas-packages/internal_pkgs/spree/src/utils/get-shipping-rates.ts b/platform/canvas-packages/internal_pkgs/spree/src/utils/get-shipping-rates.ts new file mode 100644 index 00000000000..7a363fa867c --- /dev/null +++ b/platform/canvas-packages/internal_pkgs/spree/src/utils/get-shipping-rates.ts @@ -0,0 +1,67 @@ +import { GraphQLFetcherResult, ShippingRateAttr } from '../types' +import { HookFetcherContext } from '@plasmicpkgs/commerce' +import type { IToken } from '@spree/storefront-api-v2-sdk/types/interfaces/Token' +import ensureIToken from './tokens/ensure-itoken' +import { IShippingMethods } from '@spree/storefront-api-v2-sdk/types/interfaces/ShippingMethod' +import { jsonApi } from '@spree/storefront-api-v2-sdk' +import { ShippingRate } from '../commerce/types/checkout' + +const getShippingRates = async ( + fetch: HookFetcherContext<{ + data: any + }>['fetch'] +): Promise => { + const token: IToken | undefined = ensureIToken() + + if (!token) { + return null + } else { + const { data: spreeSuccessResponse } = await fetch< + GraphQLFetcherResult + >({ + variables: { + methodPath: 'checkout.shippingRates', + arguments: [ + token, + { + include: ['shipping_rates', 'stock_location'].join(','), + }, + ], + }, + }) + + let ShippingRates = [] + + spreeSuccessResponse.data.forEach((shippingMethod) => { + const relationships = shippingMethod.relationships + const shipmentShippingRates: ShippingRate[] = relationships.shipping_rates + ?.data + ? jsonApi + .findRelationshipDocuments( + spreeSuccessResponse, + shippingMethod, + 'shipping_rates' + ) + .map((shippingRate): ShippingRate => { + return { + id: shippingRate.id, + name: shippingRate.attributes.name, + selected: shippingRate.attributes.selected, + finalPrice: shippingRate.attributes.final_price, + displayFinalPrice: shippingRate.attributes.display_final_price, + cost: shippingRate.attributes.cost, + displayCost: shippingRate.attributes.display_cost, + taxAmount: shippingRate.attributes.tax_amount, + displayTaxAmount: shippingRate.attributes.display_tax_amount, + shippingMethodId: shippingMethod.id, + free: shippingRate.attributes.free, + } + }) + : null + ShippingRates = ShippingRates.concat(shipmentShippingRates) + }) + return ShippingRates + } +} + +export default getShippingRates diff --git a/platform/canvas-packages/internal_pkgs/spree/src/utils/normalizations/normalize-cart.ts b/platform/canvas-packages/internal_pkgs/spree/src/utils/normalizations/normalize-cart.ts index b7987785be6..337aed93091 100644 --- a/platform/canvas-packages/internal_pkgs/spree/src/utils/normalizations/normalize-cart.ts +++ b/platform/canvas-packages/internal_pkgs/spree/src/utils/normalizations/normalize-cart.ts @@ -1,5 +1,11 @@ import type { ProductVariant } from '../../types/cart' -import { Cart, LineItem, SelectedOption } from '../../types/cart' +import { + Cart, + LineItem, + SelectedOption, + Payment, + Shipment, +} from '../../types/cart' import MissingLineItemVariantError from '../../errors/MissingLineItemVariantError' import { requireConfigValue } from '../../isomorphic-config' import type { OrderAttr } from '@spree/storefront-api-v2-sdk/types/interfaces/Order' @@ -9,12 +15,16 @@ import { jsonApi } from '@spree/storefront-api-v2-sdk' import createGetAbsoluteImageUrl from '../create-get-absolute-image-url' import getMediaGallery from '../get-media-gallery' import type { + AddressAttr, LineItemAttr, OptionTypeAttr, SpreeProductImage, SpreeSdkResponse, VariantAttr, + ShipmentAttr, + PaymentAttr, } from '../../types' +import { AddressFields } from '../../commerce/types/customer/address' const placeholderImage = requireConfigValue('lineItemPlaceholderImageUrl') as | string @@ -169,6 +179,65 @@ const normalizeLineItem = ( } } +const normalizeAddress = ( + address: AddressAttr, + type: string +): AddressFields => { + return { + type, + firstName: address.attributes.firstname, + lastName: address.attributes.lastname, + company: address.attributes.company, + streetNumber: address.attributes.address1, + city: address.attributes.city, + state: address.attributes.state_name, + country: address.attributes.country_id, + zipCode: address.attributes.zipcode, + phone: address.attributes.phone, + } +} + +const normalizePayment = (payment: PaymentAttr): Payment => { + return { + amount: parseFloat(payment.attributes.amount), + sourceType: payment.attributes.source_type, + state: payment.attributes.state, + responseCode: payment.attributes.response_code, + avsResponse: payment.attributes.avs_response, + createdAt: payment.attributes.created_at, + updatedAt: payment.attributes.updated_at, + number: payment.attributes.number, + cvvResponseCode: payment.attributes.cvv_response_code, + cvvResponseMessage: payment.attributes.cvv_response_message, + displayAmount: payment.attributes.display_amount, + } +} + +const normalizeShipment = (shipment: ShipmentAttr): Shipment => { + return { + tracking: shipment.attributes.tracking, + number: shipment.attributes.number, + cost: shipment.attributes.cost, + shippedAt: shipment.attributes.shipped_at, + state: shipment.attributes.state, + createdAt: shipment.attributes.created_at, + updatedAt: shipment.attributes.updated_at, + adjustmentTotal: shipment.attributes.adjustment_total, + additionalTaxTotal: shipment.attributes.additional_tax_total, + promoTotal: shipment.attributes.promo_total, + includedTaxTotal: shipment.attributes.included_tax_total, + preTaxAmount: shipment.attributes.pre_tax_amount, + taxableAdjustmentTotal: shipment.attributes.taxable_adjustment_total, + nonTaxableAdjustmentTotal: shipment.attributes.non_taxable_adjustment_total, + displayDiscountedCost: shipment.attributes.display_discounted_cost, + displayItemCost: shipment.attributes.display_item_cost, + displayAmount: shipment.attributes.display_amount, + displayFinalPrice: shipment.attributes.display_final_price, + displayCost: shipment.attributes.display_cost, + trackingUrl: shipment.attributes.tracking_url, + } +} + const normalizeCart = ( spreeSuccessResponse: SpreeSdkResponse, spreeCart: OrderAttr @@ -181,6 +250,46 @@ const normalizeCart = ( ) .map((lineItem) => normalizeLineItem(spreeSuccessResponse, lineItem)) + const relationships = spreeCart.relationships + + const billingAddress = relationships.billing_address?.data + ? normalizeAddress( + jsonApi.findSingleRelationshipDocument( + spreeSuccessResponse, + spreeCart, + 'billing_address' + ), + 'billing' + ) + : null + + const shippingAddress = relationships.shipping_address?.data + ? normalizeAddress( + jsonApi.findSingleRelationshipDocument( + spreeSuccessResponse, + spreeCart, + 'shipping_address' + ), + 'shipping' + ) + : null + + const payments = jsonApi + .findRelationshipDocuments( + spreeSuccessResponse, + spreeCart, + 'payments' + ) + .map((payment) => normalizePayment(payment)) + + const shipments = jsonApi + .findRelationshipDocuments( + spreeSuccessResponse, + spreeCart, + 'shipments' + ) + .map((shipment) => normalizeShipment(shipment)) + return { id: spreeCart.id, createdAt: spreeCart.attributes.created_at.toString(), @@ -193,6 +302,10 @@ const normalizeCart = ( customerId: spreeCart.attributes.token, email: spreeCart.attributes.email, discounts: [], // TODO: Implement when the template starts displaying them. + shippingAddress, + billingAddress, + payments, + shipments, } } diff --git a/platform/canvas-packages/internal_pkgs/spree/src/utils/submit-checkout.ts b/platform/canvas-packages/internal_pkgs/spree/src/utils/submit-checkout.ts index 53161593914..9022269d69e 100644 --- a/platform/canvas-packages/internal_pkgs/spree/src/utils/submit-checkout.ts +++ b/platform/canvas-packages/internal_pkgs/spree/src/utils/submit-checkout.ts @@ -11,7 +11,6 @@ import createEmptyCart from './create-empty-cart' import isLoggedIn from './tokens/is-logged-in' import { setCartToken } from './tokens/cart-token' import { IPayment } from '@spree/storefront-api-v2-sdk/types/interfaces/attributes/Payment' -import { OrderUpdate } from '@spree/storefront-api-v2-sdk/types/interfaces/Checkout' import normalizeCart from './normalizations/normalize-cart' import type { AddressFields } from '../commerce/types/customer/address' import { Checkout, CheckoutBody } from '../commerce/types/checkout' @@ -40,12 +39,14 @@ const submitCheckout = async ( const { email, - special_instructions, - billing_address, - shipping_address, + specialInstructions, + billingAddress, + shippingAddress, payments, shipments, - onSuccessAction, + shippingMethodId, + paymentMethodId, + action, } = input if (!email) { @@ -84,14 +85,16 @@ const submitCheckout = async ( 'line_items.variant.product.option_types', ].join(',') - const orderUpdateParameters: OrderUpdate = { + const orderUpdateParameters = { order: { email, - special_instructions, - bill_address_attributes: buildAddress(billing_address), - ship_address_attributes: buildAddress(shipping_address), + special_instructions: specialInstructions, + bill_address_attributes: buildAddress(billingAddress), + ship_address_attributes: buildAddress(shippingAddress), payments_attributes, shipments_attributes, + shipping_method_id: shippingMethodId, + payment_method_id: paymentMethodId, }, include: includeParams, } @@ -100,25 +103,11 @@ const submitCheckout = async ( GraphQLFetcherResult >({ variables: { - methodPath: 'checkout.orderUpdate', + methodPath: `checkout.${action}`, arguments: [token, orderUpdateParameters], }, }) - - if (onSuccessAction) { - const { data: checkoutActionResponse } = await fetch< - GraphQLFetcherResult - >({ - variables: { - methodPath: `checkout.${onSuccessAction}`, - arguments: [token], - include: includeParams, - }, - }) - spreeCartResponse = checkoutActionResponse - } else { - spreeCartResponse = spreeSuccessResponse - } + spreeCartResponse = spreeSuccessResponse } catch (updateItemError) { if ( updateItemError instanceof FetcherError && diff --git a/platform/canvas-packages/package.json b/platform/canvas-packages/package.json index e1051790b83..89a46ebf2d8 100644 --- a/platform/canvas-packages/package.json +++ b/platform/canvas-packages/package.json @@ -70,7 +70,7 @@ "axios": "^1.5.1", "chart.js": "^4.2.1", "classnames": "^2.3.2", - "commerce-spree": "^0.1.4", + "commerce-spree": "^0.2.1", "copy-to-clipboard": "^3.3.3", "date-fns": "^2.30.0", "dayjs": "^1.11.10", diff --git a/platform/canvas-packages/yarn.lock b/platform/canvas-packages/yarn.lock index fce6cc2f570..ae459a338ab 100644 --- a/platform/canvas-packages/yarn.lock +++ b/platform/canvas-packages/yarn.lock @@ -7314,10 +7314,10 @@ commander@^7.2.0: resolved "https://registry.yarnpkg.com/commander/-/commander-7.2.0.tgz#a36cb57d0b501ce108e4d20559a150a391d97ab7" integrity sha512-QrWXB+ZQSVPmIWIhtEO9H+gwHaMGYiF5ChvoJ+K9ZGHG/sVsa6yiesAD1GC/x46sET00Xlwo1u49RVVVzvcSkw== -commerce-spree@^0.1.4: - version "0.1.4" - resolved "https://registry.yarnpkg.com/commerce-spree/-/commerce-spree-0.1.4.tgz#95beebbe268ec48a8b38afb4b3b144d7b272b746" - integrity sha512-MemZraulWtULn0sbIAlHAtqds6jCwfciKVRmNRWWW8BhKpIaZBnq0boGeabrRTMAk2TeVbVG0v7ehKgwuD4lcQ== +commerce-spree@^0.2.1: + version "0.2.1" + resolved "https://registry.yarnpkg.com/commerce-spree/-/commerce-spree-0.2.1.tgz#660afc65b77b4b7d2d8ba6a7101fb23da3f4aeda" + integrity sha512-YXUakxDG6RLXKfdr2XET1jnydQjsVDUUg9R+fDcOPD/Sdj/s3ZK0ctOiEmKKHpGfZgSpYydxPr8a7m3YuO6byw== dependencies: "@plasmicpkgs/commerce" "0.0.206" "@spree/storefront-api-v2-sdk" "^5.1.1" diff --git a/platform/loader-bundle-env/package.json b/platform/loader-bundle-env/package.json index 68af2412149..e5836c26bc9 100644 --- a/platform/loader-bundle-env/package.json +++ b/platform/loader-bundle-env/package.json @@ -26,7 +26,7 @@ "@plasmicpkgs/commerce-saleor": "^0.0.170", "@plasmicpkgs/commerce-shopify": "^0.0.213", "@plasmicpkgs/commerce-swell": "^0.0.215", - "commerce-spree": "^0.1.4", + "commerce-spree": "^0.2.1", "@plasmicpkgs/framer-motion": "^0.0.206", "@plasmicpkgs/lottie-react": "^0.0.199", "@plasmicpkgs/plasmic-basic-components": "^0.0.231", diff --git a/platform/loader-bundle-env/yarn.lock b/platform/loader-bundle-env/yarn.lock index e16250d8c30..dd683dfb159 100644 --- a/platform/loader-bundle-env/yarn.lock +++ b/platform/loader-bundle-env/yarn.lock @@ -6651,10 +6651,10 @@ combined-stream@^1.0.6, combined-stream@^1.0.8: dependencies: delayed-stream "~1.0.0" -commerce-spree@^0.1.4: - version "0.1.4" - resolved "https://registry.yarnpkg.com/commerce-spree/-/commerce-spree-0.1.4.tgz#95beebbe268ec48a8b38afb4b3b144d7b272b746" - integrity sha512-MemZraulWtULn0sbIAlHAtqds6jCwfciKVRmNRWWW8BhKpIaZBnq0boGeabrRTMAk2TeVbVG0v7ehKgwuD4lcQ== +commerce-spree@^0.2.1: + version "0.2.1" + resolved "https://registry.yarnpkg.com/commerce-spree/-/commerce-spree-0.2.1.tgz#660afc65b77b4b7d2d8ba6a7101fb23da3f4aeda" + integrity sha512-YXUakxDG6RLXKfdr2XET1jnydQjsVDUUg9R+fDcOPD/Sdj/s3ZK0ctOiEmKKHpGfZgSpYydxPr8a7m3YuO6byw== dependencies: "@plasmicpkgs/commerce" "0.0.206" "@spree/storefront-api-v2-sdk" "^5.1.1"