Skip to content

Commit 7d210ae

Browse files
committed
fix(paypal): Force reduced PayPal token expiration and reset on first create payment with 401
1 parent 585c66f commit 7d210ae

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ export const getPaypalAxios = async () => {
5151
);
5252
if (data?.access_token) {
5353
_tokenExpiresAt = data.expires_in > 0
54-
? Date.now() + (Math.min(data.expires_in, 9 * 60 * 60) * 1000)
55-
: Date.now() + (9 * 60 * 60 * 1000);
54+
? Date.now() + (Math.min(data.expires_in, 8 * 60 * 60) * 1000)
55+
: Date.now() + (8 * 60 * 60 * 1000);
5656
docRef.set({
5757
data,
5858
expiresAt: Timestamp.fromMillis(_tokenExpiresAt),
@@ -79,8 +79,20 @@ export const getPaypalAxios = async () => {
7979

8080
export const createPaypalPayment = async (paypalPayment: Record<string, any>) => {
8181
const paypalAxios = await getPaypalAxios();
82-
const { data } = await paypalAxios.post('/v1/payments/payment', paypalPayment);
83-
return data as Record<string, any>;
82+
try {
83+
const { data } = await paypalAxios.post('/v1/payments/payment', paypalPayment);
84+
return data as Record<string, any>;
85+
} catch (_err) {
86+
const err = _err as AxiosError;
87+
if (err.response?.status === 401) {
88+
const { PAYPAL_CLIENT_ID } = process.env;
89+
const docRef = getFirestore().doc(`paypalTokens/${PAYPAL_CLIENT_ID}`);
90+
await docRef.delete().catch(logger.warn);
91+
_paypalAxios = undefined;
92+
_tokenExpiresAt = undefined;
93+
}
94+
throw err;
95+
}
8496
};
8597

8698
export const readPaypalPayment = async (paymentId: string) => {

0 commit comments

Comments
 (0)