Skip to content

Commit e6cf7fb

Browse files
leomp12claude
andcommitted
fix(asaas): Improve API key tracking and configuration management
- Add asaasKeyId fingerprinting for better configuration sync - Enhance webhook auth token security using storeId + asaasKeyId - Ensure PIX keys and webhooks are recreated when API key changes 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 0786138 commit e6cf7fb

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

packages/apps/asaas/src/asaas-create-transaction.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export default async (modBody: AppModuleBody<'create_transaction'>) => {
3535
message: 'Chave de API não configurada (lojista deve configurar o aplicativo)',
3636
};
3737
}
38+
const asaasKeyId = `${ASAAS_API_KEY}`.substring(0, 6) + `${ASAAS_API_KEY}`.slice(-3);
3839

3940
const {
4041
order_id: orderId,
@@ -101,11 +102,12 @@ export default async (modBody: AppModuleBody<'create_transaction'>) => {
101102
asaasPayment.billingType = 'PIX';
102103
const docRef = getFirestore().doc('asaasSetup/pixKey');
103104
const docSnap = await docRef.get();
104-
if (!docSnap.data()?.key) {
105+
if (docSnap.data()?.asaasKeyId !== asaasKeyId) {
105106
const {
106107
data: pixKeyData,
107108
} = await asaasAxios.post('/v3/pix/addressKeys', { 'type': 'EVP' });
108-
await docRef.set(pixKeyData).catch(logger.warn);
109+
await docRef.set({ asaasKeyId, pixKeyData })
110+
.catch(logger.warn);
109111
}
110112
} else if (paymentMethod.code === 'banking_billet') {
111113
asaasPayment.billingType = 'BOLETO';
@@ -189,6 +191,7 @@ export default async (modBody: AppModuleBody<'create_transaction'>) => {
189191
}
190192

191193
const {
194+
storeId,
192195
httpsFunctionOptions,
193196
settingsContent,
194197
} = config.get();
@@ -197,7 +200,7 @@ export default async (modBody: AppModuleBody<'create_transaction'>) => {
197200
const webhookUrl = `${appBaseUri}/asaas-webhook`;
198201
const docRef = getFirestore().doc('asaasSetup/webhook');
199202
const docSnap = await docRef.get();
200-
if (docSnap.data()?.url !== webhookUrl) {
203+
if (docSnap.data()?.asaasKeyId !== asaasKeyId) {
201204
try {
202205
const {
203206
data: webhookData,
@@ -207,7 +210,7 @@ export default async (modBody: AppModuleBody<'create_transaction'>) => {
207210
'email': appData.webhook_email || settingsContent.email,
208211
'enabled': true,
209212
'interrupted': false,
210-
'authToken': `w1_${ASAAS_API_KEY}`,
213+
'authToken': `${storeId}_${asaasKeyId}`,
211214
'sendType': 'SEQUENTIALLY',
212215
'events': [
213216
'PAYMENT_CREDIT_CARD_CAPTURE_REFUSED',
@@ -225,7 +228,8 @@ export default async (modBody: AppModuleBody<'create_transaction'>) => {
225228
'PAYMENT_AWAITING_RISK_ANALYSIS',
226229
],
227230
});
228-
await docRef.set(webhookData).catch(logger.warn);
231+
await docRef.set({ asaasKeyId, webhookData })
232+
.catch(logger.warn);
229233
} catch (_err) {
230234
const err = _err as AxiosError;
231235
logger.warn('Failed saving Asaas webhook', {

0 commit comments

Comments
 (0)