[英]Python w/ Stripe: How do I get valid JSON from a charge response?
I have a python wrapper method called succeed
that looks like the following: 我有一个名为
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))
I am trying to pass the result of a Stripe API call to charge a credit card back to another service using this method. 我试图传递Stripe API调用的结果,使用此方法将信用卡收回另一个服务。 Here is the method call, inside of another class:
这是方法调用,在另一个类中:
succeed(self, dict(success=True, charge_id=charge.id, response=charge))
When I do so, I get a 'charge is not JSON serializable' error. 当我这样做时,我得到'充电不是JSON可序列化'错误。 How can I pass all of the charge ID responses along as JSON with this code?
如何使用此代码将所有费用ID响应作为JSON传递? Here is the full response:
以下是完整的回复:
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": []
}
Use the .to_dict()
method to turn a Stripe charge object into a python dictionary. 使用
.to_dict()
方法将Stripe计费对象转换为python字典。
Serializing a dict is an exercise left to the reader. 序列化词典是留给读者的练习。
As an additional fun point, I highly suggest the dir
function: It lets you see all the possible attributes and methods. 作为一个额外的有趣点,我强烈建议使用
dir
函数:它可以让你看到所有可能的属性和方法。
For example: 例如:
>>> 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']
>>>
From here you can see the to_dict
method. 从这里你可以看到
to_dict
方法。 You can also see the serialize
method, though it's not clear to me what it does. 你也可以看到
serialize
方法,虽然我不清楚它的作用。
Though its late, the following answer may help someone 虽然很晚,但以下答案可能对某人有所帮助
As per the stripe team to_dict
method is deprecated. 根据条带团队,
to_dict
方法已被弃用。 See here 看这里
Since the Stripe object is the inheritance of dict
, we can use them as native data type. 由于Stripe对象是
dict
的继承,我们可以将它们用作本机数据类型。
Also calling str("Stripe Object")
will return JSON representation of the object. 同样调用
str("Stripe Object")
将返回对象的JSON表示。 See here 看这里
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.