【问题标题】:Python w/ Stripe: How do I get valid JSON from a charge response?Python w/ Stripe:如何从收费响应中获取有效的 JSON?
【发布时间】:2014-03-10 06:33:45
【问题描述】:

我有一个名为succeed 的python 包装器方法,如下所示:

def succeed(handler, data):
"""Send the given |data| dict as a JSON response in |handler.response|."""
set_headers(handler)
handler.response.write(json.dumps(data))

我正在尝试将 Stripe API 调用的结果传递给使用此方法从信用卡扣款的另一项服务。这是另一个类中的方法调用:

succeed(self, dict(success=True, charge_id=charge.id, response=charge))

当我这样做时,我收到“费用不是 JSON 可序列化”错误。如何使用此代码将所有费用 ID 响应作为 JSON 传递?以下是完整回复:

    TypeError: <Charge charge id=ch_103Tsv2kD9PLZlzDG5ce7TE1 at 0x113003b50> JSON: {
  "amount": 3500, 
  "amount_refunded": 0, 
  "balance_transaction": "xxxxxx", 
  "captured": true, 
  "card": {
    "address_city": null, 
    "address_country": null, 
    "address_line1": null, 
    "address_line1_check": null, 
    "address_line2": null, 
    "address_state": null, 
    "address_zip": null, 
    "address_zip_check": null, 
    "country": "US", 
    "customer": null, 
    "cvc_check": "pass", 
    "exp_month": 5, 
    "exp_year": 2015, 
    "fingerprint": "xxxxxxxxxxxxxx", 
    "last4": "4242", 
    "name": "stackoverflow@example.com", 
    "object": "card", 
    "type": "Visa"
  }, 
  "created": 1392181282, 
  "currency": "usd", 
  "customer": null, 
  "description": "X0G0 FEOMSI NA", 
  "dispute": null, 
  "failure_code": null, 
  "failure_message": null, 
  "invoice": null, 
  "livemode": false, 
  "metadata": {
    "email": "stackoverflow@exmple.com"
  }, 
  "object": "charge", 
  "paid": true, 
  "refunded": false, 
  "refunds": []
}

【问题讨论】:

    标签: python json stripe-payments


    【解决方案1】:

    使用.to_dict() 方法将Stripe 电荷对象转换为python 字典。

    序列化字典是留给读者的练习。


    作为一个有趣的点,我强烈推荐dir 函数:它可以让你看到所有可能的属性和方法。

    例如:

    >>> import stripe
    >>> dir(stripe.Charge)
    ['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__', '__getattr__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'all', 'capture', 'class_name', 'class_url', 'clear', 'close_dispute', 'construct_from', 'copy', 'create', 'fromkeys', 'get', 'has_key', 'instance_url', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'refresh', 'refresh_from', 'refund', 'request', 'retrieve', 'save', 'serialize', 'serialize_metadata', 'setdefault', 'stripe_id', 'to_dict', 'update', 'update_dispute', 'values', 'viewitems', 'viewkeys', 'viewvalues']
    >>>
    

    从这里你可以看到to_dict 方法。您还可以看到 serialize 方法,虽然我不清楚它的作用。

    More docs

    【讨论】:

    • 实际上,在我使用的条带库版本中,这有点浅序列化。我能够通过以下方式解决这个问题: json.loads(str(stripe_object)) 给你一个真正的字典,没有条纹对象,只有纯数据。
    • 我还可以确认“.to_dict()”没有产生可序列化的 json,但是使用@ustun 建议的方法确实有效。 json.loads(str(stripe_object))
    【解决方案2】:

    虽然已经晚了,但以下答案可能对某人有所帮助

    根据条纹团队的说法,to_dict 方法已被弃用。 See here

    由于Stripe对象是dict的继承,我们可以将它们用作原生数据类型。

    同时调用str("Stripe Object") 将返回对象的JSON 表示。 See here

    【讨论】:

      【解决方案3】:

      感谢this change,您现在可以拨打电话了

      stripe.util.convert_to_dict(your_stripe_object)
      

      得到一个递归构造的dict

      【讨论】:

        猜你喜欢
        • 2019-05-08
        • 2018-02-11
        • 1970-01-01
        • 2017-07-31
        • 2020-12-16
        • 2019-01-12
        • 2020-12-07
        • 2021-12-19
        • 1970-01-01
        相关资源
        最近更新 更多