簡體   English   中英

查詢 Firestore 以檢查集合和文檔

[英]query to firestore to check collection and document

我對 firebase / firestore 有點陌生。 我正在使用條紋 api。

一旦用戶在預建的條帶結帳頁面上點擊start trial ,那么它應該 go 到 firestore 並創建一個名為subscriptions的新集合,其中包含所有用戶信息。 似乎正在這樣做,但是,我創建了一個名為successPage的頁面,它基本上會檢查以確保它創建了它。

請找到以下代碼:

 const successPage = props => { 

firebase.auth().onAuthStateChanged((user) => {
    if(user) {
      console.log("calling success page : " + user.uid)


//checking if user is paying for subscription
firestore.collection('customers').doc(user.uid).collection('subscriptions')
.where('status', 'in', ['trialing', 'active']).get()
.then(activeSubscriptions => {
  // if this is true, the user has no active subscription.
  if (activeSubscriptions.empty === true) {
    console.log(user.uid)
    firestore.collection('customers').doc(user.uid)
    .get().then(
    doc => {
      if (doc.exists) {
        firestore.collection('customers').doc(user.uid).collection('subscriptions').get().
          then(sub => {
            if (sub.docs.length > 0) {
              var activityStatus = "canceled"
              createCheckoutSession(activityStatus)
              console.log('subcollection exists');
            } else {
              alert("Your account has been created, but your payment details we're not successfully created. You will now be redirected to the checkout page")
                createCheckoutSession()
                console.log(user.uid)
                console.log("does not exist!")
            }
          });
      }
    });
  } else if (activeSubscriptions.size > 1){
    alert("you have more then one active subscription. please manage your subscriptions and cancel one of your subscriptions to access the application")
  } else {
    firestore.collection("profiledata").doc(user.uid).update({
      accountStatus: "active"
    }).then (() => {
      
      
      firestore
      .collection("roger@x.ca")
      .add({
        to: user.email,
        message: {
          
        },
      })
      .then(() => console.log("email out for delivery!"));

    props.history.push('/clients')
    
    })
    
  }
});
}

  })

  return (

     <input type="hidden"></input>
  )
}

它檢查訂閱集合,其中 status = 為試用或活動,然后檢查訂閱內的所有內容以查看發生了什么,但由於某種原因,即使訂閱集合有,它仍會繼續重定向到條帶頁面(createCheckoutSession)被創建。 這是時間問題嗎?

當創建新訂閱並在 Firestore 中創建文檔時,Stripe 會觸發您的服務器/雲功能的 Webhook。 此過程可能需要一些時間,同時您的用戶可能已被重定向到成功頁面。 如果尚未創建文檔,則您將無法顯示交易狀態。

這是您可以執行的解決方法:

  1. 在您的服務器上創建 Stripe Checkout session 時,您實際上可以創建訂閱文檔,但將名為“stripe_response”的字段設置為 false,並將新的訂閱文檔 ID 作為查詢參數添加到 stripe success_url。 所以你 url 可能看起來像: https://domain.ext/paymentSuccess?id=<that_document_id>
  2. 現在,當用戶在成功頁面上時,查找具有查詢參數中提到的 ID 的特定訂閱文檔。 如果“stripe_response”仍然是假的,那么可能 webhook 還沒有完成它的工作。 只需在 5 秒后重試請求,然后向用戶顯示狀態。 確保在 webhook 中將stripe_response設置為 true。

只需第 2 步,您只需在文檔上附加一個實時偵聽器,這樣當狀態更新時,您會盡快獲得響應,而無需依賴輪詢。

來到“不斷重定向”部分,您能否具體說明一下具體的重定向情況? 它在哪里重定向以及所有這些?

這絕對是一種競爭條件,但如果您按照上述步驟操作,它應該會處理好它。 如果您需要更多幫助來解決此問題,請告訴我。

暫無
暫無

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

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