Socket
Socket
Sign inDemoInstall

react-paypal-button-v2

Package Overview
Dependencies
20
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    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).


Version published
Weekly downloads
6.2K
decreased by-10.78%
Maintainers
1
Install size
372 kB
Created
Weekly downloads
 

Readme

Source

:large_blue_diamond: 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.

:information_source: 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. :books: Props

7. options Prop Fieldnames/Parameters

8. Alternative Usage Example

9. Alternative Production Example

10. Example Project

11. Contribute

12. License








:large_blue_diamond: 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


:large_blue_diamond: 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







:large_blue_diamond: 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.








:large_blue_diamond: 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

:large_blue_diamond: API

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


:large_blue_diamond: Props

PropsDescriptionTypeDefault
amountThe amount value of the transaction.string or number
currencyThe currency of the transaction.string"USD"
shippingPreferenceThe 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_ADDRESSstring"NO_SHIPPING"
onSuccessThe successful completion of the transaction. (details: object, data: object) => voidFunction
catchErrorTransaction declined or errored. (err: object) => voidFunction
optionsYou 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"}
onButtonReadyA function called when PayPal's API is loaded. () => voidFunction
onErrorIf 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) => voidFunction
createOrderA 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) => voidFunction
onApproveA 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) => voidFunction
createSubscriptionA 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) => voidFunction
stylePayPal 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{}
onShippingChangeA 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 }) => FunctionFunction
onCancelShow a cancellation page or return to the shopping cart. (data: object) => voidFunction

:information_source: Learn more about the integration proccess along with more props and advance use cases starting at PayPal's docs.








:small_blue_diamond: 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.

OptionDescriptionTypeDefault
clientIdYour PayPal REST client ID. While you're testing in sandbox, you can use client-id=sb as a shortcut.string"sb"
currencyThe currency of the transaction.string"USD"
merchantIdThe merchant for who you are facilitating a transaction.stringautomatic
intentThe intent of the payment.string"capture"
commitSet to true if the transaction is Pay Now, or false if the amount captured changes after the buyer returns to your site.boolean or stringtrue
vaultSet to true if the transaction sets up a billing agreement, or uses a vault.boolean or stringfalse
componentsA comma-separated list of components to enable. Defaults to allow Smart Payment Buttons. Other components are optional.stringbuttons
disableFundingFunding sources to disallow from showing in the Smart Payment Buttons.stringnone
disableCardCards to disable from showing in the Smart Payment Buttons.stringnone
integrationDateThe date of integration. Used to ensure backwards compatibility.stringautomatic
localeThe locale used to localize any components. PayPal recommends not setting this parameter, as the buyer's locale is automatically set by PayPal.stringautomatic
buyerCountryThe buyer country. For testing purposes only.stringautomatic
debugEnable debug mode for ease of debugging. Do not enable for production traffic.boolean or stringfalse

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




LueHsoft LueH LABS Lue Hang luehang


:large_blue_diamond: 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
OptionDescriptionTypeDefault
client-idYour PayPal REST client ID. While you're testing in sandbox, you can use client-id=sb as a shortcut.string"sb"
currencyThe 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
              })
            });
          });
        }}
      />
    );
  }
}







:large_blue_diamond: 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
OptionDescriptionTypeDefault
client-idYour PayPal REST client ID.string"sb"
currencyThe 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


:large_blue_diamond: Example Project

Perform steps 1-2 to run locally:

  1. Clone the Repo
  2. Install and Run

:small_blue_diamond: 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

:small_blue_diamond: 2. Install and Run

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



LueHsoft LueH LABS Lue Hang luehang


:large_blue_diamond: Contribute

Pull requests are welcomed.


:small_blue_diamond: Beginners

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








:large_blue_diamond: License

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

Keywords

FAQs

Last updated on 10 Feb 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc