[英]Firebase sign user up only after successful payment
I'm trying to implement the following flow:我正在尝试实现以下流程:
Subscribe now
Subscribe now
Stripe Checkout
(because it's easier)Stripe Checkout
(因为它更容易) Long story short: give user an access only when he paid.长话短说:只有在用户付费时才授予他访问权限。
I'm using Firebase for that, but it seems like there are many roadblocks/headaches:我为此使用 Firebase,但似乎有很多障碍/头痛:
I looked all around the web about subscription model while using Firebase, but there are almost none.我用Firebase的时候到处找web关于订阅model,几乎没有。
My question is: anyone implemented subscription service with Stripe (any, doesn't have to be Checkout - flow is the same) and Firebase ?我的问题是:任何人都使用Stripe实现了订阅服务(任何,不必是 Checkout - 流程是一样的)和Firebase ? Or should I go for my own server and use Firebase only for database?
或者我应该为我自己的服务器使用 go 而仅将 Firebase 用于数据库?
I really don't find it as a good UX when user has to first sign up and then pay (too much friction).当用户必须先注册然后付款(太多摩擦)时,我真的不觉得它是一个好的 UX。
Using cloud functions:使用云函数:
stripe.customers.create({ email: 'user_email' }) // grab customer id
(use userRecord.uid
id into doc()
id) (将
userRecord.uid
id 用于doc()
id)
firebaseAdmin.auth().createUser({
email: 'email',
password: 'temporary_password',
displayName: 'fullname'
})
.then((userRecord) => {
return firebaseAdminSDK.firestore()
.collection('users').doc(userRecord.uid)
.set({ stripe_account: 'cus_HILXPWljT9fKRA' })
})
On client-side:在客户端:
firebase.auth().signInWithEmailAndPassword(data.email, data.password)
when user logs in grab the stripe_account
from users collection and fetch stripe subscription status and show it to user.当用户登录时,从用户集合中获取
stripe_account
并获取条带订阅状态并将其显示给用户。
ask user verify its email or change password.要求用户验证其 email 或更改密码。 disable all editing until user verifies its email or changes its password.
禁用所有编辑,直到用户验证其 email 或更改其密码。
I am thinking about the following setup using the firebase Stripe extension:我正在考虑使用 firebase 条纹扩展的以下设置:
User fills in e-mail address and clicks submit.用户填写电子邮件地址并单击提交。
signInAnonymously()
is called and user e-mail adress is set with updateEmail()
. signInAnonymously()
并使用updateEmail()
设置用户电子邮件地址。
Then a checkout sessions is created with the uid attached:然后创建一个附加 uid 的结帐会话:
async stripeCheckout({ state }, payload) { const uid = state.authUser.uid const ref = await this.$fire.firestore.collection('users').doc(uid).collection('checkout_sessions').add({ price: payload.price, success_url: SUCCES_URL, cancel_url: CANCEL_URL, }); // Wait for the CheckoutSession to get attached by the extension ref.onSnapshot((snap) => { const { error, url } = snap.data(); if (error) { // Show an error to your customer and // inspect your Cloud Function logs in the Firebase console. alert(`An error occured: ${error.message}`); } if (url) { // We have a Stripe Checkout URL, let's redirect. window.location.assign(url); } }) }
CANCEL_URL can be any url CANCEL_URL 可以是任何 url
SUCCESS_URL will be an url + query parameter with the e-mail SUCCESS_URL 将是一个 url + 带有电子邮件的查询参数
Behind the SUCCESS_URL you'll call sendSignInLinkToEmail()
在 SUCCESS_URL 后面,您将调用
sendSignInLinkToEmail()
In stripe you set a custom claim in your subscription, for example: stripeRole
equals premium
.在 stripe 中,您可以在订阅中设置自定义声明,例如:
stripeRole
equals premium
。 This claim will be added to firebase auth for subscribed users only.此声明将添加到 firebase 授权,仅供订阅用户使用。
Your frontend can use an auth guard or middleware to check for the custom claim您的前端可以使用 auth guard 或中间件来检查自定义声明
If you use firestore to serve content to premium members, you should use security rules like: request.auth.token.stripeRole == "premium";
如果您使用 firestore 为高级会员提供内容,您应该使用如下安全规则:
request.auth.token.stripeRole == "premium";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.