Strapi plugin logo for Payments

Payments

Accept payments with Stripe and Paypal

Strapi plugin Payments

Enable payments in your Strapi application.

Integrate with any other plugin or custom controller and create orders using a service-oriented approach.

Features

  • Use Stripe and Paypal to process payments.
  • Use available services to find, create and confirm orders.
  • Configure everything from the settings dashboard.

Video tutorial

Requirements

  • Stripe account
    • Secret key
  • Paypal merchant app
    • Client ID
    • Client secret

Installation

In the root of your strapi application, run the following command:

npm i strapi-plugin-payments

Configuration

Once installed, go to settings, then Payments Plugin and under Stripe, set the Stripe private key and the success URL and cancel url.

Similarly, in order to accept payments with Paypal, open the Paypal tab, set the Brand Name, the PayPal client ID, the PayPal client secret and the return URL and cancel url.

Usage

The plugin allows to create, find and confirm orders through a few service APIs.

Each service may return an error if something goes wrong or the plugin is not configured properly.

The returned errors are objects with the following structure:

1{
2  error: boolean;
3  status: string;
4  msg: string;
5}

The status and msg can be used to return an HTTP response, for instance, this could be your custom controller:

1if (result.error) {
2  return ctx[result.status](result.msg)
3}

create

For creating orders, call the asynchronous service create, which receives the following parameters:

NameTypeDescription
userUser from User & Permissions plugin
payment_methodstring"credit_card" or "paypal"
payloadJSONThis data will be available for you when confirming and finding orders.
itemsArray of objectsEach object in the array must have the propierties: label (string) price (number) and quantity (number).

Example

1const params = {
2  user: ctx.state.user,
3  payment_method: "credit_card",
4  payload: {courses_ids: [4]},
5  items: [
6    {
7      label: "Python Essentials",
8      price: 15.99,
9      quantity: 1
10    }
11  ]
12}
13
14let result
15
16try {
17  result = await strapi.service("plugin::payments.orders").create(params)
18  if (result.error) {
19    return ctx[result.status](result.msg)
20  }
21} catch(err) {
22  console.log(err)
23  return ctx.internalServerError("Something went wrong")
24}

If the order was created successfully, this service returns an object with a few properties that can be used to redirect the user to checkout.

When paying with credit card, the returned object will have the following properties:

NameType
idstring

id is the order's checkout session, which can be used in the frontend to redirect the user to the checkout page, like the following snippet.

Notice that stripe.redirectToCheckout is a function that comes from the package @stripe/stripejs for redirecting the user to Stripe's checkout page.

1const url = `${STRAPI}/api/orders`
2
3const options = {
4  method: "POST",
5  headers: {
6    "Authorization": `Bearer ${user.token}`,
7    "Content-type": "application/json"
8  },
9  body: JSON.stringify({
10    courses: [6,43], // these are courses IDs
11    method: "credit_card"
12  })
13}
14
15const res = await fetch(url, options)
16const data = await res.json()
17
18const { id } = data
19
20stripe.redirectToCheckout({
21  sessionId: id
22})

When paying with Paypal, the returned object will have the following properties:

NameType
idstring
linksArray

id is the order's ID generated by paypal, and links is an array of links that we can use to redirect the user to Paypal's checkout page.

For instance, check the following code snippet that runs in the frontend.

1const url = `${STRAPI}/api/orders`
2
3// Paying with paypal.
4const options = {
5  method: "POST",
6  headers: {
7    "Authorization": `Bearer ${user.token}`,
8    "Content-type": "application/json"
9  },
10  body: JSON.stringify({
11    courses: [6,43], // these are courses IDs
12    method: "paypal"
13  })
14}
15
16const res = await fetch(url, options)
17const data = await res.json()
18
19const { links } = data
20const link = links.find(l => l.rel === "approve")
21
22// Redirect to checkout.
23document.location = link.href

confirm

This asynchronous service sends a request to either Paypal or Stripe to confirm a payment.

It receives the following parameters:

NameTypeDescription
userUser from User & Permissions plugin
checkout_sessionstringthe token in the success url page

When confirming a payment, the user in the parameter must be the same as the user which the order is related to.

If the payment is confirmed, this service returns the order object with a field confirmed set to true.

Otherwise, the service returns an error object with the same properties as the create service.

Order object structure:

PropertyType
idstring
amountnumber
createdAtDate
userObject with id as the only property
confirmedboolean
checkout_sessionstring
payment_method"credit_card" or "paypal"
payloadJSON
itemsJSON
responseJSON representing additional information

find

Finding the orders of a user is done through the find service.

It receives a user and returns an array containing all the orders of the user.

For instance, this could be your custom controller to return the user's orders.

1const { user } = ctx.state
2
3const orders = await strapi.service("plugin::payments.orders").find(user)
4
5ctx.body = {
6  orders
7}

findOne

This service is for searching a specific order by checkout session, taking two parameters: the user and the order's checkout session.

See the following example of a custom controller invoking this service.

1const { user } = ctx.state
2const { id } = ctx.params;
3
4const order = await strapi.service("plugin::payments.orders").findOne(user)
5
6ctx.body = {
7  order
8}

In case the order is not found, the return value will be null.

The user provided as a parameter must be equal to the user which the order belongs to. Otherwise, a forbidden error is returned.

Install now

npm install strapi-plugin-payments

STATS

2 GitHub stars1 weekly download

Last updated

1018 days ago

Strapi Version

4.0.0 and above

Author

github profile image for Luis G. Villegas
Luis G. Villegas

Useful links

Create your own plugin

Check out the available plugin resources that will help you to develop your plugin or provider and get it listed on the marketplace.