//= require solidus_paypal_braintree/constants /** * Constructor for PayPal button object * @constructor * @param {object} element - The DOM element of your PayPal button */ SolidusPaypalBraintree.PaypalButton = function(element, paypalOptions, options) { this._element = element; this._paypalOptions = paypalOptions || {}; this._options = options || {}; this._client = null; this._environment = this._paypalOptions.environment || 'sandbox'; delete this._paypalOptions.environment; if(!this._element) { throw new Error("Element for the paypal button must be present on the page"); } }; /** * Creates the PayPal session using the provided options and enables the button * * @param {object} options - The options passed to tokenize when constructing * the PayPal instance * * See {@link https://braintree.github.io/braintree-web/3.9.0/PayPal.html#tokenize} */ SolidusPaypalBraintree.PaypalButton.prototype.initialize = function() { this._client = new SolidusPaypalBraintree.createClient({useDataCollector: true, usePaypal: true}); return this._client.initialize().then(this.initializeCallback.bind(this)); }; SolidusPaypalBraintree.PaypalButton.prototype.initializeCallback = function() { this._paymentMethodId = this._client.paymentMethodId; paypal.Button.render({ env: this._environment, payment: function () { return this._client.getPaypalInstance().createPayment(this._paypalOptions); }.bind(this), onAuthorize: function (data, actions) { return this._client.getPaypalInstance().tokenizePayment(data, this._tokenizeCallback.bind(this)); }.bind(this) }, this._element); }; /** * Default callback function for when tokenization completes * * @param {object|null} tokenizeErr - The error returned by Braintree on failure * @param {object} payload - The payload returned by Braintree on success */ SolidusPaypalBraintree.PaypalButton.prototype._tokenizeCallback = function(tokenizeErr, payload) { if (tokenizeErr) { SolidusPaypalBraintree.config.braintreeErrorHandle(tokenizeErr); return; } var params = this._transactionParams(payload); return Spree.ajax({ url: SolidusPaypalBraintree.config.paths.transactions, type: 'POST', dataType: 'json', data: params, success: function(response) { window.location.href = response.redirectUrl; }, error: function(xhr) { console.error("Error submitting transaction"); }, }); }; /** * Builds the transaction parameters to submit to Solidus for the given * payload returned by Braintree * * @param {object} payload - The payload returned by Braintree after tokenization */ SolidusPaypalBraintree.PaypalButton.prototype._transactionParams = function(payload) { return { "payment_method_id" : this._paymentMethodId, "options": this._options, "transaction" : { "email" : payload.details.email, "phone" : payload.details.phone, "nonce" : payload.nonce, "payment_type" : payload.type, "address_attributes" : this._addressParams(payload) } }; }; /** * Builds the address parameters to submit to Solidus using the payload * returned by Braintree * * @param {object} payload - The payload returned by Braintree after tokenization */ SolidusPaypalBraintree.PaypalButton.prototype._addressParams = function(payload) { var first_name, last_name; var payload_address = payload.details.shippingAddress || payload.details.billingAddress; if (payload_address.recipientName) { first_name = payload_address.recipientName.split(" ")[0]; last_name = payload_address.recipientName.split(" ")[1]; } if (!first_name || !last_name) { first_name = payload.details.firstName; last_name = payload.details.lastName; } return { "first_name" : first_name, "last_name" : last_name, "address_line_1" : payload_address.line1, "address_line_2" : payload_address.line2, "city" : payload_address.city, "state_code" : payload_address.state, "zip" : payload_address.postalCode, "country_code" : payload_address.countryCode }; };