Home Reference Source

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:

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

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:

Quick Start

Installing

npm install --save @paypal/sdk-client

Getting Started

Building

npm run build

Tests