Random Coupon Generator

Hello,

I was wondering if anyone could help with a ‘Random Coupon Generator’ Which would active and send a 15% off coupon to the subscriber via email attached with a message.

My way might not be the best way. I use emailjs to send emails and stripe to charge, so my code will only work for you if you use these two services. But the general idea is to randomly generate a code using tokenize function and save the code in local storage, and send an email to the user with a link that has the token in it. When the user clicks the link I sent to him, I can tell that the user is using my coupon and I will give him the discount. Or you can also skip the link and just send the token to him, this is the code I originally used to verify the user’s email. The send Email function is using emiljs’s API.

$w.onReady(async function () {
 //TODO: write your page related code here...
 const token = await tokenize();
 const sendEmailRes = await send_random_coupon(token);
});
export function send_random_coupon(token) {
    local.setItem("key", token);
 let myprovider = "gmail";

 let mytemplate = "inquiry_form"; //use her the template name you defined in emailJS

 let myparams = {

 "from_email": "***@gmail.com",

 "to_email": email,

 "link": `http://mysite.com/approve?token=${token}`,

 "token": token,

 "replyto_email": "***@gmail.com",

 "bcc_email": "***@gmail.com",

    };

    sendEmail(myprovider, mytemplate, myparams)
}
export function tokenize() {
 let token = rand() + rand(); // to make it longer
 return (token)
}
var rand = function () {
 return Math.random().toString(36).substr(2); // remove `0.`
};


//the http://mysite.com/approve page

import wixLocation from 'wix-location';
import {local} from 'wix-storage';
import wixData from 'wix-data';
$w.onReady(async function () {
 //TODO: write your page related code here...
// let token = wixLocation.query.token;
 //console.log(token)
 let value = local.getItem("key"); // "value"
    console.log(wixLocation.query.token)
    console.log(value)
 let token = wixLocation.query.token
 if (value === token)
    {
       console.log("approved");
       //you can make the price *85% here
    }
 else {
        console.log("not approved");
    }
});

Is this still a manual method or is this completely automated?

@support73796 automated

@lichade2016 Perfect! I will look into this :slight_smile: Thankyou so much!