Skip to content

Instantly share code, notes, and snippets.

@shanwixcode
Created January 20, 2020 18:22
Show Gist options
  • Save shanwixcode/931f5d42f1352fa79258951aa21fef1c to your computer and use it in GitHub Desktop.
Save shanwixcode/931f5d42f1352fa79258951aa21fef1c to your computer and use it in GitHub Desktop.
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