6

我想通過firebase函數設置添加到條帶的最後一張卡片作爲默認值,雖然我似乎無法使其工作。stripe firebase函數設置默認支付

// Add a payment source (card) for a user by writing a stripe payment source token to Realtime database 
exports.addPaymentSource = functions.database.ref('/users/{userId}/sources/{pushId}/token').onWrite(event => { 
    const source = event.data.val(); 
    if (source === null) return null; 
    return admin.database().ref(`/users/${event.params.userId}/customer_id`).once('value').then(snapshot => { 
    return snapshot.val(); 
    }).then(customer => { 
    return stripe.customers.createSource(customer, {source}); 
    return stripe.customers.update(customer, {default_source: source}); 
    }).then(response => { 
     return event.data.adminRef.parent.set(response); 
    }, error => { 
     return event.data.adminRef.parent.child('error').set(userFacingMessage(error)).then(() => { 
     // return reportError(error, {user: event.params.userId}); 
     consolg.log(error, {user: event.params.userId}); 
     }); 
    }); 
}); 

回答

2

您試圖在此函數中返回兩件事。這是行不通的。它應該創建源,但不會更新它。

return stripe.customers.createSource(customer, {source}); 
return stripe.customers.update(customer, {default_source: source}); 
+0

是它創建並沒有更新,我需要更新默認卡,因爲它關鍵正確的卡充電 –

+1

您不能返回兩個函數。因此,您需要創建源代碼,然後在創建源代碼後添加一個額外的回調函數,然後更新默認源代碼。我不得不挖掘他們的文檔,但這個想法是。 createSource()。然後(=> update())。然後(=>更新數據庫)。您嘗試同時創建和更新(並嘗試返回這兩個函數)。所以只需將第二個功能添加到自己的回調函數中即可。 – Notmfb

+1

這是正確的。只有第一個return語句會用來傳播promise,第二個將永遠不會被評估。 @ Paul'Whippet'McGuane請考慮使用諸如jslint或eslint之類的分析工具來靜態捕獲這些類型的shenanigans。 –