Skip to content

Commit

Permalink
Added Customer.charge and modified Customer.purchase
Browse files Browse the repository at this point in the history
The charging aspect of Customer.purchase has been moved to Customer.charge and
Customer.purchase renamed to subscribe. This makes one-off charges simpler and
not all tied up with plan subscription.

BACKWARDS INCOMPATIBLE
  • Loading branch information
brosner committed Mar 4, 2013
1 parent 24a159f commit 3288109
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 37 deletions.
4 changes: 2 additions & 2 deletions docs/signals.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ card_changed
:providing_args: `stripe_response`


purchase_made
-------------
subscription_made
-----------------

:providing_args: `plan`, `stripe_response`

Expand Down
2 changes: 1 addition & 1 deletion payments/management/commands/init_customers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ def handle(self, *args, **options):
trial_days = TRIAL_PERIOD_FOR_USER_CALLBACK(user)
cus = Customer.create(user=user)
if DEFAULT_PLAN and trial_days:
cus.purchase(plan=DEFAULT_PLAN, trial_days=trial_days)
cus.subscribe(plan=DEFAULT_PLAN, trial_days=trial_days)
print "Created customer for %s" % user.email
57 changes: 26 additions & 31 deletions payments/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from payments.settings import PAYMENTS_PLANS, INVOICE_FROM_EMAIL
from payments.settings import plan_from_stripe_id
from payments.signals import WEBHOOK_SIGNALS
from payments.signals import purchase_made, cancelled, card_changed
from payments.signals import subscription_made, cancelled, card_changed
from payments.signals import webhook_processing_error


Expand Down Expand Up @@ -409,44 +409,39 @@ def sync_current_subscription(self, cu=None):
return sub_obj

def update_plan_quantity(self, quantity, charge_immediately=False):
self.purchase(
self.subscribe(
plan=plan_from_stripe_id(self.stripe_customer.subscription.plan.id),
quantity=quantity,
charge_immediately=charge_immediately
)

def purchase(self, plan, quantity=1, trial_days=None, charge_immediately=True):
def subscribe(self, plan, quantity=1, trial_days=None, charge_immediately=True):
cu = self.stripe_customer
if settings.PAYMENTS_PLANS[plan].get("stripe_plan_id"):
if trial_days:
resp = cu.update_subscription(
plan=PAYMENTS_PLANS[plan]["stripe_plan_id"],
trial_end=timezone.now() + datetime.timedelta(days=trial_days),
quantity=quantity
)
else:
resp = cu.update_subscription(
plan=PAYMENTS_PLANS[plan]["stripe_plan_id"],
quantity=quantity
)

self.sync_current_subscription()

if charge_immediately:
self.send_invoice()
if trial_days:
resp = cu.update_subscription(
plan=PAYMENTS_PLANS[plan]["stripe_plan_id"],
trial_end=timezone.now() + datetime.timedelta(days=trial_days),
quantity=quantity
)
else:
# It's just a single transaction
resp = stripe.Charge.create(
amount=PAYMENTS_PLANS[plan]["price"] * 100,
currency=settings.PAYMENTS_PLANS[plan].get("currency", "usd"),
customer=self.stripe_id,
description=PAYMENTS_PLANS[plan]["name"]
resp = cu.update_subscription(
plan=PAYMENTS_PLANS[plan]["stripe_plan_id"],
quantity=quantity
)
obj = self.record_charge(resp["id"])
obj.description = resp["description"]
obj.save()
obj.send_receipt()
purchase_made.send(sender=self, plan=plan, stripe_response=resp)
self.sync_current_subscription()
if charge_immediately:
self.send_invoice()
subscription_made.send(sender=self, plan=plan, stripe_response=resp)

def charge(self, amount, currency="usd", description=None):
resp = stripe.Charge.create(
amount=amount,
currency=currency,
customer=self.stripe_id,
description=description,
)
obj = self.record_charge(resp["id"])
obj.send_receipt()

def record_charge(self, charge_id):
data = stripe.Charge.retrieve(charge_id)
Expand Down
2 changes: 1 addition & 1 deletion payments/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

cancelled = Signal(providing_args=["stripe_response"])
card_changed = Signal(providing_args=["stripe_response"])
purchase_made = Signal(providing_args=["plan", "stripe_response"])
subscription_made = Signal(providing_args=["plan", "stripe_response"])
webhook_processing_error = Signal(providing_args=["data", "exception"])

WEBHOOK_SIGNALS = {
Expand Down
4 changes: 2 additions & 2 deletions payments/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def change_plan(request):
current_plan = None
if form.is_valid():
try:
request.user.customer.purchase(form.cleaned_data["plan"])
request.user.customer.subscribe(form.cleaned_data["plan"])
data = {
"form": PlanForm(initial={
"plan": current_plan
Expand Down Expand Up @@ -88,7 +88,7 @@ def subscribe(request, form_class=PlanForm):
try:
customer = request.user.customer
customer.update_card(request.POST.get("stripe_token"))
customer.purchase(form.cleaned_data["plan"])
customer.subscribe(form.cleaned_data["plan"])
data["form"] = form_class()
data["location"] = reverse("payments_history")
except stripe.StripeError, e:
Expand Down

0 comments on commit 3288109

Please sign in to comment.