【问题标题】:How to retrieve metadata from Stripe session object NodeJS如何从 Stripe 会话对象 NodeJS 中检索元数据
【发布时间】:2020-03-27 07:45:36
【问题描述】:

我正在尝试使用 Stripe Checkout 会话对象(使用 元数据)传递 UID 和购买 ID。在我的服务器上生成附加元数据的会话 ID 工作得很好。 Stripe 还将所有内容正确地发布到我的 webhook 服务器。从 Stripe 发布的会话对象中检索元数据时会出现问题。

这是我得到的错误

TypeError: Cannot read property 'metadata' of undefined at /app/app.js:35:32

这是 Stripe 发布的会话 obj-

{
  "id": "evt_1GRC7lAfcfWZXl7jQ3VzNo4y",
  "object": "event",
  "api_version": "2019-10-17",
  "created": 1585292221,
  "data": {
    "object": {
      "id": "cs_test_gLsHqtF8XhB3C3DlWKcLtNdTitp0St8ju5qgJgl6tHrMxxWvju9gb9Li",
      "object": "checkout.session",
      "billing_address_collection": null,
      "cancel_url": "https://andropaym.firebaseapp.com/fail.html",
      "client_reference_id": null,
      "customer": "cus_GzASi1Klpydh8x",
      "customer_email": null,
      "display_items": [
        {
          "amount": 37500,
          "currency": "inr",
          "custom": {
            "description": "Carefully modified Linux Distro Bundle for Android.",
            "images": null,
            "name": "Modded OS Bundle"
          },
          "quantity": 1,
          "type": "custom"
        }
      ],
      "livemode": false,
      "locale": null,
      "metadata": {
        "uid": "EB1m6nAOTVNcQhHO2O7COspap8y1",
        "payID": "GPA.5620-9852-7063-44324"
      },
      "mode": "payment",
      "payment_intent": "pi_1GRC7EAfcfWZXl7jhixrWHRS",
      "payment_method_types": [
        "card"
      ],
      "setup_intent": null,
      "shipping": null,
      "shipping_address_collection": null,
      "submit_type": null,
      "subscription": null,
      "success_url": "https://andropaym.firebaseapp.com/success.html"
    }
  },
  "livemode": false,
  "pending_webhooks": 4,
  "request": {
    "id": null,
    "idempotency_key": null
  },
  "type": "checkout.session.completed"
}

这是我的 webhook 代码 -

const express = require('express');

const app = express();
const bodyParser = require('body-parser');

const stripe = require('stripe')('sk_test_xxxx');
const endpointSecret = 'whsec_xxxx';

// set the port of our application
// process.env.PORT lets the port be set by Heroku
var port = process.env.PORT || 8080;


app.post('/', bodyParser.raw({type: 'application/json'}), (request, response) => {
const sig = request.headers['stripe-signature'];
let event;

try {
    event = stripe.webhooks.constructEvent(request.body, sig, endpointSecret);
} catch (err) {
    return response.status(400).send(`Webhook Error: ${err.message}`);
}

function handleCheckoutSession(uid) {
    // Here we are getting the session obj and we can process it to check for the things we need

    console.log("UID is " + uid);


}

// Handle the checkout.session.completed event
if (event.type === 'checkout.session.completed') {
    const session = event.data.object;
    let uid = request.data.metadata.uid;

    // Fulfill the purchase...
    handleCheckoutSession(uid);
}

// Return a response to acknowledge receipt of the event
response.json({received: true});
});

app.listen(port, function () {
console.log('Our app is running on http://localhost:' + port);
});

module.exports = app;

代码可以正常工作,无需解析元数据

更多代码链接: 1. 高亮错误webhook代码-https://gist.github.com/imprakharshukla/1e2315615983e0e9d492d2288e159832#file-webhook_backend-js-L40

【问题讨论】:

  • 在调用constructEventconsole.log(`event: ${event}`); console.log(`type: ${typeof event}`); 之后,您是否会立即添加event 及其type 的日志

标签: node.js json stripe-payments


【解决方案1】:

你需要使用stripe.constructEvent返回的对象,而不是请求体。

改变

let uid = request.data.metadata.uid;

let uid = session.metadata.uid

它应该按预期工作。

【讨论】:

    猜你喜欢
    • 2020-01-16
    • 1970-01-01
    • 1970-01-01
    • 2019-01-11
    • 1970-01-01
    • 2020-09-26
    • 2020-07-18
    • 2021-12-24
    • 1970-01-01
    相关资源
    最近更新 更多