Stripe Tutorial - Dude Lemon
//stripe.js | |
import {fetch} from 'wix-fetch'; | |
const apiKey = "pk_test_xxxxxx"; //Stripe Public API Key | |
export async function stripeToken(cardObject) { | |
let values = encodeCard(cardObject); | |
const response = await fetch("https://api.stripe.com/v1/tokens", { | |
method: 'post', | |
headers: { | |
"Content-Type": "application/x-www-form-urlencoded", | |
"Authorization": "Bearer " + apiKey | |
}, | |
body: values | |
}); | |
if (response.status >= 200 && response.status < 300) { | |
const json = await response.json(); | |
return json; | |
} | |
const json = await response.json(); | |
let sendBack = { | |
message: json.error.message, | |
statusCode: json.error.type | |
}; | |
return sendBack; | |
} | |
function encodeCard(cardObject) { | |
let encoded = ""; | |
for (let [k, v] of Object.entries(cardObject)) { | |
encoded = encoded.concat("card[", k, "]=", encodeURI(v), "&"); | |
} | |
return encoded.substr(0, encoded.length - 1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment