📅  最后修改于: 2023-12-03 15:34:38.968000             🧑  作者: Mango
React PayPal Express Checkout is a library that allows developers to easily integrate PayPal Express Checkout into their React applications. This library is written in JavaScript and is designed to be easy to use and customize.
To use the React PayPal Express Checkout library, you must first install it as a dependency in your React application.
npm install react-paypal-express-checkout
Once you have installed the library, you can use it in your React application by importing it and using it in your code.
import React, { Component } from 'react';
import PayPalExpressBtn from 'react-paypal-express-checkout';
class App extends Component {
render() {
const onSuccess = (payment) => {
// Congratulation, it came here means everything's fine!
console.log("The payment was succeeded!", payment);
// You can bind the "payment" object's value to your state or props or whatever here, please see below for sample returned data
}
const onCancel = (data) => {
// User pressed "cancel" or close Paypal's popup!
console.log('The payment was cancelled!', data);
// You can bind the "data" object's value to your state or props or whatever here, please see below for sample returned data
}
const onError = (err) => {
// The main Paypal's script cannot be loaded or somethings block the loading of that script!
console.log("Error!", err);
// Because the Paypal's main script is loaded asynchronously from "https://www.paypalobjects.com/api/checkout.js"
// => sometimes it may take about 0.5 second for everything to get set, or for the button to appear
}
let env = 'sandbox'; // you can set here to 'production' for production
let currency = 'USD'; // or you can set this value from your props or state
const client = {
sandbox: 'YOUR-SANDBOX-APP-ID',
production: 'YOUR-PRODUCTION-APP-ID',
}
return (
<div className="App">
<PayPalExpressBtn env={env} client={client} currency={currency} total={1.00}
onError={onError} onSuccess={onSuccess} onCancel={onCancel} />
</div>
);
}
}
export default App;
In this code, we define three callback functions for when the user successfully completes a payment, cancels the payment, or there is an error with the payment. We also define the PayPal environment ('sandbox' or 'production'), the client ID for each environment, and the currency and total amount for the sale.
The PayPalExpressBtn
component then handles the PayPal Express Checkout button and passes the appropriate data to the onSuccess
, onCancel
, and onError
functions based on the user's actions.
Using the React PayPal Express Checkout library makes it easy for developers to integrate PayPal into their React applications and provide a seamless user experience during the payment process.
In conclusion, the React PayPal Express Checkout library is a powerful tool for developers looking to integrate PayPal into their React applications. With its easy-to-use API and customizable settings, this library helps developers provide a seamless user experience during the payment process.