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

43 lines
1.4 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
fields = ('url', 'method', 'data', 'is_active')
def validate_data(self, attrs, source):
plugin = PaymentMethod.get(attrs['method'])
serializer_class = plugin().get_serializer()
serializer = serializer_class(data=attrs[source])
if not serializer.is_valid():
raise serializers.ValidationError(serializer.errors)
return attrs
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
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