Permalink
Browse files

Added Customer.charge and modified Customer.purchase

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...
1 parent 24a159f commit 3288109cfb87b61e81d880d95501c93ab2a21ef9 @brosner brosner committed Mar 4, 2013
Showing with 32 additions and 37 deletions.
  1. +2 −2 docs/signals.rst
  2. +1 −1 payments/management/commands/init_customers.py
  3. +26 −31 payments/models.py
  4. +1 −1 payments/signals.py
  5. +2 −2 payments/views.py
View
@@ -19,8 +19,8 @@ card_changed
:providing_args: `stripe_response`
-purchase_made
--------------
+subscription_made
+-----------------
:providing_args: `plan`, `stripe_response`
@@ -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
View
@@ -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
@@ -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)
View
@@ -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 = {
View
@@ -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
@@ -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:

0 comments on commit 3288109

Please sign in to comment.