2014-07-23 16:24:56 +00:00
|
|
|
from rest_framework import serializers
|
|
|
|
|
2014-07-28 17:28:00 +00:00
|
|
|
from orchestra.apps.accounts.serializers import AccountSerializerMixin
|
2014-07-23 16:24:56 +00:00
|
|
|
|
2014-07-28 17:28:00 +00:00
|
|
|
from .methods import PaymentMethod
|
|
|
|
from .models import PaymentSource, Transaction
|
2014-07-23 16:24:56 +00:00
|
|
|
|
2014-07-28 17:28:00 +00:00
|
|
|
|
|
|
|
class PaymentSourceSerializer(AccountSerializerMixin, serializers.HyperlinkedModelSerializer):
|
2014-07-23 16:24:56 +00:00
|
|
|
class Meta:
|
|
|
|
model = PaymentSource
|
2014-07-28 17:28:00 +00:00
|
|
|
fields = ('url', 'method', 'data', 'is_active')
|
|
|
|
|
|
|
|
def validate_data(self, attrs, source):
|
|
|
|
plugin = PaymentMethod.get_plugin(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_plugin(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()
|
2014-07-28 17:28:00 +00:00
|
|
|
for method in PaymentMethod.get_plugins()
|
|
|
|
}
|
|
|
|
return meta
|
2014-07-23 16:24:56 +00:00
|
|
|
|
|
|
|
|
2014-07-28 17:28:00 +00:00
|
|
|
class TransactionSerializer(AccountSerializerMixin, serializers.HyperlinkedModelSerializer):
|
2014-07-23 16:24:56 +00:00
|
|
|
class Meta:
|
2014-07-28 17:28:00 +00:00
|
|
|
model = Transaction
|