Skip to content

Commit

Permalink
fix create_price
Browse files Browse the repository at this point in the history
  • Loading branch information
hhartwell committed Jan 19, 2024
1 parent 6f43a1f commit e60147e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 31 deletions.
38 changes: 25 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 18 additions & 16 deletions ckc/stripe/subscriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
23 changes: 21 additions & 2 deletions tests/integration/test_payment_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit e60147e

Please sign in to comment.