Hi! I (ChatGPT xD) have created an appwrite function with node.js, which looks like this:
const sdk = require("node-appwrite");
const stripe = require("stripe");
module.exports = async function (req, res) {
const client = new sdk.Client();
const stripeClient = stripe(req.variables["STRIPE_SECRET_KEY"]);
if (
!req.variables["APPWRITE_FUNCTION_ENDPOINT"] ||
!req.variables["APPWRITE_FUNCTION_API_KEY"]
) {
console.warn(
"Environment variables are not set. Function cannot use Appwrite SDK."
);
} else {
client
.setEndpoint(req.variables["APPWRITE_FUNCTION_ENDPOINT"])
.setProject(req.variables["APPWRITE_FUNCTION_PROJECT_ID"])
.setKey(req.variables["APPWRITE_FUNCTION_API_KEY"])
.setSelfSigned(true);
}
let data = req.payload;
try {
let event = stripeClient.webhooks.constructEvent(
data,
req.headers["stripe-signature"],
"your-stripe-webhook-secret"
);
if (event.type === "checkout.session.completed") {
let session = event.data.object;
let user_id = session.metadata.user_id;
console.log(
`Payment for user ${user_id} was successful.`
);
//Here, a database update is going to be made to mark as successful
}
res.send("Webhook handled successfully.", 200);
} catch (err) {
console.error(`Failed to handle webhook: ${err}`);
res.send("Webhook handling failed.", 500);
}
};
And I created a webhook in stripe, that should create an execution on that function (Reference: Image).
This webhook triggers on the checkout.session.complete event.
So when I trigger the event of a successful payment, I get the following error:
(Next message)