Skip to content

Commit

Permalink
More work to rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
paltman committed Nov 19, 2015
1 parent e5f980d commit c3532cd
Show file tree
Hide file tree
Showing 17 changed files with 221 additions and 253 deletions.
3 changes: 2 additions & 1 deletion pinax/stripe/actions/customers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import stripe

from . import syncs
from . import invoices
from ..conf import settings
from .. import hooks
from .. import proxies
Expand Down Expand Up @@ -40,5 +41,5 @@ def create(user, card=None, plan=None, charge_immediately=True):
syncs.sync_customer(cus, stripe_customer)

if plan and charge_immediately:
cus.send_invoice()
invoices.create_and_pay(cus)
return cus
19 changes: 14 additions & 5 deletions pinax/stripe/actions/refunds.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import stripe

def refund(self, amount=None):
self.stripe_charge.refund(
amount=utils.convert_amount_for_api(self.calculate_refund_amount(amount=amount), self.currency)
)
signals.charge_refunded.send(sender=self, charge_proxy=self)
from .. import signals
from .. import utils


def create(charge, amount=None):
if amount is None:
stripe.Refund.create(charge=charge.stripe_id)
else:
stripe.Refund.create(
charge=charge.stripe_id,
amount=utils.convert_amount_for_api(charge.calculate_refund_amount(amount=amount), charge.currency)
)
signals.charge_refunded.send(sender=charge, charge_proxy=charge)
3 changes: 2 additions & 1 deletion pinax/stripe/actions/subscriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ def update(subscription, plan=None, quantity=None, prorate=True, coupon=None, ch
if coupon:
stripe_subscription.coupon = coupon
sub = stripe_subscription.save()
syncs.sync_subscription_from_stripe_data(sub)
customer = proxies.CustomerProxy.objects.get(pk=subscription.customer.pk)
syncs.sync_subscription_from_stripe_data(customer, sub)


def create(customer, plan, quantity=None, trial_days=None, charge_immediately=True, token=None, coupon=None):
Expand Down
26 changes: 13 additions & 13 deletions pinax/stripe/actions/syncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@ def sync_payment_source_from_stripe_data(customer, source):
if source.id.startswith("card_"):
defaults = dict(
customer=customer,
name=source.name,
address_line_1=source.address_line1,
address_line_1_check=source.address_line1_check,
address_line_2=source.address_line2,
address_city=source.address_city,
address_state=source.address_state,
address_country=source.address_country,
address_zip=source.address_zip,
address_zip_check=source.address_zip_check,
name=source.name or "",
address_line_1=source.address_line1 or "",
address_line_1_check=source.address_line1_check or "",
address_line_2=source.address_line2 or "",
address_city=source.address_city or "",
address_state=source.address_state or "",
address_country=source.address_country or "",
address_zip=source.address_zip or "",
address_zip_check=source.address_zip_check or "",
brand=source.brand,
country=source.country,
cvc_check=source.cvc_check,
dynamic_last4=source.dynamic_last4,
dynamic_last4=source.dynamic_last4 or "",
exp_month=source.exp_month,
exp_year=source.exp_year,
funding=source.funding,
last4=source.last4,
fingerprint=source.fingerprint
funding=source.funding or "",
last4=source.last4 or "",
fingerprint=source.fingerprint or ""
)
card, created = proxies.CardProxy.objects.get_or_create(
stripe_id=source.id,
Expand Down
26 changes: 13 additions & 13 deletions pinax/stripe/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,47 @@ class CustomerManager(models.Manager):

def started_during(self, year, month):
return self.exclude(
subscription_set__status="trialing"
subscription__status="trialing"
).filter(
subscription_set__start__year=year,
subscription_set__start__month=month
subscription__start__year=year,
subscription__start__month=month
)

def active(self):
return self.filter(
subscription_set__status="active"
subscription__status="active"
)

def canceled(self):
return self.filter(
subscription_set__status="canceled"
subscription__status="canceled"
)

def canceled_during(self, year, month):
return self.canceled().filter(
subscription_set__canceled_at__year=year,
subscription_set__canceled_at__month=month,
subscription__canceled_at__year=year,
subscription__canceled_at__month=month,
)

def started_plan_summary_for(self, year, month):
return self.started_during(year, month).values(
"subscription_set__plan"
"subscription__plan"
).order_by().annotate(
count=models.Count("subscription_set__plan")
count=models.Count("subscription__plan")
)

def active_plan_summary(self):
return self.active().values(
"subscription_set__plan"
"subscription__plan"
).order_by().annotate(
count=models.Count("subscription_set__plan")
count=models.Count("subscription__plan")
)

def canceled_plan_summary_for(self, year, month):
return self.canceled_during(year, month).values(
"subscription_set__plan"
"subscription__plan"
).order_by().annotate(
count=models.Count("subscription_set__plan")
count=models.Count("subscription__plan")
)

def churn(self):
Expand Down
2 changes: 0 additions & 2 deletions pinax/stripe/proxies/charges.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
from .. import hooks
from .. import managers
from .. import models
from .. import signals
from .. import utils


class ChargeProxy(models.Charge):
Expand Down
2 changes: 0 additions & 2 deletions pinax/stripe/proxies/customers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

from .. import managers
from .. import models
from .. import signals
from .. import utils


class CustomerProxy(models.Customer):
Expand Down
5 changes: 5 additions & 0 deletions pinax/stripe/proxies/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from .customers import CustomerProxy
from .exceptions import EventProcessingExceptionProxy
from .subscriptions import SubscriptionProxy
from .transfers import TransferProxy


Expand Down Expand Up @@ -66,6 +67,10 @@ def process(self): # @@@ to complex, fix later # noqa
self.message["data"]["object"]
)
elif self.kind.startswith("customer.subscription."):
if self.kind == "customer.subscription.deleted":
sub = next(iter(SubscriptionProxy.objects.filter(stripe_id=self.validated_message["data"]["object"]["id"])), None)
if sub is not None:
sub.delete()
if self.customer:
signals.customer_subscription_event.send(sender=EventProxy, event=self, customer=self.customer)
elif self.kind == "customer.deleted":
Expand Down
1 change: 0 additions & 1 deletion pinax/stripe/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
charge_refunded = Signal(providing_args=["charge_proxy"])
charge_captured = Signal(providing_args=["charge_proxy"])
cancelled = Signal(providing_args=["stripe_response"])
card_changed = Signal(providing_args=["stripe_response"])
charge_event_received = Signal(providing_args=["event"])
customer_subscription_event = Signal(providing_args=["event", "customer"])
invoice_event_received = Signal(providing_args=["event"])
Expand Down
13 changes: 7 additions & 6 deletions pinax/stripe/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ def setUp(self):
@patch("stripe.Customer.retrieve")
@patch("stripe.Customer.create")
def test_init_customer_creates_customer(self, CreateMock, RetrieveMock):
CreateMock.return_value.id = "cus_XXXXX"
CreateMock.return_value.active_card.fingerprint = "X"
CreateMock.return_value.active_card.last4 = "1234"
CreateMock.return_value.active_card.type = "Visa"
cu = CreateMock()
cu.account_balance = 0
cu.delinquent = False
cu.default_source = "card_178Zqj2eZvKYlo2Cr2fUZZz7"
cu.currency = "usd"
cu.id = "cus_XXXXX"
management.call_command("init_customers")
customer = CustomerProxy.get_for_user(self.user)
self.assertEquals(customer.stripe_id, "cus_XXXXX")
Expand All @@ -45,13 +47,12 @@ def test_plans_create(self, CreateMock):
@patch("pinax.stripe.actions.syncs.sync_customer")
@patch("pinax.stripe.actions.syncs.sync_invoices_for_customer")
@patch("pinax.stripe.actions.syncs.sync_charges_for_customer")
def test_sync_customers(self, SyncChargesMock, SyncInvoicesMock, SyncSubscriptionMock, SyncMock, RetrieveMock):
def test_sync_customers(self, SyncChargesMock, SyncInvoicesMock, SyncMock, RetrieveMock):
user2 = get_user_model().objects.create_user(username="thomas")
get_user_model().objects.create_user(username="altman")
CustomerProxy.objects.create(stripe_id="cus_XXXXX", user=self.user)
CustomerProxy.objects.create(stripe_id="cus_YYYYY", user=user2)
management.call_command("sync_customers")
self.assertEqual(SyncChargesMock.call_count, 2)
self.assertEqual(SyncInvoicesMock.call_count, 2)
self.assertEqual(SyncSubscriptionMock.call_count, 2)
self.assertEqual(SyncMock.call_count, 2)

0 comments on commit c3532cd

Please sign in to comment.