Error when trying to purchase multiple items with one transaction - node.js

I have gotten the PayPal API to work with one item and I'm now trying to get it to work with a whole "shopping-cart". I have encountered an error that I don't know how to solve. I suspect it might have to do something with the payment-jsons total value that represents the total cost of the whole transaction. However I don't know what to do about it.
Here is the error:
Error: Response Status : 400
at IncomingMessage.<anonymous> (E:\Users\willi\Documents\Node\Store\node_modules\paypal-rest-sdk\lib\client.js:130:23)
at IncomingMessage.emit (events.js:327:22)
at endReadableNT (internal/streams/readable.js:1327:12)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
response: {
name: 'MALFORMED_REQUEST',
message: 'Incoming JSON request does not map to API request',
information_link: 'https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST',
debug_id: '9e8898a463ee3',
httpStatusCode: 400
},
httpStatusCode: 400
}
And here is the code in question
const pay = (req, res) => {
async function f() {
items = [];
req_items = req.body.body
let itemsProcessed = 0
req_items.forEach(item => {
console.log(item.id)
const param = item.id
Item.find({ _id: param })
.then((result) => {
const item_body = {
"name": result[0].title,
"sku": "001",
"price": parseFloat(result[0].price),
"currency": "EUR",
"quantity": item.amount
}
items.push(item_body)
itemsProcessed = itemsProcessed + 1
})
.catch((err) => {
console.log(err)
})
})
let promise = new Promise((resolve, reject) => {
setTimeout(() => resolve("done!"), 1000)
});
let result = await promise; // wait until the promise resolves (*)
console.log(items)
const create_payment_json = {
"intent": "sale",
"payer": {
"payment_method": "paypal"
},
"redirect_urls": {
"return_url": "http://localhost:3000/success",
"cancel_url": "http://localhost:3000/cancel"
},
"transactions": [{
"item_list": {
"items": [items]
},
"amount": {
"currency": "EUR",
"total": parseFloat(req.body.subtotal) // 25
},
"description": "Purcahsed from the Store"
}]
};
// console.log(req.body)
// console.log(create_payment_json.transactions[0])
paypal.payment.create(create_payment_json, function (error, payment) {
if (error) {
throw error;
} else {
for(let i = 0;i < payment.links.length;i++){
if(payment.links[i].rel === 'approval_url'){
res.redirect(payment.links[i].href);
}
}
}
});
}
f();
}

