django-orchestra/orchestra/contrib/payments/serializers.py

46 lines
1.5 KiB
Python
Raw Normal View History

2014-07-23 16:24:56 +00:00
from rest_framework import serializers
2015-04-05 10:46:24 +00:00
from orchestra.contrib.accounts.serializers import AccountSerializerMixin
2014-07-23 16:24:56 +00:00
from .methods import PaymentMethod
from .models import PaymentSource, Transaction
2014-07-23 16:24:56 +00:00
class PaymentSourceSerializer(AccountSerializerMixin, serializers.HyperlinkedModelSerializer):
2014-07-23 16:24:56 +00:00
class Meta:
model = PaymentSource
2015-04-30 09:51:55 +00:00
fields = ('url', 'id', 'method', 'data', 'is_active')
2015-07-29 09:05:07 +00:00
def validate(self, data):
""" validate data according to method """
data = super(PaymentSourceSerializer, self).validate(data)
plugin = PaymentMethod.get(data['method'])
serializer_class = plugin().get_serializer()
2015-07-29 09:05:07 +00:00
serializer = serializer_class(data=data['data'])
if not serializer.is_valid():
raise serializers.ValidationError(serializer.errors)
2015-07-29 09:05:07 +00:00
return data
def transform_data(self, obj, value):
if not obj:
return {}
if obj.method:
plugin = PaymentMethod.get(obj.method)
serializer_class = plugin().get_serializer()
return serializer_class().to_native(obj.data)
return obj.data
2015-04-23 14:34:04 +00:00
# TODO
def metadata(self):
meta = super(PaymentSourceSerializer, self).metadata()
meta['data'] = {
2014-11-12 16:33:40 +00:00
method.get_name(): method().get_serializer()().metadata()
for method in PaymentMethod.get_plugins()
}
return meta
2014-07-23 16:24:56 +00:00
class TransactionSerializer(AccountSerializerMixin, serializers.HyperlinkedModelSerializer):
2014-07-23 16:24:56 +00:00
class Meta:
model = Transaction