How to create a paypal billing agreement using NodeJS - javascript

I am struggling to understand how the Paypal REST API is used to create and execute a subscription plan using Node.js
Right now I'm doing the following:
paypal.configure({
mode: 'sandbox',
client_id: secrets.paypal.client_id,
client_secret: secrets.paypal.client_secret
});
Then I create a billing plan object including a trial period and the normal subscription amount which I pass to the create method, then when I get a response I update it to the 'ACTIVE' state, and then call the get method again which in my understanding should give me links which the user can then use to subscribe to this billing plan.
Am I way off?
paypal.billingPlan.create(billingPlanAttributes, function (error, billingPlan) {
req.session.paymentId = billingPlan.id;
var BillingID = billingPlan.id;
var links = billingPlan.links;
var billing_plan_update_attributes = [
{
"op": "replace",
"path": "/",
"value": {
"state": "ACTIVE"
}
}
];
paypal.billingPlan.get(BillingID, function (error, billingPlan) {
paypal.billingPlan.update(BillingID, billing_plan_update_attributes, function (error, response) {
paypal.billingPlan.get(BillingID, function (error, billingPlan) {
for (var i = 0; i < links.length; i++) {
if (links[i].rel === 'self') {
res.render('api/paypal', {
approvalUrl: links[i].href
});
}
}
});
}); // End update
}); // End get
});

Related

JavaScript loop to accommodate filtered array of objects received from a webhook

