Skip to content

Commit 466f10e

Browse files
author
Tyler
committed
Updated directive per Angular team style guide
1 parent ad40f8b commit 466f10e

File tree

1 file changed

+68
-62
lines changed

1 file changed

+68
-62
lines changed

paypal-checkout.js

Lines changed: 68 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,80 @@
1-
'use strict'
2-
var module = angular.module('paypal-checkout', [])
1+
(function () {
2+
'use strict'
3+
angular.module('paypal-checkout', [])
34

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)
188

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
3023

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+
}
3535

36-
function delayAndShowButton() {
37-
$timeout(showButton, 1000)
38-
}
36+
function showButton() {
37+
scope.showButton = true
38+
scope.$apply()
39+
}
3940

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+
}
4744

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' }]
5849
})
59-
}
50+
delayAndShowButton()
51+
}
6052

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+
}
6975
}
70-
}
7176

77+
}
7278
}
7379
}
74-
})
80+
})()

0 commit comments

Comments
 (0)