npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

paypal-spb

v1.1.5

Published

PayPal Smart Payment Button React component

Downloads

22

Readme

paypal-spb

React component that renders the latest version of PayPal's Smart Payment Button!

See https://developer.paypal.com/docs/checkout/ for more details

Prerequisites

-PayPal REST App credentials - See https://developer.paypal.com/docs/api/overview/#get-credentials

Install

npm i paypal-spb

Usage

  1. Copy and paste the sample object below into your project. This will be used to initialize the PayPal SDK, configure the button, pass payment details, etc. This object will be passed into your component as props.
let paypalConfigData = {
  client_id: "YOUR PAYPAL CLIENT_ID",
  div: "EMPTY DIV TO DISPLAY BUTTON",
  errorDiv: "EMPTY DIV TO DISPLAY ERROR",
  redirect_urls: {
    shipping: "URL FOR SHIPPING/BILLING PAGE (payment not complete)",
    confirm: "URL FOR PAYMENT CONFIRMATION PAGE (payment is complete)"
  },
  sdkCustomize: {
    commit: false,
    disable_funding: "card,bancontact", // card, credit, bancontact
    locale: "en_US"
  },
  style: {
    layout: "vertical", // vertical || horizontal
    color: "gold", // gold || blue || silver || white || black
    shape: "rect", // rect || pill
    label: "checkout", // paypal || checkout || buynow || pay || installment (only in MX or BR)
    tagline: false // must be false for vertical layout
  },
  paymentData: {
    purchase_units: [
      {
        amount: {
          value: "0.01"
        }
      }
    ]
  }
};

You will also need to create two empty divs; One is used to display the PayPal button, the other is used to display any error messages (outputted as a JSON string)

There are two scenarios in which this button can be used:

Scenario 1. From the product details page or cart page

When paypalConfigData.sdkCustomize.commit: false

If this button is placed on the product details or cart page, the payment will not be immediately completed. The user will log into PayPal and select their funding source. NO PAYMENT IS COMPLETED AT THIS TIME. An order id will be generated during this process and after the user leaves the PayPal flow, the orderId will be added to the URL as a query parameter. You can then call the Show order details api to prepopulate the shipping/billing page with the buyers name, email address and shipping/billing address.

https://developer.paypal.com/docs/api/orders/v2/#orders_get

You will then need to capture the payment on this page using the Capture payment for order api.

https://developer.paypal.com/docs/api/orders/v2/#orders_capture

To invoke this flow, set:

paypalConfigData.sdkCustomize.commit: false

The buyer will be redirected to the url set in paypalConfigData.redirectUrls.shipping

Scenario 2. From the shipping/billing page

When paypalConfigData.sdkCustomize.commit: true

If the button is placed on the shipping/billing page, the payment will be completed and you will be redirected to your confirmation page, with the orderId added to the URL as a query parameter.

To invoke this flow, set:

paypalConfigData.sdkCustomize.commit: true

The buyer will be redirected to the url set in paypalConfigData.redirectUrls.confirm

You can then call the Show order details api to retrieve information about the payment.

Sample

I recommend using React.lazy() when adding the component - https://reactjs.org/docs/code-splitting.html#reactlazy

//import PayPalButton from "paypal-spb";
const PayPalButton = React.lazy(() => import("paypal-spb"));
let paypalConfigData = {
    client_id:
      "your_client_id", //obtained at https://developer.paypal.com
    div: "paypal-button-container", // empty div for PayPal button
    errorDiv: "paypal-error-container", // empty div for PayPal error message
    redirect_urls: {
      confirm: "http://localhost:3000/confirmation", // for sdkCustomize.commit = false
      thankyou: "http://localhost:3000/thankyou" // for sdkCustomize.commit = true
    },
    sdkCustomize: {
      commit: false, //if true, payment is completed.
      // display pay now button withinin the PayPal experience
      // and redirect to thank you page.
      //if false, payment is not completed.
      // display a continue button within the PayPal experience
      // and redirect to confirmation page. payment will need to be completed on this page.
      disable_funding: "card,bancontact", // card, credit, bancontact
      locale: "en_US"
    },
    style: {
      layout: "vertical", // vertical || horizontal
      color: "black", // gold || blue || silver || white || black
      shape: "rect", // rect || pill
      label: "checkout", // paypal || checkout || buynow || pay || installment (only in MX or BR)
      tagline: false // must be false for vertical layout
    },
    paymentData: {
      // see link to documentation below for available parameters
      // https://developer.paypal.com/docs/api/orders/v2/#definition-purchase_unit_request
      purchase_units: [
        {
          amount: {
            value: "699.00",
            currency_code: "USD",
            breakdown: {
              item_total: {
                currency_code: "USD",
                value: "699.00"
              }
            }
          },
          description: "Phone",
          items: [
            {
              name: "Phone",
              unit_amount: {
                currency_code: "USD",
                value: "699.00"
              },
              quantity: "1"
            }
          ]
        }
      ]
    }
  };

...

<div id="paypal-button-container">
    <Suspense fallback={<p>Pay with PayPal...</p>}>
        <PayPalButton data={paypalConfigData} />
    </Suspense>
</div>
<div id="paypal-error-container"></div>

Built With

This package was built using React Hooks!

License

This project is licensed under the MIT License - see the LICENSE.md file for details