Last active
October 13, 2019 15:24
-
-
Save rafaelpadovani/280963c4167a016698e673730cbd3769 to your computer and use it in GitHub Desktop.
Stripe Payment Method API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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