-
Notifications
You must be signed in to change notification settings - Fork 1
/
Code.js
46 lines (43 loc) · 1.12 KB
/
Code.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* Simple Google Apps Script Library project for creating a
* PayPal IPN listener as part of a published Apps Script Web App.
*/
/**
* Tests an HTTP request to determine if it claims to be from PayPal.
*
* @param {Event} e The Google Apps Script HTTP Event object.
*
* @return {boolean}
*/
function isIpnMessage (e) {
return (e.parameter.verify_sign) ? true : false;
}
/**
* Returns a PayPal IPN base URL.
*
* @access private
*
* @param {boolean} isTest If true, gets the sandbox URL.
*
* @return string
*/
function getBaseUrl_ (isTest) {
return (isTest)
? 'https://ipnpb.sandbox.paypal.com/cgi-bin/webscr'
: 'https://ipnpb.paypal.com/cgi-bin/webscr';
}
/**
* Validate PayPal IPN message.
*
* @param {Event} e The Google Apps Script HTTP Event object.
*
* @return {boolean}
*
* @see https://developers.google.com/apps-script/guides/web#request_parameters
*/
function isValid (e) {
var qs = '?cmd=_notify-validate&' + e.postData.contents;
var url = getBaseUrl_(e.parameter.test_ipn) + qs;
var resp = UrlFetchApp.fetch(url);
return 'VERIFIED' === resp.getContentText();
}