Skip to content

Instantly share code, notes, and snippets.

@shanwixcode
Last active June 28, 2020 05:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shanwixcode/95432c1e608efc1603144ec35a61dbd5 to your computer and use it in GitHub Desktop.
Save shanwixcode/95432c1e608efc1603144ec35a61dbd5 to your computer and use it in GitHub Desktop.
Stripe Tutorial - Dude Lemon
//lightbox code
import wixWindow from 'wix-window';
import wixLocation from 'wix-location';
import {stripeToken} from 'public/stripe.js';
import wixData from 'wix-data';
import wixUsers from 'wix-users';
import {createCustomer, createSubscription} from 'backend/stripeProcessor.jsw';
$w.onReady(function () {
let pmtdata = wixWindow.lightbox.getContext();
planId = pmtdata.planId;
currency = pmtdata.currency;
});
var currency;
var planId;
//-------------------------------------------------------Subscription button click-------------------------------------------------------//
export function subscribe_click(event) {
createCardToken();
}
//-------------------------------------------------------Encode & Create Card Token-------------------------------------------------------//
function createCardToken() {
$w("#processorText").text = 'Validating your card details';
let nombre = $w("#cardName").value;
let card = $w('#cardNumber').value;
let mm = $w("#mm").value;
let yyyy = $w("#yyyy").value;
let cvc = $w("#cvc").value;
let line1 = $w("#street").value;
let city = $w("#city").value;
let state = $w("#state").value;
let zip = $w("#postal").value;
let country = $w("#country").value;
createToken(nombre, card, mm, yyyy, cvc, line1, city, state, zip, country)
.then( (response) => {
if(response.id !== undefined) {
let token = response.id;
let lastFour = response.card.last4;
let brand = response.card.brand;
customerCreation(token, lastFour, brand);
} else {
//handle error
}
});
}
function createToken(nombre, card, mm, yyyy, cvc, line1, city, state, zip, country) {
let cardObject = {
"name": nombre,
"number": card,
"exp_month": mm,
"exp_year": yyyy,
"cvc": cvc,
"address_line1": line1,
"address_city": city,
"address_state": state,
"address_zip": zip,
"address_country": country
};
return stripeToken(cardObject)
.then( (response) => {
return response;
});
}
//-----------------------------------------------------------------Create Customer-----------------------------------------------------------------//
function customerCreation(token, lastFour, brand) {
let emailId = $w("#email").value;
createCustomer(token, emailId)
.then( (response) => {
if(response.id !== undefined) {
let cus = response.id;
subscription(cus, lastFour, brand);
} else {
//handle error
}
});
}
//-----------------------------------------------------------------Subscribe-----------------------------------------------------------------//
async function subscription(cus, lastFour, brand) {
let plan = [];
let data = {
plan: planId,
quantity: 1
};
await plan.push(data);
createSubscription(cus, plan)
.then( (response) => {
if(response.id !== undefined && response.status === 'active') {
let subStatus = 'ACTIVE';
signUpMember(response, cus, lastFour, brand, subStatus);
} else if(response.id !== undefined && response.status !== 'active') {
let subStatus = 'PENDING';
signUpMember(response, cus, lastFour, brand, subStatus);
} else {
//handle error
}
});
}
//--------------------------------------------Sign up member--------------------------------------------//
async function signUpMember(response, cus, lastFour, brand, subStatus) {
let array = [];
let phone = $w("#phone").value;
await array.push(phone);
wixUsers.register($w("#email").value, $w("#password").value, {
"contactInfo": {
"firstName": $w("#fName").value,
"lastName": $w("#lName").value,
"phones": array,
"country": $w("#country").value
}
})
.then( (result) => {
confirmInsertSuccess(response, cus, lastFour, brand, subStatus);
})
.catch( (err) => {
//handle error
});
}
//-----------------------------------------------------------Continue With Sign Up-------------------------------------------------------//
function confirmInsertSuccess(response, cus, lastFour, brand, subStatus) {
let data = {
firstName: $w("#fName").value,
lastName: $w("#lName").value,
email: newEmail,
customerId: cus,
subscriptionId: response.id,
lastFour: String(lastFour),
cardType: brand,
subscriptionStatus: subStatus
};
wixData.insert('payingMembers', data)
.then( (result) => {
let item = result;
wixLocation.to(`/my-dashboard/${item._id}`); //redirect to member page (dynamic)
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment