serializers.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. from django.core.exceptions import SuspiciousOperation
  2. from djstripe.models import Customer, Price, Product, Subscription, SubscriptionItem
  3. from rest_framework import serializers
  4. from rest_framework.serializers import ModelSerializer
  5. from glitchtip.exceptions import ConflictException
  6. from organizations_ext.models import OrganizationUserRole
  7. from .rest_framework.serializers import (
  8. SubscriptionSerializer as BaseSubscriptionSerializer,
  9. )
  10. class BaseProductSerializer(ModelSerializer):
  11. class Meta:
  12. model = Product
  13. fields = ("id", "name", "description", "type", "metadata")
  14. class BasePriceSerializer(ModelSerializer):
  15. class Meta:
  16. model = Price
  17. fields = (
  18. "id",
  19. "nickname",
  20. "currency",
  21. "type",
  22. "unit_amount",
  23. "human_readable_price",
  24. "metadata",
  25. )
  26. class PriceSerializer(BasePriceSerializer):
  27. product = BaseProductSerializer()
  28. class Meta(BasePriceSerializer.Meta):
  29. fields = (
  30. "id",
  31. "nickname",
  32. "currency",
  33. "unit_amount",
  34. "human_readable_price",
  35. "metadata",
  36. "product",
  37. )
  38. class ProductSerializer(BaseProductSerializer):
  39. prices = BasePriceSerializer(many=True)
  40. class Meta(BaseProductSerializer.Meta):
  41. fields = ("id", "name", "description", "type", "prices", "metadata")
  42. class SubscriptionItemSerializer(ModelSerializer):
  43. price = PriceSerializer()
  44. class Meta:
  45. model = SubscriptionItem
  46. fields = ("id", "price")
  47. class SubscriptionSerializer(BaseSubscriptionSerializer):
  48. items = SubscriptionItemSerializer(many=True)
  49. class OrganizationPrimaryKeySerializer(serializers.PrimaryKeyRelatedField):
  50. def get_queryset(self):
  51. user = self.context["request"].user
  52. return user.organizations_ext_organization.filter(
  53. organization_users__role=OrganizationUserRole.OWNER
  54. )
  55. class OrganizationSelectSerializer(serializers.Serializer):
  56. """Organization in which user is owner of"""
  57. organization = OrganizationPrimaryKeySerializer()
  58. class PriceForOrganizationSerializer(OrganizationSelectSerializer):
  59. price = serializers.SlugRelatedField(queryset=Price.objects.all(), slug_field="id")
  60. class CreateSubscriptionSerializer(PriceForOrganizationSerializer):
  61. """A serializer used to create a Subscription. Only works with free prices."""
  62. subscription = SubscriptionSerializer(read_only=True)
  63. def create(self, validated_data):
  64. organization = validated_data["organization"]
  65. price = validated_data["price"]
  66. if price.unit_amount != 0.0:
  67. raise SuspiciousOperation(
  68. "Cannot subscribe to non-free plan without payment"
  69. )
  70. customer, _ = Customer.get_or_create(subscriber=organization)
  71. if (
  72. Subscription.objects.filter(customer=customer)
  73. .exclude(status="canceled")
  74. .exists()
  75. ):
  76. raise ConflictException("Customer already has subscription")
  77. subscription = customer.subscribe(items=[{"price": price}])
  78. return {
  79. "price": price,
  80. "organization": organization,
  81. "subscription": subscription,
  82. }