django-payments
django-payments copied to clipboard
Cybersource payments not working
Python version: 3.11.2
When using version 1.0.0 from pip, I have the following error:
Object of type RuleResultItem is not JSON serializable
I added the following function which I found here: https://stackoverflow.com/a/34844428
import datetime
def object_to_dict(obj):
if isinstance(obj, (str, bool, int, float, datetime.datetime, datetime.date, datetime.time, dict)):
return obj
data_dict = {}
try:
all_keys = obj.__dict__.keys() # vars(obj).keys()
except AttributeError:
return obj
fields = [k for k in all_keys if not k.startswith('_')]
for field in fields:
val = getattr(obj, field)
if isinstance(val, (list, tuple)):
data_dict[field] = []
for item in val:
data_dict[field].append(object_to_dict(item))
else:
data_dict[field] = object_to_dict(val)
return data_dict
And used it in models.py:
class PaymentAttributeProxy:
# ...
def __setattr__(self, key, value):
if key == "_payment":
return super().__setattr__(key, value)
try:
data = json.loads(self._payment.extra_data)
except ValueError:
data = {}
data[key] = value
self._payment.extra_data = json.dumps(data, default=object_to_dict) # <= here
After that it seems to work perfectly, but it isn't an ideal solution.
Would anyone know why this error appears and a better way to fix it?
Also, the pip package should probably receive an update if there is a possibility of fixing this, since there is another Cybersource-related fix that's not released yet.
Was this working for you with a previous version of django-payments?