簡體   English   中英

Firebase 支付成功后才注冊用戶

[英]Firebase sign user up only after successful payment

我正在嘗試實現以下流程:

  1. 用戶進入網站
  2. 用戶點擊Subscribe now
  3. 用戶被重定向到Stripe Checkout (因為它更容易)
  4. 用戶支付成功
  5. 用戶現在可以登錄(甚至更好 - 自動登錄)
  6. 每月自動向用戶收費,更新數據庫也是如此(以控制訪問)

長話短說:只有在用戶付費時才授予他訪問權限。

我為此使用 Firebase,但似乎有很多障礙/頭痛:

我用Firebase的時候到處找web關於訂閱model,幾乎沒有。

我的問題是:任何人都使用Stripe實現了訂閱服務(任何,不必是 Checkout - 流程是一樣的)和Firebase 或者我應該為我自己的服務器使用 go 而僅將 Firebase 用於數據庫?

當用戶必須先注冊然后付款(太多摩擦)時,我真的不覺得它是一個好的 UX。

使用雲函數:

  1. 在結賬時創建一個條紋客戶。
stripe.customers.create({ email: 'user_email' }) // grab customer id
  1. 創建 firebase 用戶文檔並將條帶客戶 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' })
    })
  • 發送 email 給用戶
  • 將 email 和密碼發送回客戶端。

在客戶端:

  1. 從雲端function服務器收到成功消息后,讓用戶自動登錄
firebase.auth().signInWithEmailAndPassword(data.email, data.password)
  1. 當用戶登錄時,從用戶集合中獲取stripe_account並獲取條帶訂閱狀態並將其顯示給用戶。

  2. 要求用戶驗證其 email 或更改密碼。 禁用所有編輯,直到用戶驗證其 email 或更改其密碼。

我正在考慮使用 firebase 條紋擴展的以下設置:

  1. 用戶填寫電子郵件地址並單擊提交。

  2. signInAnonymously()並使用updateEmail()設置用戶電子郵件地址。

  3. 然后創建一個附加 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 可以是任何 url

  5. SUCCESS_URL 將是一個 url + 帶有電子郵件的查詢參數

  6. 在 SUCCESS_URL 后面,您將調用sendSignInLinkToEmail()

  7. 在 stripe 中,您可以在訂閱中設置自定義聲明,例如: stripeRole equals premium 此聲明將添加到 firebase 授權,僅供訂閱用戶使用。

  8. 您的前端可以使用 auth guard 或中間件來檢查自定義聲明

  9. 如果您使用 firestore 為高級會員提供內容,您應該使用如下安全規則: request.auth.token.stripeRole == "premium";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM