React.js PayPal Smart Button
Overview
A simple and flexible React.js wrapper of the latest version of PayPal's Smart Payment Buttons. This makes it easy and fast to set up accept PayPal transactions in your React.js app.
Installation
npm i paypal-button-react
Or
yarn add paypal-button-react
Quickstart
Simplest example to get you going.
import React from 'react';import PayPalSmartButton from 'paypal-react' Component { //The data argument returns transaction details such as shipping details, address, country, etc. console } { //Here is where you would set state indicating the transaction was cancelled console } { //Set state showing some kind of error happend console } const credentials = "sandbox": "YOUR_SANDBOX_CREDENTIALS" "production": "YOUR_PRODUCTION_CREDENTIALS" { return <PayPalSmartButton ="sandbox" // ! =// ="USD" ="100.0" = = /> }
Advanced Setup
This setup is entirely optional.
You can send the returned orderID
from a PayPal transaction to a backend service to verify it, write it to a database, perform other operations, etc.
Here is a Full-Stack example where the React.js app sends the orderID
to an express.js endpoint. From there, the app looks up the transaction from the ID then returns a response status based verification properties from that transaction. Example below shows this as followed.
Front-end Setup
Send the orderID
to the backend upon a successful transaction.
async { const fetchOrder = await const response = await fetchOrder console}<PayPalSmartButton //... =/>
Back-end Setup
First, install the required server dependency.
yarn add @paypal/checkout-server-sdk
Or
npm i @paypal/checkout-server-sdk
Create the endpoint.
const express = ;const router = express;const cors = ;const checkoutNodeJssdk = ;router; //These functions essentially take your credentials and return a usable client method which can be used to query and authorize transactions. { let clientId = 'MY_CLIENT_ID'; //Same one used on the PayPal button let clientSecret = 'MY_CLIENT_SECRET'; //Obtained from console //This is set to sandbox, you would set this to the production method. return clientId clientSecret ;} { return ;} router;
Other Callback Events
Here is a list of other callback events that you can use for many different use-cases such as validating user input, localizing transactions, disabling the button, etc.
Event: onShippingChange
This event fires when a customer updates their shipping information. For example, if you are a US-based company that can only ship in North America, you would use this to event to appropriately respond. In the example below, any transaction that is not from United States, Canada or Mexico is declined.
{ ifdatashipping_addresscountry !== "US" || "CA" || "MX" actions //Decline the transaction. }
<PayPalButton //... =/>
Event: onInit
This event is fired immediately after render. Use this if you wanted to prevent a user from continuing with the transaction before they fill out some input fields.
import React from 'react';import PayPalSmartButton from 'paypal-react' Component state = checkBox: false { ifthisstatecheckBox actions else //Set state showing they need to check the box first. actions } { return <PayPalSmartButton //... = /> }
Event: onClick
Use this event in conjunction with onInit
to visually alert the user with validations
import React from 'react';import PayPalSmartButton from 'paypal-react' Component state = checkBox: false { ifthisstatecheckBox actions else actions } { //Some kind of business logic that tells the user to fill out all fields } { return <PayPalSmartButton //... = /> }
Props
Prop Name | Type | Example Value | Description |
---|---|---|---|
credentials | object | {"sandbox": "MYSANDBOXID", "production": "MYPRODUCTIONID"} |
Used to authenticate with the API. Seperate tokens are used in sandox and production. Visit here to create an app and get a token. |
env | string | sandbox or production |
Used to specify whether to use the sandbox or production version of the clientID |
currency | string | "USD" or "CAD" |
Must be a formatted as a 3-letter ISO code representing a countries currency. The full list of supported fiat currencies located here. |
total | string or number | 100.0 or "5.23" |
The total amount you are charging on the transaction. This also includes sales tax (where applicable) |
style | object | { layout: "horizontal", color: "black" } |
Allows styling options for colors, shapes and layouts. Consult the latest reference for all possible styles |
loadingComponent | jsx or string | "Loading ..." or <span>Loading</span> |
Due to a delay in render, a loading component may be specified before the buttons load to inform the user. |
errorComponent | jsx or string | "Error" or <span>PayPal could not be loaded</span> |
This component will render when PayPal couldn't be loaded or an error occurred on their part. |
onSuccess | function | onSuccess(data) |
Event that fires when a customer completed the transaction without any errors. |
onShippingChange | function | onShippingChange(data, actions) |
Used for detecting changes in a customer shipping address. Fires when they update their account address or use a different one for the transaction. |
onCancel | function | onCancel() |
Callback event that fires when a user cancels the transaction before paying. |
onInit | function | onInit(data, actions) |
This fires off immediately after render. |
onClick | function | onClick() |
Fires off when you click any of the buttons. |
TODO
- Support for server-side rendering
- Test and specify peer-dependency requirements
Contributing
- Pull requests welcome!