Goal
Capture each event sent through a webhook and turn it into a Slack post. Events include new blog posts, questions, discussions, wiki page, etc. (qualified as contents) and comments (qualified as comments) posted in an online community. Sometimes multiple events are sent in the webhook at once.
Attempted method
This simple JavaScript Azure Function is intended to
Receive one or more webhook events sent in a JSON array
Filter objects qualified as contents from those qualified as comments
Send an API request for each content and/or comment object (both have their own URL endpoint)
Parse each object returned (contents and comments return a similar but different hierarchy of keys)
Assemble the values into JSON objects (one per event, regardless of whether it is a content or comment) and send to Slack
Results
The following code worked fine for a single webhook event until I attempted to add the for loop to accommodate multiple webhook events sent in one array.
Code
Example JSON from webhook
{
"events": [{
"TypeId": "9999-999e",
"DateOccurred": "2018-12-15T20:39:42.2487557Z",
"EventData": {
"ActorUserId": 1234,
"ContentId": "5678-999c",
"ContentTypeId": "9012-999d",
"WikiPageId": 3456,
"WikiId": 1
}
},
{
"TypeId": "1111-111f",
"DateOccurred": "2018-12-15T22:55:37.7846546Z",
"EventData": {
"ActorUserId": 2345,
"ContentId": "2222-222b",
"ContentTypeId": "3333-333a",
"ForumReplyId": 4567,
"ForumThreadId": 8901,
"ForumId": 2
}
},
{
"TypeId": "9012-888f",
"DateOccurred": "2018-12-15T22:44:57.7091846Z",
"EventData": {
"ActorUserId": 9876,
"CommentId": "8900-123a"
}
}
]
}
Example JSON returned from API request
The slightly different structure in hierarchies is accurate.
(for contents)
{
"Content": {
"CreatedByUser": {
"ProfileUrl": "https://<company>.telligenthosting.net/members/<user>",
"Username": "<user>"
},
"HtmlName": "Title",
"HtmlDescription": "Text",
"Url": "https://<company>.telligenthosting.net/<link>"
}
}
(for comments)
{
"Comment": {
"Content": {
"CreatedByUser": {
"ProfileUrl": "https://<company>.telligenthosting.net/members/<user>",
"Username": "<user>"
},
"HtmlName": "Title",
"HtmlDescription": "Text",
"Url": "https://<company>.telligenthosting.net/<link>"
}
}
}
JavaScript file (as an Azure Function)
module.exports = function (context, data) {
var json = data.body;
var request = require('request');
// Parse the webhook event JSON body
var unparsed = JSON.stringify(json.events);
var parsed = JSON.parse(unparsed);
console.log(parsed) // RESULTS ARE AS EXPECTED (the JSON nested beneath `events`, beginning and ending with `[]`)
for (var i = 0; i < parsed.length; i++) {
// Parse out Id of webhook event (for all content types but comments)
// This Id retrieves details about the content
var ContentId, ContentTypeId;
if (parsed[i].EventData.hasOwnProperty('ContentId')) {
var ContentId = parsed[i].EventData.ContentId;
var ContentTypeId = parsed[i].EventData.ContentTypeId;
console.log(ContentTypeId); // RESULTS ARE NOT AS EXPECTED: Prints the same Id twice
var options = {
url: "https://<company>.telligenthosting.net/api.ashx/v2/genericcontent/" + ContentId + "/" + ContentTypeId + ".json",
headers: {
"Rest-User-Token": "<token>",
"Content-Type": "application/json"
}
};
};
// Parse out Id of a webhook event (for comments only)
// This Id retrieves details about a comment
var CommentId;
if (parsed[i].EventData.hasOwnProperty('CommentId')) {
var CommentId = parsed[i].EventData.CommentId;
var options = {
url: "https://<company>.telligenthosting.net/api.ashx/v2/comments/" + CommentId + ".json",
headers: {
"Rest-User-Token": "<token>",
"Content-Type": "application/json"
}
};
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
var info = JSON.parse(body);
//For all content types but comments
var username, profileUrl, subject, url, text;
if (info.hasOwnProperty('Content')) {
username = info.Content.CreatedByUser.Username;
profileUrl = info.Content.CreatedByUser.ProfileUrl;
subject = info.Content.HtmlName;
url = info.Content.Url;
text = info.Content.HtmlDescription;
};
//For comments
if (info.hasOwnProperty('Comment')) {
username = info.Comment.User.DisplayName;
profileUrl = info.Comment.User.ProfileUrl;
subject = info.Comment.Content.HtmlName;
url = info.Comment.Url;
text = info.Comment.Body;
};
};
//Send to Slack
function sendToSlack(theUsername, theIconEmoji) {
var theUsername = "Bot";
var theIconEmoji = ":bot:";
var payload = {
attachments: [{
author_name: username,
author_link: profileUrl,
title: subject,
title_link: url,
text: text
}]
};
if (theUsername !== undefined) {
payload.username = theUsername;
}
if (theIconEmoji !== undefined) {
payload.icon_emoji = theIconEmoji;
}
var theRequest = {
url: urlWebHook,
method: "POST",
json: payload
};
request(theRequest, function (error, response, body) {});
}
var urlWebHook = "https://hooks.slack.com/services/<Id>";
sendToSlack();
};
};
request(options, callback);
};
Issue
As commented out in the code above, it appears that the loop is not going through each event but rather through the first event multiple times.
Much of what I read indicates for (var i = 0; i < json.length; i++) { is the proper approach but no matter what I try the Azure Function throws a 500 Internal Service Error and eventually times out. No information is provided in the debug console even though detailed logging is turned on.
Thank you
Thank you for any advice or education.
EventData is not defined because you're not constructing your object properly.
Here's how you might do it:
var json = require("./test.json");
var unparsedEvents = json.events;
for (let event of unparsedEvents) {
var ContentId = event.EventData.ContentId;
var ContentTypeId = event.EventData.ContentTypeId;
var CommentId = event.EventData.CommentId;
var options = new Object();
console.log("ContentId:", ContentId);
console.log("ContentTypeId:", ContentTypeId);
console.log("CommentId:", CommentId);
if (CommentId) {
options.url = "https://<company>.telligenthosting.net/api.ashx/v2/comments/" + CommentId + ".json";
options.headers = {
"Rest-User-Token": "<token>",
"Content-Type": "application/json",
};
} else {
options.url = "https://<company>.telligenthosting.net/api.ashx/v2/genericcontent/" + ContentId + "/" + ContentTypeId + ".json";
options.headers = {
"Rest-User-Token": "<token>",
"Content-Type": "application/json",
};
}
console.log("options:", options);
console.log();
}
I believe you need to change parsed[0] to parsed[i]. Currently you are looping through the array but only accessing the first element, which is why you see the first event multiple times.

How to wait for promise to complete with returned value using angularjs

I’m having an issue with my project. In my angularjs controller a function is being executed and then my function to make a call to my database to update a record is executing without waiting for the first function to complete and therefore sending over an undefined result variable.
Below you can find my code snippets with my attempts so far.
Submit button function:
$scope.submitNewStarters = function () {
// result is returning as undefined <<<<< Issue
var result = $scope.sendNewStarterDetailsToApi();
$scope.updateArchivedImportFlag(result);
};
Controller function handling the logic:
$scope.sendNewStarterDetailsToApi = function () {
swal({
title: "Confirmation",
text: "Are you sure you want to import the new starter details?",
icon: "info",
dangerMode: true,
buttons: ["No", "Yes"]
}).then(function (approve) {
if (approve) {
// Get each of the new starter details that have been set to true for import.
var newStartsToImport = $scope.tableParams.data.filter(x => x.imported == true);
for (let i = 0; i < newStartsToImport.length; i++) {
// Parses the current new starter object into a stringified object to be sent to the api.
$scope.newStartsToImport = $scope.createApiObject(newStartsToImport[i]);
// A check to ensure that nothing has went wrong and that the stringify object has worked.
if ($scope.newStartsToImport !== "") {
apiFactory.postNewStarterDetailsToApi($scope.newStartsToImport).then(function (response) {
var isSuccessful = response.data.d.WasSuccessful;
if (isSuccessful)
toastr.success("New starter details successfully sent to API.", "Success!");
else {
var errorMessage = response.data.d.ErrorMessage;
toastr.error("New starter details were unsuccessfully sent to API. Please try again. \n" + errorMessage, "Error!");
}
});
}
else {
toastr("An error has occurred when attempting to create the data object to be sent to API. The process has stopped!", "Error!");
break;
}
}
return newStartsToImport;
}
else
toastr.info("No new starter details were sent to API", "Information!");
});
};
Factory function for API call:
postNewStarterDetailsToApi: function (data) {
return $http({
url: "https://www.example.com/services/service.svc/Import",
method: "POST",
data: data,
headers: {
'Content-Type': 'application/json; charset=utf-8',
}
}).then(function successCallbwack(response) {
// this callback will be called asynchronously
// when the response is available
return response;
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
console.log('An error has occured during the function call postNewStarterDetailsToApi(): ', response);
});
}
So with the concept of promises how am I able to execute the sendNewStarterDetailsToApi function, wait for it to complete and then return the populated array? Once the populated array (result) is returned then execute the updateArchivedImportFlag function.
Below I've added an illustration of what I'd like to achieve:
The approach I am using is , save all the promises in an array .
Use any promise library or es6 Promise, and use .all function to wait for all promises to execute
The syntax i wrote is not totally correct. Since you are using angular js , you can use $q.all
$scope.sendNewStarterDetailsToApi = function () {
swal({
title: "Confirmation",
text: "Are you sure you want to import the new starter details?",
icon: "info",
dangerMode: true,
buttons: ["No", "Yes"]
}).then(function (approve) {
var res = [];
if (approve) {
// Get each of the new starter details that have been set to true for import.
var newStartsToImport = $scope.tableParams.data.filter(x => x.imported == true);
for (let i = 0; i < newStartsToImport.length; i++) {
// Parses the current new starter object into a stringified object to be sent to the api.
$scope.newStartsToImport = $scope.createApiObject(newStartsToImport[i]);
// A check to ensure that nothing has went wrong and that the stringify object has worked.
if ($scope.newStartsToImport !== "") {
res.push(apiFactory.postNewStarterDetailsToApi($scope.newStartsToImport))
}
else {
toastr("An error has occurred when attempting to create the data object to be sent to API. The process has stopped!", "Error!");
break;
}
}
return Promise.all(res);
}
else
toastr.info("No new starter details were sent to API", "Information!");
}).then(function (data) {
data.forEach((response) => {
var isSuccessful = response.data.d.WasSuccessful;
if (isSuccessful)
toastr.success("New starter details successfully sent to API.", "Success!");
else {
var errorMessage = response.data.d.ErrorMessage;
toastr.error("New starter details were unsuccessfully sent to API. Please try again. \n" + errorMessage, "Error!");
}
})
}).then((res) => {
//call Submit new starters
})
};

Only One Session Variable undefined

i've got a Problem that i can't solve and i can't find any related solutions on SO or somewhere else, unfortunatly.
Basically i just want to send 3 Arrays with Data to the Client Javascript. Its working fine for 2 Arrays, but the 3rd one becomes empty when i refresh the page in browser, and i don't know why.
heres the related code;
app.post("/", function(req, res) {
ssn = req.session;
ssn.anlagen = [];
var Betreiber = {
TableName: "XXX",
KeyConditionExpression: "#usr = :user",
ExpressionAttributeNames: {
"#usr": "User",
},
ExpressionAttributeValues: {
":user": req.body.name
},
};
docClient.query(Betreiber, function(err, data) {
if (data.Count == 0 || req.body.passwort != data.Items[0].Passwort) {
res.render(__dirname + '/views/login', {text: "Der Nutzername oder das Passwort sind falsch! Bitte versuchen Sie es erneut."});
}
else if (req.body.passwort == data.Items[0].Passwort) {
anlagenarray = [];
ssn.user = data.Items[0].User; // WORKS FINE
ssn.admin = data.Items[0].Admin; // WORKS FINE
if (ssn.admin == false) { // Normal
for (var i = 0; i < data.Items[0].Anlagen.length; i++) {
ssn.ident = data.Items[0].Anlagen[Object.keys(data.Items[0].Anlagen)[i]];
var Anlagenbezeichnung = {
TableName: "SCR-Anlagen",
KeyConditionExpression: "#ident = :id",
ExpressionAttributeNames: {
"#ident": "Id",
},
ExpressionAttributeValues: {
":id": ssn.ident
},
};
docClient.query(Anlagenbezeichnung, function(err, data) {
ssn.anlagen = data.Items[0].Bezeichnung // this Variable gets "[]" after refresh
ssn.anlagen.sort();
});
}
}
Heres the Code to send Arrays to the Client Side JS;
app.post("/anlagen", function(req, res) {
if (ssn.user) {
res.send({
name: ssn.user, // WORKS
adminstatus: ssn.admin, // WORKS
bezeichnungen: ssn.anlagen // [] after Page refresh
});
}
else {
res.render(__dirname + '/views/login');
}
});
I just can't find any solution for this. i literally tried everything, but i keep getting this error.
detailed solution approaches highly appreciated.
it's my first try on backend web development, don't be too hard, if my complete code is total bullshit^^
thanks in advance.
Fixed the Problem by myself.
If you have a similar Problem. Just put req.session.save(); inside the query.
like this:
docClient.query(params, function(err, data) {
req.session.bezeichnung.push([data.Items[0].Bezeichnung,data.Items[0].Id]);
req.session.save();
});
This will make the Variable accessible outside of the query scope.

How to use servicebus topic sessions in azure functionapp using javascript

I have an Azure Functionapp that processes some data and pushes that data into an Azure servicebus topic.
I require sessions to be enabled on my servicebus topic subscription. I cannot seem to find a way to set the session id when using the javascript functionapp API.
Here is a modified extract from my function app:
module.exports = function (context, streamInput) {
context.bindings.outputSbMsg = [];
context.bindings.logMessage = [];
function push(response) {
let message = {
body: CrowdSourceDatum.encode(response).finish()
, customProperties: {
protoType: manifest.Type
, version: manifest.Version
, id: functionId
, rootType: manifest.RootType
}
, brokerProperties: {
SessionId: "1"
}
context.bindings.outputSbMsg.push(message);
}
.......... some magic happens here.
push(crowdSourceDatum);
context.done();
}
But the sessionId does not seem to get set at all. Any idea on how its possible to enable this?
I tested sessionid on my function, I can set the session id property of a message and view it in Service Bus explorer. Here is my sample code.
var connectionString = 'servicebus_connectionstring';
var serviceBusService = azure.createServiceBusService(connectionString);
var message = {
body: '',
customProperties:
{
messagenumber: 0
},
brokerProperties:
{
SessionId: "1"
}
};
message.body= 'This is Message #101';
serviceBusService.sendTopicMessage('testtopic', message, function(error)
{
if (error)
{
console.log(error);
}
});
Here is the test result.
Please make sure you have enabled the portioning and sessions when you created the topic and the subscription.

How to get the revision history of user from Rally using Node js scripting

My requirement is to get the exact date/time when a user gets disabled.
To achieve this, I need to query for the line 'user set to INACTIVE' in the Revision History to find the date the user was switched from enabled to disabled.
How can I get the revision history using node js ?
I tried below code, but its not working as Rally support team mentioned that its older code and i have to try with v2.0.
Can somebody help me to achieve my requirement ?
var revisions = story.RevisionHistory.Revisions;
revisions.sort(byRevisionNumber);
var story_was_blocked = false;
// it doesn't matter how many revs have been in BLOCKED state, presence of one is sufficient
for (var rix = 0; rix < revisions.length && story_was_blocked === false; rix++) {
var rev = revisions[rix];
if (rev.Description.indexOf("BLOCKED changed from ") >= 0) {
story_was_blocked = true;
}
}
Here's a brief example of how this might be accomplished. Note that this just shows how to fetch all revisions in the collection on a User, you'd need to iterate through them and match the Description field on 'Disabled':
var rally = require('rally'),
queryUtils = rally.util.query,
rallyApi = rally({
// Example key, not valid
apiKey: '_UkMasZfjPZfquDIMExfEKnAboQUlyT2SP4UppMHir',
server: 'https://rally1.rallydev.com',
requestOptions: {
headers: {
'X-RallyIntegrationName': 'Query User Revisions',
'X-RallyIntegrationVendor': 'Stackoverflow user4211235',
'X-RallyIntegrationVersion': '1.0'
}
}
});
function onError(error) {
console.log('Failure!', error);
}
function queryUserRevisions(result) {
var revisions = result.Revisions;
rallyApi.query({
ref: revisions,
start: 1,
limit: Infinity,
order: 'RevisionNumber',
fetch: ['RevisionNumber','Description','CreationDate']
}, function(error, result) {
if(error) {
onError(error);
} else {
console.log('Success querying User Revisions...');
console.log('Summary of revisions on User:')
console.log(result);
}
});
}
function queryUserRevisionHistory(result) {
rallyApi.query({
ref: result.Results[0].RevisionHistory,
start: 1,
limit: Infinity,
fetch: ['Revisions','RevisionNumber','Description','CreationDate']
}, function(error, result) {
if(error) {
onError(error);
} else {
console.log('Success querying User Revision History. Querying Revisions...');
queryUserRevisions(result);
}
});
}
function queryUser(callback) {
rallyApi.query({
type: 'user',
start: 1,
pageSize: 2,
limit: 10,
order: 'CreationDate',
fetch: ['UserName', 'EmailAddress', 'RevisionHistory'],
query: queryUtils.where('UserName', '=', "user#company.com")
}, function(error, result) {
if(error) {
onError(error);
} else {
console.log('Success querying User. Querying Revision History...');
callback(result);
}
});
}
queryUser(queryUserRevisionHistory);

Categories

Resources