-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathunpaidInvoice.gs
172 lines (140 loc) · 4.56 KB
/
unpaidInvoice.gs
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
function myFunction() {
var client_id='';
var secret_id='';
try{
var authorizationObj = getAuthorizationToken(client_id,secret_id);
if(authorizationObj.error==true){
//Send Error to admin
throw(new Error(authorizationObj.message));
}
var authorizationToken=authorizationObj.access_token;
unpaidInvoiceObj=getUnpaidInvoice(authorizationToken);
if(unpaidInvoiceObj.error){
throw(new Error(unpaidInvoiceObj.message));
}
if(unpaidInvoiceObj.invoices.length>0){
var htmlBody = htmlifyObject(unpaidInvoiceObj.invoices);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange(5, 1, htmlBody.length, 5);
var values = range.setValues(htmlBody);
}
}catch(e){
Logger.log(e.message);
MailApp.sendEmail("[email protected]","Contact Admin- Automated invoicing Api not working",e.message);
}
}
function getUnpaidInvoice(authorizationToken){
head = {
'Authorization':"Bearer "+ authorizationToken,
'Content-Type': 'application/json'
}
params = {
headers: head,
method : "post",
muteHttpExceptions: true,
payload : JSON.stringify({
status: ["UNPAID","SENT"],
page:0,
page_size:50
})
}
tokenEndpoint='https://api.paypal.com/v1/invoicing/search';
request = UrlFetchApp.getRequest(tokenEndpoint, params);
response = UrlFetchApp.fetch(tokenEndpoint, params);
var responseCode = response.getResponseCode();
var responseBody = response.getContentText();
var unpaidInvoiceResponse={};
if (responseCode === 200) {
var responseJson = JSON.parse(responseBody);
unpaidInvoiceResponse.error=false;
unpaidInvoiceResponse.invoices=responseJson.invoices;
return unpaidInvoiceResponse;
} else {
unpaidInvoiceResponse.error=true;
unpaidInvoiceResponse.message=Utilities.formatString("Request failed. Expected 200, got %d: %s", responseCode, responseBody);
return unpaidInvoiceResponse;
}
}
function htmlifyObject(invoices){
var outputValues=[];
invoices.forEach(function(invoice){
var row=[];
// add invoice number
if(invoice.id){
row.push(invoice.id);
}else{
row.push('N/A');
}
//add merchant email
if(invoice.billing_info&&invoice.billing_info.length>0&&invoice.billing_info[0].email){
row.push(invoice.billing_info[0].email);
}else{
row.push('N/A');
}
//add invoice DAte
if(invoice.invoice_date){
row.push(invoice.invoice_date);
}else{
row.push('N/A');
}
//add total amount
if(invoice.total_amount&&invoice.total_amount.value){
row.push(invoice.total_amount.value);
}else{
row.push('N/A');
}
// add status
if(invoice.status){
row.push(invoice.status);
}else{
row.push('N/A');
}
outputValues.push(row);
});
return outputValues;
}
function getAuthorizationToken(client_id,secret_id){
var tokenEndpoint = "https://api.paypal.com/v1/oauth2/token";
var head = {
'Authorization':"Basic "+ Utilities.base64Encode(client_id+':'+secret_id),
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded'
}
var postPayload = {
"grant_type" : "client_credentials"
}
var params = {
headers: head,
contentType: 'application/x-www-form-urlencoded',
method : "post",
muteHttpExceptions: true,
payload : postPayload
}
var request = UrlFetchApp.getRequest(tokenEndpoint, params);
var response = UrlFetchApp.fetch(tokenEndpoint, params);
var responseCode = response.getResponseCode()
var responseBody = response.getContentText()
if (responseCode === 200) {
var tokenResponse={};
var responseJson = JSON.parse(responseBody);
if(responseJson&&responseJson.error){
tokenResponse.error=true;
tokenResponse.message=responseJson.error;
return tokenResponse;
}
if(responseJson.access_token){
tokenResponse.error=false;
tokenResponse.access_token=responseJson.access_token;
return tokenResponse;
}
tokenResponse.error=true;
tokenResponse.message='Access Token not found';
return tokenResponse;
} else {
var tokenResponse={};
tokenResponse.error=true;
tokenResponse.message=Utilities.formatString("Request failed. Expected 200, got %d: %s", responseCode, responseBody);
return tokenResponse;
}
}