How to implement Stripe payment in Firestore without using firebase.auth
P粉731861241
2023-09-01 22:10:29
<p>I'm trying to use Stripe payments in my node.js app to sell products to users on whatsapp. </p>
<p>I installed the stripe extension: stripe/firestore-stripe-payments@0.3.3 and followed the instructions in the documentation: </p>
<pre class="brush:php;toolbar:false;">const docRef = await db
.collection('customers')
.doc(currentUser.uid)
.collection('checkout_sessions')
.add({
price: 'price_1GqIC8HYgolSxxxxxxxxx',
success_url: window.location.origin,
cancel_url: window.location.origin,
});
// Wait for the extension to attach CheckoutSession
docRef.onSnapshot((snap) => {
const { error, url } = snap.data();
if (error) {
// Show the error to the client and check the cloud function logs in the Firebase console.
alert(`An error occurred: ${error.message}`);
}
if (url) {
// We have a Stripe Checkout URL, let's redirect.
window.location.assign(url);
}
});</pre>
<p>I receive the following error in the cloud console: </p>
<p><code>❗️[Error]: Failed to create a Checkout session for document [maxxxxxxxxxA]: The provided identifier does not have a corresponding user record. </code></p>
<p>I found that I need to use the userId from Firebase Authentication, but since I'm running my service in a function (backend server), there is no client/user in the auth. </p>
<p>Is there another way to create (individual) stripe checkout links? Is there a way to use Firebase Authentication without a client? </p>
<p>Thanks in advance. </p>
<p>I tried using auth,</p>
<pre class="brush:php;toolbar:false;">import { getAuth } from "firebase/auth";
const auth = getAuth();
const user = auth.currentUser;</pre>
<p>But I get the following error: <code>FirebaseError: Firebase: Options need to be provided when not deploying to hosting via source. (app/no-options)</code></p>
<p>I tried this: https://stackoverflow.com/a/74732162/21612440</p>
<p>and got<code>TypeError: Cannot read property 'getProvider' of undefined (read 'getProvider')</code></p>
Cloud functions (in principle) run without the context of the currently authenticated user. Therefore, in cloud functions,
auth.currentUser
is almost meaningless since each request may come from a different user.However, you can pass any UID you want to the Firestore database here
.doc(currentUser.uid)
. So if you can determine the relevant UID in some other (safe) way, you can pass that value and the Stripe extension will handle it for that user.