Skip to content

Commit

Permalink
Merge branch 'master' into improve-webhooks-process
Browse files Browse the repository at this point in the history
  • Loading branch information
paltman committed Nov 8, 2017
2 parents 6afaf71 + 3b8e612 commit 5baf123
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 16 deletions.
17 changes: 10 additions & 7 deletions pinax/stripe/actions/charges.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,30 @@ def calculate_refund_amount(charge, amount=None):
Args:
charge: a pinax.stripe.models.Charge object
amount: optionally, the decimal.Decimal amount you wish to refund
idempotency_key: Any string that allows retries to be performed safely.
"""
eligible_to_refund = charge.amount - (charge.amount_refunded or 0)
if amount:
return min(eligible_to_refund, amount)
return eligible_to_refund


def capture(charge, amount=None):
def capture(charge, amount=None, idempotency_key=None):
"""
Capture the payment of an existing, uncaptured, charge.
Args:
charge: a pinax.stripe.models.Charge object
amount: the decimal.Decimal amount of the charge to capture
"""
stripe_charge = charge.stripe_charge.capture(
amount=utils.convert_amount_for_api(
amount if amount else charge.amount,
charge.currency
),
expand=["balance_transaction"]
amount = utils.convert_amount_for_api(
amount if amount else charge.amount,
charge.currency
)
stripe_charge = stripe.Charge(charge.stripe_id).capture(
amount=amount,
idempotency_key=idempotency_key,
expand=["balance_transaction"],
)
sync_charge_from_stripe_data(stripe_charge)

Expand Down
1 change: 1 addition & 0 deletions pinax/stripe/actions/subscriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def create(customer, plan, quantity=None, trial_days=None, token=None, coupon=No
if token:
subscription_params["source"] = token

subscription_params["stripe_account"] = customer.stripe_account_stripe_id
subscription_params["customer"] = customer.stripe_id
subscription_params["plan"] = plan
subscription_params["quantity"] = quantity
Expand Down
16 changes: 16 additions & 0 deletions pinax/stripe/migrations/0018_merge_20171107_1540.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.6 on 2017-11-07 14:40
from __future__ import unicode_literals

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('pinax_stripe', '0017_merge_20171106_2201'),
('pinax_stripe', '0017_merge_20171106_2109'),
]

operations = [
]
10 changes: 10 additions & 0 deletions pinax/stripe/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,16 @@ class Charge(StripeAccountFromCustomerMixin, StripeObject):

objects = ChargeManager()

def __repr__(self):
return "Charge(customer={!r}, source={!r}, amount={!r}, captured={!r}, paid={!r}, stripe_id={!r})".format(
self.customer,
str(self.source),
self.amount,
self.captured,
self.paid,
str(self.stripe_id),
)

@property
def stripe_charge(self):
return stripe.Charge.retrieve(
Expand Down
50 changes: 41 additions & 9 deletions pinax/stripe/tests/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,19 +239,21 @@ def test_create_app_fee_dest_acct_and_dest_amt_raises_exception(self, CreateMock
)

@patch("pinax.stripe.actions.charges.sync_charge_from_stripe_data")
@patch("stripe.Charge.retrieve")
def test_capture(self, RetrieveMock, SyncMock):
charges.capture(Charge(amount=decimal.Decimal("100"), currency="usd"))
self.assertTrue(RetrieveMock.return_value.capture.called)
@patch("stripe.Charge.capture")
def test_capture(self, CaptureMock, SyncMock):
charges.capture(Charge(stripe_id="ch_A", amount=decimal.Decimal("100"), currency="usd"))
self.assertTrue(CaptureMock.called)
self.assertTrue(SyncMock.called)

@patch("pinax.stripe.actions.charges.sync_charge_from_stripe_data")
@patch("stripe.Charge.retrieve")
def test_capture_with_amount(self, RetrieveMock, SyncMock):
charges.capture(Charge(amount=decimal.Decimal("100"), currency="usd"), amount=decimal.Decimal("50"))
self.assertTrue(RetrieveMock.return_value.capture.called)
_, kwargs = RetrieveMock.return_value.capture.call_args
@patch("stripe.Charge.capture")
def test_capture_with_amount(self, CaptureMock, SyncMock):
charge = Charge(stripe_id="ch_A", amount=decimal.Decimal("100"), currency="usd")
charges.capture(charge, amount=decimal.Decimal("50"), idempotency_key="IDEM")
self.assertTrue(CaptureMock.called)
_, kwargs = CaptureMock.call_args
self.assertEquals(kwargs["amount"], 5000)
self.assertEquals(kwargs["idempotency_key"], "IDEM")
self.assertTrue(SyncMock.called)

@patch("pinax.stripe.actions.charges.sync_charge")
Expand Down Expand Up @@ -918,6 +920,36 @@ def test_subscription_create_token(self, SubscriptionCreateMock, CustomerMock):
_, kwargs = SubscriptionCreateMock.call_args
self.assertEquals(kwargs["source"], "token")

@patch("stripe.Subscription.create")
def test_subscription_create_with_connect(self, SubscriptionCreateMock):
SubscriptionCreateMock.return_value = {
"object": "subscription",
"id": "sub_XX",
"application_fee_percent": None,
"cancel_at_period_end": False,
"canceled_at": None,
"current_period_start": 1509978774,
"current_period_end": 1512570774,
"ended_at": None,
"quantity": 1,
"start": 1509978774,
"status": "active",
"trial_start": None,
"trial_end": None,
"plan": {
"id": self.plan.stripe_id,
}}
subscriptions.create(self.connected_customer, self.plan.stripe_id)
SubscriptionCreateMock.assert_called_once_with(
coupon=None,
customer=self.connected_customer.stripe_id,
plan="the-plan",
quantity=4,
stripe_account="acct_xx",
tax_percent=None)
subscription = Subscription.objects.get()
self.assertEqual(subscription.customer, self.connected_customer)

@patch("stripe.Subscription.retrieve")
@patch("stripe.Subscription.create")
def test_retrieve_subscription_with_connect(self, CreateMock, RetrieveMock):
Expand Down
4 changes: 4 additions & 0 deletions pinax/stripe/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ def test_customer_str_and_repr(self):
self.assertTrue("None" in str(c))
self.assertEquals(repr(c), "Customer(pk=None, user=None, stripe_id='')")

def test_charge_repr(self):
charge = Charge()
self.assertEquals(repr(charge), "Charge(customer=None, source='', amount=None, captured=None, paid=None, stripe_id='')")

def test_plan_display_invoiceitem(self):
p = Plan(amount=decimal.Decimal("5"), name="My Plan", interval="monthly", interval_count=1)
p.save()
Expand Down

0 comments on commit 5baf123

Please sign in to comment.