I have a python wrapper method called succeed
that looks like the following:
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. 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. How can I pass all of the charge ID responses along as JSON with this code? 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": "[email protected]",
"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": "[email protected]"
},
"object": "charge",
"paid": true,
"refunded": false,
"refunds": []
}
Use the .to_dict()
method to turn a Stripe charge object into a python dictionary.
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.
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. You can also see the serialize
method, though it's not clear to me what it does.
More docs
Though its late, the following answer may help someone
As per the stripe team to_dict
method is deprecated. See here
Since the Stripe object is the inheritance of dict
, we can use them as native data type.
Also calling str("Stripe Object")
will return JSON representation of the object. See here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With