Stripe Webhook constructEvent method is returning 400 error when pointed to ec2 instance - node.js

I have developed my application in windows OS and integrated Stripe webhook with the help of ngrok. Everything was smooth and I was able to receive the events from webhook. but once I moved it to ec2 instance on AWS Cloud, it is throwing me an error.
Error: (In Stripe Dashboard Webhook Attempts Section)
Webhook Error: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received undefined
It is strange that have not changed any piece of code and it still works on windows.
Complete Error:
TypeError [ERR_INVALID_ARG_TYPE]: The "key" argument must be of type string or an instance of Buffer, TypedArray, DataView, or KeyObject
. Received undefined
at prepareSecretKey (internal/crypto/keys.js:322:11)
at new Hmac (internal/crypto/hash.js:111:9)
at Object.createHmac (crypto.js:147:10)
at Object._computeSignature (/home/psuser/middleware/node_modules/stripe/lib/Webhooks.js:65:8)
at Object.verifyHeader (/home/psuser/middleware/node_modules/stripe/lib/Webhooks.js:107:36)
at Object.constructEvent (/home/psuser/middleware/node_modules/stripe/lib/Webhooks.js:12:20)
at /home/psuser/middleware/routes/carts.js:210:29
at Layer.handle [as handle_request] (/home/psuser/middleware/node_modules/express/lib/router/layer.js:95:5)
at next (/home/psuser/middleware/node_modules/express/lib/router/route.js:137:13)
at /home/psuser/middleware/node_modules/body-parser/lib/read.js:130:5
at invokeCallback (/home/psuser/middleware/node_modules/raw-body/index.js:224:16)
at done (/home/psuser/middleware/node_modules/raw-body/index.js:213:7)
at IncomingMessage.onEnd (/home/psuser/middleware/node_modules/raw-body/index.js:273:7)
at IncomingMessage.emit (events.js:327:22)
at endReadableNT (_stream_readable.js:1327:12)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
code: 'ERR_INVALID_ARG_TYPE'
}
The "key" argument must be of type string or an instance of Buffer, TypedArray, DataView, or KeyObject. Received undefined
// End point Code
router.post('/webhook', async (request, response) => {
const sig = request.headers['stripe-signature'];
let event;
try {
event = stripe.webhooks.constructEvent(request.rawBody, sig, process.env.STRIPE_WEBHOOK_SECRET);
}
catch (err) {
console.log(err.message);
return response.status(400).send(`Webhook Error: ${err.message}`);
}
});
// In app.js
app.use(express.json({
// Because Stripe needs the raw body, we compute it but only when hitting the Stripe callback URL.
verify: function (req, res, buf) {
var url = req.originalUrl;
if (url.endsWith('/webhook')) {
req.rawBody = buf.toString()
}
}
}));

You can install micro (npm install micro) which has its buffer method
const { buffer } = require('micro')
in order to create the required type for the request:
const reqBuffer = await buffer(req)
then you can do:
event = stripe.webhooks.constructEvent(reqBuffer, sig, process.env.STRIPE_WEBHOOK_SECRET);
But I think your problem is simply an ENV variable missing in your EC2 server did you export STRIPE_WEBHOOKS_SECRET=secret in your server?

Related

What format should data sent to a Google Cloud Functions onCall request be in?

