You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I just experienced quite unexpected KeyError exception the following code:
# pi is a `stripe.PaymentIntent`if'damage_deposit_amount'inpi.metadata:
delpi.metadata['damage_deposit_amount'] # <- raises `KeyError`
According to the information I have in my error report 'damage_deposit_amount' is indeed not in pi.metadata. So, I first thought 'key' in StripeObject always returns True.
However, after testing it in a python console I figured out the following:
In [1]: fromutils.stripeimportstripe# (= import stripe + stripe.api_key = settings.STRIPE['SECRET_KEY'])In [2]: pi=stripe.PaymentIntent.retrieve('pi_deadbeef')
In [3]: pi.metadataOut[3]:
<StripeObjectat0x7f92140c5098>JSON: {
"booking_id": "13506",
"damage_deposit_amount": "20000",
"waiting_for_webhook": "True"
}
In [4]: 'foobar'inpi.metadataOut[4]: FalseIn [5]: delpi.metadata['damage_deposit_amount']
---------------------------------------------------------------------------KeyErrorTraceback (mostrecentcalllast)
<ipython-input-5-e399e0ff7d29>in<module>---->1delpi.metadata['damage_deposit_amount']
/usr/share/virtualenvs/cocoonr/lib/python3.5/site-packages/stripe/stripe_object.pyin__delitem__(self, k)
136# Allows for unpickling in Python 3.x137ifhasattr(self, "_unsaved_values"):
-->138self._unsaved_values.remove(k)
139140# Custom unpickling method that uses `update` to update the dictionaryKeyError: 'damage_deposit_amount'In [6]: pi.metadataOut[6]:
<StripeObjectat0x7f92140c5098>JSON: {
"booking_id": "13506",
"waiting_for_webhook": "True"
}
In [7]: pi.save()
Out[7]:
<PaymentIntentpayment_intentid=pi_deadbeefat0x7f9214136598>JSON: {
...
"metadata": {
"booking_id": "13506",
"waiting_for_webhook": "True"
},
...
}
In [8]: pi=stripe.PaymentIntent.retrieve('pi_deadbeef')
In [9]: pi.metadataOut[9]:
<StripeObjectat0x7f92168b07c8>JSON: {
"booking_id": "13506",
"damage_deposit_amount": "20000",
"waiting_for_webhook": "True"
}
In [10]:
To sum it up:
del pi.metadata['damage_deposit_amount'] deleted the key, but raised a KeyError.
Saving the object returned an object without the deleted key.
Reloading the object showed that the key was actually not deleted.
The text was updated successfully, but these errors were encountered:
I just released version 2.33.1, using del should no longer throw a KeyError. Though note that to unset metadata keys you'd still need to use one of the two ways I showed in my previous message.
python: 3.5.3
stripe-python: 2.29.4
I just experienced quite unexpected
KeyError
exception the following code:According to the information I have in my error report
'damage_deposit_amount'
is indeed not inpi.metadata
. So, I first thought'key' in StripeObject
always returnsTrue
.However, after testing it in a python console I figured out the following:
To sum it up:
del pi.metadata['damage_deposit_amount']
deleted the key, but raised a KeyError.The text was updated successfully, but these errors were encountered: