· 9 years ago · Dec 28, 2016, 02:46 PM
1var cardDetails = {
2 "card[number]": '4242424242424242',
3 "card[exp_month]": '09',
4 "card[exp_year]": '2019',
5 "card[cvc]": '303'
6 };
7
8 var formBody = [];
9 for (var property in cardDetails) {
10 var encodedKey = encodeURIComponent(property);
11 var encodedValue = encodeURIComponent(cardDetails[property]);
12 formBody.push(encodedKey + "=" + encodedValue);
13 }
14 formBody = formBody.join("&");
15
16 let response = await fetch(stripe_url + 'tokens', {
17 method: 'post',
18 headers: {
19 'Accept': 'application/json',
20 'Content-Type': 'application/x-www-form-urlencoded',
21 'Authorization': 'Bearer ' + secret_key
22 },
23 body: formBody
24 }).then(function (response) {
25 if (response.ok) {
26 console.log("response")
27 console.log(response)
28 } else {
29 console.log('Network response was not ok.');
30 }
31 })
32 .catch(function (error) {
33 console.log('There has been a problem with your fetch operation: ' + error.message);
34 });