Skip to content

Instantly share code, notes, and snippets.

@morkhorwaad
Last active June 24, 2019 21:33
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 morkhorwaad/4596b05d02a130069deed76496db8c0f to your computer and use it in GitHub Desktop.
Save morkhorwaad/4596b05d02a130069deed76496db8c0f to your computer and use it in GitHub Desktop.
Wix IPN Listener
import {created, serverError, ok} from 'wix-http-functions';
import { fetch } from 'wix-fetch';
import wixCrm from 'wix-crm-backend';
import { parse } from 'querystring';
// HTTP POST - IPN Handler
// Listens for PayPayl's payment notifications and hands off the data to be verified before processing
// Right now, processing == sending a thank you email to the person who paid
export async function post_IPNHandler(request) {
// get the text of the body, fire off the async func, return 200
logDebugMessage("In the IPN handler!");
let text = await request.body.text();
// post_IPNHandler has to return OK before the rest of the requests go through
// so - set it going asynchronously and return OK
setTimeout(verifyIPNRequest.bind(null, text), 0);
return ok();
}
// checks back with the IPN service to tell it we're the right folks
// if successful, sends off to processing
async function verifyIPNRequest(request) {
const isProduction = true;
logDebugMessage("In verify. Is production: " + isProduction)
const prodFields = {
url: "https://ipnpb.paypal.com/cgi-bin/webscr",
email: "youremailhere@whatever.com"
}
const testFields = {
url: "https://ipnpb.sandbox.paypal.com/cgi-bin/webscr",
email: "seller@paypalsandbox.com"
}
var fields = testFields;
if (isProduction) {
fields = prodFields;
}
logDebugMessage("In verify, right before payload processing")
const payload = "cmd=_notify-validate&" + request;
const query = parse(request);
var options = {
url: fields.url,
method: 'POST',
headers: {
'Connection': 'close',
'User-Agent': 'WIX-JS-ValidationScript'
},
body: payload
};
logDebugMessage("about to make the call..." + payload)
var resp = fetch(fields.url, options);
resp
.then(r => r.text())
.then(r => {
logDebugMessage("We got a response! Response: " + r)
if (r != 'VERIFIED') {
logErrorMessage("Donation was not verified. Full data: " + request)
return;
}
if(query.payment_status != 'Completed') {
logErrorMessage("Donation not completed. Full data: " + request)
return;
}
if(query.receiver_email != fields.email) {
logErrorMessage("Emails do not match: " + query.receiver_email + " / " + fields.email + ". Full data: " + request )
return;
}
const donationData = {
donor_email: query.payer_email,
donor_name: query.first_name,
donor_last_name: query.last_name,
donation_amt: query.mc_gross,
donation_currency: query.mc_currency,
donation_time: query.payment_date
}
logDebugMessage("Donation successful: " + JSON.stringify(donationData));
if(isProduction) {
sendThankYouEmail(donationData.donor_email, donationData.donor_name, donationData.donor_last_name);
}
})
.catch(e => {
console.log("error..." + e);
logErrorMessage("There was an error parsing response: " + request)
})
}
function sendThankYouEmail(emailAddr, firstName, lastName) {
// we're assuming that if the paypal validation goes through that we don't need to worry about auth.
const emailId = "RCLPplg";
// don't think Wix is hip to new javascript shorthand stuff
const contact = {
firstName: firstName,
lastName: lastName,
emails: [
emailAddr
],
phones: []
}
// create the contact, send the email
wixCrm.createContact(contact)
.then(contactId => {
wixCrm.emailContact(emailId, contactId);
})
.catch(e => {
logErrorMessage("Something went wrong sending an email to donator. Error: " + e + ". Other data: " + request)
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment