Skip to content

Instantly share code, notes, and snippets.

@shanwixcode
Created June 30, 2020 06:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shanwixcode/e07c371c410630a5c7ca36b8b70284c8 to your computer and use it in GitHub Desktop.
Save shanwixcode/e07c371c410630a5c7ca36b8b70284c8 to your computer and use it in GitHub Desktop.
//stripeAPI.js
import {fetch} from 'wix-fetch';
export async function createToken(card) {
const apiKey = "PUBLIC_API_KEY";
const response = await fetch("https://api.stripe.com/v1/tokens", {
method: 'post',
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "Bearer " + apiKey
},
body: card
});
if (response.status >= 200 && response.status < 300) {
const json = await response.json();
return json.id;
}
const responseText = await response.text();
return response.status;
}
export function encodeCard(card){
let encoded = "";
for (let [k, v] of Object.entries(card)) {
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