Skip to content

Commit c145218

Browse files
committed
chore: cleanup
1 parent b6f0c67 commit c145218

File tree

2 files changed

+18
-20
lines changed

2 files changed

+18
-20
lines changed

plugins/braintree-payment/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,14 @@
6262
"@medusajs/icons": "^2.8.2",
6363
"@medusajs/medusa": "2.8.2",
6464
"@medusajs/test-utils": "^2.8.2",
65-
"@medusajs/ui": "^4.0.3"
65+
"@medusajs/ui": "^4.0.3",
66+
"jsonwebtoken": "^9.0.2"
6667
},
6768
"engines": {
6869
"node": ">=20"
6970
},
7071
"dependencies": {
71-
"braintree": "^3.30.0",
72-
"jsonwebtoken": "^9.0.2"
72+
"braintree": "^3.30.0"
73+
7374
}
7475
}

plugins/braintree-payment/src/providers/payment-braintree/src/core/braintree-base.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ export interface BraintreePaymentSessionData {
5656
braintreeTransaction?: Transaction;
5757
}
5858

59+
const buildTokenCacheKey = (customerId: string) => `braintree:clientToken:${customerId}`;
60+
5961
class BraintreeBase extends AbstractPaymentProvider<BraintreeOptions> {
6062
identifier = 'braintree';
6163
protected readonly options_: BraintreeOptions;
@@ -74,21 +76,15 @@ class BraintreeBase extends AbstractPaymentProvider<BraintreeOptions> {
7476
}
7577

7678
async saveClientTokenToCache(clientToken: string, customerId: string, expiresOn: number): Promise<void> {
77-
if (!customerId) {
78-
return;
79-
}
80-
if (!clientToken) {
81-
return;
82-
}
83-
const expiryTime = expiresOn*1000 - Math.floor(Date.now())-1000;
84-
if(expiryTime < 0){
85-
return;
86-
}
87-
await this.cache.set(`braintree:clientToken:${customerId}`, clientToken, Math.floor(expiryTime / 1000));
79+
const expiryTime = expiresOn * 1000 - Math.floor(Date.now()) - 1000;
80+
81+
if (!customerId || !clientToken || expiryTime < 0) return;
82+
83+
await this.cache.set(buildTokenCacheKey(customerId), clientToken, Math.floor(expiryTime / 1000));
8884
}
8985

9086
async getClientTokenFromCache(customerId: string): Promise<string | null> {
91-
const token = (await this.cache.get(`braintree:clientToken:${customerId}`)) as string | null;
87+
const token = (await this.cache.get(buildTokenCacheKey(customerId))) as string | null;
9288
return token;
9389
}
9490

@@ -97,16 +93,17 @@ class BraintreeBase extends AbstractPaymentProvider<BraintreeOptions> {
9793
const generatedToken = await this.gateway.clientToken.generate({});
9894
return generatedToken.clientToken;
9995
}
96+
10097
const token = await this.getClientTokenFromCache(customerId);
101-
if (token) {
102-
return token;
103-
}
98+
99+
if (token) return token;
100+
104101
const generatedToken = await this.gateway.clientToken.generate({});
105-
const defaultExpiryTime = Math.floor(Date.now()/1000) + 24 * 3600 * 1000; // 24 hours default
102+
const defaultExpiryTime = Math.floor(Date.now() / 1000) + 24 * 3600 * 1000; // 24 hours default
103+
106104
await this.saveClientTokenToCache(generatedToken.clientToken, customerId, defaultExpiryTime);
107105
return generatedToken.clientToken;
108106
}
109-
110107

111108
private async parsePaymentSessionData(data: Record<string, unknown>): Promise<BraintreePaymentSessionData> {
112109
return {

0 commit comments

Comments
 (0)