forked from uncloud/uncloud
125 lines
4.4 KiB
Python
125 lines
4.4 KiB
Python
from django.contrib.auth import get_user_model
|
|
from rest_framework import serializers
|
|
from .models import *
|
|
from .helpers import get_balance_for
|
|
|
|
from functools import reduce
|
|
from uncloud_vm.serializers import VMProductSerializer
|
|
from uncloud_vm.models import VMProduct
|
|
|
|
import uncloud_pay.stripe as stripe
|
|
|
|
###
|
|
# Users.
|
|
|
|
class UserSerializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = get_user_model()
|
|
fields = ['username', 'email', 'balance']
|
|
|
|
# Display current 'balance'
|
|
balance = serializers.SerializerMethodField('get_balance')
|
|
def __sum_balance(self, entries):
|
|
return reduce(lambda acc, entry: acc + entry.amount, entries, 0)
|
|
|
|
def get_balance(self, user):
|
|
return get_balance_for(user)
|
|
|
|
###
|
|
# Payments and Payment Methods.
|
|
|
|
class PaymentSerializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = Payment
|
|
fields = ['owner', 'amount', 'source', 'timestamp']
|
|
|
|
class PaymentMethodSerializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = PaymentMethod
|
|
fields = ['source', 'description', 'primary']
|
|
|
|
class CreditCardSerializer(serializers.Serializer):
|
|
number = serializers.IntegerField()
|
|
exp_month = serializers.IntegerField()
|
|
exp_year = serializers.IntegerField()
|
|
cvc = serializers.IntegerField()
|
|
|
|
class CreatePaymentMethodSerializer(serializers.ModelSerializer):
|
|
credit_card = CreditCardSerializer()
|
|
|
|
class Meta:
|
|
model = PaymentMethod
|
|
fields = ['source', 'description', 'primary', 'credit_card']
|
|
|
|
def create(self, validated_data):
|
|
credit_card = stripe.CreditCard(**validated_data.pop('credit_card'))
|
|
user = self.context['request'].user
|
|
customer = stripe.create_customer(user.username, user.email)
|
|
# TODO check customer error
|
|
customer_id = customer['response_object']['id']
|
|
stripe_card = stripe.create_card(customer_id, credit_card)
|
|
# TODO: check credit card error
|
|
validated_data['stripe_card_id'] = stripe_card['response_object']['id']
|
|
class CreditCardSerializer(serializers.Serializer):
|
|
number = serializers.IntegerField()
|
|
exp_month = serializers.IntegerField()
|
|
exp_year = serializers.IntegerField()
|
|
cvc = serializers.IntegerField()
|
|
|
|
class CreatePaymentMethodSerializer(serializers.ModelSerializer):
|
|
credit_card = CreditCardSerializer()
|
|
|
|
class Meta:
|
|
model = PaymentMethod
|
|
fields = ['source', 'description', 'primary', 'credit_card']
|
|
|
|
def create(self, validated_data):
|
|
credit_card = stripe.CreditCard(**validated_data.pop('credit_card'))
|
|
user = self.context['request'].user
|
|
customer = stripe.create_customer(user.username, user.email)
|
|
# TODO check customer error
|
|
customer_id = customer['response_object']['id']
|
|
stripe_card = stripe.create_card(customer_id, credit_card)
|
|
# TODO: check credit card error
|
|
validated_data['stripe_card_id'] = stripe_card['response_object']['id']
|
|
payment_method = PaymentMethod.objects.create(**validated_data)
|
|
return payment_method
|
|
payment_method = PaymentMethod.objects.create(**validated_data)
|
|
return payment_method
|
|
|
|
###
|
|
# Bills
|
|
|
|
# TODO: remove magic numbers for decimal fields
|
|
class BillRecordSerializer(serializers.Serializer):
|
|
order = serializers.CharField()
|
|
description = serializers.CharField()
|
|
recurring_period = serializers.CharField()
|
|
recurring_price = serializers.DecimalField(max_digits=10, decimal_places=2)
|
|
amount = serializers.DecimalField(max_digits=10, decimal_places=2)
|
|
|
|
class BillSerializer(serializers.ModelSerializer):
|
|
records = BillRecordSerializer(many=True, read_only=True)
|
|
class Meta:
|
|
model = Bill
|
|
fields = ['owner', 'total', 'due_date', 'creation_date',
|
|
'starting_date', 'ending_date', 'records', 'final']
|
|
|
|
###
|
|
# Orders & Products.
|
|
|
|
class OrderRecordSerializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = OrderRecord
|
|
fields = ['setup_fee', 'recurring_price', 'description']
|
|
|
|
|
|
class OrderSerializer(serializers.ModelSerializer):
|
|
records = OrderRecordSerializer(many=True, read_only=True)
|
|
class Meta:
|
|
model = Order
|
|
fields = ['uuid', 'creation_date', 'starting_date', 'ending_date',
|
|
'bill', 'recurring_period', 'records', 'recurring_price', 'setup_fee']
|
|
|
|
class ProductSerializer(serializers.Serializer):
|
|
vms = VMProductSerializer(many=True, read_only=True)
|