paypal/index.js

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

/**
 * Options for {@link module:braintree-web/paypal.create|create}
 * @typedef {object} createOptions
 * @property {Client} client A {@link Client} instance.
 * @property {string} options.flow Set to 'checkout' for one-time payment flow, or 'vault' for Vault flow.
 * @property {string|number} [options.amount] The amount of the transaction. Required when using the Checkout flow.
 * @property {string} [options.currency] The currency code of the amount, such as 'USD'. Required when using the Checkout flow.
 * @property {string} [options.displayName] The merchant name displayed inside of the PayPal lightbox; defaults to the company name on your Braintree account
 * @property {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.
 * @property {boolean} [options.enableShippingAddress=false] Returns a shipping address object in {@link PayPal#tokenize}.
 * @property {object=} options.shippingAddressOverride Allows you to pass a shipping address you have already collected into the PayPal payment flow.
 * @property {string} options.shippingAddressOverride.line1 Street address.
 * @property {string=} options.shippingAddressOverride.line2 Street address (extended).
 * @property {string} options.shippingAddressOverride.city City.
 * @property {string} options.shippingAddressOverride.state State.
 * @property {string} options.shippingAddressOverride.postalCode Postal code.
 * @property {string} options.shippingAddressOverride.countryCode Country.
 * @property {string=} options.shippingAddressOverride.phone Phone number.
 * @property {string=} options.shippingAddressOverride.recipientName Recipient's name.
 * @property {boolean} [options.shippingAddressEditable=true] Set to false to disable user editing of the shipping address.
 * @property {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.
 */

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

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

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

  config = options.client.getConfiguration();

  if (config.analyticsMetadata.sdkVersion !== VERSION) {
    callback(new BraintreeError({
      type: BraintreeError.types.MERCHANT,
      message: 'Client and PayPal 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 {module:braintree-web/paypal~createOptions} options Object containing configuration options for this module.
   * @param {errback} 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
};