docs: update storefront development guides to use JS SDK [2] (#12015)
This commit is contained in:
@@ -14,32 +14,30 @@ export const metadata = {
|
||||
|
||||
# {metadata.title}
|
||||
|
||||
In this guide, you'll learn how to complete the cart and place the order. This is the last step of your checkout flow.
|
||||
|
||||
## How to Complete Cart in Storefront Checkout
|
||||
|
||||
Once you finish any required actions with the third-party payment provider, you can complete the cart and place the order.
|
||||
|
||||
To complete the cart, send a request to the [Complete Cart API route](!api!/store#carts_postcartsidcomplete).
|
||||
To complete the cart, send a request to the [Complete Cart API route](!api!/store#carts_postcartsidcomplete). For example:
|
||||
|
||||
For example:
|
||||
<Note title="Tip">
|
||||
|
||||
Learn how to install and configure the JS SDK in the [JS SDK documentation](../../../js-sdk/page.mdx).
|
||||
|
||||
</Note>
|
||||
|
||||
```ts
|
||||
fetch(
|
||||
`http://localhost:9000/store/carts/${cartId}/complete`,
|
||||
{
|
||||
credentials: "include",
|
||||
headers: {
|
||||
"x-publishable-api-key": process.env.NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY || "temp",
|
||||
},
|
||||
method: "POST",
|
||||
}
|
||||
)
|
||||
.then((res) => res.json())
|
||||
.then(({ type, cart, order, error }) => {
|
||||
if (type === "cart" && cart) {
|
||||
sdk.store.cart.complete(cart.id)
|
||||
.then((data) => {
|
||||
if (data.type === "cart" && data.cart) {
|
||||
// an error occured
|
||||
console.error(error)
|
||||
} else if (type === "order" && order) {
|
||||
console.error(data.error)
|
||||
} else if (data.type === "order" && data.order) {
|
||||
// TODO redirect to order success page
|
||||
alert("Order placed.")
|
||||
console.log(order)
|
||||
console.log(data.order)
|
||||
// unset cart ID from local storage
|
||||
localStorage.removeItem("cart_id")
|
||||
}
|
||||
@@ -59,22 +57,29 @@ When the cart completion is successful, it's important to unset the cart ID from
|
||||
|
||||
For example, to complete the cart when the default system payment provider is used:
|
||||
|
||||
<Note title="Tip">
|
||||
|
||||
This example uses the `useCart` hook defined in the [Cart React Context guide](../../cart/context/page.mdx).
|
||||
|
||||
</Note>
|
||||
|
||||
export const highlights = [
|
||||
["4", "useCart", "The `useCart` hook was defined in the Cart React Context documentation."],
|
||||
["10", "handlePayment", "This function sends the request\nto the Medusa application to complete the cart."],
|
||||
["21", "TODO", "If you're integrating a third-party payment provider,\nyou perform the custom logic before completing the cart."],
|
||||
["24", "fetch", "Send a request to the Medusa application\nto complete the cart and place the order."],
|
||||
["36", `type === "cart"`, "If the `type` returned is `cart`,\nit means an error occurred and the cart wasn't completed."],
|
||||
["39", `type === "order"`, "If the `type` returned is `order`,\nit means the cart was completed and the order was placed successfully."],
|
||||
["43", "refreshCart", "Unset and reset the cart."],
|
||||
["50", "button", "This button triggers the `handlePayment` function when clicked."]
|
||||
["11", "handlePayment", "This function sends the request\nto the Medusa application to complete the cart."],
|
||||
["22", "TODO", "If you're integrating a third-party payment provider,\nyou perform the custom logic before completing the cart."],
|
||||
["25", "complete", "Send a request to the Medusa application\nto complete the cart and place the order."],
|
||||
["27", `data.type === "cart"`, "If the `type` returned is `cart`,\nit means an error occurred and the cart wasn't completed."],
|
||||
["30", `type === "order"`, "If the `type` returned is `order`,\nit means the cart was completed and the order was placed successfully."],
|
||||
["34", "refreshCart", "Unset and reset the cart."],
|
||||
["41", "button", "This button triggers the `handlePayment` function when clicked."]
|
||||
]
|
||||
|
||||
```tsx highlights={highlights}
|
||||
"use client" // include with Next.js 13+
|
||||
|
||||
import { useState } from "react"
|
||||
import { useCart } from "../../providers/cart"
|
||||
import { useCart } from "@/providers/cart"
|
||||
import { sdk } from "@/lib/sdk"
|
||||
|
||||
export default function SystemDefaultPayment() {
|
||||
const { cart, refreshCart } = useCart()
|
||||
@@ -94,25 +99,15 @@ export default function SystemDefaultPayment() {
|
||||
// TODO perform any custom payment handling logic
|
||||
|
||||
// complete the cart
|
||||
fetch(
|
||||
`http://localhost:9000/store/carts/${cart.id}/complete`,
|
||||
{
|
||||
credentials: "include",
|
||||
headers: {
|
||||
"x-publishable-api-key": process.env.NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY || "temp",
|
||||
},
|
||||
method: "POST",
|
||||
}
|
||||
)
|
||||
.then((res) => res.json())
|
||||
.then(({ type, cart, order, error }) => {
|
||||
if (type === "cart" && cart) {
|
||||
sdk.store.cart.complete(cart.id)
|
||||
.then((data) => {
|
||||
if (data.type === "cart" && data.cart) {
|
||||
// an error occured
|
||||
console.error(error)
|
||||
} else if (type === "order" && order) {
|
||||
console.error(data.error)
|
||||
} else if (data.type === "order" && data.order) {
|
||||
// TODO redirect to order success page
|
||||
alert("Order placed.")
|
||||
console.log(order)
|
||||
console.log(data.order)
|
||||
refreshCart()
|
||||
}
|
||||
})
|
||||
@@ -133,13 +128,13 @@ export default function SystemDefaultPayment() {
|
||||
In the example above, you create a `handlePayment` function in the payment component. In this function, you:
|
||||
|
||||
- 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.
|
||||
- Send a request to the Complete Cart API route once all actions with the third-party payment provider are performed.
|
||||
- Send a request to the [Complete Cart API route](!api!/store#carts_postcartsidcomplete) once all actions with the third-party payment provider are performed.
|
||||
- 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.
|
||||
- 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.
|
||||
- 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.
|
||||
- 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 framework you're using.
|
||||
|
||||
---
|
||||
|
||||
## React Example with Third-Party Provider
|
||||
## React Example with Third-Party Payment Provider
|
||||
|
||||
Refer to the [Stripe guide](../payment/stripe/page.mdx) for an example on integrating a third-party provider and implementing card completion.
|
||||
|
||||
Reference in New Issue
Block a user