Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
freem
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Openai/69111a25-dab4-8000-8929-8d5e16a9da88
(section)
Add languages
Page
Discussion
English
Read
Edit
Edit source
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
Edit source
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Install: === <syntaxhighlight lang="bash">npm init -y npm install express body-parser stripe dotenv </syntaxhighlight> Create .env with: <syntaxhighlight>STRIPE_SECRET_KEY=sk_test_xxx PORT=3000 </syntaxhighlight> server.js: <syntaxhighlight lang="js">require('dotenv').config(); const express = require('express'); const bodyParser = require('body-parser'); const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY); const app = express(); app.use(bodyParser.json()); /** * Expect body: * { token: "<gateway token string>", amount: 1000, currency: "usd" } */ app.post('/charge', async (req, res) => { try { const { token, amount = 1000, currency = 'usd', description = 'Test Google Pay charge' } = req.body; if (!token) return res.status(400).json({error: 'missing token'}); // Some Google Pay flows deliver token as JSON string. Attempt to parse if possible let parsedToken = token; try { parsedToken = JSON.parse(token); } catch (e) { // not JSON β leave as is (stripe can accept plain token) } // Create PaymentIntent using the token as a payment method // method: use payment_method_data with card.token const pi = await stripe.paymentIntents.create({ amount, currency, payment_method_types: ['card'], payment_method_data: { type: 'card', card: { token: parsedToken } }, confirm: true, capture_method: 'automatic', description }); return res.json({ success: true, paymentIntent: pi.id, status: pi.status }); } catch (err) { console.error(err); const message = err && err.raw && err.raw.message ? err.raw.message : err.message; return res.status(500).json({ error: message }); } }); app.listen(process.env.PORT || 3000, () => console.log('Server running on port', process.env.PORT || 3000)); </syntaxhighlight> Warnings & tips: * In test mode, use your Stripe test secret key. Confirm on Stripe dashboard. * Different Google Pay flows may wrap the token into a JSON object (hence the JSON.parse attempt). * If you get a payment_method or card parsing error, inspect the token structure in logs and adapt creation accordingly. * For production you should create a PaymentIntent on the server first, then confirm on client with stripe.confirmCardPayment using the token; the example here is a simple server-side confirm for test.
Summary:
Please note that all contributions to freem are considered to be released under the Creative Commons Attribution-ShareAlike 4.0 (see
Freem:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)