'use strict';
var analytics = require('../lib/analytics');
var BraintreeError = require('../lib/error');
var browserDetection = require('../lib/browser-detection');
var deferred = require('../lib/deferred');
var errors = require('./shared/errors');
var throwIfNoCallback = require('../lib/throw-if-no-callback');
var PayPal = require('./external/paypal');
var sharedErrors = require('../errors');
var VERSION = require('package.version');
function create(options, callback) {
var config, pp, clientVersion;
throwIfNoCallback(callback, 'create');
callback = deferred(callback);
if (options.client == null) {
callback(new BraintreeError({
type: sharedErrors.INSTANTIATION_OPTION_REQUIRED.type,
code: sharedErrors.INSTANTIATION_OPTION_REQUIRED.code,
message: 'options.client is required when instantiating PayPal.'
}));
return;
}
config = options.client.getConfiguration();
clientVersion = config.analyticsMetadata.sdkVersion;
if (clientVersion !== VERSION) {
callback(new BraintreeError({
type: sharedErrors.INCOMPATIBLE_VERSIONS.type,
code: sharedErrors.INCOMPATIBLE_VERSIONS.code,
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(errors.PAYPAL_NOT_ENABLED));
return;
}
if (!browserDetection.supportsPopups()) {
callback(new BraintreeError(errors.PAYPAL_BROWSER_NOT_SUPPORTED));
return;
}
analytics.sendEvent(options.client, 'paypal.initialized');
pp = new PayPal(options);
pp._initialize(function () {
callback(null, pp);
});
}
module.exports = {
create: create,
VERSION: VERSION
};