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
|
|
|
|
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
|
2015-04-30 09:51:55 +00:00
|
|
|
fields = ('url', 'id', 'method', 'data', 'is_active')
|
2014-07-28 17:28:00 +00:00
|
|
|
|
|
|
|
def validate_data(self, attrs, source):
|
2015-03-31 12:39:08 +00:00
|
|
|
plugin = PaymentMethod.get(attrs['method'])
|
2014-07-28 17:28:00 +00:00
|
|
|
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:
|
2015-03-31 12:39:08 +00:00
|
|
|
plugin = PaymentMethod.get(obj.method)
|
2014-07-28 17:28:00 +00:00
|
|
|
serializer_class = plugin().get_serializer()
|
|
|
|
return serializer_class().to_native(obj.data)
|
|
|
|
return obj.data
|
|
|
|
|
2015-04-23 14:34:04 +00:00
|
|
|
# TODO
|
2014-07-28 17:28:00 +00:00
|
|
|
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
|