|
1 |
| -'use strict' |
2 |
| -var module = angular.module('paypal-checkout', []) |
| 1 | +(function () { |
| 2 | + 'use strict' |
| 3 | + angular.module('paypal-checkout', []) |
3 | 4 |
|
4 |
| -module.directive("paypalCheckout", function($http, $q, $timeout) { |
5 |
| - return { |
6 |
| - templateUrl: 'paypal-checkout.html', |
7 |
| - restrict: 'EA', |
8 |
| - scope: {}, |
9 |
| - link: function(scope, ele, attrs) { |
10 |
| - var environment = 'sandbox' // CHANGE AS NEEDED |
11 |
| - var merchantId = '6XF3MPZBZV6HU' // YOUR MERCHANT ID HERE (or import with scope) |
12 |
| - var req = { |
13 |
| - method: 'POST', |
14 |
| - url: 'http://foo.bar', // YOUR SERVER HERE (or import with scope) |
15 |
| - data: { foo: 'bar' } // YOUR DATA HERE (or import with scope) |
16 |
| - } |
17 |
| - scope.showButton = false |
| 5 | + angular |
| 6 | + .module('paypal-checkout') |
| 7 | + .directive('paypalCheckout', paypalCheckout) |
18 | 8 |
|
19 |
| - function sendRequest(data) { |
20 |
| - var deferred = $q.defer() |
21 |
| - $http(data) |
22 |
| - .success(function(data, status) { |
23 |
| - return deferred.resolve(data) |
24 |
| - }).error(function(data, status) { |
25 |
| - if (status === 0) { data = 'Connection error' } |
26 |
| - return deferred.reject(data) |
27 |
| - }) |
28 |
| - return deferred.promise |
29 |
| - } |
| 9 | + function paypalCheckout ($http, $q, $timeout) { |
| 10 | + return { |
| 11 | + templateUrl: 'paypal-checkout.html', |
| 12 | + restrict: 'EA', |
| 13 | + scope: {}, |
| 14 | + link: function(scope, ele, attrs) { |
| 15 | + var environment = 'sandbox' // CHANGE AS NEEDED |
| 16 | + var merchantId = '6XF3MPZBZV6HU' // YOUR MERCHANT ID HERE (or import with scope) |
| 17 | + var req = { |
| 18 | + method: 'POST', |
| 19 | + url: 'http://foo.bar', // YOUR SERVER HERE (or import with scope) |
| 20 | + data: { foo: 'bar' } // YOUR DATA HERE (or import with scope) |
| 21 | + } |
| 22 | + scope.showButton = false |
30 | 23 |
|
31 |
| - function showButton() { |
32 |
| - scope.showButton = true |
33 |
| - scope.$apply() |
34 |
| - } |
| 24 | + function sendRequest(data) { |
| 25 | + var deferred = $q.defer() |
| 26 | + $http(data) |
| 27 | + .success(function(data, status) { |
| 28 | + return deferred.resolve(data) |
| 29 | + }).error(function(data, status) { |
| 30 | + if (status === 0) { data = 'Connection error' } |
| 31 | + return deferred.reject(data) |
| 32 | + }) |
| 33 | + return deferred.promise |
| 34 | + } |
35 | 35 |
|
36 |
| - function delayAndShowButton() { |
37 |
| - $timeout(showButton, 1000) |
38 |
| - } |
| 36 | + function showButton() { |
| 37 | + scope.showButton = true |
| 38 | + scope.$apply() |
| 39 | + } |
39 | 40 |
|
40 |
| - function loadPaypalButton() { |
41 |
| - paypal.checkout.setup(merchantId, { |
42 |
| - environment: environment, |
43 |
| - buttons: [{ container: 't1', shape: 'rect', size: 'medium' }] |
44 |
| - }) |
45 |
| - delayAndShowButton() |
46 |
| - } |
| 41 | + function delayAndShowButton() { |
| 42 | + $timeout(showButton, 1000) |
| 43 | + } |
47 | 44 |
|
48 |
| - scope.initPaypal = function() { |
49 |
| - if (scope.showButton == false) { return } |
50 |
| - paypal.checkout.initXO() |
51 |
| - return sendRequest(req) |
52 |
| - .then(function(res) { |
53 |
| - return paypal.checkout.startFlow(res.href) |
54 |
| - }) |
55 |
| - .catch(function(err) { |
56 |
| - console.log('Problem with checkout flow', err) |
57 |
| - return paypal.checkout.closeFlow() |
| 45 | + function loadPaypalButton() { |
| 46 | + paypal.checkout.setup(merchantId, { |
| 47 | + environment: environment, |
| 48 | + buttons: [{ container: 't1', shape: 'rect', size: 'medium' }] |
58 | 49 | })
|
59 |
| - } |
| 50 | + delayAndShowButton() |
| 51 | + } |
60 | 52 |
|
61 |
| - if (window.paypalCheckoutReady != null) { |
62 |
| - scope.showButton = true |
63 |
| - } else { |
64 |
| - var s = document.createElement('script') |
65 |
| - s.src = '//www.paypalobjects.com/api/checkout.js' |
66 |
| - document.body.appendChild(s) |
67 |
| - window.paypalCheckoutReady = function() { |
68 |
| - return loadPaypalButton() |
| 53 | + scope.initPaypal = function() { |
| 54 | + if (scope.showButton == false) { return } |
| 55 | + paypal.checkout.initXO() |
| 56 | + return sendRequest(req) |
| 57 | + .then(function(res) { |
| 58 | + return paypal.checkout.startFlow(res.href) |
| 59 | + }) |
| 60 | + .catch(function(err) { |
| 61 | + console.log('Problem with checkout flow', err) |
| 62 | + return paypal.checkout.closeFlow() |
| 63 | + }) |
| 64 | + } |
| 65 | + |
| 66 | + if (window.paypalCheckoutReady != null) { |
| 67 | + scope.showButton = true |
| 68 | + } else { |
| 69 | + var s = document.createElement('script') |
| 70 | + s.src = '//www.paypalobjects.com/api/checkout.js' |
| 71 | + document.body.appendChild(s) |
| 72 | + window.paypalCheckoutReady = function() { |
| 73 | + return loadPaypalButton() |
| 74 | + } |
69 | 75 | }
|
70 |
| - } |
71 | 76 |
|
| 77 | + } |
72 | 78 | }
|
73 | 79 | }
|
74 |
| -}) |
| 80 | +})() |
0 commit comments