if the payment was successful you want to either send an email or present a message in the browser to the user?
Yes Shlomi. This is exactly what i want to do. I would prefer showing a message or payment summary.
Below I am sharing with you my codes.
PAGE CODE
//paymentAPI
import {createToken, encodeCard} from "public/stripeAPI.js";
import {charge} from 'backend/stripeProxy';
var payment;
export function payNow(event) {
createToken(encodeCard(createCard()))
.then((token) => {
console.log("Card token: " + token);
charge(token, payment)
.then((chargeResponse) => {
console.log("Charge ID: " + chargeResponse.id);
});
});
}
function createCard() {
return {
"name": $w("#input1").value,
"number": $w("#input4").value,
"cvc": $w("#input5").value,
"exp_year": $w("#input3").value,
"exp_month": $w("#input2").value
};
}
function changeState() {
payment = {
"amount": ($w("#input8").value * 100),
"currency": "EUR",
"description": $w("#input9").value
};
}
export function button1_click(event, $w) {
payNow();
}
export function input8_change(event, $w) {
//Add your code for this event here:
changeState();
}
export function input9_change(event, $w) {
//Add your code for this event here:
changeState();
}
BACKEND FILE
//stripeProxy.jsw
import {fetch} from 'wix-fetch';
export async function charge(token,payment) {
const cart = payment;
const apiKey = "sk_xxxxxxxxxxxxxxxxxxxx";
const response = await fetch("https://api.stripe.com/v1/charges", {
method: 'post',
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "Bearer " + apiKey
},
body: encodeBody(token, cart)
});
if (response.status >= 200 && response.status < 300) {
return await response.json();
}
return await response.text();
}
function encodeBody(token, cart){
let encoded = "";
for (let [k, v] of Object.entries(cart)) {
encoded = encoded.concat(k,"=", encodeURI(v), "&");
}
encoded = encoded.concat("source=", encodeURI(token));
return encoded;
}
PUBLIC
//stripeAPI.js
import {fetch} from 'wix-fetch';
export async function createToken(card) {
const apiKey = "pk_test_xxxxxxxxxxxxxxx";
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();
console.log(responseText);
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);
}
Thank you for taking out time to help me