Skip to content

Commit

Permalink
Add currency to support_level
Browse files Browse the repository at this point in the history
  • Loading branch information
Sjors committed Apr 30, 2019
1 parent 37c6a35 commit d1f0b09
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 14 deletions.
4 changes: 2 additions & 2 deletions app/api/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def update_sub():

@bp.route('/v1/square/<int:price>', methods=['GET', 'POST'])
@login_required
def process_square(price):
def process_square(price, currency):
'''
Receives a nonce from Square, and uses the nonce to
charge the card. Upon successful charge, it updates the
Expand Down Expand Up @@ -147,7 +147,7 @@ def process_square(price):
transactions_api = TransactionsApi(api_client)
idempotency_key = str(uuid.uuid1())
cents = price * 100
amount = {'amount': cents, 'currency': 'USD'}
amount = {'amount': cents, 'currency': currency}
body = {
'idempotency_key': idempotency_key,
'customer_id': current_user.square_id,
Expand Down
11 changes: 10 additions & 1 deletion app/main/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,19 @@ def support():
name='Patron',
description="You're a patron!",
price=10,
currency='USD'
)
level_2 = PriceLevel(
name='Cooler Patron',
description="You're a cooler patron!",
price=20,
currency='USD'
)
level_3 = PriceLevel(
name='Coolest Patron',
description="You're the best!",
price=60,
currency='USD'
)
db.session.add(level_1)
db.session.add(level_2)
Expand All @@ -117,6 +120,7 @@ def support():
def credit_card():
# directs user to sqpaymentform.js
price = request.args.get('price')
currency = request.args.get('currency') or 'USD'
if price is None:
flash('There was an error. Try again.')
return redirect(url_for('main.support'))
Expand All @@ -127,6 +131,7 @@ def credit_card():
application_id=square.application_id,
location_id=square.location_id,
price=price,
currency=currency
)
else:
return redirect(url_for('main.index'))
Expand All @@ -152,6 +157,7 @@ def create_invoice():
else:
plan = price_level.name
price = price_level.price
currency = price_level.currency or 'USD'
else:
return redirect(url_for('main.support'))
else:
Expand All @@ -160,6 +166,9 @@ def create_invoice():
return redirect(url_for('main.support'))
plan = request.args.get('name')
price = int(string_price)
currency = request.args.get('currency')
if currency is None:
currency = 'USD'
compare = PriceLevel.query.filter_by(price=price).first()
if compare is None:
return redirect(url_for('main.support'))
Expand All @@ -183,7 +192,7 @@ def create_invoice():
try:
inv_data = btc_client.create_invoice({
"price": price,
"currency": "USD",
"currency": currency,
"buyer": {
"name": current_user.username,
"email": current_user.email,
Expand Down
8 changes: 4 additions & 4 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class PriceLevel(db.Model):
name = db.Column(db.String(64), index=True, unique=True)
price = db.Column(db.Integer, index=True, unique=True)
description = db.Column(db.Text)

currency = db.Column(db.String(3))

class BTCPayClientStore(db.Model):
# object for storing pickled BTCPay API client
Expand Down Expand Up @@ -100,9 +100,9 @@ def __str__(self):
expire_date = self.expiration.date()
return f'''
{self.id},
{self.username},
{self.email},
{expire_date},
{self.username},
{self.email},
{expire_date},
{self.role},
{self.mail_opt_out}
'''
Expand Down
2 changes: 1 addition & 1 deletion app/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def renewals_square(begin):
failed_list.append(user)
continue
cents = price_level.price * 100
amount = {'amount': cents, 'currency': 'USD'}
amount = {'amount': cents, 'currency': price_level.currency or 'USD'}
body = {
'idempotency_key': idempotency_key,
'customer_id': user.square_id,
Expand Down
5 changes: 2 additions & 3 deletions app/templates/main/creditcard.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
the Transaction API charge endpoint URL you want to POST the nonce to
(for example, "/process-card")
-->
<form id="nonce-form" novalidate action="{{ url_for('api.process_square', price=price) }}" method="post">
<form id="nonce-form" novalidate action="{{ url_for('api.process_square', price=price, currency=currency) }}" method="post">
<fieldset>
<span class="label">Card Number</span>
<div id="sq-card-number"></div>
Expand All @@ -49,7 +49,7 @@
</div>
</fieldset>

<button id="sq-creditcard" class="button-credit-card" onclick="requestCardNonce(event)">Subscribe for ${{ price }}</button>
<button id="sq-creditcard" class="button-credit-card" onclick="requestCardNonce(event)">Subscribe for {{ currency }}{{ price }}</button>

<div id="error"></div>

Expand All @@ -64,4 +64,3 @@
{% endblock main %}
{% block extrajs %}
{% endblock extrajs %}

6 changes: 3 additions & 3 deletions app/templates/main/support.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ <h1 class="display-4">Support Levels</h1>
<h4 class="my-0 font-weight-normal">{{ level.name }}</h4>
</div>
<div class="card-body">
<h1 class="card-title pricing-card-title">${{ level.price }} <small class="text-muted">/ mo</small></h1>
<h1 class="card-title pricing-card-title">{{ level.currency }}{{ level.price }} <small class="text-muted">/ mo</small></h1>
<p>{{ level.description }}</p>
<p><a class="btn btn-primary" href="{{ url_for('main.create_invoice', name=level.name, price=level.price) }}">Subscribe (Bitcoin)</a></p>
<p><a class="btn btn-primary" href="{{ url_for('main.create_invoice', name=level.name, price=level.price, currency=level.currency) }}">Subscribe (Bitcoin)</a></p>
{%if square %}
<p><a class="btn btn-primary" href="{{ url_for('main.credit_card', name=level.name, price=level.price) }}">Subscribe (Fiat)</a></p>
<p><a class="btn btn-primary" href="{{ url_for('main.credit_card', name=level.name, price=level.price, currency=level.currency) }}">Subscribe (Fiat)</a></p>
{% endif %}
</div>
</div>
Expand Down
23 changes: 23 additions & 0 deletions migrations/versions/c13ea9c15280_add_currency_to_price_level.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""Add currency to price level
Revision ID: c13ea9c15280
Revises: 5346190a2f75
Create Date: 2019-04-30 20:08:08.487672
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'c13ea9c15280'
down_revision = '5346190a2f75'
branch_labels = None
depends_on = None


def upgrade():
op.add_column('price_level', sa.Column('currency', sa.String(length=3), default="USD"))

def downgrade():
op.drop_column('price_level', 'currency')
3 changes: 3 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,19 @@ def init_database(new_user, test_mail):
level_1 = PriceLevel(
name='Patron',
description="You're a patron!",
currency="USD",
price=10,
)
level_2 = PriceLevel(
name='Cooler Patron',
description="You're a cooler patron!",
currency="USD",
price=20,
)
level_3 = PriceLevel(
name='Coolest Patron',
description="You're the best!",
currency="USD",
price=60,
)
db.session.add(level_1)
Expand Down

0 comments on commit d1f0b09

Please sign in to comment.