Skip to content

Instantly share code, notes, and snippets.

@shanwixcode
Created June 30, 2020 06:38
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
import {createToken, encodeCard} from "public/stripeAPI.js";
import {subscription, createCustomer} from "backend/stripe";
import wixLocation from 'wix-location';
$w.onReady(function () {
});
var items = []; //your plan ID will go here (plan_XXXXXXXXXXXXXX)
var customer;
function payNow() {
var accepted = false;
createToken(encodeCard(createCard())) //we start the process by encoding the card and creating a token
.then((token) => {
createCustomer(customer) //create a new customer
.then((id) => {
let item = {
"customer": id, //this is your customer ID
"items[0][plan]": items //this is your plan id
};
subscription(token, item) //subscribe
.then((response) => {
if (response.chargeId !== undefined) {
accepted = true;
wixLocation.to('/success'); //redirect on success
} else {
$w("#error").text = response.error; //show error message on failure
}
});
});
});
}
function createCard() {
return {
"address_city": $w("#city").value,
"address_country": $w("#country").value,
"address_state": $w("#state").value,
"address_zip": $w("#zip").value,
"address_line1": $w("#line1").value,
"address_line2": $w("#line2").value,
"name": $w("#name").value,
"number": $w("#card").value,
"cvc": $w("#cvv").value,
"exp_year": $w("#yyyy").value,
"exp_month": $w("#mm").value
};
}
export function pay_click(event) {
payNow(); //start payment (you will need a button)
}
export function email_change(event) {
customer = $w("#email").value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment