@@ -102,65 +102,80 @@ function parseResponse(responseApi) {
102
102
}
103
103
104
104
105
- function fetchBuffer ( path , method = 'GET' , payload = null , query = null , version = 'v2' ) {
105
+ function fetchBuffer ( path , method = 'GET' , payload = null , query = null , version = "v2" , environment = null , privateKeyPem = null , challengeId = null ) {
106
106
let user = new getDefaultUser ( ) ;
107
- let hostname = getHostname ( user . environment . toLowerCase ( ) , version ) ;
108
- let options = { method : method } ;
107
+ if ( ! user . privateKey ) {
108
+ throw JSON . stringify ( { "message" : "Erro de autenticação! Por favor, faça login novamente." } ) ;
109
+ }
110
+ if ( ! environment ) {
111
+ environment = environment || user . environment . toLowerCase ( ) ;
112
+ }
113
+ let hostname = getHostname ( environment , version ) ;
114
+ let options = {
115
+ method : method ,
116
+ muteHttpExceptions : true ,
117
+ } ;
109
118
let url = hostname + path ;
110
119
111
120
if ( query ) {
112
- let queryString = '' ;
113
- let separator = '?' ;
114
- for ( let key in query ) {
115
- if ( query [ key ] ) {
116
- queryString += separator + key + '=' + query [ key ] ;
117
- separator = '&' ;
121
+ let queryString = '' ;
122
+ let separator = '?' ;
123
+ for ( let key in query ) {
124
+ if ( query [ key ] ) {
125
+ queryString += separator + key + '=' + query [ key ] ;
126
+ separator = '&' ;
127
+ }
118
128
}
119
- }
120
- url += queryString ;
129
+ url += queryString ;
121
130
}
122
131
123
- paths = [ "/session" , "/boleto-payment" ]
124
-
125
- if ( paths . includes ( path ) && method != "GET" ) {
126
- var accessId = KeyGen . generateMemberAccessId ( user . workspaceId , user . email )
127
- } else {
128
- var accessId = user . accessId ;
129
- }
130
-
131
- let accessTime = Math . round ( ( new Date ( ) ) . getTime ( ) / 1000 ) . toString ( ) ;
132
- options [ 'headers' ] = {
133
- 'Access-Id' : accessId ,
134
- 'User-Agent' : 'GoogleSheets-SDK-0.4.3' ,
135
- 'Accept-Language' : 'pt-BR' ,
136
- 'Content-Type' : 'application/json' ,
137
- 'Access-Time' : accessTime
138
- } ;
139
-
140
- let body = JSON . stringify ( payload ) ;
141
- if ( ! payload ) {
142
- body = "" ;
143
- }
132
+ if ( privateKeyPem ) {
133
+ var accessId = KeyGen . generateMemberAccessId ( user . workspaceId , user . email )
134
+ } else {
135
+ var accessId = user . accessId ;
136
+ }
144
137
145
- options [ 'payload' ] = body ;
146
-
147
138
if ( ! privateKeyPem ) {
148
- var privateKeyPem = user . privateKey ;
139
+ var privateKeyPem = user . privateKey ;
149
140
}
150
-
151
- let message = accessId + ':' + accessTime + ':' + body ;
141
+
142
+ let accessTime = Math . round ( ( new Date ( ) ) . getTime ( ) / 1000 ) . toString ( ) ;
143
+ options [ 'headers' ] = {
144
+ 'Access-Id' : accessId ,
145
+ 'User-Agent' : 'App-StarkBank-GSheets-v0.6.5b' ,
146
+ 'User-Agent-Override' : 'App-StarkBank-GSheets-v0.6.5b' ,
147
+ 'PlatFormId' : 'gsheets' ,
148
+ 'PlatFormVersion' : '0.6.5' ,
149
+ 'Accept-Language' : 'pt-BR' ,
150
+ 'Content-Type' : 'application/pdf' ,
151
+ 'Access-Time' : accessTime
152
+ } ;
153
+
154
+ let body = ""
155
+ if ( payload ) {
156
+ body = payload ;
157
+ }
158
+
159
+ options [ 'payload' ] = body ;
160
+
161
+ let message = accessId + ':' + accessTime + ':' + body
162
+
163
+ if ( challengeId ) {
164
+ message += ":" + challengeId
165
+ options [ 'headers' ] [ 'Access-Challenge-Ids' ] = challengeId
166
+ }
167
+
152
168
let signature = easySign ( message , privateKeyPem ) ;
153
169
options [ 'headers' ] [ 'Access-Signature' ] = signature ;
154
170
155
171
response = UrlFetchApp . fetch ( url , options ) ;
156
172
157
- let content = response . getAs ( "application/pdf" ) ;
158
173
let status = response . getResponseCode ( ) ;
159
174
160
175
if ( status != 200 ) {
161
- Browser . msgBox ( parseResponse ( response ) [ 0 ] [ "errors" ] [ 0 ] [ "message" ] )
162
- return new Error ( )
176
+ return [ parseResponse ( response ) [ 0 ] [ "errors" ] [ 0 ] [ "code" ] , status ]
163
177
} else {
178
+ let content = response . getAs ( "application/pdf" ) ;
164
179
return [ content , status ]
165
180
}
166
181
}
0 commit comments