Skip to content

Commit

Permalink
Merge af1c631 into 036f985
Browse files Browse the repository at this point in the history
  • Loading branch information
Hispar committed Sep 15, 2014
2 parents 036f985 + af1c631 commit fa4fd61
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
14 changes: 13 additions & 1 deletion payments/models.py
Expand Up @@ -574,7 +574,7 @@ def subscribe(self, plan, quantity=None, trial_days=None,
return resp

def charge(self, amount, currency="usd", description=None,
send_receipt=True):
send_receipt=True, capture=True):
"""
This method expects `amount` to be a Decimal type representing a
dollar amount. It will be converted to cents so any decimals beyond
Expand All @@ -589,6 +589,7 @@ def charge(self, amount, currency="usd", description=None,
currency=currency,
customer=self.stripe_id,
description=description,
captured=capture,
)
obj = self.record_charge(resp["id"])
if send_receipt:
Expand Down Expand Up @@ -820,6 +821,7 @@ class Charge(StripeObject):
paid = models.NullBooleanField(null=True)
disputed = models.NullBooleanField(null=True)
refunded = models.NullBooleanField(null=True)
captured = models.NullBooleanField(null=True)
fee = models.DecimalField(decimal_places=2, max_digits=9, null=True)
receipt_sent = models.BooleanField(default=False)
charge_created = models.DateTimeField(null=True, blank=True)
Expand All @@ -841,6 +843,15 @@ def refund(self, amount=None):
)
Charge.sync_from_stripe_data(charge_obj)

def capture(self, amount=None):
self.captured = True
charge_obj = stripe.Charge.retrieve(
self.stripe_id
).capture(
amount=convert_amount_for_api(self.calculate_refund_amount(amount=amount), self.currency)
)
Charge.sync_from_stripe_data(charge_obj)

@classmethod
def sync_from_stripe_data(cls, data):
customer = Customer.objects.get(stripe_id=data["customer"])
Expand All @@ -856,6 +867,7 @@ def sync_from_stripe_data(cls, data):
obj.amount = convert_amount_for_db(data["amount"], obj.currency)
obj.paid = data["paid"]
obj.refunded = data["refunded"]
obj.captured = data["captured"]
obj.fee = convert_amount_for_db(data["fee"]) # assume in usd only
obj.disputed = data["dispute"] is not None
obj.charge_created = convert_tstamp(data, "created")
Expand Down
7 changes: 7 additions & 0 deletions payments/tests/test_customer.py
Expand Up @@ -289,6 +289,7 @@ def test_record_charge(self, RetrieveMock):
"currency": "usd",
"paid": True,
"refunded": False,
"captured": True,
"fee": 499,
"dispute": None,
"created": 1363911708,
Expand Down Expand Up @@ -325,6 +326,7 @@ def test_refund_charge(self, RetrieveMock):
"currency": "usd",
"paid": True,
"refunded": True,
"captured": True,
"amount_refunded": 1000,
"fee": 499,
"dispute": None,
Expand Down Expand Up @@ -386,6 +388,7 @@ def test_charge_converts_dollars_into_cents(self, ChargeMock, RetrieveMock):
"currency": "usd",
"paid": True,
"refunded": False,
"captured": True,
"fee": 499,
"dispute": None,
"created": 1363911708,
Expand All @@ -410,6 +413,7 @@ def test_record_charge_in_jpy_with(self, RetrieveMock):
"currency": "jpy",
"paid": True,
"refunded": False,
"captured": True,
"fee": 499,
"dispute": None,
"created": 1363911708,
Expand All @@ -434,6 +438,7 @@ def test_refund_charge_in_jpy(self, RetrieveMock):
currency="jpy",
paid=True,
refunded=False,
captured=True,
fee=decimal.Decimal("4.99"),
disputed=False
)
Expand All @@ -449,6 +454,7 @@ def test_refund_charge_in_jpy(self, RetrieveMock):
"refunded": True,
"amount_refunded": 1000,
"fee": 499,
"captured": True,
"dispute": None,
"created": 1363911708,
"customer": "cus_xxxxxxxxxxxxxxx"
Expand Down Expand Up @@ -508,6 +514,7 @@ def test_charge_do_not_converts_dollars_in_jpy(self, ChargeMock, RetrieveMock):
"currency": "jpy",
"paid": True,
"refunded": False,
"captured": True,
"fee": 499,
"dispute": None,
"created": 1363911708,
Expand Down
2 changes: 2 additions & 0 deletions payments/tests/test_email.py
Expand Up @@ -37,6 +37,7 @@ def test_email_receipt_renders_amount_properly(self, ChargeMock, RetrieveMock):
"currency": "usd",
"paid": True,
"refunded": False,
"captured": True,
"fee": 499,
"dispute": None,
"created": 1363911708,
Expand All @@ -61,6 +62,7 @@ def test_email_receipt_renders_amount_in_JPY_properly(self, ChargeMock, Retrieve
"currency": "jpy",
"paid": True,
"refunded": False,
"captured": True,
"fee": 499,
"dispute": None,
"created": 1363911708,
Expand Down

0 comments on commit fa4fd61

Please sign in to comment.