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
|
|
|
|
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'])
|
2014-07-28 17:28:00 +00:00
|
|
|
serializer_class = plugin().get_serializer()
|
2015-07-29 09:05:07 +00:00
|
|
|
serializer = serializer_class(data=data['data'])
|
2014-07-28 17:28:00 +00:00
|
|
|
if not serializer.is_valid():
|
|
|
|
raise serializers.ValidationError(serializer.errors)
|
2015-07-29 09:05:07 +00:00
|
|
|
return data
|
2014-07-28 17:28:00 +00:00
|
|
|
|
|
|
|
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
|
2020-06-18 07:47:27 +00:00
|
|
|
fields = '__all__'
|