我们正在尝试在我们的项目中执行贝宝付款。我们使用了沙盒环境,并使用了paypal-rest-sdk并成功完成了付款。但是,我们尝试实施webhook,以便在连续付款后获得webhook响应/通知。我们实现了如下所示的webhooks,在成功端点被点击但响应为空之后,我们确实获得了webhooks的响应!
我们尝试了很多谷歌搜索和阅读文档,但都无济于事。如果那里的某个人以前使用paypal-rest-sdk做到了这一点,请帮助我们。
//webhooks.js
const paypal = require("paypal-rest-sdk");
let webhook_json = {
url: "https://ac193954.ngrok.io/ewebhook",
event_types: [
{
name: "PAYMENT.AUTHORIZATION.CREATED"
},
{
name: "BILLING.SUBSCRIPTION.CREATED"
},
{
name: "PAYMENT.CAPTURE.COMPLETED"
}
]
};
let sync = async () => {
return new Promise((resolve, reject) => {
paypal.notification.webhook.create(webhook_json, function(error, webhook) {
if (error) {
reject(error);
} else {
resolve();
}
});
});
};
module.exports = { paypalWebhook: sync };
//server.js
const { paypalWebhook } = require("./webhooks");
app.get("/success", (req, res) => {
let paymentToken = req.query.token;
paypal.billingAgreement.execute(paymentToken, {}, function(
error,
billingAgreement
) {
if (error) {
throw error;
} else {
console.log("Billing agreement", JSON.stringify(billingAgreement));
let date = billingAgreement.start_date;
let amountString =
billingAgreement.plan.payment_definitions[1].amount.value;
let trial =
billingAgreement.plan.payment_definitions[0].frequency_interval;
let frequencyInterval =
billingAgreement.plan.payment_definitions[1].frequency_interval;
let frequency = billingAgreement.plan.payment_definitions[1].frequency;
let totalFrequency = frequencyInterval + " " + frequency;
let period = [trial, totalFrequency];
let amount = parseInt(amountString);
try {
Payment.create({
id: uuidv1(),
date: date,
amount: amount,
period: period
});
} catch (err) {
throw new Error(err);
}
res.render("index");
}
});
});
app.get("/cancel", (req, res) =>
res.send("Transaction cancelled please go back to the home page.")
);
try {
let resultWebhook = await paypalWebhook();
if (typeof resultWebhook == "Error") {
return;
}
console.log("result", resultWebhook);
} catch (err) {
console.log("Webhook error", err);
}
//This route is being triggered about 5 seconds after the /success
route is triggered, but there are no any responses.
app.post("/ewebhook", (req, res) => {
console.log("Webhook req res", req, res);
});
这是执行/ success路由后对/ ewebhook的请求: https://gist.github.com/BikalNepal/4d2fd47063538d772c4432fa6d19c26d
以下是响应: https://gist.github.com/BikalNepal/4f09eb79e3204be624bef239f945d971