【问题标题】:How to get customer id from a charge response of stripe?如何从条带的收费响应中获取客户 ID?
【发布时间】:2018-02-11 05:19:02
【问题描述】:

我正在尝试从收费实体的响应中获取条带客户的 customer_id。但响应并未提供客户 ID 作为回报。

&stripe.Charge JSON: {
  "id": "ch_1AxWbTFytruJp2FXW6iuRd1X",
  "object": "charge",
  "amount": 100,
  "amount_refunded": 0,
  "application": null,
  "application_fee": null,
  "balance_transaction": "txn_17JOXKFytruJp2FXS4XNisQd",
  "captured": false,
  "created": 1504339423,
  "currency": "usd",
  "customer": null,
  "description": "My First Test Charge (created for API docs)",
  "destination": null,
  "dispute": null,
  "failure_code": null,
  "failure_message": null,
  "fraud_details": {
  },
  "invoice": null,
  "livemode": false,
  "metadata": {
  },
  "on_behalf_of": null,
  "order": null,
  "outcome": null,
  "paid": true,
  "receipt_email": null,
  "receipt_number": null,
  "refunded": false,
  "refunds": {
    "object": "list",
    "data": [

    ],
    "has_more": false,
    "total_count": 0,
    "url": "/v1/charges/ch_1AxWbTFytruJp2FXW6iuRd1X/refunds"
  },
  "review": null,
  "shipping": null,
  "source": {
    "id": "card_1AxWPmFytruJp2FXw4m0V0fN",
    "object": "card",
    "address_city": null,
    "address_country": null,
    "address_line1": null,
    "address_line1_check": null,
    "address_line2": null,
    "address_state": null,
    "address_zip": "10001",
    "address_zip_check": "unchecked",
    "brand": "Visa",
    "country": "US",
    "customer": null,
    "cvc_check": "unchecked",
    "dynamic_last4": null,
    "exp_month": 4,
    "exp_year": 2024,
    "fingerprint": "sNtyd9sZ2vA6o4IM",
    "funding": "credit",
    "last4": "4242",
    "metadata": {
    },
    "name": "Mandeep",
    "tokenization_method": null
  },
  "source_transfer": null,
  "statement_descriptor": null,
  "status": "succeeded",
  "transfer_group": null
}

但它在对象内部有一个客户字段,该字段为空。谁能告诉我为什么我会得到这个空值?

我想做的是建立一个系统,客户可以在该系统上匿名预订,并在创建预订时注册客户并按预订总额收取费用。我需要跟踪客户的条带帐户 ID 和卡 ID。所以问题是,如果我正在创建客户,那么我无法获得其卡 ID,但是当我向客户收费时,我无法获得客户 ID。

客户反应:

&stripe.Customer JSON: {
  "id": "cus_BKAxGZre2HCNIU",
  "object": "customer",
  "account_balance": 0,
  "created": 1504339424,
  "currency": "usd",
  "default_source": null,
  "delinquent": false,
  "description": null,
  "discount": null,
  "email": null,
  "livemode": false,
  "metadata": {
  },
  "shipping": null,
  "sources": {
    "object": "list",
    "data": [

    ],
    "has_more": false,
    "total_count": 0,
    "url": "/v1/customers/cus_BKAxGZre2HCNIU/sources"
  },
  "subscriptions": {
    "object": "list",
    "data": [

    ],
    "has_more": false,
    "total_count": 0,
    "url": "/v1/customers/cus_BKAxGZre2HCNIU/subscriptions"
  }
}

【问题讨论】:

    标签: stripe-payments


    【解决方案1】:

    当您仅使用信用卡令牌创建 charge 时,不会创建 Stripe 客户,您只会创建没有关联客户的付款。 所以你API返回customer: null是正常的。

    我认为您应该向新客户收费,而不是向信用卡收费。 在您的后端代码中,您可以分两步处理付款:

    • STEP 1:创建一个新客户,将信用卡令牌传递给存储 客户卡
    • 步骤 2:使用返回的客户 ID 向客户收费 第 1 步 API 调用。

    这样做,您使用存储在客户数据中的信用卡向客户收费。

    更多详情,请查看:https://stripe.com/docs/quickstart#saving-card-information

    有意义吗?

    【讨论】:

      【解决方案2】:

      您可以在使用以下代码(在 Golang 中)创建新客户时访问 card_id:

      for _, data := range customer.Sources.Values{
              card_id = data.ID
          }
      
          fmt.Println(card_id)
      

      我花了将近 1 天的时间来弄清楚。实际上在客户结构中(在条带包下)有一些具有嵌入式类型的字段,这些字段进一步连接到不同文件中的一些其他结构。所以有层次结构可以访问上面的结构字段。

      希望这能解决您的问题。

      谢谢!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-10
        • 2022-11-09
        • 2021-12-30
        • 2016-10-07
        相关资源
        最近更新 更多