|
1 | | -;(function($, window) { |
| 1 | +;(function ($, window) { |
2 | 2 | 'use strict'; |
3 | 3 |
|
4 | 4 | $.plugin('swagPayPalUnifiedExpressCheckoutButton', { |
|
123 | 123 | */ |
124 | 124 | expressCheckoutButton: null, |
125 | 125 |
|
126 | | - init: function() { |
| 126 | + init: function () { |
127 | 127 | this.applyDataAttributes(); |
128 | 128 | this.applyOrderNumberDataAttribute(); |
| 129 | + this.buttonIsRendered = false; |
129 | 130 |
|
130 | 131 | if (this.isProductExcluded()) { |
131 | 132 | return; |
|
146 | 147 | } |
147 | 148 | }, |
148 | 149 |
|
149 | | - applyOrderNumberDataAttribute: function() { |
| 150 | + applyOrderNumberDataAttribute: function () { |
150 | 151 | this.opts.productNumber = this.$el.attr('data-productNumber'); |
151 | 152 | }, |
152 | 153 |
|
153 | 154 | /** |
154 | 155 | * Will be triggered when the selected variant was changed. |
155 | 156 | * Re-initializes this plugin. |
156 | 157 | */ |
157 | | - onChangeVariant: function() { |
| 158 | + onChangeVariant: function () { |
158 | 159 | window.StateManager.addPlugin('*[data-paypalUnifiedEcButton="true"]', 'swagPayPalUnifiedExpressCheckoutButton'); |
159 | 160 | }, |
160 | 161 |
|
|
163 | 164 | * |
164 | 165 | * @return {boolean} |
165 | 166 | */ |
166 | | - isProductExcluded: function() { |
| 167 | + isProductExcluded: function () { |
167 | 168 | var me = this, |
168 | 169 | productNumber = me.opts.productNumber, |
169 | 170 | excludedProductNumbers, |
|
193 | 194 | /** |
194 | 195 | * Creates the PayPal express checkout button with the loaded PayPal javascript |
195 | 196 | */ |
196 | | - createButton: function() { |
| 197 | + createButton: function () { |
197 | 198 | var me = this, |
198 | 199 | $head = $('head'); |
199 | 200 |
|
| 201 | + this.payPalObjectInterval = setInterval(this.payPalObjectCheck.bind(this), this.opts.interval); |
200 | 202 | if (!$head.data(me.opts.paypalScriptLoadedSelector)) { |
201 | 203 | $.ajax({ |
202 | 204 | url: me.renderSdkUrl(me.opts.clientId, me.opts.currency), |
203 | 205 | dataType: 'script', |
204 | 206 | cache: true, |
205 | | - success: function() { |
| 207 | + success: function () { |
206 | 208 | $head.data(me.opts.paypalScriptLoadedSelector, true); |
207 | | - me.renderButton(); |
208 | 209 | } |
209 | 210 | }); |
210 | | - } else { |
211 | | - me.renderButton(); |
212 | 211 | } |
213 | 212 | }, |
214 | 213 |
|
215 | | - renderSdkUrl: function(clientId, currency) { |
| 214 | + payPalObjectCheck: function () { |
| 215 | + if (window.paypal === undefined || window.paypal === null || typeof window.paypal.Buttons !== 'function') { |
| 216 | + return; |
| 217 | + } |
| 218 | + |
| 219 | + clearInterval(this.payPalObjectInterval); |
| 220 | + this.paypal = window.paypal; |
| 221 | + this.renderButton(); |
| 222 | + }, |
| 223 | + |
| 224 | + renderSdkUrl: function (clientId, currency) { |
216 | 225 | var enabledFundings = this.opts.enabledFundings, |
217 | 226 | params = { |
218 | 227 | 'client-id': clientId, |
|
253 | 262 | /** |
254 | 263 | * Renders the ECS button |
255 | 264 | */ |
256 | | - renderButton: function() { |
| 265 | + renderButton: function () { |
257 | 266 | var me = this; |
258 | 267 |
|
259 | | - if (this.opts.isListing && this.opts.showPayLater) { |
260 | | - $('.' + this.opts.hasNotPayLaterClass).removeClass(this.opts.hasNotPayLaterClass).addClass(this.opts.hasPayLaterClass); |
| 268 | + if (this.buttonIsRendered) { |
| 269 | + return; |
261 | 270 | } |
262 | 271 |
|
263 | | - // wait for the PayPal javascript to be loaded |
264 | | - me.buffer(function() { |
265 | | - me.expressCheckoutButton = paypal |
266 | | - .Buttons(me.createPayPalButtonConfiguration()) |
267 | | - .render(me.$el.get(0)); |
| 272 | + this.buttonIsRendered = true; |
268 | 273 |
|
269 | | - $.publish('plugin/swagPayPalUnifiedExpressCheckoutButtonCart/createButton', [me, me.expressCheckoutButton]); |
270 | | - }); |
271 | | - }, |
272 | | - |
273 | | - /** |
274 | | - * Buffer helper function to set a timeout for a function |
275 | | - * |
276 | | - * @param {function} fn |
277 | | - * @param {number} timeout |
278 | | - * @return {number} |
279 | | - */ |
280 | | - buffer: function(fn, timeout) { |
281 | | - var me = this; |
| 274 | + if (this.opts.isListing && this.opts.showPayLater) { |
| 275 | + $('.' + this.opts.hasNotPayLaterClass).removeClass(this.opts.hasNotPayLaterClass).addClass(this.opts.hasPayLaterClass); |
| 276 | + } |
282 | 277 |
|
283 | | - timeout = timeout || 100; |
| 278 | + me.expressCheckoutButton = paypal |
| 279 | + .Buttons(me.createPayPalButtonConfiguration()) |
| 280 | + .render(me.$el.get(0)); |
284 | 281 |
|
285 | | - return window.setTimeout(fn.bind(me), timeout); |
| 282 | + $.publish('plugin/swagPayPalUnifiedExpressCheckoutButtonCart/createButton', [me, me.expressCheckoutButton]); |
286 | 283 | }, |
287 | 284 |
|
288 | 285 | /** |
289 | 286 | * Creates the configuration for the button |
290 | 287 | * |
291 | 288 | * @return {Object} |
292 | 289 | */ |
293 | | - createPayPalButtonConfiguration: function() { |
| 290 | + createPayPalButtonConfiguration: function () { |
294 | 291 | var me = this, |
295 | 292 | config; |
296 | 293 |
|
|
330 | 327 | * This method dispatches a request to the `\Shopware_Controllers_Widgets_PaypalUnifiedV2ExpressCheckout`-controller (default) |
331 | 328 | * which initialises an order at PayPal. |
332 | 329 | */ |
333 | | - createOrder: function() { |
| 330 | + createOrder: function () { |
334 | 331 | var me = this, |
335 | 332 | data = {}; |
336 | 333 |
|
|
350 | 347 | return $.ajax({ |
351 | 348 | url: me.opts.createOrderUrl, |
352 | 349 | data: data |
353 | | - }).then(function(response) { |
| 350 | + }).then(function (response) { |
354 | 351 | if (response.riskManagementFailed === true) { |
355 | 352 | me.isRiskManagementError = true; |
356 | 353 |
|
357 | 354 | return; |
358 | 355 | } |
359 | 356 |
|
360 | 357 | return response.token; |
361 | | - }, function() { |
| 358 | + }, function () { |
362 | 359 | }).promise(); |
363 | 360 | }, |
364 | 361 |
|
365 | 362 | /** |
366 | 363 | * This method dispatches a request to the `\Shopware_Controllers_Widgets_PaypalUnifiedV2ExpressCheckout`-controller (default) |
367 | 364 | * which creates a new customer account and also logs the user in. |
368 | 365 | */ |
369 | | - onApprove: function(data, actions) { |
| 366 | + onApprove: function (data, actions) { |
370 | 367 | var me = this; |
371 | 368 |
|
372 | 369 | return $.ajax({ |
373 | 370 | url: me.opts.onApproveUrl, |
374 | 371 | data: data |
375 | | - }).then(function(response) { |
| 372 | + }).then(function (response) { |
376 | 373 | var params = { |
377 | 374 | expressCheckout: response.expressCheckout, |
378 | 375 | token: response.token |
379 | 376 | }; |
380 | 377 |
|
381 | 378 | actions.redirect($.swagPayPalRenderUrl(me.opts.confirmUrl, params)); |
382 | | - }, function() { |
| 379 | + }, function () { |
383 | 380 | me.onPayPalAPIError(); |
384 | 381 | }).promise(); |
385 | 382 | }, |
386 | 383 |
|
387 | | - onPayPalAPIError: function() { |
| 384 | + onPayPalAPIError: function () { |
388 | 385 | var content, |
389 | 386 | config; |
390 | 387 |
|
|
418 | 415 | * |
419 | 416 | * @returns {Number} |
420 | 417 | */ |
421 | | - getProductQuantity: function() { |
| 418 | + getProductQuantity: function () { |
422 | 419 | var me = this, |
423 | 420 | quantity = $(me.opts.productQuantitySelector).val(); |
424 | 421 |
|
|
432 | 429 | /** |
433 | 430 | * Destroys the plugin and unsubscribes from subscribed events |
434 | 431 | */ |
435 | | - destroy: function() { |
| 432 | + destroy: function () { |
436 | 433 | var me = this; |
437 | 434 |
|
438 | 435 | $.unsubscribe(me.getEventName('plugin/swAjaxVariant/onRequestData')); |
|
445 | 442 | * After the loading another product variant, we lose the |
446 | 443 | * plugin instance, therefore, we have to re-initialize it here. |
447 | 444 | */ |
448 | | - $.subscribe('plugin/swAjaxVariant/onRequestData', function() { |
| 445 | + $.subscribe('plugin/swAjaxVariant/onRequestData', function () { |
449 | 446 | window.StateManager.addPlugin('*[data-paypalUnifiedEcButton="true"]', 'swagPayPalUnifiedExpressCheckoutButton'); |
450 | 447 | }); |
451 | 448 |
|
452 | | - $.subscribe('plugin/swInfiniteScrolling/onFetchNewPageFinished', function() { |
| 449 | + $.subscribe('plugin/swInfiniteScrolling/onFetchNewPageFinished', function () { |
453 | 450 | window.StateManager.addPlugin('*[data-paypalUnifiedEcButton="true"]', 'swagPayPalUnifiedExpressCheckoutButton'); |
454 | 451 | }); |
455 | 452 |
|
456 | | - $.subscribe('plugin/swInfiniteScrolling/onLoadPreviousFinished', function() { |
| 453 | + $.subscribe('plugin/swInfiniteScrolling/onLoadPreviousFinished', function () { |
457 | 454 | window.StateManager.addPlugin('*[data-paypalUnifiedEcButton="true"]', 'swagPayPalUnifiedExpressCheckoutButton'); |
458 | 455 | }); |
459 | 456 |
|
|
0 commit comments