WooCommerce Nodejs Wrapper returns undefined - javascript

I am learning to work with the WooCommerce API and I am trying to modify the payment address and the shipping address of a user, but I get this error all the time and I do not understand what I am doing wrong.
The error:
TypeError: undefined is not a function
This is my code:
var WooCommerceAPI = require("woocommerce-api");
var api = new WooCommerceAPI({
url: 'https://site.es/',
consumerKey: 'ck_xxxx',
consumerSecret: 'cs_xxxx',
wpAPI: true,
version: 'wc/v3'
});
const data = {
first_name: "James",
billing: {
first_name: "James",
postcode: "99502",
},
shipping: {
first_name: "James",
postcode: "99502",
}
};
api.putAsync('customers/33', data).then(function(result) {
console.log(result);
});
I would really appreciate any help, because really, I don't know what's wrong. I have read a lot about this and I don't see the error.
Regards.

You are using an outdated version of the library. Use the following one in which all the methods are promisified by default - so, you don't have to perpend "Async" any more.
https://github.com/woocommerce/woocommerce-rest-api-js-lib
const WooCommerceRestApi = require("#woocommerce/woocommerce-rest-api").default;
const api = new WooCommerceRestApi({
url: "http://example.com",
consumerKey: "ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
consumerSecret: "cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
version: "wc/v3"
});
api.put("customers/33", data)
.then((response) => {
...

Related

Custom checkout paytm integration - checksum provided is invalid error 2005

I am using https://github.com/paytm/Paytm_Node_Checksum/blob/master/PaytmChecksum.js for checksum generation. But I am getting the following error when initiate transaction api is invoked:
resultInfo: Object { resultStatus: "F", resultCode: "2005", resultMsg: "Checksum provided is invalid" }
​​​​
resultCode: "2005"
​​​​
resultMsg: "Checksum provided is invalid"
​​​​
resultStatus: "F"
The params which I am sending are:
params = {
body: {
requestType: 'Payment',
mid: 'myMerchantId',
orderId: 'OREDRID_98765',
websiteName: 'WEBSTAGING',
txnAmount: { value: '1.00', currency: 'INR' },
userInfo: { custId: 'CUST_001' },
callbackUrl: 'http://localhost:19006/'
},
head: {
signature: 'QSN7GmO2uFpNevi4aZ4K2qTpcYqlxw89Q9Jf56ZX/h2Scmu0s5EyoTme/OwcfUXY8+v/C+tP4nq65sepQDCZuLPEa6/QRDd26b/4lNLG9fi05ZskqjXqzml1ibZUcbzn'
}
}
}
And the checksum is generated like this:
PaytmChecksum.generateSignature(
JSON.stringify(params),
myMerchantKey
);
Help is much appreciated
I think the below line is causing the error:
JSON.stringify(params)
You are sending params object as String for generating the checksum.
You should actually send body object as String.
The above line should be changed to:
JSON.stringify(body)
Its due to invalid API credentials and parameters. Please check 'mid(merchant id)', 'merchant key', 'websiteName' values.
In your code, you are sending 'signature' in params!!! Here, we are about to generate the Signature, so how you can add it before!!
below is my code:
require_once("PaytmChecksum.php");
$orderId = 'ORDERID_'.time();
$paytmParams = array();
$paytmParams["body"] = array("requestType"=>"Payment","mid"=>"<merchant id>","websiteName"=>"WEBSTAGING","orderId"=>$orderId,"callbackUrl"=>"https://answebtechnologies.in/callback","txnAmount"=>array("value"=>"1.00","currency"=>"INR"),"userInfo"
=>array("custId"=>"CUST_001"));
$checksum = PaytmChecksum::generateSignature(json_encode($paytmParams["body"], JSON_UNESCAPED_SLASHES), "<merchant key>");
$paytmParams["head"] = array("signature"=> $checksum);
$post_data = json_encode($paytmParams, JSON_UNESCAPED_SLASHES);
Please refer to initiate transaction API on Paytm developer's portal for sample request (code snippet)

Razor Pay checkout form not appearing and giving error as not a function

I am implementing razorPay payment in angular7 project.I am trying to open checkout form by creating a single instance of Razorpay and accessing its function open(). But instead it is throwing error "is not a function". I couldn't find any proper documentation regarding this.
However, If I try to simple create payment using razorpay, it opens up the window but there is no option for user to choose any method and hence it throws error again because directly creating payment will not add up any card(if method is 'card'). So it is necessary to open checkout form.
Here is my function I am using, firstly i create order on server to generate id and then proceed to payment.
buyPlan() {
let postdata = {
amount: 1000,
currency: "INR",
receipt: "1",
notes: {},
payment_capture: true
}
this.common.createOrder(postdata).subscribe(
(result: any) => {
console.log(result)
var razorpay = new this.winRef.nativeWindow.Razorpay({
key: 'dashboard_key_id',
image: 'assets/images/logo-1.png',
});
var data = {
amount: 1000,
currency: "INR",
email: 'abc#example.com',
contact: '9874563210',
notes: {
address: 'Sector 65 Delhi',
},
method: 'card',
order_id: result.data.id,
handler: function (response) {
alert(response.razorpay_payment_id);
}
};
razorpay.open();
}, (err: HttpErrorResponse) => {
}
);
}
For Custom Integration
Remove:
<script src="https://checkout.razorpay.com/v1/checkout.js"></script>
from index.html file
and put:
<script type="text/javascript" src="https://checkout.razorpay.com/v1/razorpay.js"></script>
inside <head></head> tag

Mongoose is not populating (.populate()) on Production (Heroku), but works on Local

Essentially I am having one of those moments. My app's on Heroku, and the DB it uses is mLab (MongoDB).
It works on local (Cloud9), but not on production (Heroku).
I can't get .populate() to work on production.
Do you see any gaps in my code below (snippet) that may cause Heroku to fail, while it works on local?
Thank you.
(I have tried purging the DB (deleting the DB and making a new one. Also I've for similar questions on this site. Also I have tried 'heroku local --tail' command to debug and ran it on my local machine; it works on local... Just not on Heroku; appears buggy.)
People.find(id).populate("friends").exec(function(err, user){
if(err){
console.log("! Error retrieving user. " + err);
reject ("! Error retrieving user. " + err);
}
else {
console.log("0! Friends should be populated: " + user);
resolve(user);
}
});
My model:
var mongoose = require('mongoose');
var personSchema = mongoose.Schema({
name: String,
friends: [
{
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "Person"
},
name: String
}
],
username: String,
password: String,
});
module.exports = mongoose.model("Person", personSchema);
Your API function looks ok.
I suspect your issue is with how your models are setup, or what is in your data-base. I had similar issues the first time I tried to use Heroku, because Localhost is more forgiving.
In order for your API to work, the following 3 things must be setup:
(1) Model file: people.js
must look like something like:
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var peopleSchema = new Schema({
name: {
type: String,
required: true,
trim: true
},
friends: [{
type: Schema.Types.ObjectId,
ref: "Friends"
}]
});
const People = mongoose.model('Peoples', peopleSchema);
module.exports = People;
And then you must have a 'Friends' model, that 'People' is referencing.
(2) Model file: friends.js
must look something like:
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
// Create the Comment schema
var friendsSchema = new Schema({
friend_name: {
type: String,
required: true,
trim: true
},
});
const Friends = mongoose.model('Friends', friendsSchema);
module.exports = Friends;
And lastly, in order for .Populate to work, you need at least two docs in the database.
(3) Database must contain a Person doc and a Friend doc
must look something like:
people.js :
"_id": {
"$oid": "5bef3480f202a8000984b3c5"
},
"name": "Monica Geller"
"friends": [
{
"$oid": "5bef3480f202a8000984b5b4"
}
]
friends.js :
"_id": {
"$oid": "5bef3480f202a8000984b5b4"
},
"friend_name": "Rachel Green"
Hopefully this helps, or gets you closer to your answer.
It was a version issue.
Had to ensure that all platforms (mLab, and my local database) were using the same version of Mongoose.
npm install mongoose#5.4.8 --save

Get message_id from Telegram message - node.js

I have a problem concerning a Telegram bot I am currently working on. I get messages from users in the following format:
update { update_id: 82618016,
message:
{ message_id: 363,
from: { id: 22303518, first_name: 'Steve', language_code: 'de-DE' },
chat: { id: 22303518, first_name: 'Steve', type: 'private' },
date: 1501501753,
text: 'j' } }
When I want to access the id of the chat I can do this without any problems by using
$.message.chat.id
As soon as a want to get the message_id or first_name I only get "undefined".
$.message.chat.first_name
$.message.message_id
Can anyone help me here? As far as I see it I understood the structure of the message correctly so I don't really know what's the problem here.
Thank you very much in advance
EDIT: I am adding a bit more of my code here:
The main code for the bot (including the webhook) is this:
initializeBot();
function initializeBot(){
const Telegram = require('telegram-node-bot');
const PingController = require('./controllers/ping');
const OtherwiseController = require('./controllers/otherwise');
const tg = new Telegram.Telegram('MY_TOKEN_IS_HERE', {
webhook: {
url: 'https://xxx.herokuapp.com',
port: process.env.PORT || 443,
host: '0.0.0.0'
}
})
tg.router.when(new Telegram.TextCommand('/ping', 'pingCommand'), new PingController())
.otherwise (new OtherwiseController());
}
When the OtherwiseController gets called the following code is called (I reduced it to the essentials to clarify the problem.
class OtherwiseController extends Telegram.TelegramBaseController {
handle($){
console.log($.message.chat.first_name);
console.log($.message.text);
console.log($.message.chat.id);
console.log($.message.message_id);
}
}
The console output for this message
update { update_id: 82618020,
message:
{ message_id: 371,
from: { id: 22303518, first_name: 'Steve', language_code: 'de-DE' },
chat: { id: 22303518, first_name: 'Steve', type: 'private' },
date: 1501509762,
text: 'hello' } }
would be:
undefined
hello
22303518
undefined
Use the below method to extract the keys of your json object, then you can access with an appropriate key:
Object.keys($.message.chat);

How can I retrieve,add data using Quickbase API and javascript

I am very new to Quickbase.
I want to query the quickbase using javascript to get a table records and also insert to it.
How should I do this?
Any help is highly appreciated.
To improve #rtpHarry's answer, the JavaScript SDK he is referencing has seen better days. It hasn't been updated in 2 years, and doesn't work in IE11.
Now this is a shameless plug, full disclosure, I am the author of this module;
But Node-QuickBase is much, much easier to use and works both in the browser and on the server - no code change.
Here is an example:
Within Nodejs:
var QuickBase = require('quickbase');
var quickbase = new QuickBase({
realm: 'www',
appToken: '*****'
});
quickbase.api('API_Authenticate', {
username: '*****',
password: '*****'
}).then(function(result){
return quickbase.api('API_DoQuery', {
dbid: '*****',
clist: '3.12',
options: 'num-5'
}).then(function(result){
return result.table.records;
});
}).map(function(record){
return quickbase.api('API_EditRecord', {
dbid: '*****',
rid: record[3],
fields: [
{ fid: 12, value: record[12] }
]
});
}).then(function(){
return quickbase.api('API_DoQuery', {
dbid: '*****',
clist: '3.12',
options: 'num-5'
});
}).then(function(result){
console.log(result);
}).catch(function(err){
console.error(err);
});
Within the Browser:
<script type="text/javascript" src="quickbase.browserify.min.js"></script>
<script type="text/javascript">
var quickbase = new QuickBase({
realm: 'www',
appToken: '*****'
});
quickbase.api('API_Authenticate', {
username: '*****',
password: '*****'
}).then(function(result){
return quickbase.api('API_DoQuery', {
dbid: '*****',
clist: '3.12',
options: 'num-5'
}).then(function(result){
return result.table.records;
});
}).map(function(record){
return quickbase.api('API_EditRecord', {
dbid: '*****',
rid: record[3],
fields: [
{ fid: 12, value: record[12] }
]
});
}).then(function(){
return quickbase.api('API_DoQuery', {
dbid: '*****',
clist: '3.12',
options: 'num-5'
});
}).then(function(result){
console.log(result);
}).catch(function(err){
console.error(err);
});
</script>
Before you begin you need to set up a user in QuickBase.
In order for you to use the API you need to have a developer key.
There is a JavaScript SDK that you can use which is available on GitHub. This will shield you from the details of the API.
However, this doesn't let you use this inside any random webpage if that's what you're attempting:
HTML pages using QuickBaseClient.js must be hosted within QuickBase
applications as file attachments or as text (code) pages because web
browsers only allow JavaScript code to access the web site the
JavaScript originated from.
If you do want do that then there are workarounds.

Categories

Resources