I have the most simple Google onCall cloud function:
// [START]
exports.echo = functions.https.onCall(async (data, context) => {
return {
value: "echo"
};
});
// [END]
I submit the following in the Google cloud console test:
{"data":"somedata"}
I am getting the following error:
{"error":{"message":"INTERNAL","status":"INTERNAL"}}
It seems there is an issue with the object that I am passing. Can anyone tell me what the correct format is? What am I missing here?
P.s: I am ultimately trying to connect an onCall function with a client application via Firebase/fire, but that is also giving the same error.
Here's the trace:
/workspace/node_modules/firebase-functions/lib/common/providers/https.js:349:16 at fixedLen (/workspace/node_modules/firebase-functions/lib/providers/https.js:66:41) at /workspace/node_modules/firebase-functions/lib/common/providers/https.js:385:32 at processTicksAndRejections (internal/process/task_queues.js:95:5)
Unhandled error TypeError: res.on is not a function at /workspace/node_modules/firebase-functions/lib/common/providers/https.js:350:17 at new Promise () at /workspace/node_modules/firebase-functions/lib/common/providers/https.js:349:16 at fixedLen (/workspace/node_modules/firebase-functions/lib/providers/https.js:66:41) at /workspace/node_modules/firebase-functions/lib/common/providers/https.js:385:32 at processTicksAndRejections (internal/process/task_queues.js:95:5)
Here's the index.ts:
import * as functions from "firebase-functions";
// The Firebase Admin SDK to access Cloud Firestore.
import admin = require("firebase-admin");
// Triggers
import { echo } from "./triggers/echo"
admin.initializeApp();
// Exports
module.exports = {
// OnCall
echo: functions.https.onCall(echo)
};
A few things:
The arrow function signature was incorrect.
async is not required for this synchronous response.
The correct type of the context parameter is CallableContext:
import * as functions from 'firebase-functions';
export const echo = functions.https.onCall((data:any, context:functions.https.CallableContext) => ({
value: "echo"
}))

when copy node js custom error class super Error variable is gone

I have a custom Err class that extends from Error class. all thing was good until I copy an instance of Err class. the super's variables of copied instance are undefined!
here is my code:
class Err extends Error {
constructor(code, msg) {
super(msg);
this.code = code;
}
}
const error = new Err(404, 'error message');
console.log(error);
/* { Error: error message
at repl:1:15
at Script.runInThisContext (vm.js:122:20)
at REPLServer.defaultEval (repl.js:332:29)
at bound (domain.js:402:14)
at REPLServer.runBound [as eval] (domain.js:415:12)
at REPLServer.onLine (repl.js:642:10)
at REPLServer.emit (events.js:203:15)
at REPLServer.EventEmitter.emit (domain.js:448:20)
at REPLServer.Interface._onLine (readline.js:308:10)
at REPLServer.Interface._line (readline.js:656:8) code: 404 }
*/
const copy = { ...error };
console.log(copy);
// { code: 404 }
// where is error.stack and error.name?
SOLUTION:
Change copy method to this
const copy= { ...error };
copy.name = error.name;
copy.message = error.message;
copy.stack = error.stack;
tanks PA.
if any one has a better solution tell it to me.
with copy = { ...error };
(1) the {} object literal operator, creates a new Object, not a new Err object,
and
(2) the ... or spread properties operator, for object literals, just copies the provided object own enumerable properties onto your new object.
To clone any javascript class instance with a perfect copy of properties, methods, getters/setters, non-enumerable properties, etc, ina a generic code is almost impossible. You may create your own copy/clone code for your particular case, with a combination of Object.assign() / Object.getPrototype() and custom tuning for inherited properties and internal classes.

TypeError: Cannot read property 'then' of null | discord.js

I am trying to make a message tracker and this error shows and I don't know why
Code: messagecounter.js
const db = require('quick.db');
module.exports = {
name: "msgc",
description: "Message Counter",
async run(client, message, args) {
// checking who wants to fetch it
let member = message.mentions.members.first() || message.member; // this checks if they mentioned a members
db.fetch(`messageSent_${member.id}`).then(obj => {
message.channel.send(`**Messages Sent:** \`${obj.value}\``);
});
}
}
Code: bot.js:70:42
client.commands.get(command).run(client, message, args);
Error:
UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'then' of null
at Object.run (C:\Users\Familia\OneDrive\Documents\Other Stuff\Visual Studio code\blade\commands\messagecounter.js:13:45)
at Client.<anonymous> (C:\Users\Familia\OneDrive\Documents\Other Stuff\Visual Studio code\blade\bot.js:70:42)
Any help would be appreciated
Using discord.js v12
After quickly glancing over "quick.db" I couldn't find a method called fetch defined on the db object. "get", however is defined and is perhaps what you meant to use.

Json cannot read property of undefined

