paypal/index.js

'use strict';
/** @module braintree-web/paypal */

/**
 * Options for {@link module:braintree-web/paypal.create|create}
 * @typedef {object} createOptions
 */

var PayPal = require('./external/paypal');
var browserDetection = require('../lib/browser-detection');
var BraintreeError = require('../lib/error');
var analytics = require('../lib/analytics');
var deferred = require('../lib/deferred');
var VERSION = require('package.version');

function create(options, callback) {
  var config, pp, clientVersion;

  callback = deferred(callback);

  if (options.client == null) {
    callback(new BraintreeError({
      type: BraintreeError.types.MERCHANT,
      message: 'options.client is required when instantiating PayPal.'
    }));
    return;
  }

  config = options.client.getConfiguration();
  clientVersion = config.analyticsMetadata.sdkVersion;

  if (clientVersion !== VERSION) {
    callback(new BraintreeError({
      type: BraintreeError.types.MERCHANT,
      message: 'Client (version ' + clientVersion + ') and PayPal (version ' + VERSION + ') components must be from the same SDK version.'
    }));
    return;
  }

  if (config.gatewayConfiguration.paypalEnabled !== true) {
    callback(new BraintreeError({
      type: BraintreeError.types.MERCHANT,
      message: 'PayPal is not enabled for this merchant.'
    }));
    return;
  }

  if (!_isBrowserSupported()) {
    callback(new BraintreeError({
      type: BraintreeError.types.CUSTOMER,
      message: 'Browser is not supported.'
    }));
    return;
  }

  if (!browserDetection.isHTTPS()) {
    callback(new BraintreeError({
      type: BraintreeError.types.MERCHANT,
      message: 'PayPal requires HTTPS.'
    }));
    return;
  }

  analytics.sendEvent(options.client, 'web.paypal.initialized');

  pp = new PayPal(options);
  pp._initialize(function () {
    callback(null, pp);
  });
}

function _isBrowserSupported() {
  return !browserDetection.isOperaMini();
}

module.exports = {
  /**
   * @static
   * @function
   * @param {object} options All creation options for the PayPal component.
   * @param {Client} options.client A {@link Client} instance.
   * @param {string} options.flow Set to 'checkout' for one-time payment flow, or 'vault' for Vault flow.
   * @param {string|number} [options.amount] The amount of the transaction. Required when using the Checkout flow.
   * @param {string} [options.currency] The currency code of the amount, such as 'USD'. Required when using the Checkout flow.
   * @param {string} [options.displayName] The merchant name displayed inside of the PayPal lightbox; defaults to the company name on your Braintree account
   * @param {string} [options.locale=en_us] Use this option to change the language, links, and terminology used in the PayPal flow to suit the country and language of your customer.
   * @param {boolean} [options.enableShippingAddress=false] Returns a shipping address object in {@link PayPal#tokenize}.
   * @param {object=} options.shippingAddressOverride Allows you to pass a shipping address you have already collected into the PayPal payment flow.
   * @param {string} options.shippingAddressOverride.line1 Street address.
   * @param {string=} options.shippingAddressOverride.line2 Street address (extended).
   * @param {string} options.shippingAddressOverride.city City.
   * @param {string} options.shippingAddressOverride.state State.
   * @param {string} options.shippingAddressOverride.postalCode Postal code.
   * @param {string} options.shippingAddressOverride.countryCode Country.
   * @param {string=} options.shippingAddressOverride.phone Phone number.
   * @param {string=} options.shippingAddressOverride.recipientName Recipient's name.
   * @param {boolean} [options.shippingAddressEditable=true] Set to false to disable user editing of the shipping address.
   * @param {boolean} [options.billingAgreementsDescription] Use this option to set the description of the preapproved payment agreement visible to customers in their PayPal profile. Max 255 characters.
   * @param {callback} callback The second argument, <code>data</code>, is the {@link PayPal} instance.
   * @returns {void}
   */
  create: create,
  /**
   * @description The current version of the SDK, i.e. `{@pkg version}`.
   * @type {string}
   */
  VERSION: VERSION
};