Node.js paypal rest api error401 - javascript

I am trying to create an invoice with Paypal rest API. I got an error
Error: Response Status : 401
Which is not saying more than that. I have double-checked my secret and id are correct and the mode is set to sandbox. I can't find anything wrong since I just copy-pasted in the example code to get started
Everything, making new client on PayPal developer portal. Just nothing makes sense anymore..
const config = require("../configs/bot_settings.json")
// PayPal //
const paypal = require('paypal-rest-sdk');
paypal.configure({
mode: config.paypal_mode, // Sandbox or live
client_id: config.paypal_id,
client_secret: config.paypal_secret
});
// PayPal //
var create_invoice_json = {
"merchant_info": {
"email": "PPX.DevNet-facilitator#gmail.com",
"first_name": "Dennis",
"last_name": "Doctor",
"business_name": "Medical Professionals, LLC",
"phone": {
"country_code": "001",
"national_number": "5032141716"
},
"address": {
"line1": "1234 Main St.",
"city": "Portland",
"state": "OR",
"postal_code": "97217",
"country_code": "US"
}
},
"billing_info": [{
"email": "example#example.com"
}],
"items": [{
"name": "Sutures",
"quantity": 100.0,
"unit_price": {
"currency": "USD",
"value": 5
}
}],
"note": "Medical Invoice 16 Jul, 2013 PST",
"payment_term": {
"term_type": "NET_45"
},
"shipping_info": {
"first_name": "Sally",
"last_name": "Patient",
"business_name": "Not applicable",
"phone": {
"country_code": "001",
"national_number": "5039871234"
},
"address": {
"line1": "1234 Broad St.",
"city": "Portland",
"state": "OR",
"postal_code": "97216",
"country_code": "US"
}
},
"tax_inclusive": false,
"total_amount": {
"currency": "USD",
"value": "500.00"
}
};
paypal.invoice.create(create_invoice_json, function (error, invoice) {
if (error) {
throw error;
} else {
console.log("Create Invoice Response");
message.channel.send(invoice.total_amount)
console.log(invoice.total_amount);
}
});

Related

How to update nested object in a MongoDB collection based on the request?

I've a collection for user given below
{
"_id": 1
"firstname": "John"
"lastname": "Doe"
"address": {
"street": "13",
"city": "Los Angeles",
"state": "California",
"country": "USA",
"pincode": "12345"
}
}
I want to make an API that will update the user data.
This is what I've tried:
First Method
Using this method some fields are being removed from collection which are not present in request.
Query:
const updateUser = (userId, data) => {
return UserDB.updateOne({ _id: userId }, {
$set: {...data }
});
};
Request:
{
"_id": 1
"firstname": "Justin"
"lastname": "Thomas"
"address": {
"country": "Canada",
"pincode": "9999"
}
}
Result:
// street, city and state is removed from collection
{
"_id": 1
"firstname": "Justin"
"lastname": "Thomas"
"address": {
"country": "Canada",
"pincode": "9999"
}
}
Second Method:
Using this method, fields are set to null which are not present in the request.
Query:
const updateUser = (userId, data) => {
return UserDB.updateOne({ _id: userId }, {
$set: {
"firstname": data.firstname,
"lastname": data.lastname,
"address.street": data.address.street,
"address.city": data.address.city,
"address.state": data.address.state,
"address.country": data.address.country,
"address.pincode": data.address.pincode
}
});
};
Request:
{
"_id": 1
"firstname": "Justin"
"lastname": "Thomas"
"address": {
"country": "Canada",
"pincode": "9999"
}
}
Result:
// street, city and state is set to null in the collection.
{
"_id": 1
"firstname": "Justin"
"lastname": "Thomas"
"address": {
"street": null,
"city": null,
"state": null,
"country": "Canada",
"pincode": "9999"
}
}
Question
How can I update a nested object in a collection?
Second method is working fine playground, please check your data.
Use $addFields in aggregation pipeline of update operation.
$addFields will only update the values that is present in the object and other fields will remain as it is.
Like this:
db.collection.update({
_id: 1
},
[
{
$addFields: {
"firstname": "Justin",
"lastname": "Thomas",
"address": {
"country": "Canada",
"pincode": "16565"
}
}
}
])
Test it here: https://mongoplayground.net/p/pjO8mIKb03o

Not getting the expected response from Backend in web service

I wrote an api call in AngularJS to get data from backend JAVA
Here is my code
$scope.memberForm = {};
$scope.memberForm.member_address = {};
$scope.memberRegForm = function() {
$scope.btnloading = true;
$('#add-btn').prop('disabled', true);
url = '/member/add';
var request = $http({
method: "post",
url: url,
data: angular.toJson($scope.memberForm),
headers: {
'Content-Type': 'application/json'
},
});
}
But I am not getting desire response from backed, Response that's I am actually getting from backend is
{
"fullname":"sdfgsdf",
"fathername":"sdfsdf",
"email":"sdfsdf#gmail.com",
"date_of_birth":"1993-03-24",
"admission_date":"2018-10-22",
"phoneno":"0987654321",
"nationality":"United States",
"member_address":
{
"address":"5 meadow st",
"state":"Connecticut",
"zipcode":"06770",
"district":"zc"
},
"college":"cvbcv",
"qualification":"bcvbcv",
"slno_ivpr":"12"
}
But here is my desire response. I need member_address as an array of ojects instead of a single object
{
"fullname": "avinash",
"fathername": "xxxxxxx",
"date_of_birth": "1991-08-08",
"nationality": "indian",
"admission_date": "2015-08-08",
"college": "ABC college",
"slno_ivpr": null,
"phoneno": "1234567890",
"email": "avinash.mitresource#gmail.com",
"remarks": "no remarks",
"member_renewal": [],
"qualification": "MCA",
"member_address": [
{
"address": "piratlanka,repalle",
"state": "AP",
"district": "Guntur",
"zipcode": "522264",
"type": "PermanentAddress"
},
{
"address": "padamata",
"state": "AP",
"district": "vijayawada",
"zipcode": "522201",
"type": "ResidencialAddress"
}
]
}

