我目前在节点上本地运行此代码:
const express = require('express')
const bodyParser = require('body-parser');
var app = express()
const port = 3000
const stripe = require('stripe')('############');
const endpointSecret = '###########'
app.use('/', express.static('static'))
app.get('/checkout', async (req, res) => {
const token = await stripe.checkout.sessions.create({
payment_method_types: ['card'],
line_items: [...],
success_url: req.protocol + '://' + req.get('host') +'/?session_id={CHECKOUT_SESSION_ID}',
cancel_url: req.protocol + '://' + req.get('host') +'/?fail',
})
res.send(token)
})
app.post(
'/webhook',
bodyParser.raw({type: 'application/json'}),
(req, res) => {
const sig = req.headers['stripe-signature'];
let event;
try {
event = stripe.webhooks.constructEvent(req.body, sig, endpointSecret);
} catch (err) {
// On error, return the error message
console.log(err)
return res.status(400).send(`Webhook Error: ${err.message}`);
}
// Do something with event
console.log('Success:', event.id);
// Return a response to acknowledge receipt of the event
res.json({received: true});
}
);
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
当Webhook收到来自Stripe的请求时,我一直收到此错误:
'StripeSignatureVerificationError'
我将其作为错误中的有效负载:
'{\n "id": "evt_#########",\n "object": "event",\n "api_version": "2019-12-03",\n "created": 1576148518,\n "data": {\n "object": {\n "id": "pi_##########k",\n "object": "payment_intent",\n "amount": 100,\n "amount_capturable": 0,\n "amount_received": 0,\n "application": null,\n "application_fee_amount": null,\n "canceled_at": null,\n "cancellation_reason": null,\n "capture_method": "automatic",\n "charges": {\n "object": "list",\n "data": [\n\n ],\n "has_more": false,\n "total_count": 0,\n "url": "/v1/charges?payment_intent=pi_1F#################k"\n },\n "client_secret": "pi_1Fo###########_secret_P#####",\n "confirmation_method": "automatic",\n "created": 1576148518,\n "currency": "gbp",\n "customer": null,\n "description": null,\n "invoice": null,\n "last_payment_error": null,\n "livemode": false,\n "metadata": {\n },\n "next_action": null,\n "on_behalf_of": null,\n "payment_method": null,\n "payment_method_options": {\n "card": {\n "installments": null,\n
"request_three_d_secure": "automatic"\n }\n },\n "payment_method_types": [\n "card"\n ],\n "receipt_email": null,\n "review": null,\n "setup_future_usage": null,\n "shipping": null,\n "source": null,\n "statement_descriptor": null,\n "statement_descriptor_suffix": null,\n "status": "requires_payment_method",\n "transfer_data": null,\n "transfer_group": null\n }\n },\n "livemode": false,\n "pending_webhooks": 2,\n "request": {\n "id": "req_PE####z#####GjT0c",\n "idempotency_key": null\n },\n "type": "payment_intent.created"\n}'
此基于:https://github.com/stripe/stripe-node/blob/master/examples/webhook-signing/express.js 但是没有运气。
我正在使用条纹CLI在本地重定向Webhooks。
谢谢!