From e60147e925356f992a8d4a6b03f0a11f23919329 Mon Sep 17 00:00:00 2001 From: Harvey Hartwell Date: Fri, 19 Jan 2024 09:33:58 -0800 Subject: [PATCH] fix create_price --- README.md | 38 +++++++++++++------- ckc/stripe/subscriptions.py | 34 +++++++++--------- tests/integration/test_payment_processing.py | 23 ++++++++++-- 3 files changed, 64 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 069947a..9986827 100644 --- a/README.md +++ b/README.md @@ -231,21 +231,33 @@ class YourAppConfig(AppConfig): import your_app.signal_handlers ``` -#### subscribing a user to a subscription using a Price object -using the `subsciptions` endpoint you a user can be subscribed to a plan. +[//]: # (#### subscribing a user to a subscription using a Price object) -note: you will need to setup a payment method for the user before subscribing them to a plan. see below for more info -```js -// REQUEST from a signed in user that wishes to subscribe to a plan -axios.post("/subscriptions/subscribe/", { price_id: price.id }) -``` +[//]: # (using the `subsciptions` endpoint you a user can be subscribed to a plan.) -#### Creating a payment method -using the stripe card element on the frontend, obtain a payment method id. and pass it up to the frontend to attach to a customer -```js -// REQUEST from a signed in user that wishes to create a payment method -axios.post("/payment-methods/", { pm_id: pm.id }) -``` +[//]: # () +[//]: # (note: you will need to setup a payment method for the user before subscribing them to a plan. see below for more info ) + +[//]: # (```js) + +[//]: # (// REQUEST from a signed in user that wishes to subscribe to a plan) + +[//]: # (axios.post("/subscriptions/subscribe/", { price_id: price.id })) + +[//]: # (```) + +[//]: # () +[//]: # (#### Creating a payment method) + +[//]: # (using the stripe card element on the frontend, obtain a payment method id. and pass it up to the frontend to attach to a customer) + +[//]: # (```js) + +[//]: # (// REQUEST from a signed in user that wishes to create a payment method) + +[//]: # (axios.post("/payment-methods/", { pm_id: pm.id })) + +[//]: # (```) #### `./manage.py` commands diff --git a/ckc/stripe/subscriptions.py b/ckc/stripe/subscriptions.py index 7eb6038..44ad394 100644 --- a/ckc/stripe/subscriptions.py +++ b/ckc/stripe/subscriptions.py @@ -2,7 +2,8 @@ from djstripe.models import Price, Product -def create_price(amount, interval, interval_count=1, currency="usd", product_name="Sample Product Name", **kwargs): +# def create_price(amount=None, interval=None, interval_count=1, currency="usd", product_name="Sample Product Name", **kwargs): +def create_price(product_kwargs, **price_kwargs): """ create and return a stripe price object @param amount: the amount to charge @@ -17,27 +18,28 @@ def create_price(amount, interval, interval_count=1, currency="usd", product_nam try: stripe_product = stripe.Product.create( - name=product_name, - description="Sample Description", + **product_kwargs, ) except stripe.error.StripeError: raise ValueError("Error creating Stripe Product") product = Product.sync_from_stripe_data(stripe_product) - recurring = kwargs.pop("recurring", {}) - recurring.update({ - "interval": interval, - "interval_count": interval_count, - }) + # recurring = kwargs.pop("recurring", {}) + # recurring.update({ + # "interval": interval, + # "interval_count": interval_count, + # }) price = Price.create( - unit_amount=amount, - currency=currency, - recurring={ - "interval": interval, - "interval_count": interval_count, - }, product=product, - active=True, - **kwargs + **price_kwargs + # unit_amount=amount, + # currency=currency, + # recurring={ + # "interval": interval, + # "interval_count": interval_count, + # }, + # product=product, + # active=True, + # **kwargs ) return price diff --git a/tests/integration/test_payment_processing.py b/tests/integration/test_payment_processing.py index 94c916c..2c4b2b4 100644 --- a/tests/integration/test_payment_processing.py +++ b/tests/integration/test_payment_processing.py @@ -88,7 +88,16 @@ def test_payment_intents(self): def test_subscriptions(self): # create the subscription plan through dj stripe price object - price = create_price(2000, "month", product_name="Sample Product Name: 0", currency="usd") + price = create_price( + dict(name="Sample Product Name: 0", description='sample description'), + amount=2000, + recurring={ + "interval": "month", + "interval_count": 1, + }, + product_name="Sample Product Name: 0", + currency="usd" + ) assert price is not None assert price.id is not None @@ -125,7 +134,17 @@ def test_subscriptions(self): def test_subscription_plan_list(self): for i in range(3): prod_name = f"Sample Product Name: {i}" - create_price(2000 + i, "month", product_name=prod_name, nickname=prod_name, currency="usd") + price = create_price( + dict(name=prod_name, description='sample description'), + recurring={ + "interval": "month", + "interval_count": 1, + }, + nickname=prod_name, + active=True, + amount=2000 + i, + currency="usd" + ) url = reverse('prices-list') resp = self.client.get(url)