Raw
1 ---
2 tags:
3 - cart
4 - order
5 - payment
6 - storefront
7 products:
8 - cart
9 - order
10 - payment
11 ---
12
13 import { CodeTabs, CodeTab, Prerequisites } from "docs-ui"
14
15 export const metadata = {
16 title: `Checkout Step 5: Complete Cart`,
17 }
18
19 # {metadata.title}
20
21 In this guide, you'll learn how to complete the cart and place the order. This is the last step of your checkout flow.
22
23 <Note title="Warning" type="warning">
24
25 Don't use the `validate` hook of `completeCartWorkflow` to mutate the cart's line items, shipping methods, or totals. The workflow retrieves the cart once before any hook runs and builds the order from that snapshot. If you mutate the cart in a hook, the created order and the authorized payment amount won't reflect the change.
26
27 If you need to change the cart before completing it, run a separate step or workflow before `completeCartWorkflow`, then refresh the cart's payment collection so that the payment session amount matches the new total.
28
29 </Note>
30
31 ## How to Complete Cart in Storefront Checkout
32
33 Once you finish any required actions with the third-party payment provider, you can complete the cart and place the order.
34
35 To complete the cart, send a request to the [Complete Cart API route](!api!/store/carts/complete-cart). For example:
36
37 <Note title="Tip">
38
39 Learn how to install and configure the JS SDK in the [JS SDK documentation](../../../js-sdk/page.mdx).
40
41 </Note>
42
43 ```ts
44 sdk.store.cart.complete(cart.id)
45 .then((data) => {
46 if (data.type === "cart" && data.cart) {
47 // an error occurred
48 console.error(data.error)
49 } else if (data.type === "order" && data.order) {
50 // TODO redirect to order success page
51 alert("Order placed.")
52 console.log(data.order)
53 // unset cart ID from local storage
54 localStorage.removeItem("cart_id")
55 }
56 })
57 ```
58
59 In the response of the request, the `type` field determines whether the cart completion was successful:
60
61 - If the `type` is `cart`, it means the cart completion failed. The `error` response field holds the error details.
62 - If the `type` is `order`, it means the cart was completed and the order was placed successfully.
63
64 When the cart completion is successful, it's important to unset the cart ID from the `localStorage`, as the cart is no longer usable.
65
66 <Note title="Tip">
67
68 Check `type === "order"` to confirm cart completion. Don't assume the payment was charged based on calling the complete cart endpoint alone.
69
70 </Note>
71
72 ---
73
74 ## Payment Failure Handling
75
76 When cart completion fails after the payment was authorized or captured, the `completeCartWorkflow` automatically reverts the payment so the customer isn't charged for an incomplete order:
77
78 - An authorized payment that wasn't yet captured is canceled.
79 - A captured payment is refunded, unless a later or concurrent completion attempt already placed an order for the same cart. In that case, the refund is skipped so that the successful order keeps its payment.
80
81 Because the payment is reverted before the response is returned, a storefront doesn't need to issue a refund itself when completion fails. The `type: "cart"` response signals that completion failed and the payment was rolled back.
82
83 ---
84
85 ## Order's Locale after Cart Completion
86
87 <Prerequisites
88 items={[
89 {
90 text: "Translation Module Configured",
91 link: "/commerce-modules/translation#configure-translation-module",
92 }
93 ]}
94 />
95
96 When you complete the cart, items in the order will be in the locale that was set for the cart. This ensures that the customer sees the order details in their preferred language.
97
98 If no locale was set for the cart, then the order's items will be in the original product content.
99
100 ---
101
102 ## React Example with Default System Payment Provider
103
104 For example, to complete the cart when the default system payment provider is used:
105
106 <Note title="Tip">
107
108 This example uses the `useCart` hook defined in the [Cart React Context guide](../../cart/context/page.mdx).
109
110 </Note>
111
112 export const highlights = [
113 ["4", "useCart", "The `useCart` hook was defined in the Cart React Context documentation."],
114 ["11", "handlePayment", "This function sends the request\nto the Medusa application to complete the cart."],
115 ["22", "TODO", "If you're integrating a third-party payment provider,\nyou perform the custom logic before completing the cart."],
116 ["25", "complete", "Send a request to the Medusa application\nto complete the cart and place the order."],
117 ["27", `data.type === "cart"`, "If the `type` returned is `cart`,\nit means an error occurred and the cart wasn't completed."],
118 ["30", `type === "order"`, "If the `type` returned is `order`,\nit means the cart was completed and the order was placed successfully."],
119 ["34", "refreshCart", "Unset and reset the cart."],
120 ["41", "button", "This button triggers the `handlePayment` function when clicked."]
121 ]
122
123 ```tsx highlights={highlights}
124 "use client" // include with Next.js 13+
125
126 import { useState } from "react"
127 import { useCart } from "@/providers/cart"
128 import { sdk } from "@/lib/sdk"
129
130 export default function SystemDefaultPayment() {
131 const { cart, refreshCart } = useCart()
132 const [loading, setLoading] = useState(false)
133
134 const handlePayment = (
135 e: React.MouseEvent<HTMLButtonElement, MouseEvent>
136 ) => {
137 e.preventDefault()
138
139 if (!cart) {
140 return
141 }
142
143 setLoading(true)
144
145 // TODO perform any custom payment handling logic
146
147 // complete the cart
148 sdk.store.cart.complete(cart.id)
149 .then((data) => {
150 if (data.type === "cart" && data.cart) {
151 // an error occurred
152 console.error(data.error)
153 } else if (data.type === "order" && data.order) {
154 // TODO redirect to order success page
155 alert("Order placed.")
156 console.log(data.order)
157 refreshCart()
158 }
159 })
160 .finally(() => setLoading(false))
161 }
162
163 return (
164 <button
165 onClick={handlePayment}
166 disabled={loading}
167 >
168 Place Order
169 </button>
170 )
171 }
172 ```
173
174 In the example above, you create a `handlePayment` function in the payment component. In this function, you:
175
176 - Optionally perform any required actions with the third-party payment provider. For example, authorize the payment. For the default system payment provider, no actions are required.
177 - Send a request to the [Complete Cart API route](!api!/store/carts/complete-cart) once all actions with the third-party payment provider are performed.
178 - In the received response of the request, if the `type` is `cart`, it means that the cart completion failed. The error is set in the `error` response field.
179 - If the `type` is `order`, it means the card was completed and the order was placed successfully. You can access the order in the `order` response field.
180 - When the order is placed, you must unset the `cart_id` from the `localStorage`. You can redirect the customer to an order success page at this point. The redirection logic depends on the storefront framework you're using.
181
182 ---
183
184 ## React Example with Third-Party Payment Provider
185
186 Refer to the [Stripe guide](../payment/stripe/page.mdx) for an example on integrating a third-party provider and implementing card completion.