条带firebase函数设置默认付款

Pau*_*ane 6 javascript stripe-payments firebase firebase-realtime-database google-cloud-functions

我试图通过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});
      });
  });
});
Run Code Online (Sandbox Code Playgroud)

小智 2

您试图在这个函数中返回两件事。那是行不通的。它应该创建源,但不会更新它。

return stripe.customers.createSource(customer, {source});
return stripe.customers.update(customer, {default_source: source});
Run Code Online (Sandbox Code Playgroud)

  • 这是对的。只有第一个 return 语句将用于传播承诺,第二个 return 语句甚至永远不会被评估。@Paul'Whippet'McGuane 请考虑使用 jslint 或 eslint 等分析工具来静态捕获此类恶作剧。 (2认同)