Exemplo n.º 1
0
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)
Exemplo n.º 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"})
Exemplo n.º 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"})
Exemplo n.º 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)
Exemplo n.º 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"})
Exemplo n.º 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"})
Exemplo n.º 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"
Exemplo n.º 8
0
    def test_create_from_djstripe_product(self, price_create_mock,
                                          product_retrieve_mock):
        fake_price = deepcopy(FAKE_PRICE)
        fake_price["product"] = Product.sync_from_stripe_data(
            self.stripe_product)
        fake_price["unit_amount"] /= 100
        assert isinstance(fake_price["product"], Product)

        price = Price.create(**fake_price)

        price_create_mock.assert_called_once_with(api_key=STRIPE_SECRET_KEY,
                                                  **FAKE_PRICE)

        self.assert_fks(price, expected_blank_fks={"djstripe.Customer.coupon"})
Exemplo n.º 9
0
    def test_create_from_product_id(self, price_create_mock,
                                    product_retrieve_mock):
        fake_price = deepcopy(FAKE_PRICE)
        fake_price["unit_amount"] /= 100
        assert isinstance(fake_price["product"], str)

        price = Price.create(**fake_price)

        expected_create_kwargs = deepcopy(FAKE_PRICE)
        expected_create_kwargs["api_key"] = STRIPE_SECRET_KEY

        price_create_mock.assert_called_once_with(**expected_create_kwargs)

        self.assert_fks(price, expected_blank_fks={"djstripe.Customer.coupon"})
Exemplo n.º 10
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)
Exemplo n.º 11
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
Exemplo n.º 12
0
    def test_create_with_metadata(self, price_create_mock,
                                  product_retrieve_mock):
        metadata = {"other_data": "more_data"}
        fake_price = deepcopy(FAKE_PRICE)
        fake_price["unit_amount"] /= 100
        fake_price["metadata"] = metadata
        assert isinstance(fake_price["product"], str)

        price = Price.create(**fake_price)

        expected_create_kwargs = deepcopy(FAKE_PRICE)
        expected_create_kwargs["metadata"] = metadata

        price_create_mock.assert_called_once_with(api_key=STRIPE_SECRET_KEY,
                                                  **expected_create_kwargs)

        self.assert_fks(price, expected_blank_fks={"djstripe.Customer.coupon"})
Exemplo n.º 13
0
 def test_price_name(self):
     price = Price(id="price_xxxx", nickname="Price Test")
     assert str(price) == "Price Test"
     price.nickname = ""
     assert str(price) == "price_xxxx"