Skip to content

Commit 9249ece

Browse files
committed
fix(vindi-webhook): try collection query by bill id if no metadata (#18)
fixes #18
1 parent 8a94424 commit 9249ece

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

functions/routes/vindi/webhook.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ exports.post = ({ appSdk, admin }, req, res) => {
99
const data = vindiEvent.data.id ? vindiEvent.data
1010
: (vindiEvent.data.bill || vindiEvent.data.charge)
1111
if (data && data.id) {
12+
const collectionRef = admin.firestore().collection('charges')
1213
const isVindiCharge = vindiEvent.type.startsWith('charge_')
1314
let axiosVindi
1415
console.log('> Vindi Hook', vindiEvent.type, data.id)
@@ -17,7 +18,7 @@ exports.post = ({ appSdk, admin }, req, res) => {
1718
if (isVindiCharge) {
1819
// console.log('> Searching charge on local database')
1920
// get metadata from local database
20-
admin.firestore().collection('charges').doc(String(data.id))
21+
collectionRef.doc(String(data.id))
2122
.get().then(documentSnapshot => {
2223
if (documentSnapshot && documentSnapshot.data) {
2324
resolve(documentSnapshot.data())
@@ -30,8 +31,20 @@ exports.post = ({ appSdk, admin }, req, res) => {
3031
switch (vindiEvent.type) {
3132
case 'bill_paid':
3233
case 'bill_canceled':
33-
// metadata included on bill payload
34-
resolve(data.metadata)
34+
if (data.metadata && data.metadata.store_id) {
35+
// metadata included on bill payload
36+
resolve(data.metadata)
37+
} else {
38+
collectionRef.where('vindi_bill_id', '==', data.id).limit(1)
39+
.get().then(querySnapshot => {
40+
if (querySnapshot.empty) {
41+
return resolve(false)
42+
}
43+
querySnapshot.forEach(documentSnapshot => {
44+
resolve(documentSnapshot.data())
45+
})
46+
}).catch(reject)
47+
}
3548
}
3649
resolve(false)
3750
}

0 commit comments

Comments
 (0)