Skip to content

Commit

Permalink
Support statement_descriptor (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
remik committed Sep 13, 2018
1 parent ca4b9ee commit 885d294
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,5 @@ ENV/
.ropeproject

*.sublime*
.vscode

5 changes: 5 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change Log
All notable changes to this project will be documented in this file.

## [0.7.1]
### Added
- statement_descriptor in charge

## [0.7.0]
### Added
- CustomerDetailsAPI endpoint to add new payment sources for a customer
Expand Down
8 changes: 5 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ First of all, make sure to obtain Stripe user token from the Stripe API, and the
customer = stripe.Customer.create(source=data["id"]) # data is the response dictionary from Stripe API (in front-end)
token = StripeToken.objects.create(user=request.user, content=data,
customer_id=customer["id"])

To charge users, create an instance of ``aa_stripe.models.StripeCharge`` model and then call the ``charge()`` method:
::

c = StripeCharge.objects.create(user=user, token=token, amount=500, # in cents
description="Charge for stuff", # sent to Stripe
comment="Comment for internal information")
comment="Comment for internal information",
statement_descriptor="My Company" # sent to Stripe
)
c.charge()

Upon successfull charge also sends signal, ``stripe_charge_succeeded`` with instance as single parameter.
Expand Down Expand Up @@ -95,7 +97,7 @@ Utility functions for subscriptions
* subscription.refresh_from_stripe() - gets updated subscription data from Stripe. Example usage: parsing webhooks - when webhook altering subscription is received it is good practice to verify the subscription at Stripe before making any actions.
* subscription.cancel() - cancels subscription at Stripe.
* StripeSubscription.get_subcriptions_for_cancel() - returns all subscriptions that should be canceled. Stripe does not support end date for subscription so it is up the user to implement expiration mechanism. Subscription has end_date that can be used for that.
* StripeSubscription.end_subscriptions() - cancels all subscriptions on Stripe that has passed end date. Use with caution, check internal comments.
* StripeSubscription.end_subscriptions() - cancels all subscriptions on Stripe that has passed end date. Use with caution, check internal comments.
* management command: end_subscription.py. Terminates outdated subscriptions in a safe way. In case of error returns it at the end, using Sentry if available or in console. Should be used in cron script. By default sets at_period_end=True.

Subscription Plans
Expand Down
2 changes: 1 addition & 1 deletion aa_stripe/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
__title__ = "Arabella Stripe"
__version__ = "0.7.0"
__version__ = "0.7.1"
__author__ = "Jacek Ostanski"
__license__ = "MIT"
__copyright__ = "Copyright 2017 Arabella"
Expand Down
20 changes: 20 additions & 0 deletions aa_stripe/migrations/0020_stripecharge_statement_descriptor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.14 on 2018-09-13 20:48
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('aa_stripe', '0019_stripecustomer_default_source'),
]

operations = [
migrations.AddField(
model_name='stripecharge',
name='statement_descriptor',
field=models.CharField(blank=True, max_length=22),
),
]
17 changes: 11 additions & 6 deletions aa_stripe/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ class StripeCharge(StripeBasicModel):
content_type = models.ForeignKey(ContentType, null=True, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField(null=True, db_index=True)
source = generic.GenericForeignKey('content_type', 'object_id')
statement_descriptor = models.CharField(max_length=22, blank=True)

def charge(self):
self.refresh_from_db() # to minimize the chance of double charging
Expand All @@ -353,13 +354,17 @@ def charge(self):
customer = StripeCustomer.get_latest_active_customer_for_user(self.user)
self.customer = customer
if customer:
params = {
"amount": self.amount,
"currency": "usd",
"customer": customer.stripe_customer_id,
"description": self.description
}
if self.statement_descriptor:
params["statement_descriptor"] = self.statement_descriptor

try:
stripe_charge = stripe.Charge.create(
amount=self.amount,
currency="usd",
customer=customer.stripe_customer_id,
description=self.description
)
stripe_charge = stripe.Charge.create(**params)
except stripe.error.CardError as e:
self.charge_attempt_failed = True
self.is_charged = False
Expand Down
2 changes: 1 addition & 1 deletion requirements/requirements-base.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
django-jsonfield>=1.0.1
stripe>=1.49.0
stripe>=1.49.0,<2.0.0
djangorestframework>=3.6.0
simplejson>=3.10.0
six>=1.10.0

0 comments on commit 885d294

Please sign in to comment.