Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (() => {
- const fs = require('fs');
- const KEYS_FILE_PATH = 'C:\\Users\\Will\\Desktop\\donation-notifications\\keys\\ids.json';
- const CHANNEL_NAME = "11111011101";
- const MAX_DONATION_CHECK = 100;
- const POLL_RATE = 4000;
- const TRANSITION_TIME = 2000;
- const STALL_TIME = 4500;
- const DONATION_TEXT_1 = " just donated ";
- const DONATION_TEXT_2 = " and says ";
- // get client id and client secret from local file
- let keys = JSON.parse(fs.readFileSync(KEYS_FILE_PATH, 'utf-8'));
- // get keys from the file
- let clientId = keys['clientId'];
- let clientSecret = keys['clientSecret'];
- let username = keys['username'];
- let password = keys['password'];
- let signature = keys['signature'];
- // get a reference to the paypal sdk
- let paypal = require('paypal-rest-sdk');
- paypal.configure({
- 'mode':'sandbox',
- 'client_id': clientId,
- 'clientSecret' : clientSecret
- });
- const https = require("https");
- const qs = require("querystring");
- let currentMs = Date.now();
- let currentMsMinusPollRate = currentMs - 1000000000;
- let startDate = (new Date(currentMsMinusPollRate)).toISOString();
- let endDate = (new Date(currentMs)).toISOString();
- const params = {
- 'user': username,
- 'pwd': password,
- 'signature': signature,
- 'method':'TransactionSearch',
- 'version':'78',
- 'trxtype':'Q',
- 'startdate': startDate,
- 'enddate': endDate
- };
- const options = {
- hostname : "api-3t.sandbox.paypal.com",
- port: 443,
- path: '/nvp/?'+qs.stringify(params),
- method : 'GET'
- };
- const req = https.request(options,(res)=>{
- let totalBuffer = '';
- res.on("data",(buffer)=>{
- totalBuffer += buffer;
- });
- res.on("end", ()=>{
- let parsedResponse = qs.parse(totalBuffer);
- // get transaction ids
- let ids = Object.entries(parsedResponse)
- .filter(([s])=>s.includes("TRANSACTIONID"))
- .map(([,v])=>v);
- for(const x in ids){
- getTransactionDetails(ids[x]);
- }
- totalBuffer = '';
- });
- });
- req.end();
- function getTransactionDetails(id){
- const params = {
- 'user': username,
- 'pwd': password,
- 'signature': signature,
- 'method':'GetTransactionDetails',
- 'version':'78',
- 'trxtype':'Q',
- 'transactionid': id
- };
- const options = {
- hostname : "api-3t.sandbox.paypal.com",
- port: 443,
- path: '/nvp?'+qs.stringify(params),
- method : 'GET'
- };
- console.log("???");
- const req2 = https.request(options,(res)=>{
- let totalBuffer = '';
- console.log("???");
- res.on("data",(buffer)=>{
- console.log("???");
- totalBuffer += buffer;
- });
- res.on("end", ()=>{
- console.log("???");
- //let parsedResponse = qs.parse(totalBuffer);
- //console.log(parsedResponse);
- // get relevant transaction information
- //console.log(parsedResponse['INVNUM']);
- //console.log(parsedResponse['CUSTOM']);
- totalBuffer = '';
- });
- });
- }
- /*
- // continuously poll the Paypal API for a list of
- // the most recent 100 transactions
- let recentTransactionIDs = [];
- // donation alert sound
- let donationSound = new Audio();
- donationSound.src = "sounds\\illuminatiSound (mp3cut.net).mp3";
- // initialize the donation list with an initial paypal api call
- // for the donation list
- // for now we'll use an empty initial list
- // call the api every few seconds and check the new donation list
- // against the old donation list
- // initial start & end query dates
- let currentMs = Date.now();
- let currentMsMinusPollRate = currentMs - POLL_RATE;
- let startDate = (new Date(currentMsMinusPollRate)).toISOString();
- let endDate = (new Date(currentMs)).toISOString();
- let recentDonationIDs = {};
- let notAnimating = true;
- function checkDonations(){
- // get appropriate start date and end date
- startDate = endDate;
- endDate = (new Date()).toISOString();
- const params = {
- 'user': username,
- 'pwd': password,
- 'signature': signature,
- 'method':'TransactionSearch',
- 'version':'78',
- 'trxtype':'Q',
- 'startdate': startDate,
- 'enddate': endDate
- };
- const options = {
- hostname : "api-3t.sandbox.paypal.com",
- port: 443,
- path: '/nvp/?'+qs.stringify(params),
- method : 'GET'
- }
- const req = https.request(options,(res)=>{
- let totalBuffer;
- res.on("data",(buffer)=>{
- totalBuffer += buffer;
- })
- res.on("end", ()=>{
- // parse response for list of transaction IDs
- let parsedResponse = qs.parse(totalBuffer);
- // get transaction IDs
- let newList = Object.entries(parsedResponse)
- .filter(([s])=>s.includes("TRANSACTIONID"))
- .map(([,v])=>v));
- // compare the new list to the old list
- // and find any differences
- let count = 0;
- let newDonationStack = [];
- while(count < MAX_DONATION_CHECK){
- let newID = newList[count];
- if(recentDonationIDs.has(newID))
- break;
- else{
- recentDonationIDs.add(newDonation);
- // get more transation details from
- // Paypal and in the callback push
- // those details as a new donation object
- // to the donation stack
- newID
- -->newDonationStack.push(newDonation);
- ++count;
- }
- }
- if(newDonationStack.length && notAnimating){
- handleAnimations();
- notAnimating = false;
- }
- // recursively display an animation for each new donation
- function handleAnimations(){
- if(newDonationStack.length){
- // set the donation alert name
- let theDonation = newDonationStack.pop();
- let theText = document.getElementById('followerText');
- theText.textContent = theName+DONATION_TEXT_1+"$"+
- theDonation.amount+DONATION_TEXT_2+theDonation.message;
- // transition the donation alert into view
- let theDonationAlert = document.getElementById('donationAlert');
- donationAlert.classList.add('isVisible');
- // play the current alert sound
- donationSound.setTime = 0.00;
- donationSound.play();
- // transition the follower alert out of view
- setTimeout(transitionAlert, TRANSITION_TIME+STALL_TIME);
- function transitionAlert(){
- // begin animation to remove the follower alert
- donationAlert.classList.remove('isVisible');
- // setup the next alert
- setTimeout(() => {
- if(!newDonationStack.length)
- notAnimating = true;
- handleAnimations();}, TRANSITION_TIME);
- }
- }
- }
- totalBuffer = "";
- console.log("done");
- })
- });
- }
- setInterval(checkDonations, POLL_RATE);
- */
- })()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement