【发布时间】:2019-08-03 14:57:38
【问题描述】:
我正在学习由 Google 开发人员制作的教程 here。
在文章中说:
Stripe 提供了两种创建支付方式的方法:令牌和来源。令牌是一次性的。附加到客户时可以多次使用源。
我一直在做的是我一直在我的 Android 应用程序中通过stripe.createToken(cardToSave, object : TokenCallback {...} 创建一个Token,并将其保存到我的数据库中。这会触发一个云函数addPaymentSource,它会创建一个“支付源(卡)”并保存到我的数据库中:
addPaymentSource 云功能
exports.addPaymentSource = functions.database
.ref('/stripe_customers/{userId}/sources/{pushId}/token').onWrite((change, context) => {
const source = change.after.val();
if (source === null){
return null;
}
return admin.database().ref(`/stripe_customers/${context.params.userId}/customer_id`)
.once('value').then((snapshot) => {
return snapshot.val();
}).then((customer) => {
return stripe.customers.createSource(customer, {source:source});
}).then((response) => {
return change.after.ref.parent.set(response);
}, (error) => {
return change.after.ref.parent.child('error').set(userFacingMessage(error));
}).then(() => {
return reportError(error, {user: context.params.userId});
});
});
那么,这是一个我可以重复用于付款的“来源”吗?我认为这是因为我能够使用这个“源”创建多个charges。
我感到困惑的部分是我使用了一个令牌来创建一个源(或者我认为它是一个源)。这是正确的吗?
另外,每当我添加另一个源/卡时,新卡都会添加到旧卡旁边的路径"stripe_customers/$currentUser/sources/"。现在当我点击付款时,它仍在向旧卡收费。如何将卡切换到新添加的卡?
编辑
在条纹网站上:
如果卡的所有者没有默认卡,则新卡将成为默认卡。但是,如果所有者已经有默认值,则它不会更改。要更改默认值,您应该更新客户以拥有新的 default_source,或更新收件人以拥有新的
所以我尝试更新default_source,但它不起作用。老实说,我对 JS 一无所知,而我只是在这里讨论一下……这段代码给了我一个错误
exports.addPaymentSource = functions.database
.ref('/stripe_customers/{userId}/sources/{pushId}/token').onWrite((change, context) => {
const source = change.after.val();
if (source === null){
return null;
}
return admin.database().ref(`/stripe_customers/${context.params.userId}/customer_id`)
.once('value').then((snapshot) => {
return snapshot.val();
}).then((customer) => {
return stripe.customers.createSource(customer, {source:source});
}).then((customer) =>{ // look here
return stripe.customers.update(customer.customer_id,{default_source: source}) // and here
}).then((response) => {
return change.after.ref.parent.set(response);
}, (error) => {
return change.after.ref.parent.child('error').set(userFacingMessage(error));
}).then(() => {
return reportError(error, {user: context.params.userId});
});
});
【问题讨论】:
标签: javascript android firebase google-cloud-functions stripe-payments