• Stars
    star
    273
  • Rank 150,780 (Top 3 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 6 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An easy and simple to use React button component to implement PayPal Checkout with Smart Payment Buttons V2 (Version 2).

🔷 react-paypal-button-v2

An easy and simple to use React button component to implement PayPal's Checkout with Smart Payment Buttons V2 (Version 2).

Check out the docs for a complete documentation.

ℹī¸ This PayPal Checkout integration uses the PayPal JavaScript SDK. Starting at the beginning of February 2019, all new users must use PayPal's version 2 integration as version 1 is now depreciated.




LueHsoft LueH LABS Lue Hang luehang


Index

1. Prerequisites

2. Install

3. Usage Example

4. Production Example

5. API

6. 📚 Props

7. options Prop Fieldnames/Parameters

8. Alternative Usage Example

9. Alternative Production Example

10. Example Project

11. Contribute

12. License








🔷 Prerequisites

To use PayPal's Smart Payment Buttons in prodution, you must have a PayPal Business account set up and verified. After this is done, you'll have access to your API credentials to use with this button.

Once you have your account set up, you will have 2 different sets of credentials for sandbox mode and production mode. You will also be able to create sandbox business and customer accounts to be tested on.




LueHsoft LueH LABS Lue Hang luehang


🔷 Install

Type in the following to the command line to install the dependency.

$ npm install react-paypal-button-v2 --save

or

$ yarn add react-paypal-button-v2







🔷 Usage Example

Add an import to the top of the file. At minimal, declare the PayPalButton component in the render() method providing a string for the amount prop and a function to the onSuccess prop.

If you like react-paypal-button-v2, please be sure to give it a star at GitHub. Thanks.

import { PayPalButton } from "react-paypal-button-v2";

export default class Example Component {
  render() {
    return (
      <PayPalButton
        amount="0.01"
        // shippingPreference="NO_SHIPPING" // default is "GET_FROM_FILE"
        onSuccess={(details, data) => {
          alert("Transaction completed by " + details.payer.name.given_name);

          // OPTIONAL: Call your server to save the transaction
          return fetch("/paypal-transaction-complete", {
            method: "post",
            body: JSON.stringify({
              orderID: data.orderID
            })
          });
        }}
      />
    );
  }
}

For alternative usage, go to the Alternative Usage Example Section.

To create subscriptions, go to the Subscriptions Example Section.








🔷 Production Example

At minimal, declare the options prop and include your business production Client ID in the clientId fieldname value.

If you like react-paypal-button-v2, please be sure to give it a star at GitHub. Thanks.

import { PayPalButton } from "react-paypal-button-v2";

export default class Example Component {
  render() {
    return (
      <PayPalButton
        amount="0.01"
        // shippingPreference="NO_SHIPPING" // default is "GET_FROM_FILE"
        onSuccess={(details, data) => {
          alert("Transaction completed by " + details.payer.name.given_name);

          // OPTIONAL: Call your server to save the transaction
          return fetch("/paypal-transaction-complete", {
            method: "post",
            body: JSON.stringify({
              orderId: data.orderID
            })
          });
        }}
        options={{
          clientId: "PRODUCTION_CLIENT_ID"
        }}
      />
    );
  }
}

For alternative usage, go to the Alternative Production Example Section.

To create subscriptions, go to the Subscriptions Example Section.



LueHsoft LueH LABS Lue Hang luehang

🔷 API

<PayPalButton /> component accepts the following props...


🔷 Props

Props Description Type Default
amount The amount value of the transaction. string or number
currency The currency of the transaction. string "USD"
shippingPreference The shipping preference: Displays the shipping address to the customer. Enables the customer to choose an address on the PayPal site. Restricts the customer from changing the address during the payment-approval process. Options: NO_SHIPPING, GET_FROM_FILE, SET_PROVIDED_ADDRESS string "NO_SHIPPING"
onSuccess The successful completion of the transaction. (details: object, data: object) => void Function
catchError Transaction declined or errored. (err: object) => void Function
options You can customize the integration by passing different query parameters/fieldnames into the options prop object which will pass it to https://paypal.com/sdk/js. These parameters help PayPal decide the optimal funding sources and buttons to show to your buyers. Before configuring the options prop, make sure you haven't manually added the script tag for https://paypal.com/sdk/js. For a list of parameters/fieldnames, go to section options Prop Fieldnames/Parameters. object {clientId: "sb", currency: "USD"}
onButtonReady A function called when PayPal's API is loaded. () => void Function
onError If an error prevents buyer checkout. This error handler is a catch-all. Errors at this point are not expected to be handled beyond showing a generic error message or page. (err: object) => void Function
createOrder A function called when the buyer clicks the PayPal button. Calls PayPal using the actions.order.create() to set up the details of the transaction. (data: object, actions: object) => void Function
onApprove A function called when the buyer approves the transaction on paypal.com. Calls PayPal using the actions.order.capture() to capture the funds from the transaction. Optionally calls PayPal using actions.order.get() to get the transaction details. (data: object, actions: object) => void Function
createSubscription A function that calls the PayPal subscription using the actions.subscription.create() to set up the details of the transaction. Subscriptions Example Section. (data: object, actions: object) => void Function
createBillingAgreement A function that calls the PayPal billing agreement using the paypal.billingAgreement.create() to set up the details of the transaction. (data: object, actions: object) => void Function
style PayPal Checkout offers several style options that you can use to customize the look and feel of your Smart Payment Button. You can also display multiple funding sources to the buyer, when appropriate. See more on what to input in the style object at Customize the PayPal Buttons page. object {}
onShippingChange A function called when the buyer initially logs into their account, submits their billing/payment information, or makes a change to their shipping address on the review your payment page. (data: { paymentToken: string, shipping_address: object, selected_shipping_method: string }, actions: { resolve: Function, reject: Function, order: Function }) => Function Function
onClick A function called on PayPal button click. Can be used for validation. (data: object, actions: object) => void Function
onCancel Show a cancellation page or return to the shopping cart. (data: object) => void Function

ℹī¸ Learn more about the integration proccess along with more props and advance use cases starting at PayPal's docs.








🔹 options Prop Fieldnames or Parameters

You can customize the integration by passing different query parameters/fieldnames into the options prop object which will pass it to https://paypal.com/sdk/js. These parameters help PayPal decide the optimal funding sources and buttons to show to your buyers. Before configuring the options prop, make sure you haven't manually added the script tag for https://paypal.com/sdk/js.

Option Description Type Default
clientId Your PayPal REST client ID. While you're testing in sandbox, you can use client-id=sb as a shortcut. string "sb"
currency The currency of the transaction. string "USD"
merchantId The merchant for who you are facilitating a transaction. string automatic
intent The intent of the payment. string "capture"
commit Set to true if the transaction is Pay Now, or false if the amount captured changes after the buyer returns to your site. boolean or string true
vault Set to true if the transaction sets up a billing agreement, or uses a vault. boolean or string false
components A comma-separated list of components to enable. Defaults to allow Smart Payment Buttons. Other components are optional. string buttons
disableFunding Funding sources to disallow from showing in the Smart Payment Buttons. string none
disableCard Cards to disable from showing in the Smart Payment Buttons. string none
integrationDate The date of integration. Used to ensure backwards compatibility. string automatic
locale The locale used to localize any components. PayPal recommends not setting this parameter, as the buyer's locale is automatically set by PayPal. string automatic
buyerCountry The buyer country. For testing purposes only. string automatic
debug Enable debug mode for ease of debugging. Do not enable for production traffic. boolean or string false

ℹī¸ To see a detail and complete list of available parameters and values, go to PayPal's Customization page.




LueHsoft LueH LABS Lue Hang luehang


🔷 Alternative Usage Example


Usage Example 1

import { PayPalButton } from "react-paypal-button-v2";

export default class Example Component {
  render() {
    return (
      <PayPalButton
        createOrder={(data, actions) => {
          return actions.order.create({
            purchase_units: [{
              amount: {
                currency_code: "USD",
                value: "0.01"
              }
            }],
            // application_context: {
            //   shipping_preference: "NO_SHIPPING" // default is "GET_FROM_FILE"
            // }
          });
        }}
        onApprove={(data, actions) => {
          // Capture the funds from the transaction
          return actions.order.capture().then(function(details) {
            // Show a success message to your buyer
            alert("Transaction completed by " + details.payer.name.given_name);

            // OPTIONAL: Call your server to save the transaction
            return fetch("/paypal-transaction-complete", {
              method: "post",
              body: JSON.stringify({
                orderID: data.orderID
              })
            });
          });
        }}
      />
    );
  }
}

Usage Example 2

Add the PayPal script to your web page, then add your sandbox or production client-id to the script tag. While you're testing in sandbox, you can use client-id=sb as a shortcut.

<script src="https://www.paypal.com/sdk/js?client-id=sb" />

Query Parameters

Option Description Type Default
client-id Your PayPal REST client ID. While you're testing in sandbox, you can use client-id=sb as a shortcut. string "sb"
currency The currency of the transaction. string "USD"

To see a detail and complete list of available parameters and values, go to PayPal's Customization page.


import { PayPalButton } from "react-paypal-button-v2";

export default class Example Component {
  render() {
    return (
      <PayPalButton
        amount="0.01"
        // shippingPreference="NO_SHIPPING" // default is "GET_FROM_FILE"
        onSuccess={(details, data) => {
          alert("Transaction completed by " + details.payer.name.given_name);

          // OPTIONAL: Call your server to save the transaction
          return fetch("/paypal-transaction-complete", {
            method: "post",
            body: JSON.stringify({
              orderID: data.orderID
            })
          });
        }}
      />
    );
  }
}

Subscription Example Usage

To create subscriptions you must first create a product and create a plan using the PayPal REST API.


import { PayPalButton } from "react-paypal-button-v2";

export default class Example Component {
  render() {
    return (
      <PayPalButton
        options={{vault: true}}
        createSubscription={(data, actions) => {
          return actions.subscription.create({
            plan_id: 'P-XXXXXXXXXXXXXXXXXXXXXXXX'
          });
        }}
        onApprove={(data, actions) => {
          // Capture the funds from the transaction
          return actions.subscription.get().then(function(details) {
            // Show a success message to your buyer
            alert("Subscription completed");

            // OPTIONAL: Call your server to save the subscription
            return fetch("/paypal-subscription-complete", {
              method: "post",
              body: JSON.stringify({
                orderID: data.orderID,
                subscriptionID: data.subscriptionID
              })
            });
          });
        }}
      />
    );
  }
}







🔷 Alternative Production Example

At minimal, add the PayPal script to your web page, then add your production client-id to the script tag.

<script src="https://www.paypal.com/sdk/js?client-id=PRODUCTION_CLIENT_ID" />

Query Parameters

Option Description Type Default
client-id Your PayPal REST client ID. string "sb"
currency The currency of the transaction. string "USD"

To see a detail and complete list of available parameters and values, go to PayPal's Customization page.


import { PayPalButton } from "react-paypal-button-v2";

export default class Example Component {
  render() {
    return (
      <PayPalButton
        amount="0.01"
        // shippingPreference="NO_SHIPPING" // default is "GET_FROM_FILE"
        onSuccess={(details, data) => {
          alert("Transaction completed by " + details.payer.name.given_name);

          // OPTIONAL: Call your server to save the transaction
          return fetch("/paypal-transaction-complete", {
            method: "post",
            body: JSON.stringify({
              orderID: data.orderID
            })
          });
        }}
      />
    );
  }
}



LueHsoft LueH LABS Lue Hang luehang


🔷 Example Project

Perform steps 1-2 to run locally:

  1. Clone the Repo
  2. Install and Run

🔹 1. Clone the Repo

Clone react-paypal-button-v2 locally. In a terminal, run:

$ git clone https://github.com/Luehang/react-paypal-button-v2.git react-paypal-button-v2

🔹 2. Install and Run

$ cd react-paypal-button-v2/
$ npm install
$ npm run start



LueHsoft LueH LABS Lue Hang luehang


🔷 Contribute

Pull requests are welcomed.


🔹 Beginners

Not sure where to start, or a beginner? Take a look at the issues page.








🔷 License

Apache 2.0 Š Lue Hang, as found in the LICENSE file.

More Repositories

1

react-native-masonry-list

An easy and simple to use React Native component to render a custom high performant masonry layout for images. It uses a smart algorithm to sort the images evenly as possible according to the index position or fill in as soon as the image is fetched. Includes support for both iOS and Android. Free and made possible along with costly maintenance and updates by [Lue Hang](https://www.facebook.com/lue.hang) (the author).
JavaScript
243
star
2

react-native-image-layout

An easy and simple to use React Native component to render a custom masonry layout for remote images and displayed on a custom interactive image viewer. Includes animations and support for both iOS and Android. Free and made possible along with costly maintenance and updates by [Lue Hang](https://www.facebook.com/lue.hang) (the author).
JavaScript
48
star
3

react-native-camera-roll-gallery

An easy and simple to use React Native component to render a custom layout for CameraRoll photos (next update will have videos) and displayed on a custom interactive image viewer for manipulation. Includes animations and support for both iOS and Android. Free and made possible along with costly maintenance and updates by [Lue Hang](https://www.facebook.com/lue.hang) (the author).
JavaScript
40
star
4

react-native-smart-gallery

An easy and simple to use React Native component to render a large to very large list of swipable high performant pages with intelligent guestures responder to cushion rough guestures and detect gestures like pan, pinch and double tap. Supporting both iOS and Android. Free and made possible along with costly maintenance and updates by [Lue Hang](https://www.facebook.com/lue.hang) (the author).
JavaScript
34
star
5

react-native-swipe-render

An easy and simple to use React Native component that renders swipable performant pages for large lists or content. Supporting both iOS and Android.
JavaScript
21
star
6

react-native-easy-view-transformer

An easy and simple to use React Native component to transform and translate a view with gestures like pan, pinch and double tap. Supporting both iOS and Android. Free and made possible along with costly maintenance and updates by [Lue Hang](https://www.facebook.com/lue.hang) (the author).
JavaScript
18
star
7

react-native-image-transformer

An easy and simple to use React Native component to transform and translate an image with gestures like pan, pinch and double tap. Supporting both iOS and Android. Free and made possible along with costly maintenance and updates by [Lue Hang](https://www.facebook.com/lue.hang) (the author).
JavaScript
12
star
8

react-native-camera-roll-selector

An easy and simple to use React Native component providing images selection from camera roll. The images in the camera roll are displayed with a custom high performant masonry layout. Supporting both iOS and Android. Free and made possible along with costly maintenance and updates by [Lue Hang](https://www.facebook.com/lue.hang) (the author).
JavaScript
9
star
9

react-native-page-list

An easy and simple to use React Native component that renders swipable performant pages with intelligent scroll/swipe detection to cushion rough guestures. Supporting both iOS and Android. Free and made possible along with costly maintenance and updates by [Lue Hang](https://www.facebook.com/lue.hang) (the author).
JavaScript
6
star
10

react-native-scrolling

Easy and simple to use React Native declarative scrolling for config and animation. Supporting both iOS and Android. Free and made possible along with costly maintenance and updates by [Lue Hang](https://www.facebook.com/lue.hang) (the author).
JavaScript
4
star
11

react-native-easy-guesture-responder

A powerful and easy to use function for React Native guesture responders. Supporting both iOS and Android. Free and made possible along with costly maintenance and updates by [Lue Hang](https://www.facebook.com/lue.hang) (the author).
JavaScript
2
star
12

react-native-smart-page

An easy and simple to use React Native component to render large to very large lists of swipable performant pages with intelligent scroll/swipe detection to cushion rough guestures. Supporting both iOS and Android. Free and made possible along with costly maintenance and updates by [Lue Hang](https://www.facebook.com/lue.hang) (the author).
JavaScript
2
star
13

cleaning-service

JavaScript
1
star
14

node-delete-partial

An easy and simple to use Node module to efficiently remove the first X number of lines from an inputstream. It uses streams, which means it doesn't need to load whole files in memory, so it is way more efficient and fast, as well can work on very large files without filling memory on the hardware. Free and made possible along with costly maintenance and updates by [Lue Hang](https://www.facebook.com/lue.hang) (the author).
JavaScript
1
star