简单地说,webhook 是一个 API 端点,当事件发生时外部服务调用它。 以下是 Web 开发中常用的 webhook 的几个示例:
关键要点是外部服务负责调用您的 webhook。您不必直接在您的应用程序中处理 SMS 消息,您可以依靠 Twilio 将 SMS 消息转换为对您的 API 的 HTTP 请求 。
通常像 Twilio 这样的服务会要求您输入 Webhook 的 URL。该 URL 必须是可公开访问的。 如果你想暴露 localhost
对于 Twilio webhook,您应该使用像 ngrok 。
一个 PayPal IPN 网络钩子
打印请求正文 模拟的 PayPal IPN webhook 。
<code class="language-javascript">use strict;const express = require(express);run().catch(err => console.log(err));async function run() {const app = express();app.use(require(body-parser).text({ type: () => true }));app.all(/ipn, (req, res) => {console.log(Got, req.body);return res.send(SENT);});await app.listen(3000);console.log(Listening on port 3000);}</code><code class="language-javascript">use strict; const express = require(express); run().catch(err => console.log(err)); async function run() { const app = express(); app.use(require(body-parser).text({ type: () => true })); app.all(/ipn, (req, res) => { console.log(Got, req.body); return res.send(SENT); }); await app.listen(3000); console.log(Listening on port 3000); }</code>
use strict;const express = require(express);run().catch(err => console.log(err));async function run() {const app = express();app.use(require(body-parser).text({ type: () => true }));app.all(/ipn, (req, res) => {console.log(Got, req.body);return res.send(SENT);});await app.listen(3000);console.log(Listening on port 3000);}use strict; const express = require(express); run().catch(err => console.log(err)); async function run() { const app = express(); app.use(require(body-parser).text({ type: () => true })); app.all(/ipn, (req, res) => { console.log(Got, req.body); return res.send(SENT); }); await app.listen(3000); console.log(Listening on port 3000); }use strict; const express = require(express); run().catch(err => console.log(err)); async function run() { const app = express(); app.use(require(body-parser).text({ type: () => true })); app.all(/ipn, (req, res) => { console.log(Got, req.body); return res.send(SENT); }); await app.listen(3000); console.log(Listening on port 3000); }
使用 PayPal 的 IPN 模拟器和 ngrok,您可以向上述服务器发送测试 webhook:

