What is @paypal/react-paypal-js?
@paypal/react-paypal-js is a React library that provides a set of components and hooks to easily integrate PayPal's payment services into a React application. It simplifies the process of adding PayPal buttons, handling transactions, and managing the PayPal SDK.
What are @paypal/react-paypal-js's main functionalities?
PayPal Buttons
This feature allows you to add PayPal buttons to your React application. The PayPalButtons component renders the PayPal buttons and handles the payment process.
import { PayPalScriptProvider, PayPalButtons } from '@paypal/react-paypal-js';
function App() {
return (
<PayPalScriptProvider options={{ "client-id": "your-client-id" }}>
<PayPalButtons style={{ layout: 'vertical' }} />
</PayPalScriptProvider>
);
}
Custom Button Styles
This feature allows you to customize the appearance of the PayPal buttons. You can change the layout, color, shape, and label of the buttons.
import { PayPalScriptProvider, PayPalButtons } from '@paypal/react-paypal-js';
function App() {
return (
<PayPalScriptProvider options={{ "client-id": "your-client-id" }}>
<PayPalButtons style={{ layout: 'horizontal', color: 'blue', shape: 'pill', label: 'pay' }} />
</PayPalScriptProvider>
);
}
Handling Payment Events
This feature allows you to handle payment events such as order creation and approval. You can define custom functions to create orders and handle successful transactions.
import { PayPalScriptProvider, PayPalButtons } from '@paypal/react-paypal-js';
function App() {
const createOrder = (data, actions) => {
return actions.order.create({
purchase_units: [{
amount: {
value: '0.01',
},
}],
});
};
const onApprove = (data, actions) => {
return actions.order.capture().then((details) => {
alert('Transaction completed by ' + details.payer.name.given_name);
});
};
return (
<PayPalScriptProvider options={{ "client-id": "your-client-id" }}>
<PayPalButtons createOrder={createOrder} onApprove={onApprove} />
</PayPalScriptProvider>
);
}
Other packages similar to @paypal/react-paypal-js
react-stripe-js
react-stripe-js is a React library for integrating Stripe's payment services into a React application. It provides components and hooks for handling payment forms, managing the Stripe SDK, and processing transactions. Compared to @paypal/react-paypal-js, react-stripe-js focuses on Stripe's payment services and offers similar functionalities for handling payments and customizing payment forms.
react-paypal-express-checkout
react-paypal-express-checkout is another React library for integrating PayPal's payment services. It provides a simpler interface for adding PayPal buttons and handling transactions. Compared to @paypal/react-paypal-js, react-paypal-express-checkout offers a more straightforward approach but may lack some of the advanced customization options available in @paypal/react-paypal-js.
react-payment-request-api
react-payment-request-api is a React library that leverages the Payment Request API to handle payments. It supports multiple payment methods, including credit cards and digital wallets like Google Pay. Compared to @paypal/react-paypal-js, react-payment-request-api offers a more generic approach to handling payments and can be used with various payment providers.
react-paypal-js
React components for the PayPal JS SDK
Why use react-paypal-js?
The Problem
Developers integrating with PayPal are expected to add the JS SDK <script>
to a website and then render components like the PayPal Buttons after the script loads. This architecture works great for simple websites but can be challenging when building single page apps.
React developers think in terms of components and not about loading external scripts from an index.html file. It's easy to end up with a React PayPal integration that's sub-optimal and hurts the buyer's user experience. For example, abstracting away all the implementation details of the PayPal Buttons into a single React component is an anti-pattern because it tightly couples script loading with rendering. It's also problematic when you need to render multiple different PayPal components that share the same global script parameters.
The Solution
react-paypal-js
provides a solution to developers to abstract away complexities around loading the JS SDK. It enforces best practices by default so buyers get the best possible user experience.
Features
- Enforce async loading the JS SDK up front so when it's time to render the buttons to your buyer, they render immediately.
- Abstract away the complexity around loading the JS SDK with the global
<PayPalScriptProvider>
component. - Support dispatching actions to reload the JS SDK and re-render components when global parameters like
currency
change. - Easy to use components for all the different PayPal product offerings:
<PayPalButtons />
<PayPalMarks />
<PayPalMessages />
Installation
To get started, install react-paypal-js with npm.
npm install @paypal/react-paypal-js
Usage
This PayPal React library consists of two main parts:
- Context Provider - this
<PayPalScriptProvider />
component manages loading the JS SDK script. Add it to the root of your React app. It uses the Context API for managing state and communicating to child components. It also supports reloading the script when parameters change. - SDK Components - components like
<PayPalButtons />
are used to render the UI for PayPal products served by the JS SDK.
import { PayPalScriptProvider, PayPalButtons } from "@paypal/react-paypal-js";
export default function App() {
return (
<PayPalScriptProvider options={{ "client-id": "sb" }}>
<PayPalButtons style={{ layout: "horizontal" }} />
</PayPalScriptProvider>
);
}
Browser Support
This library supports all popular browsers, including IE 11. It provides the same browser support as the JS SDK. Here's the full list of supported browsers.