1 | class MainViewModel
|
2 | constructor: ->
|
3 | @services = [
|
4 | 'slack'
|
5 | ]
|
6 |
|
7 | @fieldnames = [
|
8 | 'HUBOT_HIGHFIVE_CHAT_SERVICE',
|
9 | 'HUBOT_HIGHFIVE_ROOM',
|
10 | 'HUBOT_HIGHFIVE_AWARD_LIMIT',
|
11 | 'HUBOT_HIGHFIVE_DAILY_LIMIT',
|
12 | 'HUBOT_TANGOCARD_ROOTURL'
|
13 | 'HUBOT_TANGOCARD_USER',
|
14 | 'HUBOT_TANGOCARD_KEY',
|
15 | 'HUBOT_TANGOCARD_CC',
|
16 | 'HUBOT_TANGOCARD_AUTH',
|
17 | 'HUBOT_TANGOCARD_CUSTOMER',
|
18 | 'HUBOT_TANGOCARD_ACCOUNT',
|
19 | 'HUBOT_TANGOCARD_EMAIL',
|
20 | 'HUBOT_HIGHFIVE_SHEET_EMAIL',
|
21 | 'HUBOT_HIGHFIVE_SHEET_KEY',
|
22 | 'HUBOT_HIGHFIVE_SHEET_DOCID',
|
23 | 'HUBOT_HIGHFIVE_SHEET_SHEETNAME',
|
24 | 'ipaddr',
|
25 | ]
|
26 |
|
27 | for f in @fieldnames
|
28 | @[f] = ko.observable()
|
29 |
|
30 | @cc_number = ko.observable ''
|
31 | @cc_expiration = ko.observable ''
|
32 | @cc_fname = ko.observable ''
|
33 | @cc_lname = ko.observable ''
|
34 | @cc_address = ko.observable ''
|
35 | @cc_city = ko.observable ''
|
36 | @cc_state = ko.observable ''
|
37 | @cc_zip = ko.observable ''
|
38 | @cc_country = ko.observable ''
|
39 | @tangocard_status = ko.observable ''
|
40 | @tangocard_class = ko.observable ''
|
41 | @cardnote = ko.observable 'optional'
|
42 | @cardclass = ko.computed =>
|
43 | if @cardnote() == 'optional'
|
44 | ''
|
45 | else
|
46 | 'success'
|
47 |
|
48 | @configoutput = ko.computed =>
|
49 | vars = []
|
50 | for k in @fieldnames
|
51 | v = @[k]()
|
52 | vars.push "#{k}='#{v}'" if v? and v != ''
|
53 | vars.join ' \\\n'
|
54 |
|
55 | basic_auth: ->
|
56 | user = @HUBOT_TANGOCARD_USER()
|
57 | pass = @HUBOT_TANGOCARD_KEY()
|
58 | "Basic " + btoa(user + ":" + pass)
|
59 |
|
60 | tangocard_get: (url, success, error) ->
|
61 | baseurl = @HUBOT_TANGOCARD_ROOTURL() || 'https://api.tangocard.com/raas/v1/'
|
62 | fullurl = "#{baseurl}#{url}"
|
63 | $.ajax
|
64 | type: 'GET'
|
65 | url: fullurl
|
66 | dataType: 'json'
|
67 | success: success
|
68 | error: error
|
69 | headers:
|
70 | Authorization: @basic_auth()
|
71 |
|
72 | tangocard_post: (url, data, success, error) ->
|
73 | baseurl = @HUBOT_TANGOCARD_ROOTURL() || 'https://api.tangocard.com/raas/v1/'
|
74 | fullurl = "#{baseurl}#{url}"
|
75 | $.ajax
|
76 | type: 'POST'
|
77 | url: fullurl
|
78 | dataType: 'json'
|
79 | success: success
|
80 | error: error
|
81 | data: JSON.stringify data
|
82 | headers:
|
83 | Authorization: @basic_auth()
|
84 |
|
85 | set_tangocard_error: (msg) ->
|
86 | @tangocard_status msg
|
87 | @tangocard_class 'fail'
|
88 |
|
89 | tangocard_setup: ->
|
90 | cc = @HUBOT_TANGOCARD_CC()
|
91 | auth = @HUBOT_TANGOCARD_AUTH()
|
92 | cust = @HUBOT_TANGOCARD_CUSTOMER()
|
93 | acct = @HUBOT_TANGOCARD_ACCOUNT() || 'HubotHighfive'
|
94 | email = @HUBOT_TANGOCARD_EMAIL()
|
95 |
|
96 | @tangocard_class ''
|
97 | @tangocard_status 'Checking customer/account status...'
|
98 |
|
99 |
|
100 |
|
101 |
|
102 | setupAccount = $.Deferred()
|
103 | @tangocard_get "accounts/#{cust}/#{acct}"
|
104 | , (resp) ->
|
105 | setupAccount.resolve()
|
106 | , (xhr, status, err) =>
|
107 | if xhr.status == 401
|
108 | @set_tangocard_error 'Invalid credentials'
|
109 | return setupAccount.reject()
|
110 | if xhr.status != 404
|
111 | @set_tangocard_error "Error: #{err}"
|
112 | return setupAccount.reject()
|
113 |
|
114 | @tangocard_post 'accounts',
|
115 | customer: cust
|
116 | identifier: acct
|
117 | email: email
|
118 | , (resp) ->
|
119 | setupAccount.resolve()
|
120 | , (xhr, status, err) =>
|
121 | @tangocard_status "Error creating account: #{xhr.responseJSON.invalid_inputs_message}"
|
122 | @tangocard_class 'fail'
|
123 | setupAccount.reject()
|
124 |
|
125 |
|
126 |
|
127 | setupAccount.then =>
|
128 |
|
129 | data =
|
130 | customer: cust
|
131 | account_identifier: acct
|
132 | client_ip: @ipaddr()
|
133 | credit_card:
|
134 | number: @cc_number()
|
135 | security_code: auth
|
136 | expiration: @cc_expiration()
|
137 | billing_address:
|
138 | f_name: @cc_fname()
|
139 | l_name: @cc_lname()
|
140 | address: @cc_address()
|
141 | city: @cc_city()
|
142 | state: @cc_state()
|
143 | zip: @cc_zip()
|
144 | country: @cc_country()
|
145 | email: email
|
146 | @tangocard_post 'cc_register', data
|
147 | , (resp) =>
|
148 | console.log resp
|
149 | @HUBOT_TANGOCARD_CC resp.cc_token
|
150 | @tangocard_status 'Success!'
|
151 | @tangocard_class 'success'
|
152 |
|
153 | , (xhr, status, err) =>
|
154 | json = xhr.responseJSON
|
155 | if xhr.responseJSON.denial_code == 'CC_DUPREGISTER'
|
156 | return @set_tangocard_error 'This card is already registered. Contact Tango Card to have it removed.'
|
157 |
|
158 | errmsg = json.invalid_inputs_message || json.error_message || json.denial_message
|
159 | return @set_tangocard_error errmsg
|
160 |
|
161 | $ ->
|
162 | window.vm = new MainViewModel()
|
163 |
|
164 | $.getJSON 'values.json', (data) ->
|
165 | console.log data
|
166 | for k,v of data
|
167 | vm[k](v)
|
168 | if (data.HUBOT_TANGOCARD_CC || '') != ''
|
169 | vm.cardnote 'already completed'
|
170 |
|
171 | ko.applyBindings vm
|
172 |
|
173 | $('.config-text-area').click ->
|
174 | @select()
|