简体   繁体   English

Firebase 支付成功后才注册用户

[英]Firebase sign user up only after successful payment

I'm trying to implement the following flow:我正在尝试实现以下流程:

  1. User enter website用户进入网站
  2. User clicks Subscribe now用户点击Subscribe now
  3. User gets redirected to Stripe Checkout (because it's easier)用户被重定向到Stripe Checkout (因为它更容易)
  4. User successfully pays用户支付成功
  5. User now can sign in (or even better - is automatically signed in)用户现在可以登录(甚至更好 - 自动登录)
  6. User is charged every month automatically and so is updated database (to control access)每月自动向用户收费,更新数据库也是如此(以控制访问)

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:使用云函数:

  1. create a stripe customer during checkout.在结账时创建一个条纹客户。
stripe.customers.create({ email: 'user_email' }) // grab customer id
  1. create firebase user document and save the stripe customer id into 'users' collection.创建 firebase 用户文档并将条带客户 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' })
    })
  • send email to user发送 email 给用户
  • send email & password back to client.将 email 和密码发送回客户端。

On client-side:在客户端:

  1. after receiving successful message from cloud function server, let user sign in automatically从云端function服务器收到成功消息后,让用户自动登录
firebase.auth().signInWithEmailAndPassword(data.email, data.password)
  1. when user logs in grab the stripe_account from users collection and fetch stripe subscription status and show it to user.当用户登录时,从用户集合中获取stripe_account并获取条带订阅状态并将其显示给用户。

  2. 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 条纹扩展的以下设置:

  1. User fills in e-mail address and clicks submit.用户填写电子邮件地址并单击提交。

  2. signInAnonymously() is called and user e-mail adress is set with updateEmail() . signInAnonymously()并使用updateEmail()设置用户电子邮件地址。

  3. 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); } }) }
  4. CANCEL_URL can be any url CANCEL_URL 可以是任何 url

  5. SUCCESS_URL will be an url + query parameter with the e-mail SUCCESS_URL 将是一个 url + 带有电子邮件的查询参数

  6. Behind the SUCCESS_URL you'll call sendSignInLinkToEmail()在 SUCCESS_URL 后面,您将调用sendSignInLinkToEmail()

  7. 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 授权,仅供订阅用户使用。

  8. Your frontend can use an auth guard or middleware to check for the custom claim您的前端可以使用 auth guard 或中间件来检查自定义声明

  9. 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.

相关问题 在注册时存储用户名仅在页面刷新后显示 (Firebase) - Storing a username on sign up only displays after page refresh (Firebase) Firebase 注册前验证用户 - Firebase verify user before sign up 注册后如何获取currentUser的uid flutter firebase - How to get uid of currentUser after sign up in flutter firebase Firebase 使用email登录后google登录报错:密码无效或用户没有密码 - Firebase Sign-in with email after google sign-in has error : The password is invalid or the user does not have a password Android studio支付成功后如何永久禁用支付按钮 - How to permanently disable payment button after successful payment in Android studio 我们如何让一个用户使用 Firebase/Firestore 注册另一个用户? - How can we make a user sign up another user using Firebase/Firestore? 注册/使用 firebase flutter - sign up/in using firebase flutter 按下注册按钮后,即使用户名文本字段为空,email 和密码也会保存在 firebase 中 - After pressing the sign up button the email and password are saved in firebase even if the username text field was empty 注册或通过 Google 登录后,如何在 Cognito 中获取用户 session? - How to get user session in Cognito after sign up or login via Google? 注册用户并添加到 Firestore - Sign up user and add to firestore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM