#josh-c_code

1 messages ยท Page 1 of 1 (latest)

uncut vergeBOT
#

๐Ÿ‘‹ Welcome to your new thread!

โฒ๏ธ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.

โฑ๏ธ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.

๐Ÿ”— This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1299004833965150239

๐Ÿ“ Have more to share? Add more details, code, screenshots, videos, etc. below.

Below are links to other discussions we've had with you in the past week in case you want to review that information. If your question is related to one of these previous discussions, please provide a comprehensive summary of the current state and what you need help with now. We help many users simultaneously, so a summary allows us to resolve your issue as soon as possible.

civic epoch
#

Do I need to register the reader on the server side first? The code above is running in my iOS app.

#

I am using Connect with Direct payments, if that helps

vestal spokeBOT
brave quail
#

Hi ๐Ÿ‘‹ based on your code it looks like you're using bluetooth reader. If that's the case then no, you don't need to register the reader.

civic epoch
#

Ok, that's what I thought. Why am I getting this error?

brave quail
#

Can you elaborate on what the error you're seeing is? I saw you said you're seeing CompletedWithError, but I'm not seeing what error is being thrown by the token provider.

civic epoch
#

Connecting to the reader failed because the app completed fetchConnectionToken with an error

#

Connection token is pst_test_YWNjdF8xTUx0bWxSOEV2Qk1xYjUzLGJ3RTZ0bDRXVWN1MGxVVTE2R3BIaVVCbXF3MXdKeDY_00gLPiV1Ys

brave quail
#

I see Connection Tokens being generated, are they being returned to your app? Have you tried logging out what you're getting back in the app to make sure it's the same as what is being generated when you hit our API?

civic epoch
#

Yeah, I am displaying the returned connection token and location ID on my Connect Readers screen currently

brave quail
#

Can you share your code for your connection token endpoint, as well as your client-side token provider?

civic epoch
#

Yeah, here's the token endpoint:

exports.stripeAccessRequested = functions.firestore
.document("stripeRequests/{requestId}")
.onCreate(async (snapshot, context) => {
const req = (await snapshot.ref.get()).data();
snapshot.ref.update({status: "starting"});
const user = await getUser(req.userId);
if (req.task == "getStripeConnectionToken") {
const stripe = require("stripe")(stripeKey);

    // Get the connection token
    const connectionToken = await stripe.terminal.connectionTokens.create({stripeAccount: user.stripeAccount});
    console.log("connection token", connectionToken);
    // Changed
    snapshot.ref.update({connectionToken: connectionToken, status: "success", location: stripeTerminalLocation, stripeAccount: user.stripeAccount});
  }
});
#

the location ID was created with back end code but is currently hard coded as a configuration variable. The Stripe account ID is retrieved from the user's account record in our Firestore DB.

#

The corresponding locationID and accountID are retrieved when the user authenticates to the app and stored in a useContext hook:

async function getStripeConnection(user:User) {
console.log("Submitting Stripe connection request...");
const req = {
userId: user!.uid,
task: "getStripeConnectionToken",
status: "submitted"
};
var reqRef = await addDoc(collection(db, "stripeRequests"), req);

 onSnapshot(reqRef, (snapshot) => {
   const respdata = snapshot.data();
   console.log("setting stripe key", respdata);
   if (respdata!.connectionToken) {
    setStripeKey(respdata!.connectionToken.secret);
    setLocation(respdata!.location);
    router.push("/home");
   }

}

#

const fetchTokenProvider = useCallback(async (): Promise<string> => {
console.log("fetching token", stripeKey);
if (!stripeKey) {
return '';
}
}, []);

#

const fetchTokenProvider = useCallback(async (): Promise<string> => {
console.log("fetching token", stripeKey);
if (!stripeKey) {
return '';
}
return stripeKey;
}, []);

#

oh wait, now it looks like it might be updating my reader?

brave quail
#

I'm not too familiar with firebase/firestore, but I'm not readily seeing where you're returning the Connection Token you got. If you're seeing it client-side I guess you are though.

Where in the fetchTokenProvider function are you hitting your token endpoint?

civic epoch
#

So the way Firestore works is it only fires when a document is created updated or deleted. I create a stripeRequest document and then attach an onSnapshot listener when the user logs in, and then I stash the connection token in my AppContext

#

fetchTokenProvider is then returning that stored token

brave quail
#

Hm, does fetchTokenProvider fetch a new connection token every time it's called, or does it keep returning the one you stored?

uncut vergeBOT
civic epoch
#

keeps returning the stored token

brave quail
#

That's not correct behavior, the function should get a brand new connection token every time it runs.