def get_or_create_starter_price():
    try:
        product = Product.objects.get(
            metadata__djstripe_example="example_subscription")
    except Product.DoesNotExist:
        print(
            "Could not find a subscription product to use. Will create one for you."
        )
        stripe_product = stripe.Product.create(
            name="Starter",
            metadata={"djstripe_example": "example_subscription"})
        product = Product.sync_from_stripe_data(stripe_product)

    try:
        return Price.objects.get(metadata__djstripe_example="starter_price")
    except Price.DoesNotExist:
        print(
            "Could not find a subscription price to use. Will create one for you."
        )
        stripe_price = stripe.Price.create(
            currency="usd",
            unit_amount="1200",
            recurring={"interval": "month"},
            product=product.id,
            metadata={"djstripe_example": "starter_price"},
        )
        return Price.sync_from_stripe_data(stripe_price)
示例#2
0
    def test_stripe_metered_price(self, price_retrieve_mock):
        price_data = deepcopy(FAKE_PRICE_METERED)
        price = Price.sync_from_stripe_data(price_data)
        assert price.id == price_data["id"]
        assert price.recurring["usage_type"] == PriceUsageType.metered
        assert price.unit_amount is not None

        self.assert_fks(price, expected_blank_fks={"djstripe.Customer.coupon"})
示例#3
0
    def test_stripe_tier_price(self, price_retrieve_mock):
        price_data = deepcopy(FAKE_TIER_PRICE)
        price = Price.sync_from_stripe_data(price_data)
        assert price.id == price_data["id"]
        assert price.unit_amount is None
        assert price.tiers is not None

        self.assert_fks(price, expected_blank_fks={"djstripe.Customer.coupon"})
示例#4
0
 def setUp(self):
     self.price_data = deepcopy(FAKE_PRICE)
     with patch(
             "stripe.Product.retrieve",
             return_value=deepcopy(FAKE_PRODUCT),
             autospec=True,
     ):
         self.price = Price.sync_from_stripe_data(self.price_data)
示例#5
0
    def test_stripe_onetime_price(self, price_retrieve_mock):
        price_data = deepcopy(FAKE_PRICE_ONETIME)
        price = Price.sync_from_stripe_data(price_data)
        assert price.id == price_data["id"]
        assert price.unit_amount is not None
        assert not price.recurring
        assert price.type == PriceType.one_time

        self.assert_fks(price, expected_blank_fks={"djstripe.Customer.coupon"})
示例#6
0
    def test_stripe_price(self, price_retrieve_mock):
        stripe_price = self.price.api_retrieve()
        price_retrieve_mock.assert_called_once_with(
            id=self.price_data["id"],
            api_key=STRIPE_SECRET_KEY,
            expand=[],
            stripe_account=None,
        )
        price = Price.sync_from_stripe_data(stripe_price)
        assert price.amount_in_cents == price.unit_amount / 100
        assert isinstance(price.amount_in_cents, float)

        self.assert_fks(price, expected_blank_fks={"djstripe.Customer.coupon"})
示例#7
0
    def test_stripe_price(self, price_retrieve_mock):
        stripe_price = self.price.api_retrieve()
        price_retrieve_mock.assert_called_once_with(
            id=self.price_data["id"],
            api_key=STRIPE_SECRET_KEY,
            expand=["tiers"],
            stripe_account=None,
        )
        price = Price.sync_from_stripe_data(stripe_price)

        self.assert_fks(price, expected_blank_fks={"djstripe.Customer.coupon"})

        assert price.human_readable_price == "$20.00 USD/month"
示例#8
0
    def test___str__(self, fake_price_data, monkeypatch):
        def mock_product_get(*args, **kwargs):
            return deepcopy(FAKE_PRODUCT)

        def mock_price_get(*args, **kwargs):
            return fake_price_data

        # monkeypatch stripe.Product.retrieve and stripe.Price.retrieve calls to return
        # the desired json response.
        monkeypatch.setattr(stripe.Product, "retrieve", mock_product_get)
        monkeypatch.setattr(stripe.Price, "retrieve", mock_price_get)

        if not fake_price_data["recurring"]:
            price = Price.sync_from_stripe_data(fake_price_data)
            assert (f"{price.human_readable_price} for {FAKE_PRODUCT['name']}"
                    ) == str(price)

        else:
            price = Price.sync_from_stripe_data(fake_price_data)
            subscriptions = Subscription.objects.filter(
                plan__id=price.id).count()
            assert (
                f"{price.human_readable_price} for {FAKE_PRODUCT['name']} ({subscriptions} subscriptions)"
            ) == str(price)
示例#9
0
    def test_human_readable(self, fake_price_data, expected_str, monkeypatch):
        def mock_product_get(*args, **kwargs):
            return deepcopy(FAKE_PRODUCT)

        def mock_price_get(*args, **kwargs):
            return fake_price_data

        # monkeypatch stripe.Product.retrieve and stripe.Price.retrieve calls to return
        # the desired json response.
        monkeypatch.setattr(stripe.Product, "retrieve", mock_product_get)
        monkeypatch.setattr(stripe.Price, "retrieve", mock_price_get)

        price = Price.sync_from_stripe_data(fake_price_data)

        assert price.human_readable_price == expected_str