Skip to content
This repository has been archived by the owner on Feb 8, 2018. It is now read-only.

Commit

Permalink
Fix credit card page for Balanced cards
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitpaulk committed May 9, 2015
1 parent 4fca7e0 commit 39567d0
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 22 deletions.
36 changes: 36 additions & 0 deletions gratipay/billing/instruments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import braintree
import balanced

class CreditCard:
def __init__(self, *args, **kwargs):
fields = [
'number',
'expiration_year',
'expiration_month',
'address_postal_code',
'cardholder_name'
]
for field in fields:
setattr(self, field, kwargs.pop(field, ''))

@classmethod
def from_route(cls, route):
if route.network == 'braintree-cc':
card = braintree.PaymentMethod.find(route.address)
return cls(
number=card.masked_number,
expiration_month=card.expiration_month,
expiration_year=card.expiration_year,
address_postal_code=card.billing_address.postal_code,
cardholder_name=card.cardholder_name
)
else:
assert route.network == 'balanced-cc'
card = balanced.Card.fetch(route.address)
return cls(
number=card.number,
expiration_month=card.expiration_month,
expiration_year=card.expiration_year,
address_postal_code=card.address['postal_code'],
cardholder_name=card.name
)
15 changes: 0 additions & 15 deletions gratipay/billing/null_objects.py

This file was deleted.

4 changes: 4 additions & 0 deletions tests/py/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ def test_credit_card_page_loads_when_there_is_a_card(self):
actual = self.client.GET('/janet/routes/credit-card.html', auth_as='janet').body.decode('utf8')
assert expected in actual

def test_credit_card_page_shows_details_for_balanced_cards(self):
response = self.client.GET('/janet/routes/credit-card.html', auth_as='janet').body.decode('utf8')
assert self.card.number in response

def test_credit_card_page_shows_card_failing(self):
ExchangeRoute.from_network(self.janet, 'balanced-cc').update_error('Some error')
expected = 'Your credit card is <em id="status">failing'
Expand Down
18 changes: 11 additions & 7 deletions www/%username/routes/credit-card.html.spt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import braintree

from gratipay.models.exchange_route import ExchangeRoute
from gratipay.utils import get_participant
from gratipay.billing.null_objects import NullCard
from gratipay.billing.instruments import CreditCard

[-----------------------------------------------------------------------------]

Expand All @@ -15,9 +15,13 @@ if not user.ANON:
status = "missing"
status_msg = _("Your credit card is {0}missing{1}")

route = ExchangeRoute.from_network(participant, 'braintree-cc')
braintree_route = ExchangeRoute.from_network(participant, 'braintree-cc')
balanced_route = ExchangeRoute.from_network(participant, 'balanced-cc')

route = braintree_route or balanced_route

if route:
card = braintree.PaymentMethod.find(route.address)
card = CreditCard.from_route(route)
last_bill_result = route.error
if last_bill_result == "":
status = "working"
Expand All @@ -26,7 +30,7 @@ if not user.ANON:
status = "failing"
status_msg = _("Your credit card is {0}failing{1}")
else:
card = NullCard()
card = CreditCard()

title = _("Credit Card")

Expand Down Expand Up @@ -82,8 +86,8 @@ title = _("Credit Card")
<span>{{ _("Credit Card Number") }}</span>
<input id="card_number" required />
<span class="invalid-msg">{{ _("This card number is invalid.") }}</span>
{% if card.masked_number %}<span>{{
_("Current: {0}", card.masked_number)
{% if card.number %}<span>{{
_("Current: {0}", card.number)
}}</span>{% endif %}
</label>

Expand All @@ -105,7 +109,7 @@ title = _("Credit Card")

<label class="half">
<span>{{ _("ZIP or Postal Code") }}</span>
<input id="zip" value="{{ card.billing_address.postal_code }}" />
<input id="zip" value="{{ card.address_postal_code }}" />
</label>

<div class="info">
Expand Down

0 comments on commit 39567d0

Please sign in to comment.