Error while running Paypal API using node js - javascript

I am running the following code in a script called pp.js. And I am running this on my index.html on loading of the page for test. I am bundling every source files using the latest webpack module.
var paypal = require('paypal-rest-sdk');
var user_config ={
'mode': 'sandbox', //sandbox or live
'client_id': 'xxxxxxx',
'client_secret': 'xxxxxxx'
};
paypal.configure(user_config);
var create_payment_json = {
"intent": "sale",
"payer": {
"payment_method": "paypal"
},
"redirect_urls": {
"return_url": "http://return.url",
"cancel_url": "http://cancel.url"
},
"transactions": [{
"item_list": {
"items": [{
"name": "item",
"sku": "item",
"price": "1.00",
"currency": "USD",
"quantity": 1
}]
},
"amount": {
"currency": "USD",
"total": "1.00"
},
"description": "This is the payment description."
}]
};
// TILL HERE THERE IS NO ERROR
paypal.payment.create(create_payment_json, function (error, payment) {
if (error) {
console.log("There seems to be some error... I hope it can be corrected.");
throw error;
} else {
console.log("Create Payment Response");
console.log(payment);
}
});
When the last bit of the code is run i.e. paypal.payment.create(create_ ... I am getting the following error in the console of my browser.
How do I rectify this?

This code should be run server-side. The same issue in PayPal SDK tracker: https://github.com/paypal/PayPal-node-SDK/issues/220
Quote 1 (from the linked ticket)
Please see #149 (comment). It seems that you are trying to run this code in the browser which may be a security problem. If the user has access to your credentials or have an access token, they can do anything that you can do as a merchant (e.g. create more payments, refund people money, etc.).
Quote 2 (from the #149 ticket):
I did some more investigation. Apparently, this request.js:54 Uncaught Error: Invalid value for opts.mode. error comes from a version of stream-http/request.js. I'm guessing that you are trying to use browserify and using stream-http in the browser to simulate node.js's built-in http module.
Are you trying to run this SDK code in the browser (instead of in a server-side node.js process)?
This node.js SDK should only be used on a secured server. I have very limited experience using browserify. Unless I'm mistaken, you need to give this SDK access to your client ID and client secret to make this SDK work, and if your code runs in the browser, you will be exposing the credentials to any browser client. This will allow any customer to be able to do anything to your account (e.g. refund all of your payments).
If it's just that part of your code is intended to be run in the browser and another part is in the server but the code is all in one project, I recommend separating out the code into 2 different projects with different package.json files so you can have separate dependencies.

Related

Cant get commerce js/ stripe to authorise test transaction

I cant get commerce.js to process transaction in the ecommerce website tutorial im following. Whenever i attempt a transaction it says
{
"status_code": 402,
"error": {
"message": "The specified payment method requires further verification",
"type": "requires_verification",
"param": "pi_3KuHUGSCnOzTd5gv1y7qWTUS_secret_UPSucrdQSjwfWzMRksWZkN3Sv"
},
"help": {
"slack": "http://slack.commercejs.com",
"_comment": "*help* & *console* error responses will not be returned when using production/live API keys."
}
}
in the commerce js logs, but im using test keys and the card 4242424242....
Log from Commercejs

HMS AnalyticsKit Demo for Javascript cannot reach server

Here is what I did:
Cloned HMS AnalyticsKit Demo for Javascript - https://github.com/HMS-Core/hms-analytics-demo-javascript.git in Germany Frankfurt AWS EC2
cd hms-analytics-demo-javascript/hmsanalyticskitdemo/
npm install
npm run dev
Use Chrome browser and visit http://x.x.x.x:8080 (EC2), complete the quiz and post score
Inspect Console - https://ibb.co/TKDm586. There is no CORS or 401 error.
This is what I find in hms-analytics-demo-javascript/hmsanalyticskitdemo/src/main.js:
var agConnectConfig =
{
"agcgw":{
"backurl": "https://lfagcgwtest01.hwcloudtest.cn:15602",
"url": "https://lfagcgwtest01cn.hwcloudtest.cn:18062"
},
"client":{
"cp_id":"4130086000000637176",
"product_id":"258913027873047688",
"client_id":"401147640282098944",
"client_secret":"ABA91A52EF1FAB651F57AF091CEC149EECAFB01757DF1224FC384D2173828480",
"app_id":"98751101985125555",
"api_key":"CgB6e3x9zs8IL+Frjllsf9DoteK4Taz7jAseM9P+ikVZBJhHu87FpdEpqq0CimpayCrJgUrkUnV2RM86fyFiD4FE"
},
"service":{
"analytics":{
"collector_url":"habackup.hwcloudtest.cn:31405,datacollector-drcn.dt.dbankcloud.cn",
"resource_id":"p1",
"channel_id":""
},
"cloudstorage":{
"storage_url":"https://agc-storage-drcn.platform.dbankcloud.cn"
},
"ml":{
"mlservice_url":"ml-api-drcn.ai.dbankcloud.com,ml-api-drcn.ai.dbankcloud.cn"
}
},
"region":"CN",
"configuration_version":"1.0"
};
Then I change it to my own agconnect-services.json (note that I changed cp_id, product_id, client_id, client_secret, app_id, package_name, api_key. My data storage location is Germany):
var agConnectConfig =
{
"agcgw":{
"backurl":"connect-dre.dbankcloud.cn",
"url":"connect-dre.hispace.hicloud.com"
},
"client":{
"cp_id":"890001000000000000",
"product_id":"736430070000000000",
"client_id":"451560030000000000",
"client_secret":"5950F0F77523D1C95914089A046DED446E09A02036D8AAAAAAAAAAAAAAAAAAAA",
"app_id":"100000000",
"package_name":"com.aaaa.aa.aaaaaaa",
"api_key":"CgB6e3x9LNm1WQuE1uhjpEI6kdQD7xD23LfdkttRcakwi404NmcxlOBVUslatUnb5dJzXXXXXXXXXXXXXXXXXXXX"
},
"service":{
"analytics":{
"collector_url":"datacollector-dre.dt.hicloud.com,datacollector-dre.dt.dbankcloud.cn",
"resource_id":"p1",
"channel_id":""
},
"cloudstorage":{
"storage_url":"https://ops-dre.agcstorage.link"
},
"ml":{
"mlservice_url":"ml-api-dre.ai.dbankcloud.com,ml-api-dre.ai.dbankcloud.cn"
}
},
"region":"DE",
"configuration_version":"1.0"
}
I repeated step 5, I saw CORS errors - https://ibb.co/HHQvMVL
POST https://datacollector-dre.dt.hicloud.com/webv3 net::ERR_FAILED
If I turn on CORS plugin on Chrome, I saw 401 errors - https://ibb.co/9tXMrHS
POST https://datacollector-dre.dt.hicloud.com/webv3 401
I see no analytics in my app > analytics > real-time overview console.
Is there something wrong with my steps? Could this is be API/server issue?
Thanks.
Your integration procedures are correct. The problem is that the latest version of SDK should be integrated.
The earlier version of the JavaScript SDK has an authentication failure issue, which causes the fault to run the demo. You are advised to download the latest SDK 5.0.4.300 which rectified the authentication failure issue, and perform the verification.

Azure Function with Node.js can't access SQL database: Entry point problem?

I am trying to connect an Azure Function to a SQL-Database that I also created in Azure. Sounds pretty simple and Microsoft even has a pretty good POC tutorial for it. However, in my case the Azure Function is dropping "entryPoint" errors that I am not able to resolve.
I checked some other stackoverflow discussions (Can't connect Node.js server to Azure SQL Database) and googled the hell out of it. Unfortunately it seems like none of this is helping. I updated the "function.json" to set my entryPoint as shown below.
{
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"access": "listen",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"entryPoint": "index",
"direction": "out",
"name": "res"
}
]
}
My index.js code is a very trivial try to connect to the database and tell me about the success.
var Connection = require('tedious').Connection;
var config = {
userName: 'XXXX',
password: 'XXXX!',
server: 'XXXX.database.windows.net',
// If you are on Microsoft Azure, you need this:
options: {encrypt: true, database: 'XXXXX'}
};
var connection = new Connection(config);
connection.on('connect', function(err) {
// If no error, then good to proceed.
console.log("Connected");
});
Normally this should connect to the database and print a "Connected" to the console but somehow it shows this error:
[Error] Executed 'Functions.TediousTest' (Failed, Id=XXXXXXX)
node exited with code 1
[error] Worker was unable to load function TediousTest: 'Unable to determine function entry point. If multiple functions are exported, you must indicate the entry point, either by naming it 'run' or 'index', or by naming it explicitly via the 'entryPoint' metadata property.',[error] Worker XXXXXX uncaught exception: ReferenceError: executeStatement is not defined
Hopefully this is enough information to understand my problem. The database has no special firewall etc. at the moment. Besides it seems like the code snippet isn't even able to get in touch with the firewall. Thanks in advance.
I believe you haven't exported your function properly based on the error message - basically missed the module.exports line.
I guess your code looks something like this
...
async function TediousTest() {
...
}
If so, just add this line to the end
module.exports = TediousTest;

Output bindings not picked up or present in Javascript Azure Functions

I'm having a bit of difficulty getting my simple Azure Function to dump a JSON payload into a CosmosDB. Here is the desired setup:
[HTTP TRIGGER] -> [JS FUNCTION] -> [INSERT JSON TO COSMOS DB]
I've added a cosmosDB output binding via Integrate in the web console
I know this part works because it will auto-create the collection upon running.
However, this is where my success ends.
I've included my code here. Maybe there is something obvious that I have missed:
function.json
{
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req"
},
{
"type": "http",
"direction": "out",
"name": "res"
},
{
"type": "documentDB",
"name": "outputDocument",
"databaseName": "outDatabase",
"collectionName": "MyCollection",
"createIfNotExists": true,
"connection": "mydocdb_DOCUMENTDB",
"direction": "out"
}
],
"disabled": false
}
index.js
module.exports = function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
context.bindings.outputDocument = {test: "hello world"}
context.done();
};
snippet output of context.log(context)
I've also peeked into the context variables, and it seems that the outbund bindings not there. I would have expected to see outputDocument here:
bindings:
{ req:
{ originalUrl: 'https://log-ugly-url-here',
method: 'POST',
query: [Object],
headers: [Object],
body: [Object],
params: {},
rawBody: '{\n "name": "Azure"\n}' } },
bind: [Function],
Executing this function in the portal gets a 200 OK, but no values show up in my cosmosdb collections.
No errors are shown anywhere.
Can anyone see what I might have missed?
Updates
It was recommended that I try to use JSON.stringify() around the payload, per this document:
context.bindings.outputDocument = JSON.stringify({test: "hello world"});
However, this has not resolved the issue. I've noticed that the documentation under the binding config sections does not recommend this strategy.
Update - Nov. 8, 2017 (10:12 AM)
I was using the Data Explorer blade in azure to see if there were results in my collections. I decided to use Robo 3T Mongo client. Now, when I query the collection I get the following error:
Error: error: {
"_t" : "OKMongoResponse",
"ok" : 0,
"code" : 1,
"errmsg" : "Unknown server error occurred when processing this request.",
"$err" : "Unknown server error occurred when processing this request."
}
I tried to look at the logs for this cosmosdb, but no errors show up.
I ran into this as well. Kept banging my head until I saw this:
https://learn.microsoft.com/en-us/azure/azure-functions/functions-integrate-store-unstructured-data-cosmosdb
“At this time, the Azure Cosmos DB trigger, input bindings, and output bindings work with SQL API and Graph API accounts only“
Looks like there is a bug in Azure that accidentally corrupts the database.

Kik Bot | Uncaught ReferenceError: require is not defined

I'm trying to create a kik bot, but I keep getting this error anytime I try and run it "Uncaught ReferenceError: request is not defined". I have installed node and the kik package but that seems not to be working here is my Javascript:
request.post({
url: "https://api.kik.com/v1/config",
auth: {
user: "<my-user-name>",
pass: "<my-api-key>"
},
json: {
"webhook": "<link>",
"features": {
"receiveReadReceipts": false,
"receiveIsTyping": false,
"manuallySendReadReceipts": false,
"receiveDeliveryReceipts": false
},
"staticKeyboard": {
"type": "suggested",
"responses": [
{
"body": "Start",
"type": "text"
},
{
"body": "Help",
"type": "text"
}
]
}
}
}, callback);
I've been trying to looking into this but came up with nothing. Could it be a Node problem?
Thank you in advance!!
From the conversation we had, I thought I put the answer here for future references.
That piece of code is supposed to be run on NodeJS in commandline, not a browser.
You need to setup a node environment, install the required dependencies using npm or whatever you like. Then run the code in node.
$ npm install request to install request.
var request = require('request') to require the installed library.
Also you need to modify the code and provide proper a callback function. You can read requests documentation here.

Categories

Resources