这是 webhook 通过时您应该看到的输出:
<code>$ node .Listening on port 3000Got payment_type=echeck&payment_date=12%3A28%3A46%20Jan%2012%2C%202020%20PST&payment_status=Completed&address_status=confirmed&payer_status=verified&first_name=John&last_name=Smith&payer_email=buyer@paypalsandbox.com&payer_id=TESTBUYERID01&address_name=John%20Smith&address_country=United%20States&address_country_code=US&address_zip=95131&address_state=CA&address_city=San%20Jose&address_street=123%20any%20street&business=seller@paypalsandbox.com&receiver_email=seller@paypalsandbox.com&receiver_id=seller@paypalsandbox.com&residence_country=US&item_name=something&item_number=AK-1234&quantity=1&shipping=3.04&tax=2.02&mc_currency=USD&mc_fee=0.44&mc_gross=12.34&mc_gross_1=12.34&txn_type=web_accept&txn_id=493449894¬ify_version=2.1&custom=xyz123&invoice=abc1234&test_ipn=1&verify_sign=AkGoM1YN05HZDh0W4ejvAx3.yitsADMYtkiSuk3NqAk7.VGXP0jlR9WB</code><code>$ node .
Listening on port 3000
Got payment_type=echeck&payment_date=12%3A28%3A46%20Jan%2012%2C%202020%20PST&payment_status=Completed&address_status=confirmed&payer_status=verified&first_name=John&last_name=Smith&payer_email=buyer@paypalsandbox.com&payer_id=TESTBUYERID01&address_name=John%20Smith&address_country=United%20States&address_country_code=US&address_zip=95131&address_state=CA&address_city=San%20Jose&address_street=123%20any%20street&business=seller@paypalsandbox.com&receiver_email=seller@paypalsandbox.com&receiver_id=seller@paypalsandbox.com&residence_country=US&item_name=something&item_number=AK-1234&quantity=1&shipping=3.04&tax=2.02&mc_currency=USD&mc_fee=0.44&mc_gross=12.34&mc_gross_1=12.34&txn_type=web_accept&txn_id=493449894¬ify_version=2.1&custom=xyz123&invoice=abc1234&test_ipn=1&verify_sign=AkGoM1YN05HZDh0W4ejvAx3.yitsADMYtkiSuk3NqAk7.VGXP0jlR9WB
</code>
$ node .Listening on port 3000Got payment_type=echeck&payment_date=12%3A28%3A46%20Jan%2012%2C%202020%20PST&payment_status=Completed&address_status=confirmed&payer_status=verified&first_name=John&last_name=Smith&payer_email=buyer@paypalsandbox.com&payer_id=TESTBUYERID01&address_name=John%20Smith&address_country=United%20States&address_country_code=US&address_zip=95131&address_state=CA&address_city=San%20Jose&address_street=123%20any%20street&business=seller@paypalsandbox.com&receiver_email=seller@paypalsandbox.com&receiver_id=seller@paypalsandbox.com&residence_country=US&item_name=something&item_number=AK-1234&quantity=1&shipping=3.04&tax=2.02&mc_currency=USD&mc_fee=0.44&mc_gross=12.34&mc_gross_1=12.34&txn_type=web_accept&txn_id=493449894¬ify_version=2.1&custom=xyz123&invoice=abc1234&test_ipn=1&verify_sign=AkGoM1YN05HZDh0W4ejvAx3.yitsADMYtkiSuk3NqAk7.VGXP0jlR9WB$ node .
Listening on port 3000
Got payment_type=echeck&payment_date=12%3A28%3A46%20Jan%2012%2C%202020%20PST&payment_status=Completed&address_status=confirmed&payer_status=verified&first_name=John&last_name=Smith&payer_email=buyer@paypalsandbox.com&payer_id=TESTBUYERID01&address_name=John%20Smith&address_country=United%20States&address_country_code=US&address_zip=95131&address_state=CA&address_city=San%20Jose&address_street=123%20any%20street&business=seller@paypalsandbox.com&receiver_email=seller@paypalsandbox.com&receiver_id=seller@paypalsandbox.com&residence_country=US&item_name=something&item_number=AK-1234&quantity=1&shipping=3.04&tax=2.02&mc_currency=USD&mc_fee=0.44&mc_gross=12.34&mc_gross_1=12.34&txn_type=web_accept&txn_id=493449894¬ify_version=2.1&custom=xyz123&invoice=abc1234&test_ipn=1&verify_sign=AkGoM1YN05HZDh0W4ejvAx3.yitsADMYtkiSuk3NqAk7.VGXP0jlR9WB
$ node .
Listening on port 3000
Got payment_type=echeck&payment_date=12%3A28%3A46%20Jan%2012%2C%202020%20PST&payment_status=Completed&address_status=confirmed&payer_status=verified&first_name=John&last_name=Smith&payer_email=buyer@paypalsandbox.com&payer_id=TESTBUYERID01&address_name=John%20Smith&address_country=United%20States&address_country_code=US&address_zip=95131&address_state=CA&address_city=San%20Jose&address_street=123%20any%20street&business=seller@paypalsandbox.com&receiver_email=seller@paypalsandbox.com&receiver_id=seller@paypalsandbox.com&residence_country=US&item_name=something&item_number=AK-1234&quantity=1&shipping=3.04&tax=2.02&mc_currency=USD&mc_fee=0.44&mc_gross=12.34&mc_gross_1=12.34&txn_type=web_accept&txn_id=493449894¬ify_version=2.1&custom=xyz123&invoice=abc1234&test_ipn=1&verify_sign=AkGoM1YN05HZDh0W4ejvAx3.yitsADMYtkiSuk3NqAk7.VGXP0jlR9WB
© 版权声明文章版权归作者所有,未经允许请勿转载。THE END
请登录后查看评论内容