Skip to content

Commit b5abd57

Browse files
committed
fix(paypal): Better error handling, fix calling webhooks and profiles one by one
1 parent 0a6ff6c commit b5abd57

File tree

2 files changed

+41
-29
lines changed

2 files changed

+41
-29
lines changed

packages/apps/paypal/src/paypal-list-payments.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,21 @@ export const paypalListPayments = async (modBody: AppModuleBody<'list_payments'>
103103
try {
104104
const docRef = getFirestore().doc(`paypalProfiles/${PAYPAL_CLIENT_ID}`);
105105
if (!(await docRef.get()).exists) {
106-
await Promise.all([
107-
createPaypalWebhook(),
108-
createPaypalProfile(),
109-
]);
106+
await createPaypalWebhook();
107+
await createPaypalProfile();
110108
}
111109
paypalPayment = await createPaypalPayment(parseToPaypalPayment(params));
112110
} catch (_err: any) {
113111
const err = _err as AxiosError;
114112
if (err.response?.status === 401) {
115113
throw err;
116114
}
115+
logger.warn('PalPal setup failing', {
116+
url: err.config?.url,
117+
request: err.config?.data,
118+
response: err.response?.data,
119+
status: err.response?.status,
120+
});
117121
logger.error(err);
118122
}
119123
}

packages/apps/paypal/src/util/paypal-api.ts

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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

104112
export 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 () => {
128136
export 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

Comments
 (0)