In rails app I have Stripe API integration. On frontend part stripe.js
library is imported and used to save/delete customer payment methods:
const stripe_key = 'pk_test_xxx';
const stripe = Stripe(stripe_key);
var elements = stripe.elements();
const paymentMethod = elements.create("card", { hidePostalCode: true })
When user enter valid card data I generate token which is send to rails backend and forward to Stripe API in order to save it as customers payment method:
document.getElementById('submitBtn').addEventListener('click', async function(event) {
event.preventDefault();
const { token, error } = await stripe.createToken(paymentMethod);
console.log(token, error)
fetch('http://localhost:3000/api/v2/payment_methods', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${bearerToken}`
},
body: JSON.stringify({
token: token.id,
is_default_payment_method: document.getElementById('set_as_default').checked
})
}).then(function(response) {
if (response.ok) {
// Payment method saved successfully
} else {
// Failed to save payment method
}
}).catch(function(error) {
console.error('Error:', error);
});
});
Now I wonder is possible to add payment methods like paypal or google pay. Can’t find those types in documentation https://docs.stripe.com/js/elements_object/create_element?type=card