-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
147 lines (132 loc) · 4.98 KB
/
popup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
document.addEventListener('DOMContentLoaded', function () {
chrome.storage.local.get('apiKey', function(data) {
if (data.apiKey) {
hideApiKeyPrompt();
}
});
document.getElementById('saveApiKeyButton').addEventListener('click', function() {
var apiKey = document.getElementById('apiKeyInput').value;
chrome.storage.local.set({apiKey: apiKey}, function() {
console.log('API key saved');
hideApiKeyPrompt();
populateAccountDropdown();
});
});
populateAccountDropdown();
const createAccountButton = document.getElementById('createAccountButton');
createAccountButton.addEventListener('click', createNewAccountNumber);
const createCardButton = document.getElementById('createCardButton');
createCardButton.addEventListener('click', createNewCard);
});
function hideApiKeyPrompt() {
document.getElementById('apiKeyInput').style.display = 'none';
document.getElementById('saveApiKeyButton').style.display = 'none';
}
function populateAccountDropdown() {
const apiUrl = 'https://api.increase.com/accounts';
chrome.storage.local.get('apiKey', function(data) {
const apiKey = data.apiKey;
fetch(apiUrl, {
method: 'GET',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json',
}
})
.then(response => response.json())
.then(data => {
const select = document.getElementById('accountSelect');
data.data.forEach(account => {
const option = document.createElement('option');
option.value = account.id;
option.textContent = account.name;
select.appendChild(option);
});
})
.catch(error => {
console.error('Error fetching accounts:', error);
});
})
}
function createNewAccountNumber() {
const apiUrl = 'https://api.increase.com/account_numbers';
const accountSelect = document.getElementById('accountSelect');
const selectedAccountId = accountSelect.options[accountSelect.selectedIndex].value;
const accountNumberName = document.getElementById('accountNumberNameInput').value.trim();
const today = new Date().toISOString().slice(0,10).replace(/-/g,"");
const accountNumberNameToUse = accountNumberName || `AccountNumber ${today}`;
chrome.storage.local.get('apiKey', function(data) {
const apiKey = data.apiKey;
fetch(apiUrl, {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
'account_id': selectedAccountId,
'name': accountNumberNameToUse
}),
})
.then(response => response.json())
.then(data => {
const resultDiv = document.getElementById('account-number-result');
resultDiv.innerHTML = '<br>Account number: ' + data.account_number + '<br>Routing number: ' + data.routing_number;
})
.catch(error => {
console.error('Error:', error);
alert('Error creating account number. Please try again.');
});
})
}
function createNewCard() {
const apiUrl = 'https://api.increase.com/cards';
const accountSelect = document.getElementById('accountSelect');
const selectedAccountId = accountSelect.options[accountSelect.selectedIndex].value;
const cardDescription = document.getElementById('cardDescriptionInput').value.trim();
const today = new Date().toISOString().slice(0,10).replace(/-/g,"");
const cardDescriptionToUse = cardDescription || `Card ${today}`;
chrome.storage.local.get('apiKey', function(data) {
const apiKey = data.apiKey;
fetch(apiUrl, {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
'account_id': selectedAccountId,
'description': cardDescriptionToUse
}),
})
.then(response => response.json())
.then(data => {
console.log('Card created:', data);
const resultDiv = document.getElementById('card-result');
resultDiv.innerHTML = 'Card number: ' + data.last4;
const apiUrl = `https://api.increase.com/cards/${data.id}/details`;
fetch(apiUrl, {
method: 'GET',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json',
},
})
.then(response => response.json())
.then(data => {
console.log('Card created:', data);
resultDiv.innerHTML = '<br>Card number: ' + data.primary_account_number;
resultDiv.innerHTML += '<br>Expiration date: ' + data.expiration_month + '/' + data.expiration_year;
resultDiv.innerHTML += '<br>Verification code: ' + data.verification_code;
})
.catch(error => {
console.error('Error:', error);
alert('Error creating card. Please try again.');
});
})
.catch(error => {
console.error('Error:', error);
alert('Error creating card. Please try again.');
});
});
}