Advertisement
Guest User

Untitled

a guest
Apr 29th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.84 KB | None | 0 0
  1. (() => {
  2. const fs = require('fs');
  3. const KEYS_FILE_PATH = 'C:\\Users\\Will\\Desktop\\donation-notifications\\keys\\ids.json';
  4. const CHANNEL_NAME = "11111011101";
  5. const MAX_DONATION_CHECK = 100;
  6. const POLL_RATE = 4000;
  7. const TRANSITION_TIME = 2000;
  8. const STALL_TIME = 4500;
  9. const DONATION_TEXT_1 = " just donated ";
  10. const DONATION_TEXT_2 = " and says ";
  11.  
  12. // get client id and client secret from local file
  13. let keys = JSON.parse(fs.readFileSync(KEYS_FILE_PATH, 'utf-8'));
  14.  
  15. // get keys from the file
  16. let clientId = keys['clientId'];
  17. let clientSecret = keys['clientSecret'];
  18. let username = keys['username'];
  19. let password = keys['password'];
  20. let signature = keys['signature'];
  21.  
  22. // get a reference to the paypal sdk
  23. let paypal = require('paypal-rest-sdk');
  24. paypal.configure({
  25. 'mode':'sandbox',
  26. 'client_id': clientId,
  27. 'clientSecret' : clientSecret
  28. });
  29.  
  30. const https = require("https");
  31. const qs = require("querystring");
  32.  
  33. let currentMs = Date.now();
  34. let currentMsMinusPollRate = currentMs - 1000000000;
  35. let startDate = (new Date(currentMsMinusPollRate)).toISOString();
  36. let endDate = (new Date(currentMs)).toISOString();
  37.  
  38. const params = {
  39. 'user': username,
  40. 'pwd': password,
  41. 'signature': signature,
  42. 'method':'TransactionSearch',
  43. 'version':'78',
  44. 'trxtype':'Q',
  45. 'startdate': startDate,
  46. 'enddate': endDate
  47. };
  48.  
  49. const options = {
  50. hostname : "api-3t.sandbox.paypal.com",
  51. port: 443,
  52. path: '/nvp/?'+qs.stringify(params),
  53. method : 'GET'
  54. };
  55.  
  56. const req = https.request(options,(res)=>{
  57.  
  58. let totalBuffer = '';
  59.  
  60. res.on("data",(buffer)=>{
  61.  
  62. totalBuffer += buffer;
  63.  
  64. });
  65.  
  66. res.on("end", ()=>{
  67.  
  68. let parsedResponse = qs.parse(totalBuffer);
  69.  
  70. // get transaction ids
  71. let ids = Object.entries(parsedResponse)
  72. .filter(([s])=>s.includes("TRANSACTIONID"))
  73. .map(([,v])=>v);
  74.  
  75. for(const x in ids){
  76. getTransactionDetails(ids[x]);
  77. }
  78.  
  79. totalBuffer = '';
  80. });
  81.  
  82. });
  83.  
  84. req.end();
  85.  
  86. function getTransactionDetails(id){
  87.  
  88. const params = {
  89. 'user': username,
  90. 'pwd': password,
  91. 'signature': signature,
  92. 'method':'GetTransactionDetails',
  93. 'version':'78',
  94. 'trxtype':'Q',
  95. 'transactionid': id
  96. };
  97.  
  98. const options = {
  99. hostname : "api-3t.sandbox.paypal.com",
  100. port: 443,
  101. path: '/nvp?'+qs.stringify(params),
  102. method : 'GET'
  103. };
  104.  
  105. console.log("???");
  106. const req2 = https.request(options,(res)=>{
  107.  
  108. let totalBuffer = '';
  109.  
  110. console.log("???");
  111.  
  112. res.on("data",(buffer)=>{
  113. console.log("???");
  114.  
  115. totalBuffer += buffer;
  116.  
  117. });
  118.  
  119. res.on("end", ()=>{
  120.  
  121. console.log("???");
  122.  
  123. //let parsedResponse = qs.parse(totalBuffer);
  124.  
  125. //console.log(parsedResponse);
  126.  
  127. // get relevant transaction information
  128. //console.log(parsedResponse['INVNUM']);
  129. //console.log(parsedResponse['CUSTOM']);
  130.  
  131. totalBuffer = '';
  132. });
  133.  
  134. });
  135. }
  136.  
  137. /*
  138. // continuously poll the Paypal API for a list of
  139. // the most recent 100 transactions
  140. let recentTransactionIDs = [];
  141.  
  142. // donation alert sound
  143. let donationSound = new Audio();
  144. donationSound.src = "sounds\\illuminatiSound (mp3cut.net).mp3";
  145.  
  146. // initialize the donation list with an initial paypal api call
  147. // for the donation list
  148. // for now we'll use an empty initial list
  149.  
  150. // call the api every few seconds and check the new donation list
  151. // against the old donation list
  152.  
  153.  
  154. // initial start & end query dates
  155. let currentMs = Date.now();
  156. let currentMsMinusPollRate = currentMs - POLL_RATE;
  157. let startDate = (new Date(currentMsMinusPollRate)).toISOString();
  158. let endDate = (new Date(currentMs)).toISOString();
  159.  
  160. let recentDonationIDs = {};
  161.  
  162. let notAnimating = true;
  163. function checkDonations(){
  164.  
  165. // get appropriate start date and end date
  166. startDate = endDate;
  167. endDate = (new Date()).toISOString();
  168.  
  169. const params = {
  170. 'user': username,
  171. 'pwd': password,
  172. 'signature': signature,
  173. 'method':'TransactionSearch',
  174. 'version':'78',
  175. 'trxtype':'Q',
  176. 'startdate': startDate,
  177. 'enddate': endDate
  178. };
  179.  
  180. const options = {
  181. hostname : "api-3t.sandbox.paypal.com",
  182. port: 443,
  183. path: '/nvp/?'+qs.stringify(params),
  184. method : 'GET'
  185. }
  186.  
  187. const req = https.request(options,(res)=>{
  188.  
  189. let totalBuffer;
  190.  
  191. res.on("data",(buffer)=>{
  192.  
  193. totalBuffer += buffer;
  194.  
  195. })
  196.  
  197. res.on("end", ()=>{
  198.  
  199. // parse response for list of transaction IDs
  200. let parsedResponse = qs.parse(totalBuffer);
  201.  
  202. // get transaction IDs
  203. let newList = Object.entries(parsedResponse)
  204. .filter(([s])=>s.includes("TRANSACTIONID"))
  205. .map(([,v])=>v));
  206.  
  207. // compare the new list to the old list
  208. // and find any differences
  209. let count = 0;
  210. let newDonationStack = [];
  211. while(count < MAX_DONATION_CHECK){
  212. let newID = newList[count];
  213. if(recentDonationIDs.has(newID))
  214. break;
  215. else{
  216. recentDonationIDs.add(newDonation);
  217.  
  218. // get more transation details from
  219. // Paypal and in the callback push
  220. // those details as a new donation object
  221. // to the donation stack
  222. newID
  223. -->newDonationStack.push(newDonation);
  224.  
  225. ++count;
  226. }
  227. }
  228.  
  229. if(newDonationStack.length && notAnimating){
  230. handleAnimations();
  231. notAnimating = false;
  232. }
  233.  
  234. // recursively display an animation for each new donation
  235. function handleAnimations(){
  236.  
  237. if(newDonationStack.length){
  238.  
  239. // set the donation alert name
  240. let theDonation = newDonationStack.pop();
  241. let theText = document.getElementById('followerText');
  242. theText.textContent = theName+DONATION_TEXT_1+"$"+
  243. theDonation.amount+DONATION_TEXT_2+theDonation.message;
  244.  
  245. // transition the donation alert into view
  246. let theDonationAlert = document.getElementById('donationAlert');
  247. donationAlert.classList.add('isVisible');
  248.  
  249. // play the current alert sound
  250. donationSound.setTime = 0.00;
  251. donationSound.play();
  252.  
  253. // transition the follower alert out of view
  254. setTimeout(transitionAlert, TRANSITION_TIME+STALL_TIME);
  255.  
  256. function transitionAlert(){
  257.  
  258. // begin animation to remove the follower alert
  259. donationAlert.classList.remove('isVisible');
  260.  
  261. // setup the next alert
  262. setTimeout(() => {
  263. if(!newDonationStack.length)
  264. notAnimating = true;
  265. handleAnimations();}, TRANSITION_TIME);
  266.  
  267. }
  268.  
  269. }
  270.  
  271. }
  272.  
  273. totalBuffer = "";
  274.  
  275. console.log("done");
  276. })
  277.  
  278. });
  279.  
  280.  
  281. }
  282.  
  283. setInterval(checkDonations, POLL_RATE);
  284. */
  285. })()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement