Last active
June 24, 2019 21:33
-
-
Save morkhorwaad/4596b05d02a130069deed76496db8c0f to your computer and use it in GitHub Desktop.
Wix IPN Listener
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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