feat(core-flows,typers,utils,medusa): add payment auth step to complete cart workflow - [complete cart part 3] (#7248)
* chore: authorize payment sessions for cart * chore: add spec for cart returns * fix: Correctly select fields for cart * chore: fix specs + address comments --------- Co-authored-by: Stevche Radevski <sradevski@live.com> Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com> Co-authored-by: Carlos R. L. Rodrigues <37986729+carlos-r-l-rodrigues@users.noreply.github.com> Co-authored-by: Carlos R. L. Rodrigues <rodrigolr@gmail.com>
This commit is contained in:
14 files changed
+391
-55
No files matched your search
@@ -1,7 +1,11 @@
|
||||
import { completeCartWorkflow } from "@medusajs/core-flows"
|
||||
import { MedusaError } from "@medusajs/utils"
|
||||
import { MedusaRequest, MedusaResponse } from "../../../../../types/routing"
|
||||
import { refetchOrder } from "../../../orders/helpers"
|
||||
import { refetchCart } from "../../helpers"
|
||||
import { defaultStoreCartFields } from "../../query-config"
|
||||
import { StoreCompleteCartType } from "../../validators"
|
||||
import { prepareRetrieveQuery } from "../../../../../utils/get-query-config"
|
||||
|
||||
export const POST = async (
|
||||
req: MedusaRequest<StoreCompleteCartType>,
|
||||
@@ -9,22 +13,49 @@ export const POST = async (
|
||||
) => {
|
||||
const cart_id = req.params.id
|
||||
|
||||
// If the idempotencyKey is present:
|
||||
// - is workflow is running?
|
||||
// = throw error
|
||||
// - else
|
||||
// - re-run the workflow at the failed step
|
||||
|
||||
const { errors, result } = await completeCartWorkflow(req.scope).run({
|
||||
input: { id: req.params.id },
|
||||
context: {
|
||||
transactionId: cart_id,
|
||||
},
|
||||
input: { id: cart_id },
|
||||
context: { transactionId: cart_id },
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
// When an error occurs on the workflow, its potentially got to with cart validations, payments
|
||||
// or inventory checks. Return the cart here along with errors for the consumer to take more action
|
||||
// and fix them
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
const error = errors[0].error
|
||||
const statusOKErrors: string[] = [
|
||||
// TODO: add inventory specific errors
|
||||
MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
|
||||
MedusaError.Types.PAYMENT_REQUIRES_MORE_ERROR,
|
||||
]
|
||||
|
||||
// If we end up with errors outside of statusOKErrors, it means that the cart is not in a state to be
|
||||
// completed. In these cases, we return a 400.
|
||||
const cart = await refetchCart(
|
||||
cart_id,
|
||||
req.scope,
|
||||
prepareRetrieveQuery(
|
||||
{},
|
||||
{
|
||||
defaults: defaultStoreCartFields,
|
||||
}
|
||||
).remoteQueryConfig.fields
|
||||
)
|
||||
|
||||
if (!statusOKErrors.includes(error.type)) {
|
||||
throw error
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
type: "cart",
|
||||
cart,
|
||||
error: {
|
||||
message: error.message,
|
||||
name: error.name,
|
||||
type: error.type,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
const order = await refetchOrder(
|
||||
@@ -33,5 +64,8 @@ export const POST = async (
|
||||
req.remoteQueryConfig.fields
|
||||
)
|
||||
|
||||
res.status(200).json({ order })
|
||||
res.status(200).json({
|
||||
type: "order",
|
||||
order,
|
||||
})
|
||||
}
|
||||
@@ -89,7 +89,6 @@ export const defaultStoreCartFields = [
|
||||
"*region.countries",
|
||||
"sales_channel_id",
|
||||
|
||||
// TODO: To be updated when payment sessions are introduces in the Rest API
|
||||
"payment_collection.id",
|
||||
"payment_collection.amount",
|
||||
"*payment_collection.payment_sessions",
|
||||
|
||||
@@ -89,3 +89,8 @@ export const StoreCompleteCart = z
|
||||
})
|
||||
.strict()
|
||||
export type StoreCompleteCartType = z.infer<typeof StoreCompleteCart>
|
||||
|
||||
export type StoreCreateCartPaymentCollectionType = z.infer<
|
||||
typeof StoreCreateCartPaymentCollection
|
||||
>
|
||||
export const StoreCreateCartPaymentCollection = z.object({}).strict()
|
||||
@@ -50,6 +50,6 @@ export const defaultStoreRetrieveOrderFields = [
|
||||
]
|
||||
|
||||
export const retrieveTransformQueryConfig = {
|
||||
defaultFields: defaultStoreRetrieveOrderFields,
|
||||
defaults: defaultStoreRetrieveOrderFields,
|
||||
isList: false,
|
||||
}
|
||||
@@ -2,10 +2,7 @@ export const defaultPaymentCollectionFields = [
|
||||
"id",
|
||||
"currency_code",
|
||||
"amount",
|
||||
"payment_sessions",
|
||||
"payment_sessions.id",
|
||||
"payment_sessions.amount",
|
||||
"payment_sessions.provider_id",
|
||||
"*payment_sessions",
|
||||
]
|
||||
|
||||
export const retrievePaymentCollectionTransformQueryConfig = {
|
||||
|
||||
Reference in new issue
Block a user