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/sdk-client

v4.0.187

Published

Shared config between PayPal/Braintree.

Downloads

4,841

Readme

PayPal SDK Client

build status code coverage npm version apache license

A shared client for PayPal/Braintree client sdk modules. Has both client-side and server-side bindings to help you build and integrate an sdk component.

Client

Your client-side sdk component can take advantage of any common utilities or functionality exposed by this module. For example:

  • Determining the current paypal url
  • Getting the merchant's client-id
  • Running experiments.
import { getClientID } from "@paypal/sdk-client/src";

fetch("https://api.paypal.com/v1/foo", {
	headers: {
		"client-id": getClientID(),
	},
});

Server

This module helps you load the payments sdk in a child window, matching the url from the parent.

Rationale

  • You're building a component that is rendered into an iframe or popup window.
  • Your component uses zoid to construct the component, and to communicate between the component and the parent page and vice-versa.
  • You want to make sure the version of the component between the parent and child, to prevent any incompatibilities in the messaging protocol zoid defines (trust me on this one)
  • You also want to take advantage of browser caching benefits, and not download dependencies like zoid multiple times in each new frame or window being rendered
  • You can't just load <script src="https://www.paypal.com/sdk/js?client-id=CLIENT_ID"></script> in the child frame or popup, because different query params could have been passed on the top level page, and to take advantage of any caching benefits you need to match the url exactly as it appeared in the parent.
  • You also probably want to avoid passing down all of the query params from /sdk/js, but that's a lot to keep track of, and you would need to update your code every time a new parameter is added.
  • Or, you could pass the entire https://www.paypal.com/sdk/js?client-id=xyz&... url down from the parent to each new child; meaning we'll get an exact match each time, with exact the same version of the code, and any new url params will automatically be included without any more integration work.
  • And of course, if we're doing that, we need to validate the url before we throw it into a script tag, to prevent xss or injection attacks

This module helps with that.

On your server

  1. Import unpackSDKMeta:
import { unpackSDKMeta } from "@paypal/sdk-client";
  1. Call unpackSDKMeta with req.query.sdkMeta, passed from the client in the query string, and pass the script tag in the page render.
// Listen for requests to your app
app.get("/my-app", (req, res) => {
	// Unpack the sdk meta payload from the client
	const { getSDKLoader } = unpackSDKMeta(req.query.sdkMeta);

	// Call getSDKLoader to build a script tag, passing in csp nonce, if applicable
	const sdkScriptTag = getSDKLoader({ nonce });

	// Insert script tag into response
	res.send(`
        <body>
            <h1>My App</h1>
            ${sdkScriptTag}
        </body>
    `);
});
  1. Ensure the sdkMeta payload is passed to the child window from the parent. If you are using zoid to construct your component, please add the following:
import { getSdkMeta } from "@paypal/sdk-client/src";

let MyComponent = zoid.create({
	tag: "my-component",
	url: "https://www.paypal.com/my-component",
	props: {
		sdkMeta: {
			type: "string",
			value: getSdkMeta,
			queryParam: true,
		},
	},
});

If you are not using zoid, please use getSdkMeta() to construct the sdkMeta payload, and pass it to your child-window or frame in a different way.

Notes:

  • The script tag should not introduce any extra latency. It has already been downloaded, pre-cached, and pre-parsed in the parent window. So it is usually safe to put in the <head> tag.
  • Assuming a zoid component integration, window.xprops, containing the props used to receive data from the parent and used to send callbacks back to the parent window, will only be available after the script tag has completed loading on the client. If any other scripts require window.xprops immediately, please ensure the sdk script tag is placed before those scripts.
  • unpackSDKMeta will throw an error if req.query.sdkMeta does not validate. This should be handled and translated into a 500 server error, indicating that the payload has been tampered with.
  • unpackSDKMeta may be passed undefined if req.query.sdkMeta is not present. This is necessary for some legacy integrations where the sdk metadata is passed via window.name entirely on the client-side.

Quick Start

Installing

npm install --save @paypal/sdk-client

Getting Started

  • Fork the module
  • Run setup: npm run setup
  • Start editing code in ./src and writing tests in ./tests
  • npm run build

Building

npm run build

Tests

  • Edit tests in ./test/tests

  • Run the tests:

    npm test