When I execute a command (the error doesn't occur when I type a normal message) in my discord bot I get an error saying:
Cannot read property 'active' of undefined
And it occurs when I try to console log an object from a json file where I store users data.
Those are the first lines of code of the index.js file of my bot, the line where I try to console log is where the error occurs
const Discord = require('discord.js');
const client = new Discord.Client();
const fs = require("fs");
const prefix = '>';
let xp = require("./storage/dbGeneral.json");
let pr = require("./storage/dbPremium.json");
let lv = require("./storage/levels.json");
client.on('message', message => {
console.log(pr[message.author.id].active);
}
This is the json file where I store the data
{
"397387465024864257": {
"active": false,
"dateStart": "",
"dateEnd": ""
}
}
This is the error:
index.js:14
console.log(pr[message.author.id].active);
^
TypeError: Cannot read property 'active' of undefined
at Client.client.on.message (index.js:14:39)
at emitOne (events.js:116:13)
at Client.emit (events.js:211:7)
at MessageCreateHandler.handle (\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34)
at WebSocketPacketManager.handle (\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:103:65)
at WebSocketConnection.onPacket (\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:333:35)
at WebSocketConnection.onMessage (\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:296:17)
at WebSocket.onMessage (\node_modules\ws\lib\event-target.js:120:16)
at emitOne (events.js:116:13)
at WebSocket.emit (events.js:211:7)
I really don't know what is the cause of the error, all other json requests work fine with the same method.
The error is caused by trying to access a property of something not defined.
To fix your problem, you must check if the user exists in your database.
let user = pr[message.author.id];
if(user) console.log(user.active)

RESTIFY: Error: socket hang up] code: 'ECONNRESET' on multiple requests

I am implementing a node app, which brings in order details from BigCommerce.
Multiple calls are made to BigCommerce API asynchronously using Restify JsonClient.
It works fine for some calls but after that i gives error: [Error: socket hang up] code: 'ECONNRESET', sslError: undefined, body: {}
I have tried turning off socket pooling ie by setting agent=false, but it still gives same error.
Following is code which makes call to BigCommerce API
makeRequest = function (url, params, headers, orderDetails, cb) {
var options = {
headers: headers
};
var client = restify.createJsonClient({
url: url
});
client.get(options, function(err, req, res, obj) {
if(err){
console.log(err);
cb(err,obj);
} else if(obj != null) {
var result = obj;
if(orderDetails == null) {
cb(null,result);
} else {
cb(null, result , orderDetails);
}
}
});
};
I get following error:
{ [Error: socket hang up] code: 'ECONNRESET', sslError: unde
fined, body: {} } Error: socket hang up
at SecurePair.error (tls.js:993:23)
at EncryptedStream.CryptoStream._done (tls.js:689:22)
at CleartextStream.read [as _read] (tls.js:490:24)
at CleartextStream.Readable.read (_stream_readable.js:320:10)
at EncryptedStream.onCryptoStreamFinish (tls.js:301:47)
at EncryptedStream.g (events.js:175:14)
at EncryptedStream.EventEmitter.emit (events.js:117:20)
at finishMaybe (_stream_writable.js:352:12)
at endWritable (_stream_writable.js:359:3)
at EncryptedStream.Writable.end (_stream_writable.js:337:5)
at EncryptedStream.CryptoStream.end (tls.js:628:31)
at Socket.onend (_stream_readable.js:483:10)
Why am i getting such error? How can i handle it?
Thanks
I just wanted to make sure that you're setting the agent setting in the right area.
Include the
"agent": false
in your options. (It's not set in the options in the code you pasted)
Per gfpacheco in the comments here: https://github.com/restify/node-restify/issues/485
By default NodeJS uses agents to keep the TCP connection open, so you can reuse it.
The problem is that if the server is closed, or it closes your connection for whatever reason you get the ECONNRESET error.
To close the connection every time you just need to set agent: false in your client creation
I've tried this solution and it worked for me.
Other than that, the
"secureOptions": "constants.SSL_OP_NO_TLSv1_2"
solution posted here sounds like it could be the right path, since you're getting an sslError.
Maybe you are running into this issue https://github.com/joyent/node/issues/5360
TL;DR: You could try with latest node version and secureOptions: constants.SSL_OP_NO_TLSv1_2 added to your options.

Resources