@@ -34,29 +34,37 @@ export const getPaypalAxios = async () => {
3434 }
3535 }
3636 if ( ! token ) {
37- const { data } = await axios . post (
38- '/v1/oauth2/token' ,
39- 'grant_type=client_credentials' ,
40- {
41- baseURL,
42- headers : {
43- 'Content-Type' : 'application/x-www-form-urlencoded' ,
37+ try {
38+ const { data } = await axios . post (
39+ '/v1/oauth2/token' ,
40+ 'grant_type=client_credentials' ,
41+ {
42+ baseURL,
43+ headers : {
44+ 'Content-Type' : 'application/x-www-form-urlencoded' ,
45+ } ,
46+ auth : {
47+ username : PAYPAL_CLIENT_ID ! ,
48+ password : PAYPAL_CLIENT_SECRET ! ,
49+ } ,
4450 } ,
45- auth : {
46- username : PAYPAL_CLIENT_ID ! ,
47- password : PAYPAL_CLIENT_SECRET ! ,
48- } ,
49- } ,
50- ) ;
51- if ( data ?. access_token ) {
52- _tokenExpiresAt = data . expires_in
53- ? Date . now ( ) + ( data . expires_in * 1000 )
54- : Date . now ( ) + ( 9 * 60 * 60 * 1000 ) ;
55- docRef . set ( {
56- data,
57- expiresAt : Timestamp . fromMillis ( _tokenExpiresAt ) ,
58- } ) . catch ( logger . warn ) ;
59- token = data . access_token ;
51+ ) ;
52+ if ( data ?. access_token ) {
53+ _tokenExpiresAt = data . expires_in
54+ ? Date . now ( ) + ( data . expires_in * 1000 )
55+ : Date . now ( ) + ( 9 * 60 * 60 * 1000 ) ;
56+ docRef . set ( {
57+ data,
58+ expiresAt : Timestamp . fromMillis ( _tokenExpiresAt ) ,
59+ } ) . catch ( logger . warn ) ;
60+ token = data . access_token ;
61+ }
62+ } catch ( _err ) {
63+ const err = _err as AxiosError ;
64+ logger . warn ( `Cannot generate PayPal token (${ err . response ?. status } )'` , {
65+ response : err . response ?. data ,
66+ } ) ;
67+ throw _err ;
6068 }
6169 }
6270 if ( ! token ) {
@@ -103,7 +111,7 @@ export const executePaypalPayment = async (
103111
104112export const createPaypalProfile = async ( ) => {
105113 const { settingsContent } = config . get ( ) ;
106- ( await getPaypalAxios ( ) ) . post ( '/v1/payment-experience/web-profiles/' , {
114+ return ( await getPaypalAxios ( ) ) . post ( '/v1/payment-experience/web-profiles/' , {
107115 name : `EComPlus_${ Date . now ( ) } ` ,
108116 presentation : {
109117 brand_name : settingsContent . name || 'Loja Virtual' ,
@@ -128,7 +136,7 @@ export const createPaypalProfile = async () => {
128136export const createPaypalWebhook = async ( ) => {
129137 const locationId = config . get ( ) . httpsFunctionOptions . region ;
130138 const appBaseUri = `https://${ locationId } -${ process . env . GCLOUD_PROJECT } .cloudfunctions.net` ;
131- ( await getPaypalAxios ( ) ) . post ( '/v1/notifications/webhooks' , {
139+ return ( await getPaypalAxios ( ) ) . post ( '/v1/notifications/webhooks' , {
132140 url : `${ appBaseUri } /paypal-webhook` ,
133141 event_types : [
134142 // v2
@@ -153,7 +161,7 @@ export const createPaypalWebhook = async () => {
153161 } ) . catch ( ( _err ) => {
154162 const err = _err as AxiosError < any > ;
155163 if ( err . response ?. data ?. name === 'WEBHOOK_URL_ALREADY_EXISTS' ) {
156- return ;
164+ return null ;
157165 }
158166 throw err ;
159167 } ) ;
0 commit comments