#oguzbilir-Elements
1 messages · Page 1 of 1 (latest)
I see this 🙂 https://labtors.com/payment
Labtors is a Marketplace for researchers and scientists. We are changing the way of how research project is done and giving everyone in the world a chance to access precious test labs.
Uncaught (in promise) SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://www.googleapis.com/identitytoolkit/v3/relyingparty/getAccountInfo?key=AIzaSyBfQPgPpZUJ5DHYRiFcwv9Q7LZutk4na1E. (Reason: CORS request did not succeed). Status code: (null).
Uncaught (in promise) IntegrationError: Invalid value for elements(): clientSecret should be a client secret of the form ${id}secret${secret}. You specified: .
These are the errors ı see in consolle.
Okay so let's start with the client secret... how are you retrieving that from your server?
AS in below; const [clientSecret, setClientSecret] = useState("");
useEffect(() => {
// Create PaymentIntent as soon as the page loads
fetch("/create-payment-intent", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ items: [{ id: "xl-tshirt" }] }),
})
.then((res) => res.json())
.then((data) => setClientSecret(data.clientSecret));
}, []);
const appearance = {
theme: 'stripe',
};
const options = {
clientSecret,
appearance
};
It doesnt log. I tried an alert function, that doesnt work either. I think fetching is the problem
Yep sounds like it
So next step is to add logs on your server
And find out if you are actually hitting the endpoint at all
Are you testing locally?
Problem is I am not a backend developer 🙂 So, the entire server code in below
const functions = require("firebase-functions");
const express = require("express");
const cors = require("cors");
const stripe = require("stripe")('sk_test_51KVrgDFxgzzZ1Krm30dEiiQOlPS7QSmNQw4uvZ6510fthbvCTz1qzaAP1GUPZeHNaTxAWr0xIdDhZbeYYIQSWOOX00ITXHqZRk');
// API
// App config
const app =express();
// Middleware
app.use(express.static("public"));
app.use(express.json());
const calculateOrderAmount = (items) => {
// Replace this constant with a calculation of the order's amount
// Calculate the order total on the server to prevent
// people from directly manipulating the amount on the client
return 1400;
};
app.post("/create-payment-intent", async (req, res) => {
const { items } = req.body;
// Create a PaymentIntent with the order amount and currency
const paymentIntent = await stripe.paymentIntents.create({
amount: calculateOrderAmount(items),
currency: "usd",
automatic_payment_methods: {
enabled: true,
},
});
res.send({
clientSecret: paymentIntent.client_secret,
});
});
// Listen command
exports.api = functions.https.onRequest(app);
Please edit that to remove your secret key
And then you should roll your API key
Even exposing your testmode secret key can allow other folks to gain access to your account
What directory is your server file in? Is it in the public directory?
It is in the functions, I am using firebase functions
Ah okay yeah I'm not familiar with firebase. You are using Express above though?
yep
Ah okay didn't actually realize you could use these together
Can you try changing public to .
Hmmm https://firebase.google.com/docs/hosting/functions might describe what you need to do actually
Specifically this part: https://firebase.google.com/docs/hosting/functions#use_a_web_framework
I will try a few things and then we can talk about it. Normally I use the card element without problem, I send a post request and instantly get the cliend_secret. However, this one need client_secret before a post request.
Yes with Payment Element you need client secret to render the Element
However, accessing the client secret should be no different than however you were doing this previously.
Just need it on load instead of on pay
Exactly, I will try to make it async function and employ axios.
Quick question: can ı use payment element as embedded in in page just like card element ? Or is it have to be a full page
No you can embed it just like card element
You just need a div to mount it
Oh you are using React
Same idea
Just a component
And you can build around it
We will just insert an iframe just like card Element
Ok, thanks a lot.