-
Notifications
You must be signed in to change notification settings - Fork 1
upgrade and add dj stripe #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hhartwell
wants to merge
23
commits into
master
Choose a base branch
from
dj-stripe
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
2d3ce59
upgrade and add dj stripe
hhartwell a11cdbd
dj stripe payment methods
hhartwell a0bf39c
checkout session util function
hhartwell cbabfcf
keep pluggin on payment intents
hhartwell 431de85
fix payment intents
hhartwell e0afde3
rename
hhartwell c35fb15
subscriptions
hhartwell 493f187
add views to subscribe
hhartwell 076cb28
bug fix
hhartwell 919c664
Add env vars to tests
ckcollab b0a187f
improve price test
hhartwell a8a11d8
embelesh readme
hhartwell 94ffc1d
flake
hhartwell f87855f
flake
hhartwell 36615d1
flake
hhartwell a780bbb
env vars
hhartwell 7d52c69
Merge branch 'master' into dj-stripe
ckcollab 2a1808b
setup signals
hhartwell c992749
readme
hhartwell a3a67c0
change version
hhartwell 6f43a1f
change version
hhartwell e60147e
fix create_price
hhartwell fc6645f
fix test
hhartwell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
STRIPE_PUBLIC_KEY= | ||
STRIPE_PRIVATE_KEY= |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import json | ||
|
||
import stripe | ||
from djstripe.models import Customer | ||
|
||
from django.conf import settings | ||
from rest_framework.exceptions import ValidationError | ||
|
||
|
||
def create_checkout_session(user, success_url, cancel_url, line_items, metadata=None, payment_method_types=None): | ||
""" | ||
create and return a stripe checkout session | ||
|
||
@param user: the user to associate the session with | ||
@param success_url: the url to redirect to after a successful payment | ||
@param cancel_url: the url to redirect to after a cancelled payment | ||
@param line_items: a list of line items to add to the session | ||
@param metadata: optional metadata to add to the session | ||
@param payment_method_types: optional payment method types to accept. defaults to ["card"] | ||
|
||
|
||
metadata = {}, | ||
success_url = "https://example.com/success", | ||
cancel_url = "https://example.com/cancel", | ||
line_items = [{ | ||
"quantity": 1, | ||
"price_data": { | ||
"currency": "usd", | ||
"unit_amount": 2000, | ||
"product_data": { | ||
"name": "Sample Product Name", | ||
"images": ["https://i.imgur.com/EHyR2nP.png"], | ||
"description": "Sample Description", | ||
}, | ||
}, | ||
}] | ||
|
||
@returns stripe.checkout.Session | ||
""" | ||
if not metadata: | ||
metadata = {} | ||
if not payment_method_types: | ||
payment_method_types = ["card"] | ||
|
||
customer, created = Customer.get_or_create(subscriber=user) | ||
session = stripe.checkout.Session.create( | ||
payment_method_types=payment_method_types, | ||
customer=customer.id, | ||
payment_intent_data={ | ||
"setup_future_usage": "off_session", | ||
# so that the metadata gets copied to the associated Payment Intent and Charge Objects | ||
"metadata": metadata | ||
}, | ||
line_items=line_items, | ||
mode="payment", | ||
success_url=success_url, | ||
cancel_url=cancel_url, | ||
metadata=metadata, | ||
) | ||
return session | ||
|
||
|
||
def create_payment_intent(payment_method_id, customer_id, amount, currency="usd", confirmation_method="automatic"): | ||
""" | ||
create and return a stripe payment intent | ||
@param payment_method_id: the id of the payment method to use | ||
@param amount: the amount to charge | ||
@param currency: the currency to charge in. defaults to "usd" | ||
@param confirmation_method: the confirmation method to use. choices are "manual" and "automatic". defaults to "automatic" | ||
if set to manual, you must call confirm_payment_intent to confirm the payment intent | ||
@returns stripe.PaymentIntent | ||
""" | ||
if not payment_method_id: | ||
raise ValueError("payment_method_id must be set") | ||
|
||
intent = None | ||
try: | ||
# Create the PaymentIntent | ||
intent = stripe.PaymentIntent.create( | ||
customer=customer_id, | ||
payment_method=payment_method_id, | ||
amount=amount, | ||
currency=currency, | ||
# confirmation_method=confirmation_method, | ||
confirm=confirmation_method == "automatic", | ||
api_key=settings.STRIPE_PRIVATE_KEY, | ||
automatic_payment_methods={ | ||
"enabled": True, | ||
"allow_redirects": 'never' | ||
}, | ||
|
||
) | ||
except stripe.error.CardError: | ||
raise ValidationError("Error encountered while creating payment intent") | ||
return intent | ||
|
||
|
||
def confirm_payment_intent(payment_intent_id): | ||
""" | ||
confirm a stripe payment intent | ||
@param payment_intent_id: the id of the payment intent to confirm | ||
@returns a tuple of (data, status_code) | ||
""" | ||
intent = stripe.PaymentIntent.confirm( | ||
payment_intent_id, | ||
api_key=settings.STRIPE_PRIVATE_KEY, | ||
) | ||
|
||
if intent.status == "requires_action" and intent.next_action.type == "use_stripe_sdk": | ||
# Tell the client to handle the action | ||
return_data = json.dumps({ | ||
"requires_action": True, | ||
"payment_intent_client_secret": intent.client_secret | ||
}), 200 | ||
pass | ||
elif intent.status == "succeeded": | ||
# The payment did not need any additional actions and completed! | ||
# Handle post-payment fulfillment | ||
return_data = json.dumps({"success": True}), 200 | ||
else: | ||
# Invalid status | ||
return_data = json.dumps({"error": "Invalid PaymentIntent status"}), 500 | ||
return return_data |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import stripe | ||
from djstripe.models import PaymentMethod, Customer, Price, Product | ||
|
||
from rest_framework import serializers | ||
|
||
|
||
class PaymentMethodSerializer(serializers.ModelSerializer): | ||
pm_id = serializers.CharField(write_only=True) | ||
|
||
class Meta: | ||
model = PaymentMethod | ||
fields = ( | ||
'pm_id', | ||
'id', | ||
'type', | ||
|
||
# 'customer', | ||
# 'stripe_id', | ||
# 'card_brand', | ||
# 'card_last4', | ||
# 'card_exp_month', | ||
# 'card_exp_year', | ||
# 'is_default', | ||
# 'created', | ||
# 'modified', | ||
Comment on lines
+17
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pull, or useful? |
||
) | ||
read_only_fields = ( | ||
'id', | ||
'type', | ||
# 'customer', | ||
# 'stripe_id', | ||
# 'card_brand', | ||
# 'card_last4', | ||
# 'card_exp_month', | ||
# 'card_exp_year', | ||
# 'is_default', | ||
# 'created', | ||
# 'modified', | ||
) | ||
|
||
def create(self, validated_data): | ||
customer, created = Customer.get_or_create(subscriber=self.context['request'].user) | ||
try: | ||
payment_method = customer.add_payment_method(validated_data['pm_id']) | ||
except stripe.error.InvalidRequestError as e: | ||
raise serializers.ValidationError(e) | ||
|
||
return payment_method | ||
|
||
|
||
class ProductSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Product | ||
fields = ( | ||
'id', | ||
'name', | ||
'description', | ||
'type', | ||
) | ||
read_only_fields = ( | ||
'id', | ||
'name', | ||
'description', | ||
'type', | ||
) | ||
|
||
|
||
class PriceSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Price | ||
fields = ( | ||
'id', | ||
'unit_amount', | ||
'currency', | ||
'recurring', | ||
'nickname', | ||
) | ||
read_only_fields = ( | ||
'id', | ||
'unit_amount', | ||
'currency', | ||
'recurring', | ||
'nickname', | ||
) | ||
|
||
|
||
class SubscribeSerializer(serializers.Serializer): | ||
price_id = serializers.CharField() | ||
|
||
class Meta: | ||
fields = ( | ||
'price_id' | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from django.dispatch import Signal | ||
|
||
# Define a signal for post-subscription | ||
post_subscribe = Signal() | ||
|
||
# Define a signal for post-cancellation | ||
post_cancel = Signal() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.