【发布时间】:2020-08-16 06:32:40
【问题描述】:
底部更新
我正在尝试在我的 Android 应用中构建一个注册页面,让用户通过 Stripe 注册订阅。我坚持的是通过云功能从 Android 添加支付源,并从 Stripe 接收令牌。
我目前已经解决了,自动将新创建的用户添加到 Stripe。在写入或更改 (/users/{userId}/membership/token) 时创建订阅。
在 Android 上,我可以通过输入获取信用卡数据..
PaymentMethodCreateParams.Card card = cardInputWidget.getPaymentMethodCard();
接下来我需要使用.. 将其提交到我的云功能。
mFunctions = FirebaseFunctions.getInstance();
mFunctions.getHttpsCallable("addPaymentSource")
.call()
.addOnCompleteListener(task -> {
...
由于我无法找到这方面的信息,这就是我对这个云功能 (Javascript) 的全部了解
exports.addPaymentSource = functions.https.onCall((data, context) =>{
const pm = await stripe.paymentMethods.attach('pm_678', {customer: 'cus_123'});
return admin.firestore().collection('users').doc(user.uid).get('membership').set({token: token});
}
我需要获取保存在 - /users/{user.uid}/customerId' 的客户编号。以及通过我的 http 数据调用传递付款方式,并传递/获取 user_id(在这之前很久就已经创建了)。
到目前为止,我观看了这个 youtube 视频并转换了我的代码。 Subscription Payments with Stripe, Angular, and Firebase
我还大量引用了 Stripe 的 Cloud Function 示例。一个问题是每个人似乎都在使用这段代码(如下),这在我的实现中不起作用。大多数指南/示例未用于订阅。
// Add a payment source (card) for a user by writing a stripe payment source token to Cloud Firestore
exports.addPaymentSource = functions.firestore.document('/stripe_customers/{userId}/tokens/{pushId}').onCreate(async (snap, context) => {
const source = snap.data();
const token = source.token;
if (source === null){
return null;
}
try {
const snapshot = await admin.firestore().collection('stripe_customers').doc(context.params.userId).get();
const customer = snapshot.data().customer_id;
const response = await stripe.customers.createSource(customer, {source: token});
return admin.firestore().collection('stripe_customers').doc(context.params.userId).collection("sources").doc(response.fingerprint).set(response, {merge: true});
} catch (error) {
await snap.ref.set({'error':userFacingMessage(error)},{merge:true});
return reportError(error, {user: context.params.userId});
}
});
更新:
做了一些小的改动来尝试让它工作..
exports.addPaymentSource = functions.https.onCall((data, context) =>{
///users/{userId}/membership/token
// Create Payment Method
const paymentMethod = stripe.paymentMethods.create(
{
type: 'card',
card: {
number: '4242424242424242',
exp_month: 5,
exp_year: 2021,
cvc: '314',
},
}).then(pm => {
console.log('paymentMethod: ', paymentMethod.id);
return stripe.paymentMethods.attach(paymentMethod.id, { customer: 'cus_HCQNxmI5CSlIV5' })
.then(pm => {
return admin.firestore().collection('users').doc(user.uid).get('membership').set({token: pm.id});
});
});
});
我快接近了,问题是 paymentMethod.id 是 'undefined'
【问题讨论】:
-
你的回调有
pm作为参数但是你正在访问一个名为paymentMethod的变量,你应该将(pm => {更改为(paymentMethod => { -
另外,您不应该在 Firestore 函数中对 PaymentMethod 进行标记化,标记化应该在您的移动应用上进行
标签: android firebase google-cloud-firestore google-cloud-functions stripe-payments