Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cybersource payments not working #343

Open
Unarelith opened this issue Feb 18, 2023 · 1 comment
Open

Cybersource payments not working #343

Unarelith opened this issue Feb 18, 2023 · 1 comment

Comments

@Unarelith
Copy link

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.

@WhyNotHugo
Copy link
Member

Was this working for you with a previous version of django-payments?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants