🍝 Deliveroo Clone with Next (React.js), GraphQL, Strapi and Stripe - πŸ’΅ Order and Checkout (part 6/7)

This tutorial is a part of the Β« NextJS, Strapi, GraphQL, Stripe Deliveroo clone (Part 6/7 - Order and Checkout) Β»:

Table of contents

Note: the source code is available on GitHub: https://github.com/ryanbelke/strapi-next.

πŸ’΅ Order and Checkout

You must start being starving... I am sure you want to be able to order!

Order

Define Content Type

We need to store the orders in our database, so we are going to create a new Content Type in our API.

Same process as usual:

  • Navigate to the Content Type Builder (http://localhost:1337/admin/plugins/content-type-builder).
  • Click on Add Content Type.
  • Set order as name.
  • Click on Add New Field and create the followings fields:
    • address with type String.
    • city with type String.
    • dishes with type JSON.
    • amount with type Number (float).
  • Click on Save.

(postal code is not used in this tutorial) Order Content Type Builder

Allow access

To create new orders from the client, we are going to hit the create endpoint of the order API. To allow access, navigate to the Roles & Permissions section (http://localhost:1337/admin/plugins/users-permissions), select the authenticated role, tick the order/create checkbox and save.

Stripe setup

In this section you will need Stripe API keys. To get them, create a Stripe account and navigate to https://dashboard.stripe.com/account/apikeys.

Add logic

If you already used Stripe, you probably know the credit card information does not go through your backend server. Instead, the credit card information is sent to the Stripe API (ideally using their SDK). Then, your frontend receives a token that can be used to charge credit cards. The id of the must be sent to your backend which will create the Stripe charge.

Not passing the credit card information through your server releieves you the responsibility to meet complicated data handling compliance, and is just far easier than worrying about securely storing sensitive data.

In order to integrate the Stripe logic, we need to update the create charge endpoint in our Strapi API. To do so, edit server/api/order/controllers/order.js and replace its content with:

Path: server/api/order/controllers/order.js

Install the stripe package inside the server direcoty:

cd server  
npm i stripe --save  

Note: in a real-world example, the amount should be checked on the backend side and the list of dishes related to the command should be stored in a more specific Content Type called orderDetail.

Do not forget to restart the Strapi server.

Checkout page

Create a new page: pages/checkout.js/,

cd pages  
touch checkout.js  

Path: pages/checkout.js


Now we are going to create the checkout form and card section component to capture the credit card info and pass it to Stripe using the react-stripe-elements package
yarn add react-stripe-elements

Create the checkout form files:

cd components  
mkdir Checkout  
cd Checkout  
touch CheckoutForm.js  

Path: components/Checkout/CheckoutForm.js

Now create a CardSection.js file to use the React Elements in, this will house the input boxes that will capture the CC information.

touch CardSection.js  

Path: components/Checkout/CardSection.js

Now if you select a dish and click order you should see:

CheckoutPicture

Now if you submit your order, you should see the order under the Strapi dashboard as foloows:

Strapi

Explanations πŸ•΅οΈ

stripe-react-elements
https://github.com/stripe/react-stripe-elements

For server side rendering with react-stripe elements, some modifications need to be made as Stripe will only be available on the client not the server.

To account for this, the stripe pk_key is set in the ComponenetDidMount lifecycle hook that only fires on the browser:

Stripe will use a <StripeProvider> component that will take in your stripe pk_key as a prop. This allows the children of the component access to the stripe key.

To use the integrated stripe components we will wrap our CheckoutForm component in the <Elements> component.

The downstream import { injectStripe } from "react-stripe-elements" inside the CheckoutForm component is required for the Elements children components to pass the CC information to Stripe.

Stripe will automatically detect which components are generating the CC information and what information to send to receive the token.

This submitOrder function will first make the call to Stripe with the CC information and receive back the Token if the CC check passed. If the token is received we next call the Strpi SDK to create the order passing in the appropriate information and token id.

This is what creates the order in Stripe and creates the DB entry in Strapi. If successful you should see your Stripe test balances increase by the amount of the test order.

You are now able to let users submit their order.

Bon appΓ©tit! πŸ‡«πŸ‡·

πŸš€ In the next (and last) section, you will learn how to deploy your Strapi app on Heroku and your frontend app on NOW https://zeit.co/now#get-started: https://blog.strapi.io/strapi-next-deploy.