mirror of
https://github.com/vercel/commerce.git
synced 2025-05-18 07:26:59 +00:00
fix(vendure): Fix issues with custom checkout flow
This commit is contained in:
parent
042558513e
commit
3c22b10635
@ -6,9 +6,9 @@ import { CommerceError } from '@vercel/commerce/utils/errors'
|
|||||||
import type { MutationHook } from '@vercel/commerce/utils/types'
|
import type { MutationHook } from '@vercel/commerce/utils/types'
|
||||||
import { useCallback } from 'react'
|
import { useCallback } from 'react'
|
||||||
import {
|
import {
|
||||||
|
AddPaymentToOrderMutation,
|
||||||
EligiblePaymentMethodsQuery,
|
EligiblePaymentMethodsQuery,
|
||||||
TransitionOrderToStateResult,
|
TransitionOrderToStateMutation,
|
||||||
AddPaymentToOrderResult,
|
|
||||||
} from '../../schema'
|
} from '../../schema'
|
||||||
import { addPaymentToOrder } from '../utils/mutations/add-payment-to-order'
|
import { addPaymentToOrder } from '../utils/mutations/add-payment-to-order'
|
||||||
import { transitionOrderToState } from '../utils/mutations/transition-order-to-state'
|
import { transitionOrderToState } from '../utils/mutations/transition-order-to-state'
|
||||||
@ -21,17 +21,20 @@ export const handler: MutationHook<SubmitCheckoutHook> = {
|
|||||||
query: addPaymentToOrder,
|
query: addPaymentToOrder,
|
||||||
},
|
},
|
||||||
async fetcher({ input: item, options, fetch }) {
|
async fetcher({ input: item, options, fetch }) {
|
||||||
const transitionResponse = await fetch<TransitionOrderToStateResult>({
|
const transitionResponse = await fetch<TransitionOrderToStateMutation>({
|
||||||
...options,
|
...options,
|
||||||
query: transitionOrderToState,
|
query: transitionOrderToState,
|
||||||
variables: {
|
variables: {
|
||||||
state: 'ArrangingPayment',
|
state: 'ArrangingPayment',
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
if (transitionResponse.__typename === 'OrderStateTransitionError') {
|
if (
|
||||||
|
transitionResponse.transitionOrderToState?.__typename ===
|
||||||
|
'OrderStateTransitionError'
|
||||||
|
) {
|
||||||
throw new CommerceError({
|
throw new CommerceError({
|
||||||
code: transitionResponse.errorCode,
|
code: transitionResponse.transitionOrderToState.errorCode,
|
||||||
message: transitionResponse.message,
|
message: transitionResponse.transitionOrderToState.message,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
const paymentMethodsResponse = await fetch<EligiblePaymentMethodsQuery>({
|
const paymentMethodsResponse = await fetch<EligiblePaymentMethodsQuery>({
|
||||||
@ -47,7 +50,7 @@ export const handler: MutationHook<SubmitCheckoutHook> = {
|
|||||||
message: 'No Eligible payment methods',
|
message: 'No Eligible payment methods',
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
const paymentResponse = await fetch<AddPaymentToOrderResult>({
|
const paymentResponse = await fetch<AddPaymentToOrderMutation>({
|
||||||
...options,
|
...options,
|
||||||
variables: {
|
variables: {
|
||||||
input: {
|
input: {
|
||||||
@ -58,20 +61,22 @@ export const handler: MutationHook<SubmitCheckoutHook> = {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
if (paymentResponse.__typename === 'Order') {
|
const addPaymentToOrderResultType =
|
||||||
|
paymentResponse.addPaymentToOrder.__typename
|
||||||
|
if (addPaymentToOrderResultType === 'Order') {
|
||||||
return {
|
return {
|
||||||
hasPayment: true,
|
hasPayment: true,
|
||||||
hasShipping: true,
|
hasShipping: true,
|
||||||
}
|
}
|
||||||
} else if (
|
} else if (
|
||||||
paymentResponse.__typename === 'IneligiblePaymentMethodError' ||
|
addPaymentToOrderResultType === 'IneligiblePaymentMethodError' ||
|
||||||
paymentResponse.__typename === 'NoActiveOrderError' ||
|
addPaymentToOrderResultType === 'NoActiveOrderError' ||
|
||||||
paymentResponse.__typename === 'OrderPaymentStateError' ||
|
addPaymentToOrderResultType === 'OrderPaymentStateError' ||
|
||||||
paymentResponse.__typename === 'OrderStateTransitionError' ||
|
addPaymentToOrderResultType === 'OrderStateTransitionError' ||
|
||||||
paymentResponse.__typename === 'PaymentDeclinedError' ||
|
addPaymentToOrderResultType === 'PaymentDeclinedError' ||
|
||||||
paymentResponse.__typename === 'PaymentFailedError'
|
addPaymentToOrderResultType === 'PaymentFailedError'
|
||||||
) {
|
) {
|
||||||
throw new CommerceError(paymentResponse)
|
throw new CommerceError(paymentResponse.addPaymentToOrder)
|
||||||
} else {
|
} else {
|
||||||
throw new CommerceError({
|
throw new CommerceError({
|
||||||
message: 'Something went wrong with Payment request',
|
message: 'Something went wrong with Payment request',
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
{
|
{
|
||||||
"provider": "vendure",
|
"provider": "vendure",
|
||||||
"features": {
|
"features": {
|
||||||
"wishlist": false
|
"wishlist": false,
|
||||||
|
"customCheckout": true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,6 @@ export const handler: SWRHook<GetAddressesHook> = {
|
|||||||
},
|
},
|
||||||
useHook: ({ useData }) =>
|
useHook: ({ useData }) =>
|
||||||
function useHook(input) {
|
function useHook(input) {
|
||||||
console.log(input, 'hello')
|
|
||||||
const response = useData({
|
const response = useData({
|
||||||
swrOptions: { revalidateOnFocus: false, ...input?.swrOptions },
|
swrOptions: { revalidateOnFocus: false, ...input?.swrOptions },
|
||||||
})
|
})
|
||||||
|
@ -8,13 +8,16 @@ import { MutationHook } from '@vercel/commerce/utils/types'
|
|||||||
import { useCallback } from 'react'
|
import { useCallback } from 'react'
|
||||||
import { setOrderBillingAddress } from '../../utils/mutations/set-order-billing-address'
|
import { setOrderBillingAddress } from '../../utils/mutations/set-order-billing-address'
|
||||||
import {
|
import {
|
||||||
ActiveOrderResult,
|
|
||||||
EligibleShippingMethodsQuery,
|
EligibleShippingMethodsQuery,
|
||||||
MutationSetOrderBillingAddressArgs,
|
MutationSetOrderBillingAddressArgs,
|
||||||
|
SetCustomerForOrderMutation,
|
||||||
|
SetCustomerForOrderMutationVariables,
|
||||||
|
SetOrderBillingAddressMutation,
|
||||||
SetOrderShippingMethodResult,
|
SetOrderShippingMethodResult,
|
||||||
} from '../../../schema'
|
} from '../../../schema'
|
||||||
import { eligibleShippingMethods } from '../../utils/queries/eligible-shipping-methods'
|
import { eligibleShippingMethods } from '../../utils/queries/eligible-shipping-methods'
|
||||||
import { setOrderShippingMethod } from '../../utils/mutations/set-order-shipping-method'
|
import { setOrderShippingMethod } from '../../utils/mutations/set-order-shipping-method'
|
||||||
|
import { setCustomerForOrder } from '../../utils/mutations/set-customer-for-order'
|
||||||
|
|
||||||
export default useAddItem as UseAddItem<typeof handler>
|
export default useAddItem as UseAddItem<typeof handler>
|
||||||
|
|
||||||
@ -36,10 +39,30 @@ export const handler: MutationHook<AddItemHook> = {
|
|||||||
countryCode: 'JP',
|
countryCode: 'JP',
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
const data = await fetch<ActiveOrderResult>({
|
const data = await fetch<SetOrderBillingAddressMutation>({
|
||||||
...options,
|
...options,
|
||||||
variables,
|
variables,
|
||||||
})
|
})
|
||||||
|
if (
|
||||||
|
data.setOrderBillingAddress.__typename === 'Order' &&
|
||||||
|
!data.setOrderBillingAddress.customer
|
||||||
|
) {
|
||||||
|
// A Customer must be assigned to the Order before we will be able to
|
||||||
|
// transition to the ArrangingPayment state.
|
||||||
|
await fetch<SetCustomerForOrderMutation>({
|
||||||
|
...options,
|
||||||
|
query: setCustomerForOrder,
|
||||||
|
variables: {
|
||||||
|
input: {
|
||||||
|
firstName: item.firstName,
|
||||||
|
lastName: item.lastName,
|
||||||
|
// TODO: we need an input for email address
|
||||||
|
// ref: https://github.com/vercel/commerce/issues/616
|
||||||
|
emailAddress: 'guest-customer@test.com',
|
||||||
|
},
|
||||||
|
} as SetCustomerForOrderMutationVariables,
|
||||||
|
})
|
||||||
|
}
|
||||||
const eligibleMethods = await fetch<EligibleShippingMethodsQuery>({
|
const eligibleMethods = await fetch<EligibleShippingMethodsQuery>({
|
||||||
...options,
|
...options,
|
||||||
query: eligibleShippingMethods,
|
query: eligibleShippingMethods,
|
||||||
@ -56,17 +79,19 @@ export const handler: MutationHook<AddItemHook> = {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data.__typename === 'Order') {
|
if (data.setOrderBillingAddress.__typename === 'Order') {
|
||||||
// TODO: Not sure what card we should return
|
// TODO: Not sure what card we should return
|
||||||
return {
|
return {
|
||||||
id: '',
|
id: '',
|
||||||
mask: '',
|
mask: '',
|
||||||
provider: '',
|
provider: '',
|
||||||
}
|
}
|
||||||
} else if (data.__typename === 'NoActiveOrderError') {
|
} else if (
|
||||||
|
data.setOrderBillingAddress.__typename === 'NoActiveOrderError'
|
||||||
|
) {
|
||||||
throw new CommerceError({
|
throw new CommerceError({
|
||||||
code: data.errorCode,
|
code: data.setOrderBillingAddress.errorCode,
|
||||||
message: data.message,
|
message: data.setOrderBillingAddress.message,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -4,6 +4,7 @@ export const cartFragment = /* GraphQL */ `
|
|||||||
fragment Cart on Order {
|
fragment Cart on Order {
|
||||||
id
|
id
|
||||||
code
|
code
|
||||||
|
state
|
||||||
createdAt
|
createdAt
|
||||||
totalQuantity
|
totalQuantity
|
||||||
subTotal
|
subTotal
|
||||||
|
@ -3,6 +3,7 @@ import { cartFragment } from '../fragments/cart-fragment'
|
|||||||
export const addPaymentToOrder = /* GraphQL */ `
|
export const addPaymentToOrder = /* GraphQL */ `
|
||||||
mutation addPaymentToOrder($input: PaymentInput!) {
|
mutation addPaymentToOrder($input: PaymentInput!) {
|
||||||
addPaymentToOrder(input: $input) {
|
addPaymentToOrder(input: $input) {
|
||||||
|
__typename
|
||||||
...Cart
|
...Cart
|
||||||
... on OrderPaymentStateError {
|
... on OrderPaymentStateError {
|
||||||
errorCode
|
errorCode
|
||||||
|
@ -3,7 +3,12 @@ import { cartFragment } from '../fragments/cart-fragment'
|
|||||||
export const setCustomerForOrder = /* GraphQL */ `
|
export const setCustomerForOrder = /* GraphQL */ `
|
||||||
mutation setCustomerForOrder($input: CreateCustomerInput!) {
|
mutation setCustomerForOrder($input: CreateCustomerInput!) {
|
||||||
setCustomerForOrder(input: $input) {
|
setCustomerForOrder(input: $input) {
|
||||||
|
__typename
|
||||||
...Cart
|
...Cart
|
||||||
|
... on ErrorResult {
|
||||||
|
errorCode
|
||||||
|
message
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
${cartFragment}
|
${cartFragment}
|
@ -3,6 +3,7 @@ import { cartFragment } from '../../utils/fragments/cart-fragment'
|
|||||||
export const setOrderBillingAddress = /* GraphQL */ `
|
export const setOrderBillingAddress = /* GraphQL */ `
|
||||||
mutation setOrderBillingAddress ($input: CreateAddressInput!){
|
mutation setOrderBillingAddress ($input: CreateAddressInput!){
|
||||||
setOrderBillingAddress(input: $input) {
|
setOrderBillingAddress(input: $input) {
|
||||||
|
__typename
|
||||||
...Cart
|
...Cart
|
||||||
... on NoActiveOrderError {
|
... on NoActiveOrderError {
|
||||||
errorCode
|
errorCode
|
||||||
|
@ -3,6 +3,7 @@ import { cartFragment } from '../../utils/fragments/cart-fragment'
|
|||||||
export const setOrderShippingAddress = /* GraphQL */ `
|
export const setOrderShippingAddress = /* GraphQL */ `
|
||||||
mutation setOrderShippingAddress($input: CreateAddressInput!) {
|
mutation setOrderShippingAddress($input: CreateAddressInput!) {
|
||||||
setOrderShippingAddress(input: $input) {
|
setOrderShippingAddress(input: $input) {
|
||||||
|
__typename
|
||||||
...Cart
|
...Cart
|
||||||
... on NoActiveOrderError {
|
... on NoActiveOrderError {
|
||||||
errorCode
|
errorCode
|
||||||
|
@ -3,6 +3,7 @@ import { cartFragment } from '../../utils/fragments/cart-fragment'
|
|||||||
export const setOrderShippingMethod = /* GraphQL */ `
|
export const setOrderShippingMethod = /* GraphQL */ `
|
||||||
mutation setOrderShippingMethod($shippingMethodId: ID!) {
|
mutation setOrderShippingMethod($shippingMethodId: ID!) {
|
||||||
setOrderShippingMethod(shippingMethodId: $shippingMethodId) {
|
setOrderShippingMethod(shippingMethodId: $shippingMethodId) {
|
||||||
|
__typename
|
||||||
...Cart
|
...Cart
|
||||||
... on OrderModificationError {
|
... on OrderModificationError {
|
||||||
errorCode
|
errorCode
|
||||||
|
@ -3,10 +3,12 @@ import { cartFragment } from '../fragments/cart-fragment'
|
|||||||
export const transitionOrderToState = /* GraphQL */ `
|
export const transitionOrderToState = /* GraphQL */ `
|
||||||
mutation transitionOrderToState($state: String!) {
|
mutation transitionOrderToState($state: String!) {
|
||||||
transitionOrderToState(state: $state) {
|
transitionOrderToState(state: $state) {
|
||||||
|
__typename
|
||||||
...Cart
|
...Cart
|
||||||
... on OrderStateTransitionError {
|
... on OrderStateTransitionError {
|
||||||
errorCode
|
errorCode
|
||||||
message
|
message
|
||||||
|
transitionError
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user