2020-02-27 11:38:04 +00:00
|
|
|
from django.contrib.auth import get_user_model
|
2020-02-27 10:59:28 +00:00
|
|
|
from rest_framework import serializers
|
2020-05-07 10:05:26 +00:00
|
|
|
from uncloud_auth.serializers import UserSerializer
|
2020-05-10 19:47:44 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2021-08-12 10:28:19 +00:00
|
|
|
from stripe.error import CardError
|
2020-05-10 19:47:44 +00:00
|
|
|
|
2020-02-27 14:15:12 +00:00
|
|
|
from .models import *
|
2020-12-29 00:43:33 +00:00
|
|
|
import uncloud_pay.stripe as uncloud_stripe
|
2021-01-01 11:41:54 +00:00
|
|
|
from uncloud import AMOUNT_DECIMALS, AMOUNT_MAX_DIGITS
|
2020-03-02 21:26:40 +00:00
|
|
|
|
2020-12-25 16:29:17 +00:00
|
|
|
###
|
2020-12-28 22:35:34 +00:00
|
|
|
# 2020-12 Checked code
|
|
|
|
|
2021-01-01 12:25:52 +00:00
|
|
|
class StripeCreditCardSerializer(serializers.HyperlinkedModelSerializer):
|
2020-12-28 22:35:34 +00:00
|
|
|
class Meta:
|
|
|
|
model = StripeCreditCard
|
2020-12-29 00:43:33 +00:00
|
|
|
exclude = [ "card_id", "owner" ]
|
2020-12-28 22:35:34 +00:00
|
|
|
read_only_fields = [ "last4", "brand", "expiry_date" ]
|
|
|
|
|
2020-12-29 00:43:33 +00:00
|
|
|
class PaymentSerializer(serializers.ModelSerializer):
|
|
|
|
owner = serializers.HiddenField(default=serializers.CurrentUserDefault())
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Payment
|
|
|
|
fields = '__all__'
|
|
|
|
read_only_fields = [ "external_reference", "source", "timestamp" ]
|
|
|
|
|
|
|
|
def validate(self, data):
|
|
|
|
data["source"] = "stripe"
|
|
|
|
return data
|
2021-08-12 10:28:19 +00:00
|
|
|
|
|
|
|
def create(self, validated_data):
|
|
|
|
try:
|
|
|
|
if validated_data['type'] == 'deposit':
|
|
|
|
return Payment.deposit(validated_data['owner'], validated_data['amount'], validated_data['source'], currency=validated_data['currency'], notes=validated_data['notes'])
|
|
|
|
else:
|
|
|
|
return Payment.objects.create(**validated_data)
|
|
|
|
except CardError as err:
|
|
|
|
raise serializers.ValidationError(err.user_message)
|
2020-12-25 16:29:17 +00:00
|
|
|
|
2021-01-01 11:41:54 +00:00
|
|
|
class BalanceSerializer(serializers.Serializer):
|
|
|
|
balance = serializers.DecimalField(max_digits=AMOUNT_MAX_DIGITS, decimal_places=AMOUNT_DECIMALS)
|
|
|
|
|
2021-01-17 14:53:30 +00:00
|
|
|
class BillingAddressSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = BillingAddress
|
|
|
|
exclude = [ "owner" ]
|
|
|
|
|
2020-12-25 16:29:17 +00:00
|
|
|
|
2021-06-20 09:51:27 +00:00
|
|
|
class VATRateSerializer(serializers.ModelSerializer):
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = VATRate
|
|
|
|
fields = '__all__'
|
|
|
|
|
|
|
|
|
2020-12-25 16:29:17 +00:00
|
|
|
################################################################################
|
|
|
|
# Unchecked code
|
|
|
|
|
|
|
|
|
2020-03-06 08:32:25 +00:00
|
|
|
###
|
|
|
|
# Orders & Products.
|
|
|
|
|
|
|
|
class OrderSerializer(serializers.ModelSerializer):
|
2020-05-07 10:05:26 +00:00
|
|
|
owner = serializers.PrimaryKeyRelatedField(queryset=get_user_model().objects.all())
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
# Don't pass the 'fields' arg up to the superclass
|
|
|
|
admin = kwargs.pop('admin', None)
|
|
|
|
|
|
|
|
# Instantiate the superclass normally
|
|
|
|
super(OrderSerializer, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
# Only allows owner in admin mode.
|
|
|
|
if not admin:
|
|
|
|
self.fields.pop('owner')
|
|
|
|
|
|
|
|
def create(self, validated_data):
|
|
|
|
billing_address = BillingAddress.get_preferred_address_for(validated_data["owner"])
|
|
|
|
instance = Order(billing_address=billing_address, **validated_data)
|
|
|
|
instance.save()
|
|
|
|
|
|
|
|
return instance
|
|
|
|
|
|
|
|
def validate_owner(self, value):
|
|
|
|
if BillingAddress.get_preferred_address_for(value) == None:
|
|
|
|
raise serializers.ValidationError("Owner does not have a valid billing address.")
|
|
|
|
|
|
|
|
return value
|
|
|
|
|
2020-03-06 08:32:25 +00:00
|
|
|
class Meta:
|
|
|
|
model = Order
|
2020-05-08 14:47:32 +00:00
|
|
|
read_only_fields = ['replaced_by', 'depends_on']
|
2021-07-19 14:36:10 +00:00
|
|
|
fields = ['owner', 'description', 'creation_date', 'starting_date', 'ending_date',
|
|
|
|
'recurring_period', 'recurring_price', 'one_time_price',
|
|
|
|
'config', 'pricing_plan', 'should_be_billed'] + read_only_fields
|
2020-03-06 08:32:25 +00:00
|
|
|
|
2020-02-27 14:15:12 +00:00
|
|
|
|
2020-03-03 08:13:04 +00:00
|
|
|
###
|
|
|
|
# Bills
|
|
|
|
|
|
|
|
# TODO: remove magic numbers for decimal fields
|
|
|
|
class BillRecordSerializer(serializers.Serializer):
|
2020-03-06 08:32:25 +00:00
|
|
|
order = serializers.HyperlinkedRelatedField(
|
|
|
|
view_name='order-detail',
|
|
|
|
read_only=True)
|
2020-04-18 08:43:23 +00:00
|
|
|
description = serializers.CharField()
|
|
|
|
one_time_price = serializers.DecimalField(AMOUNT_MAX_DIGITS, AMOUNT_DECIMALS)
|
|
|
|
recurring_price = serializers.DecimalField(AMOUNT_MAX_DIGITS, AMOUNT_DECIMALS)
|
2020-10-06 13:46:22 +00:00
|
|
|
# recurring_period = serializers.ChoiceField()
|
2020-04-18 08:43:23 +00:00
|
|
|
recurring_count = serializers.DecimalField(AMOUNT_MAX_DIGITS, AMOUNT_DECIMALS)
|
2020-04-18 09:43:55 +00:00
|
|
|
vat_rate = serializers.DecimalField(AMOUNT_MAX_DIGITS, AMOUNT_DECIMALS)
|
|
|
|
vat_amount = serializers.DecimalField(AMOUNT_MAX_DIGITS, AMOUNT_DECIMALS)
|
2020-04-18 08:43:23 +00:00
|
|
|
amount = serializers.DecimalField(AMOUNT_MAX_DIGITS, AMOUNT_DECIMALS)
|
2020-04-18 09:43:55 +00:00
|
|
|
total = serializers.DecimalField(AMOUNT_MAX_DIGITS, AMOUNT_DECIMALS)
|
2020-03-03 08:13:04 +00:00
|
|
|
|
|
|
|
class BillSerializer(serializers.ModelSerializer):
|
2020-04-18 06:30:45 +00:00
|
|
|
billing_address = BillingAddressSerializer(read_only=True)
|
2020-03-03 08:13:04 +00:00
|
|
|
records = BillRecordSerializer(many=True, read_only=True)
|
2020-04-15 14:01:31 +00:00
|
|
|
|
2020-03-03 08:13:04 +00:00
|
|
|
class Meta:
|
|
|
|
model = Bill
|
2021-07-19 14:36:10 +00:00
|
|
|
fields = ['owner', 'sum', 'vat_rate',
|
2020-04-18 09:43:55 +00:00
|
|
|
'due_date', 'creation_date', 'starting_date', 'ending_date',
|
2021-07-19 14:36:10 +00:00
|
|
|
'records', 'is_closed', 'billing_address']
|
2020-04-15 13:17:38 +00:00
|
|
|
|
|
|
|
# We do not want users to mutate the country / VAT number of an address, as it
|
|
|
|
# will change VAT on existing bills.
|
|
|
|
class UpdateBillingAddressSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = BillingAddress
|
2021-07-19 14:36:10 +00:00
|
|
|
fields = ['street', 'city', 'postal_code']
|