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!
=== Replace YOUR_STRIPE_PUBLISHABLE_KEY with your Stripe publishable key (test). Serve this on HTTPS (required by Google Pay in production; for local dev use localhost and http is OK for testing). === <syntaxhighlight lang="html"><!-- index.html --> <!doctype html> <html> <head> <meta charset="utf-8" /> <title>Google Pay + Stripe Test</title> <script async src="https://pay.google.com/gp/p/js/pay.js"></script> <script src="https://js.stripe.com/v3/"></script> </head> <body> <button id="googlePayButton">Pay with Google Pay (TEST)</button> <script> const stripePublishableKey = "YOUR_STRIPE_PUBLISHABLE_KEY"; // test publishable key const stripe = Stripe(stripePublishableKey); const paymentsClient = new google.payments.api.PaymentsClient({environment: 'TEST'}); // PaymentDataRequest (gateway = "stripe") const baseRequest = { apiVersion: 2, apiVersionMinor: 0 }; const allowedCardNetworks = ["VISA","MASTERCARD","AMEX","DISCOVER"]; const allowedCardAuthMethods = ["PAN_ONLY","CRYPTOGRAM_3DS"]; // Tokenization spec for stripe gateway in test const tokenizationSpecification = { type: 'PAYMENT_GATEWAY', parameters: { gateway: 'stripe', 'stripe:publishableKey': stripePublishableKey, 'stripe:version': '2020-08-27' // set a supported stripe API version } }; const cardPaymentMethod = { type: 'CARD', parameters: { allowedAuthMethods: allowedCardAuthMethods, allowedCardNetworks: allowedCardNetworks }, tokenizationSpecification: tokenizationSpecification }; const paymentDataRequest = Object.assign({}, baseRequest, { allowedPaymentMethods: [cardPaymentMethod], transactionInfo: { totalPriceStatus: 'FINAL', totalPrice: '10.00', currencyCode: 'USD' }, merchantInfo: { merchantName: 'Example, Inc.' }, emailRequired: true }); document.getElementById('googlePayButton').addEventListener('click', async () => { try { const paymentData = await paymentsClient.loadPaymentData(paymentDataRequest); // paymentData contains paymentMethodData.tokenizationData.token (gateway token) const token = paymentData.paymentMethodData.tokenizationData.token; // Send token to your server to create/confirm a Stripe PaymentIntent const res = await fetch('/charge', { method: 'POST', headers: { 'Content-Type': 'application/json'}, body: JSON.stringify({ token: token, amount: 1000, currency: 'usd', description: 'Test charge via Google Pay (stripe)' }) }); const json = await res.json(); if (res.ok) { alert('Payment success: ' + JSON.stringify(json)); } else { alert('Payment failed: ' + JSON.stringify(json)); } } catch (err) { console.error('Google Pay failed', err); alert('Google Pay cancelled or failed: ' + err.statusMessage || err); } }); </script> </body> </html> </syntaxhighlight> Notes: * environment: 'TEST' forces use of Google test cards. Use only for integration verification. * The response's token will be a Stripe-compatible gateway token if your tokenization spec is correct. * Save this file as index.html, serve it, click the button to get the Google Pay flow in the browser.
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)