docs: update Next.js documentation based on the new storefront (#1880)
* update next.js storefront docs * made changes to Next.js sections
This commit is contained in:
@@ -22,7 +22,9 @@ Additionally, you need a Medusa server installed and set up. If not, you can fol
|
||||
|
||||
You also need [Medusa Admin](../admin/quickstart.md) installed to enable PayPal as a payment provider. You can alternatively use our [REST APIs](https://docs.medusajs.com/api/admin/auth).
|
||||
|
||||
## Plugin Installation
|
||||
## Medusa Server
|
||||
|
||||
### Install the PayPal Plugin
|
||||
|
||||
In the root of your Medusa server, run the following command to install the PayPal plugin:
|
||||
|
||||
@@ -30,7 +32,7 @@ In the root of your Medusa server, run the following command to install the PayP
|
||||
npm install medusa-payment-paypal
|
||||
```
|
||||
|
||||
### Plugin Configuration
|
||||
### Configure the PayPal Plugin
|
||||
|
||||
Next, you need to add configurations for your PayPal plugin.
|
||||
|
||||
@@ -68,7 +70,15 @@ That’s all you need to install PayPal on your Medusa server!
|
||||
|
||||
## Admin Setup
|
||||
|
||||
The next step is to enable PayPal as a payment provider in at least one region from the Medusa Admin.
|
||||
This section will guide you through adding PayPal as a payment provider in a region using your Medusa admin dashboard.
|
||||
|
||||
This step is required for you to be able to use PayPal as a payment provider in your storefront.
|
||||
|
||||
### Admin Prerequisites
|
||||
|
||||
If you don’t have a Medusa admin installed, make sure to follow along with [the guide on how to install it](../admin/quickstart.md) before continuing with this section.
|
||||
|
||||
### Add PayPal to Regions
|
||||
|
||||
Run your Medusa server and Medusa admin, then open the Medusa admin and login.
|
||||
|
||||
@@ -86,171 +96,52 @@ Once you’re done, click Save. PayPal is now a payment provider in your store i
|
||||
|
||||
This section will take you through the steps to add PayPal as a payment method on the storefront. It includes the steps necessary when using one of Medusa’s official storefronts as well as your own custom React-based storefront.
|
||||
|
||||
### Storefront Prerequisites
|
||||
|
||||
All storefronts require that you obtain your PayPal Client ID. You can retrieve it from your PayPal developer dashboard.
|
||||
|
||||
### Process Overview
|
||||
|
||||
Aside from the Next.js Storefront, you need to add the implementation with PayPal manually.
|
||||
|
||||
:::note
|
||||
|
||||
It is recommended to read through the [Frontend Checkout Flow](/advanced/storefront/how-to-implement-checkout-flow) first to fully understand how payment is implemented on the storefront.
|
||||
|
||||
:::
|
||||
|
||||
### Process Overview
|
||||
|
||||
Although the 3 next sections have different implementations to add PayPal into your storefront, they essentially follow the same process:
|
||||
Although the next sections have different implementations to add PayPal into your storefront, they essentially follow the same process:
|
||||
|
||||
1. Show PayPal’s button if the PayPal provider is available for the current cart.
|
||||
2. When the button is clicked, open PayPal’s payment portal and wait for the customer to authorize the payment.
|
||||
3. If the payment is authorized successfully, set PayPal’s [Payment Session](../advanced/backend/payment/overview.md#payment-session) as the session used to perform the payment for the current cart, then update the Payment Session on the server with the data received from PayPal’s payment portal. This data is essential to the server to verify the authorization and perform additional payment processing later such as capturing payment.
|
||||
4. Complete the cart to create the order.
|
||||
|
||||
:::note
|
||||
:::info
|
||||
|
||||
In Medusa, by default, payments are authorized during checkout, but the payment is not captured right away. The payment should be manually [captured from the Medusa Admin](#capture-payment).
|
||||
|
||||
:::
|
||||
|
||||
## Next.js Storefront
|
||||
### Add to Next.js Storefront
|
||||
|
||||
Medusa has a Next.js storefront that you can easily use with your Medusa server. If you don’t have the storefront installed, you can follow [this quickstart guide](../starters/nextjs-medusa-starter.md).
|
||||
|
||||
In your `.env.local` file (or the file you’re using for your environment variables), add the following variable with its value set to the Client ID:
|
||||
In your `.env.local` file (or the file you’re using for your environment variables), add the following variable:
|
||||
|
||||
```bash
|
||||
NEXT_PUBLIC_PAYPAL_CLIENT_ID=<CLIENT_ID>
|
||||
NEXT_PUBLIC_PAYPAL_CLIENT_ID=<YOUR_CLIENT_ID>
|
||||
```
|
||||
|
||||
Then, install [PayPal’s React components](https://www.npmjs.com/package/@paypal/react-paypal-js) library:
|
||||
Make sure to replace `<YOUR_CLIENT_ID>` with your PayPal Client ID.
|
||||
|
||||
```bash npm2yarn
|
||||
npm install @paypal/react-paypal-js
|
||||
```
|
||||
Now, if you run your Medusa server and your storefront, on checkout you’ll be able to use PayPal].
|
||||
|
||||
Next, create a new file `components/checkout/paypal.jsx` with the following content:
|
||||
|
||||
```jsx
|
||||
import { PayPalButtons, PayPalScriptProvider } from "@paypal/react-paypal-js";
|
||||
import React, { useContext, useState } from "react";
|
||||
|
||||
import { BiLeftArrowAlt } from "react-icons/bi";
|
||||
import DisplayContext from "../../context/display-context";
|
||||
import StoreContext from "../../context/store-context";
|
||||
import { createClient } from "../../utils/client"
|
||||
import styles from "../../styles/injectable-payment-card.module.css";
|
||||
import { useRouter } from "next/router";
|
||||
|
||||
const paypalClientId = process.env.NEXT_PUBLIC_PAYPAL_CLIENT_ID || ""
|
||||
|
||||
const Paypal = () => {
|
||||
const [error, setError] = useState(null);
|
||||
const [processing, setProcessing] = useState("");
|
||||
const { cart, setPaymentSession } = useContext(StoreContext);
|
||||
const { updateCheckoutStep } = useContext(DisplayContext);
|
||||
|
||||
const router = useRouter();
|
||||
|
||||
const client = createClient()
|
||||
|
||||
const handleSubmit = async (data, actions) => {
|
||||
actions.order.authorize().then(async (authorization) => {
|
||||
if (authorization.status !== 'COMPLETED') {
|
||||
setError(`An error occurred, status: ${authorization.status}`);
|
||||
setProcessing(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const { cart } = await setPaymentSession("paypal")
|
||||
|
||||
if (!cart) {
|
||||
setProcessing(false)
|
||||
return
|
||||
}
|
||||
|
||||
await client.carts.updatePaymentSession(cart.id, "paypal", {
|
||||
data: {
|
||||
data: {
|
||||
...authorization
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
setError(null);
|
||||
setProcessing(false);
|
||||
router.push(`/payment`);
|
||||
})
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<PayPalScriptProvider options={{
|
||||
"client-id": paypalClientId,
|
||||
"currency": cart.region.currency_code.toUpperCase(),
|
||||
"intent": "authorize"
|
||||
}}>
|
||||
{error && (
|
||||
<span className="text-rose-500 mt-4">{error}</span>
|
||||
)}
|
||||
<PayPalButtons
|
||||
style={{ layout: "horizontal" }}
|
||||
onApprove={handleSubmit}
|
||||
disabled={processing}
|
||||
/>
|
||||
</PayPalScriptProvider>
|
||||
<button
|
||||
className={styles.stepBack}
|
||||
onClick={() => updateCheckoutStep(2)}
|
||||
>
|
||||
<BiLeftArrowAlt /> Back to shipping method
|
||||
</button>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export default Paypal
|
||||
```
|
||||
|
||||
Here’s briefly what this code snippet does:
|
||||
|
||||
1. This component renders a PayPal button to initialize the payment using PayPal. You use the components from the PayPal React components library to render the button and you pass the `PayPalScriptProvider` component the Client ID.
|
||||
2. When the button is clicked, the `handleSubmit` function is executed. In this method, you initialize the payment authorization using `actions.order.authorize()`. It takes the customer to another page to log in with PayPal and authorize the payment.
|
||||
3. After the payment is authorized successfully on PayPal’s portal, the fulfillment function passed to `actions.order.authorize().then` will be executed.
|
||||
4. In the fulfillment function, you first ensure that the payment session for the PayPal payment provider is set as the [selected Payment Session in the cart](https://docs.medusajs.com/api/store/cart/select-a-payment-session). Then, you send a request to the server to [update the payment session](https://docs.medusajs.com/api/store/cart/update-a-payment-session) data with the authorization data received from PayPal.
|
||||
5. After all that is done successfully, you navigate to the `/payment` page which will automatically [complete the cart and place the order](https://docs.medusajs.com/api/store/cart/complete-a-cart).
|
||||
|
||||
The last step is to add this component as the component to render when PayPal is available as a payment provider.
|
||||
|
||||
In `src/checkout/payment-step.jsx` you’ll find in the return statement a switch statement that checks the payment provider for each payment session and renders the component based on the ID. Add before the `default` case a case for `paypal`:
|
||||
|
||||
```jsx
|
||||
switch (ps.provider_id) {
|
||||
case "stripe":
|
||||
//..
|
||||
case "manual":
|
||||
//...
|
||||
case "paypal":
|
||||
return (
|
||||
<div key="paypal">
|
||||
<h2>PayPal Payment</h2>
|
||||
<Paypal />
|
||||
</div>
|
||||
)
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
```
|
||||
|
||||
Make sure to also import the `Paypal` component at the top of the file:
|
||||
|
||||
```jsx
|
||||
import Paypal from "./paypal";
|
||||
```
|
||||
|
||||
That’s all you need to integrate PayPal into the Next.js storefront.
|
||||
|
||||
Now, start the Medusa server and the Next.js storefront server. Try adding an item into the cart and proceeding to checkout. When you reach the payment step, you should see the PayPal button.
|
||||
|
||||

|
||||

|
||||
|
||||
You can test out the payment with PayPal using your sandbox account.
|
||||
|
||||
### Gatsby Storefront
|
||||
### Add to Gatsby Storefront
|
||||
|
||||
Medusa also has a Gatsby storefront that you can use as your ecommerce storefront. If you don’t have the storefront installed, you can follow [this quickstart guide](../starters/gatsby-medusa-starter.md).
|
||||
|
||||
@@ -399,7 +290,7 @@ Now, start the Medusa server and the Gatsby storefront server. Try adding an ite
|
||||
|
||||
You can test out the payment with PayPal using your sandbox account.
|
||||
|
||||
### Custom Storefront
|
||||
### Add to Custom Storefront
|
||||
|
||||
This section guides you to add PayPal into a React-based framework. The instructions are general instructions that you can use in your storefront.
|
||||
|
||||
@@ -503,7 +394,7 @@ If you run the Medusa server and the storefront server, you should see the PayPa
|
||||
|
||||

|
||||
|
||||
## Capture Payment
|
||||
## Capture Payments
|
||||
|
||||
After the customer places an order, you can see the order on the admin panel. In the payment information under the “Payment” section, you should see a “Capture” button.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user