API deprecation notice
You are integrating the deprecated v1/payments PayPal API. You shouldn't be doing so for a new integration; the current API is v2/checkout/orders, documented here.
Typically you'll want to create two routes on your own server, 'Create Order' and 'Capture Order', which return their own JSON when called. Then you can pair those two routes with the following approval flow: https://developer.paypal.com/demo/checkout/#/pattern/server
But as for your problem, debugging an issue like this is much simpler if you simply log your request JSON to see what the problem with it is.
If you do so, you will see that the "items" array you are sending has an array inside of an array of only one item (the other array). That array shouldn't be there.
This seems the culprit:
"items": [items]
Here you decided to make an array, which was useful when "items" was a single item (no array). But when items is already an array, you shouldn't be putting the array into a new array -- the resulting JSON won't map to an API request, and PayPal will return an error.
What you should do is get rid of those brackets and ensure that at this point in the code execution, "items" is already an array (if it wasn't before).

Related

NodeJS get Google Sheet list

I have a google sheet with a list (dropdown, or data validation - list from ranges, as Google Sheets call it), like so:
Image of sheet
Imagine that in the list I have 4 values to select from. My goal is not only to get all table values, but also all values that constitute the list ["Beer","Wine","Rum","Martini"].
I've tried 2 different ways to retrieve the info and the list:
a) With sheets.spreadsheets.values.get, I get the table values in a digestible way, but not the content of the dropdown. Instead, the cell comes in as blank ("") [Comment: on Apps Script, you would get this information]
b) With sheets.spreadsheets.getByDataFilter, I get much more than I need and in a horrible format. However, I do not get the dropdown content as an array (as I'd want), but rather as a refence: (userEnteredValue: "=Input!$F$5:$F$7")
The question is, how do I get only the table, including the dropdown content as an array? I know it is possible and easy to do in Google Apps Script (I have it implemented), but not on Node.
Below the code as a reference for other programmers.
var {google} = require("googleapis");
let privatekey = require("./client_secret.json");
// configure a JWT auth client
let jwtClient = new google.auth.JWT(
privatekey.client_email,
null,
privatekey.private_key,
['https://www.googleapis.com/auth/spreadsheets',
]);
//authenticate request
jwtClient.authorize(function (err, tokens) {
if (err) {
console.log(err);
return;
} else {
console.log("Successfully connected!");
}
});
//Google Sheets API
let spreadsheetId = '<SPREADSHEET ID>';
let sheetName = 'Input!A1:B4'
let sheets = google.sheets('v4');
exports.fetch = (req, res) => {
sheets.spreadsheets.values.get({
auth: jwtClient,
spreadsheetId: spreadsheetId,
range: sheetName,
}, function (err, response) {
if (err) {
console.log('The API returned an error: ' + err);
} else {
res.json(response);
}
});
// OR
sheets.spreadsheets.getByDataFilter({
auth: jwtClient,
spreadsheetId: spreadsheetId,
"includeGridData": true,
}, function (err, response) {
if (err) {
console.log('The API returned an error: ' + err);
} else {
res.json(response);
}
});
}
What you are interested in is dataValidation
In order to retrieve it, you can use the method spreadsheets.get, setting the parameter fields to sheets/data/rowData/values/dataValidation
If your data validation is set as List of items, the response will look like:
{
"sheets": [
{
"data": [
{
"rowData": [
{
"values": [
{
"dataValidation": {
"condition": {
"type": "ONE_OF_LIST",
"values": [
{
"userEnteredValue": "Beer"
},
{
"userEnteredValue": "Wine"
},
{
"userEnteredValue": "Martini"
}
]
},
"showCustomUi": true
}
}
]
}
]
}
]
}
]
}
If your data validation is set as List form a range the response will look like:
{
"sheets": [
{
"data": [
{
"rowData": [
{
"values": [
{
{
"dataValidation": {
"condition": {
"type": "ONE_OF_RANGE",
"values": [
{
"userEnteredValue": "=Sheet1!$B$1:$B$3"
}
]
},
"showCustomUi": true
}
}
]
}
]
}
]
}
]
}
In the latter case you would need to call subsequently the method spreadsheets.values.get on =Sheet1!$B$1:$B$3 - that is the range returned as userEnteredValue within the object values nested within the object dataValidation.

javascript object in lambda function

I am working on AWS Lambda and creating method by using node.js.
I need an object like this:
[
{
"TeamName" : "Sales",
"2020-01-01": "90",
"2020-01-02": "92",
"2020-01-03": "95",
"2020-01-04": "90",
"2020-01-05": "56",
"2020-01-06": "70",
"2020-01-07": "73"
},
]
but my current response is this:
[
{
"TeamName": "Billing",
"DateTime": "2020-06-13T00:00:00.000Z",
"Score": 9
},
{
"TeamName": "Billing",
"DateTime": "2020-06-13T00:00:00.000Z",
"Score": 9
},
{
"TeamName": "Billing",
"DateTime": "2020-06-11T00:00:00.000Z",
"Score": 5
},
]
Here is my Lambda method. I am not good at creating javascript object so please help me to make a response like this, Thanks.
exports.handler = (event, context, callback) => {
console.log('Events:',event);
let UserHierarchyGroupID = event['hierarchyGroupId'];
let team = [];
// allows for using callbacks as finish/error-handlers
context.callbackWaitsForEmptyEventLoop = false;
pool.getConnection(function(err, connection) {
if (err) throw err;
let sql = `SELECT date(Feedback.DateTime) as datetime,Feedback.Score,UserHierarchy.Layer5
FROM ctrData2.Feedback
LEFT OUTER JOIN ctrData2.CallDetail ON CallDetail.ContactId = Feedback.FeedbackID
LEFT OUTER JOIN ctrData2.UserTable ON UserTable.UserID = CallDetail.UserID
LEFT OUTER JOIN ctrData2.UserHierarchy ON UserTable.UserID = UserHierarchy.UserID
WHERE UserTable.UserHierarchyGroupID=?`;
let field = [UserHierarchyGroupID];
connection.query(sql,field, function (err, result, fields) {
if (err) throw err;
// console.log(result);
connection.release();
var date;
var score;
if(result.length>0){
result.forEach((item)=>{
team.push({
"TeamName": item.Layer5,
"DateTime": item.datetime,
"Score": item.Score
});
});
}else{
callback(null,{
status: 404,
Body: "Not found"
});
}
callback(null,team);
// FomratObjects(result,(formattedResponse)=>{
// // console.log(formattedResponse);
// callback(formattedResponse);
// });
});
});
};
Its doesn't look possible to create an object exactly like you mentioned but you can do this to assign value to every single date.
Hope it will be helpful.
function formatData(data){
var nObject = {};
data.forEach(d=>{
nObject[moment(d.datetime).format('MM-DD-YYYY')]=d.Score;
});
return nObject;
}

Unable to write item(s) to DynamoDB table utilizing DocumentClient - Nodejs

I'm absolutely brand new to DynamoDb and I'm trying to simply write an object from a NodeJS Lambda. Based on what I've read and researched I should probably be using DocumentClient from the aws-sdk. I also found the following question here regarding issues with DocumentClient, but it doesn't seem to address my specific issue....which I can't really find/pinpoint unfortunately. I've set up a debugger to help with SAM local development, but it appears to be only providing some of the errors.
The code's implementation is shown here.
var params = {
TableName: "March-Madness-Teams",
Item: {
"Id": {"S": randstring.generate(9)},
"School":{"S": team_name},
"Seed": {"S": seed},
"ESPN_Id": {"S": espn_id}
}
}
console.log(JSON.stringify(params))
dynamodb.put(params, (error,data) => {
if (error) {
console.log("Error ", error)
} else {
console.log("Success! ", data)
}
})
Basically I'm scrubbing a website utilizing cheerio library and cherry picking values from the DOM and saving them into the json object shown below.
{
"TableName": "March-Madness-Teams",
"Item": {
"Id": {
"S": "ED311Oi3N"
},
"School": {
"S": "BAYLOR"
},
"Seed": {
"S": "1"
},
"ESPN_Id": {
"S": "239"
}
}
}
When I attempt to push this json object to Dynamo, I get errors says
Error MultipleValidationErrors: There were 2 validation errors:
* MissingRequiredParameter: Missing required key 'TableName' in params
* MissingRequiredParameter: Missing required key 'Item' in params
The above error is all good in well....I assume it didn't like the fact that I had wrapped those to keys in strings, so I removed the quotes and sent the following
{
TableName: "March-Madness-Teams",
Item: {
"Id": {
"S": "ED311Oi3N"
},
"School": {
"S": "BAYLOR"
},
"Seed": {
"S": "1"
},
"ESPN_Id": {
"S": "239"
}
}
}
However, when I do that...I kind of get nothing.
Here is a larger code snippet.
return new Promise((resolve,reject) => {
axios.get('http://www.espn.com/mens-college-basketball/bracketology')
.then(html => {
const dynamodb = new aws.DynamoDB.DocumentClient()
let $ = cheerio.load(html.data)
$('.region').each(async function(index, element){
var preregion = $(element).children('h3,b').text()
var region = preregion.substr(0, preregion.indexOf('(') - 1)
$(element).find('a').each(async function(index2, element2){
var seed = $(element2).siblings('span.rank').text()
if (seed.length > 2){
seed = $(element2).siblings('span.rank').text().substring(0, 2)
}
var espn_id = $(element2).attr('href').split('/').slice(-2)[0]
var team_name = $(element2).text()
var params = {
TableName: "March-Madness-Teams",
Item: {
"Id": randstring.generate(9),
"School":team_name,
"Seed": seed,
"ESPN_Id": espn_id
}
}
console.log(JSON.stringify(params))
// dynamodb.put(params)
// .then(function(data) {
// console.log(`Success`, data)
// })
})
})
})
})
Can you try without the type?
Instead of
"School":{"S": team_name},
for example, use
"School": team_name,
From your code, I can see the mis promise on the dynamodb request. Try to change your lines :
dynamodb.put(params).then(function(data) {
console.log(`Success`, data)
})
to be :
dynamodb.put(params).promise().then(function(data) {
console.log(`Success`, data)
})
you can combine with await too :
await dynamodb.put(params).promise().then(function(data) {
console.log(`Success`, data)
})
exports.lambdaHandler = async (event, context) => {
const html = await axios.get('http://www.espn.com/mens-college-basketball/bracketology')
let $ = cheerio.load(html.data)
const schools = buildCompleteSchoolObject(html, $)
try {
await writeSchoolsToDynamo(schools)
return { statusCode: 200 }
} catch (error) {
return { statusCode: 400, message: error.message }
}
}
const writeSchoolsToDynamo = async (schools) => {
const promises = schools.map(async school => {
await dynamodb.put(school).promise()
})
await Promise.all(promises)
}
const buildCompleteSchoolObject = (html, $) => {
const schools = []
$('.region').each(loopThroughSubRegions(schools, $))
return schools
}
const loopThroughSubRegions = (schools, $) => {
return (index, element) => {
var preregion = $(element).children('h3,b').text()
var region = preregion.substr(0, preregion.indexOf('(') - 1)
$(element).find('a').each(populateSchoolObjects(schools, $))
}
}
const populateSchoolObjects = (schools, $) => {
return (index, element) => {
var seed = $(element).siblings('span.rank').text()
if (seed.length > 2) {
seed = $(element).siblings('span.rank').text().substring(0, 2)
}
var espn_id = $(element).attr('href').split('/').slice(-2)[0]
var team_name = $(element).text()
schools.push({
TableName: "March-Madness-Teams",
Item: {
"Id": randstring.generate(9),
"School": team_name,
"Seed": seed,
"ESPN_Id": espn_id
}
})
}
}
I know this is drastically different from what I started with but I did some more digging and kind of kind of worked to this...I'm not sure if this is the best way, but I seemed to get it to work...Let me know if something should change!
Oh I understand what you want.
Maybe you can see the code above works, but there is one concept you have to improve here about async - await and promise especially on lambda function.
I have some notes here from your code above, maybe can be your consideration to improve your lambda :
Using await for every promise in lambda is not the best approach because we know the lambda time limitation. But sometimes we can do that for other case.
Maybe you can change the dynamodb.put method to be dynamodb.batchWriteItem :
The BatchWriteItem operation puts or deletes multiple items in one or more tables.
Or If you have to use dynamodb.put instead, try to get improve the code to be like so :
const writeSchoolsToDynamo = async (schools) => {
const promises = schools.map(school => {
dynamodb.put(school).promise()
})
return Promise.all(promises)
}

Request to Cloudflare DNS from Cloudflare worker not returning the DNS result

I have a Cloudflare (CF) worker that I want to have make a few DNS requests using the CF DNS (https://developers.cloudflare.com/1.1.1.1/dns-over-https/json-format/).
So a pretty basic worker:
/**
* readRequestBody reads in the incoming request body
* Use await readRequestBody(..) in an async function to get the string
* #param {Request} request the incoming request to read from
*/
async function readRequestBody(request) {
const { headers } = request
const contentType = headers.get('content-type')
if (contentType.includes('application/json')) {
const body = await request.json()
return JSON.stringify(body)
}
return ''
}
/**
* Respond to the request
* #param {Request} request
*/
async function handleRequest(request) {
let reqBody = await readRequestBody(request)
var jsonTlds = JSON.parse(reqBody);
const fetchInit = {
method: 'GET',
}
let promises = []
for (const tld of jsonTlds.tlds) {
//Dummy request until I can work out why I am not getting the response of the DNS query
var requestStr = 'https://cloudflare-dns.com/dns-query?ct=application/dns-json&name=example.com&type=A'
let promise = fetch(requestStr, fetchInit)
promises.push(promise)
}
try {
let results = await Promise.all(promises)
return new Response(JSON.stringify(results), {status: 200})
} catch(err) {
return new Response(JSON.stringify(err), {status: 500})
}
}
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
I have just hardcoded the DNS query at the moment to:
https://cloudflare-dns.com/dns-query?ct=application/dns-json&name=example.com&type=A
and I would expect that the JSON result I would get is:
{
"Status": 0,
"TC": false,
"RD": true,
"RA": true,
"AD": true,
"CD": false,
"Question": [
{
"name": "example.com.",
"type": 1
}
],
"Answer": [
{
"name": "example.com.",
"type": 1,
"TTL": 9540,
"data": "93.184.216.34"
}
]
}
however instead in results I get what appears to be the outcome of the websocket established as part of the fetch() (assuming I go around the loop once)
[
{
"webSocket": null,
"url": "https://cloudflare-dns.com/dns-query?ct=application/dns-json&name=example.com&type=A",
"redirected": false,
"ok": true,
"headers": {},
"statusText": "OK",
"status": 200,
"bodyUsed": false,
"body": {
"locked": false
}
}
]
So my question is, what am I doing wrong here such that I am not getting the DNS JSON response from the 1.1.1.1 API?
fetch() returns a promise for a Response object, which contains the response status, headers, and the body stream. This object is what you're seeing in your "results". In order to read the response body, you must make further calls.
Try defining a function like this:
async function fetchJsonBody(req, init) {
let response = await fetch(req, init);
if (!response.ok()) {
// Did not return status 200; throw an error.
throw new Error(response.status + " " + response.statusText);
}
// OK, now we can read the body and parse it as JSON.
return await response.json();
}
Now you can change:
let promise = fetch(requestStr, fetchInit)
to:
let promise = fetchJsonBody(requestStr, fetchInit)

How to return unauthorized response from before hook in feathersJS

I have parts of app (modules) that gonna be forbidden for certain people, so I wanna check that in before hook and send unauthorized response if its needed.
I'm successfully throwing error on backend, but on my frontend I still get successful response as if there was no error.
Here is how my code looks like:
1.Function that checks if app is forbidden for user that sent request:
function isAppForbidden(hook) {
let forbiddenApps = [];
hook.app.services.settings.find({
query: {
$limit: 1,
$sort: {
createdAt: -1
}
}
}).then(res => {
let array = hook.params.user.hiddenApps;
if(array.indexOf('qualitydocs') >= 0 || res.data[0].forbiddenApps.indexOf('qualitydocs') >= 0) {
hook.response = Promise.reject({error: '401 Unauthorized'});
//this part is important, the rest not so much
//what im expecting to do here is just to return unauthorized response
}
});
return hook;
}
But this for now just throws error on backend like:
"error: Unhandled Rejection at: Promise Promise {
{ error: { code: '401', message: 'Unauthorized' } } } code=401, message=Unauthorized"
And frontend still gets successful response (200 with requested data)
And I just call this function in before hooks:
before: {
all: [
authenticate('jwt'),
hook => includeBefore(hook),
hook => isAppForbidden(hook) //here, rest is not important
],
find: [],
get: [],
create: [(hook) => {
hook.data.authorId = hook.params.user.id;
}],
update: [],
patch: [],
remove: []
},
the response im expecting to get, looks something like this:
Found the solution... the key was to wrap the content of function in promise... so it now looks like this:
function isAppForbidden(hook) {
return new Promise((resolve, reject) => {
hook.app.services.settings.find({
query: {
$limit: 1,
$sort: {
createdAt: -1
}
}
}).then(res => {
if (hook.params.user.hiddenApps.indexOf('qualitydocs') >= 0 || res.data[0].forbiddenApps.indexOf('qualitydocs') >= 0) {
reject(new errors.NotAuthenticated());
} else {
resolve();
}
})
})
}
and it works as a charm

Resources