Query JSON data sharing same value

Sample JSON Data:
{
"results": [
{
"name": "John Smith",
"state": "NY",
"phone": "555-555-1111"
},
{
"name": "Mary Jones",
"state": "PA",
"phone": "555-555-2222"
},
{
"name": "Edward Edwards",
"state": "NY",
"phone": "555-555-3333"
},
{
"name": "Abby Abberson",
"state": "RI",
"phone": "555-555-4444"
},
]}
With this sample data I can display individual values from the results [] array with object.name and object.phone to look something like:
John Smith 555-555-1111<br />
Mary Jones 555-555-2222<br />
Edward Edwards 555-555-3333<br />
Abby Abberson 555-555-4444
What I am trying to do now is select just the people who's state value is NY and only display their object.name and object.phone:
John Smith 555-555-1111<br />
Edward Edwards 555-555-3333
I tried this lovely little block but all it did was print all the names, which makes sense after I tried it.
if (object.state = "NY") {
div.append(repName);
}
I can't seem to think of a way to only display those that share a the same state.
I'm probably searching for the wrong terms or have to go about this another way... please help!
You are using =(assignment operator),which is wrong.
You have to use ==(comparison operator)
So do like below:-
if (object.state == "NY") {
div.append(repName);
}
Working sample-
var obj = {
"results": [
{
"name": "John Smith",
"state": "NY",
"phone": "555-555-1111"
},
{
"name": "Mary Jones",
"state": "PA",
"phone": "555-555-2222"
},
{
"name": "Edward Edwards",
"state": "NY",
"phone": "555-555-3333"
},
{
"name": "Abby Abberson",
"state": "RI",
"phone": "555-555-4444"
},
]};
$(obj.results).each(function(k,object){
if (object.state == "NY") {
$('#final_data').append(object.name +" : "+object.phone+"<br/>");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="final_data"></div>
My one cent solution:
var obj = {
"results": [
{
"name": "John Smith",
"state": "NY",
"phone": "555-555-1111"
},
{
"name": "Mary Jones",
"state": "PA",
"phone": "555-555-2222"
},
{
"name": "Edward Edwards",
"state": "NY",
"phone": "555-555-3333"
},
{
"name": "Abby Abberson",
"state": "RI",
"phone": "555-555-4444"
},
]};
obj.results.forEach((value) => {
if (value.state === "NY") {
const li = document.createElement("li");
li.innerHTML = `${value.name} : ${value.phone}`;
document.querySelector("#final_data").appendChild(li);
}
});
<ul id="final_data"></ul>
Like Alive said you used the assignment operator = instead of comparison operator === or ==.

JSON.PARSE without loosing header array field

function main(message){
...
phone= JSON.parse(message.phoneNumbers);
... }
My input JSON is
{
"firstName": "John",
"lastName": "Smith",
"isAlive": true,
"age": 25,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100"
},
"phoneNumbers": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "office",
"number": "646 555-4567"
},
{
"type": "mobile",
"number": "123 456-7890"
}
],
"children": [],
"spouse": null
}
The result I receive is omitting the "phoneNumbers" but I do want it.
Your data is correct, when i JSON.parse it, i get everything allright.
But you don't seem to access to your data in the right way. You must first parse the whole JSON, then you have a javascript object, and only then you can acces your property.
in detail:
var obj = JSON.parse(message);
var phone = obj.phoneNumbers;
or in short:
var phone = (JSON.parse(message)).phoneNumbers;

number of addresses in JSON?

I have the following JSON (which I validated and verified is correct) and I'm trying to get the number of addresses.
When I do a
var location = req.body;
I get
{ AddressValidateRequest:
{ '-USERID': 'xxxxxxxxxxx',
Address: [ [Object], [Object], [Object] ] } }
How do I get the number of addresses?
{
"AddressValidateRequest": {
"-USERID": "xxxxxxxxxxx",
"Address": [
{
"-ID": "0",
"FirmName": "firmname",
"Address1": "address1here",
"Address2": "13 infinite loop",
"City": "new york",
"State": "NY",
"Zip5": "zip5here",
"Zip4": "zip4here"
},
{
"-ID": "1",
"FirmName": "firmhere",
"Address1": "address1here",
"Address2": "1 Smith Ct ",
"City": "San Predo",
"State": "CA",
"Zip5": "ziphere",
"Zip4": "ziphere1"
},
{
"-ID": "1",
"FirmName": "firmhere",
"Address1": "address1here",
"Address2": "12 John Rd ",
"City": "Newark",
"State": "PA",
"Zip5": "ziphere",
"Zip4": "ziphere1"
}
]
}
}
This should do it
req.body.AddressValidateRequest.Address.length
To get a specific address, (e.g., the first)
req.body.AddressValidateRequest.Address[0]
To get a field on an address
req.body.AddressValidateRequest.Address[0].City
// "New York"

Categories