如何从条带的收费响应中获取客户ID?

时间:2017-09-02 08:56:13

标签: stripe-payments

我正在尝试从计费实体的响应中获取条带客户的customer_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
}

但它在对象中有一个客户字段为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"
  }
}

2 个答案:

答案 0 :(得分:2)

如果仅使用信用卡令牌创建charge,则不会创建任何条带客户,您只需创建没有关联客户的付款。 因此,API返回customer: null是正常的。

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

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

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

有关详情,请点击此处:https://stripe.com/docs/quickstart#saving-card-information

有意义吗?

答案 1 :(得分:0)

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

for _, data := range customer.Sources.Values{
        card_id = data.ID
    }

    fmt.Println(card_id)

我花了将近一天的时间才弄明白。实际上在客户结构中(在条带包下),有一些字段具有嵌入类型,并且这些字段进一步连接到不同文件中的某些其他结构。因此,存在层次结构来访问上面的结构字段。

希望这能解决您的问题。

谢谢!

相关问题