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:
Shahed Nasser
2022-07-20 12:08:08 +03:00
committed by GitHub
parent f623a85c5d
commit 45dcf420c6
3 changed files with 182 additions and 185 deletions
+32 -141
View File
@@ -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 @@ Thats 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 dont 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 youre 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 Medusas 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 PayPals button if the PayPal provider is available for the current cart.
2. When the button is clicked, open PayPals payment portal and wait for the customer to authorize the payment.
3. If the payment is authorized successfully, set PayPals [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 PayPals 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 dont have the storefront installed, you can follow [this quickstart guide](../starters/nextjs-medusa-starter.md).
In your `.env.local` file (or the file youre 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 youre 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 [PayPals 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 youll 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
```
Heres 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 PayPals 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` youll 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";
```
Thats 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.
![PayPal Button](https://i.imgur.com/bDhBcV3.png)
![PayPal Button](https://i.imgur.com/F8OvsOJ.png)
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 dont 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
![PayPal Button](https://i.imgur.com/PsibgPY.png)
## 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.
+16 -20
View File
@@ -28,7 +28,7 @@ This section guides you over the steps necessary to add Stripe as a payment prov
If you dont have a Medusa server installed yet, you must follow our [quickstart guide](../quickstart/quick-start) first.
### Plugin Installation
### Install the Stripe Plugin
In the root of your Medusa server, run the following command to install the stripe plugin:
@@ -36,7 +36,7 @@ In the root of your Medusa server, run the following command to install the stri
npm install medusa-payment-stripe
```
### Plugin Configuration
### Configure the Stripe Plugin
Next, you need to add configurations for your stripe plugin.
@@ -63,7 +63,7 @@ You might find that this code is already available but commented out. You can pr
The Stripe plugin uses 2 configuration options. The `api_key` is essential to both your development and production environments. As for the `webhook_secret`, its essential for your production environment. So, if youre only using Stripe for development you can skip adding the value for this option at the moment.
### Retrieving The Keys
### Retrieve Stripe's Keys
On the [dashboard](https://dashboard.stripe.com) of your Stripe account click on the Developers link at the top right. This will take you to the developer dashboard.
@@ -101,11 +101,11 @@ This section will guide you through adding Stripe as a payment provider in a reg
This step is required for you to be able to use Stripe as a payment provider in your storefront.
### Prerequisites
### Admin Prerequisites
If you dont have a Medusa admin installed, make sure to follow along with [the guide on how to install it](https://github.com/medusajs/admin#-quickstart) before continuing with this section.
### Adding Stripe
### Add Stripe to Regions
First, make sure that both your Medusa server and Medusa Admin are running.
@@ -123,31 +123,27 @@ Once youre done, click Save. Stripe is now a payment provider in your store i
This guide will take you through how to set up Stripe payments in your Medusa storefront. It includes the steps necessary when using one of Medusas official storefronts as well as your own custom React-based storefront.
### Prerequisites
### Storefront Prerequisites
All storefronts require that you obtain your Stripes Publishable Key. You can retrieve it from your Stripes developer dashboard by choosing API Keys and then copying the Publishable Key.
### 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 dont have the storefront installed, you can follow [this quickstart guide](../starters/nextjs-medusa-starter).
In your `.env.local` file (or the file youre using for your environment variables), add the following variable with its value set to the Publishable Key:
In your `.env.local` file (or the file youre using for your environment variables), add the following variable:
```jsx
NEXT_PUBLIC_STRIPE_KEY=pk_...
```bash
NEXT_PUBLIC_STRIPE_KEY=<YOUR_PUBLISHABLE_KEY>
```
:::note
This variable might be available in your `.env` file but commented out. You can instead remove the comment and change its value.
:::
Make sure to replace `<YOUR_PUBLISHABLE_KEY>` with your Stripe Publishable Key.
Now, if you run your Medusa server and your storefront, on checkout youll be able to use Stripe.
![Next.js Stripe Form](https://i.imgur.com/1XvW776.png)
![Next.js Stripe Form](https://i.imgur.com/h5mWdJT.png)
### Gatsby Storefront
### Add to Gatsby Storefront
Medusa also has a Gatsby storefront that you can use as your ecommerce store. If you dont have the storefront installed, you can follow [this quickstart guide](../starters/gatsby-medusa-starter).
@@ -167,7 +163,7 @@ Now, if you run your Medusa server and your storefront, on checkout youll be
![Gatsby Stripe Form](https://i.imgur.com/1XvW776.png)
### Custom Storefront
### Add to Custom Storefront
This section will go over how to add Stripe into a React-based framework. The instructions are general instructions that you can use in your storefront.
@@ -180,7 +176,7 @@ The integration with stripe must have the following workflow:
3. After the user enters their card details and submits the form, confirm the payment with Stripe.
4. If the payment is confirmed successfully, [complete the order](https://docs.medusajs.com/api/store/cart/complete-a-cart) in Medusa. Otherwise show an error.
#### Installing Dependencies
#### Install Dependencies
Before you start the implementations you need to install the necessary dependencies. Youll be using Stripes React libraries to show the UI and handle the payment confirmation:
@@ -360,7 +356,7 @@ If you run your server and storefront now, youll see the Stripe UI element an
![Stripe Form](https://i.imgur.com/NOi8THw.png)
## Capturing Payment
## Capture Payments
After the customer places an order, youll be able to see the order on the admin panel. In the payment information under the “Payment” section, you should see a “Capture” button.