Skip to content

Instantly share code, notes, and snippets.

@rafaelpadovani
Last active October 13, 2019 15:24
Show Gist options
  • Save rafaelpadovani/280963c4167a016698e673730cbd3769 to your computer and use it in GitHub Desktop.
Save rafaelpadovani/280963c4167a016698e673730cbd3769 to your computer and use it in GitHub Desktop.
Stripe Payment Method API
export const createPaymentMethod = (number, month, year, cvc) => {
const cardDetails = {
"card[number]": number,
"card[exp_month]": month,
"card[exp_year]": year,
"card[cvc]": cvc
};
let formBody = [];
for (let property in cardDetails) {
let encodedKey = encodeURIComponent(property);
let encodedValue = encodeURIComponent(cardDetails[property]);
formBody.push(encodedKey + "=" + encodedValue);
}
formBody = formBody.join("&");
fetch('https://api.stripe.com/v1/payment_methods?type=card&' + formBody, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + secret_key
},
})
.then((response) => {
if (response.status == 200) {
return response.json();
}
})
.then((responseJson) => {
return responseJson;
})
.catch((error) => {
return error;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment