Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add currency to support level #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions app/api/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ def update_sub():
return "Invalid transaction ID.", 400


@bp.route('/v1/square/<int:price>', methods=['GET', 'POST'])
@bp.route('/v1/square/<int:price>/<string:currency>', 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
14 changes: 13 additions & 1 deletion app/main/routes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from app import blog_engine, db
from app.main import bp
from app.models import BTCPayClientStore, Square, PriceLevel
from babel import numbers
from datetime import datetime
from flask import redirect, url_for, flash, render_template, request,\
current_app
Expand Down Expand Up @@ -87,16 +88,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 @@ -109,6 +113,7 @@ def support():
'main/support.html',
levels=price_levels,
square=square,
numbers=numbers
)


Expand All @@ -117,6 +122,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 +133,8 @@ def credit_card():
application_id=square.application_id,
location_id=square.location_id,
price=price,
currency=currency,
numbers=numbers
)
else:
return redirect(url_for('main.index'))
Expand All @@ -152,6 +160,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 +169,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 +195,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
3 changes: 2 additions & 1 deletion app/templates/admin/custom_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ <h2>Pricing Setup</h2>
<div style="font-size:x-small">
<p>Notes if Square Recurring Billing is Active:</p>
<ol>
<li> Deleting a price level will deactivate recurring credit card billing for all users that that price level. On their renewal date, they'll get an email with a link to choose a new plan.</li>
<li>The currency must match your Square home currency</li>
<li>Deleting a price level will deactivate recurring credit card billing for all users that that price level. On their renewal date, they'll get an email with a link to choose a new plan.</li>
<li>Changing the name of a price level will also deactivate recurring credit card billing for all users that that price level. On their renewal date, they'll get an email with a link to choose a new plan.</li>
<li>Changing the price of a given plan will not alter recurring billing so long as the plan keeps the same name. Your users will be billed the new price, so to not blindside your users it is suggested that you post an update with your new pricing and set said update to be emailed.</li>
<li>Changing the description of a price level has no effects on recurring billing.</li>
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 {{ numbers.format_currency(price, currency, locale='en') }}</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">{{ numbers.format_currency(level.price, level.currency, locale='en') }} <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
1 change: 1 addition & 0 deletions migrations/alembic.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# the 'revision' command, regardless of autogenerate
# revision_environment = false

script_location = .

# Logging configuration
[loggers]
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')
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
alembic==1.0.5
APScheduler==3.5.3
asn1crypto==0.24.0
Babel==2.6.0
blinker==1.4
btcpay-python==1.1.0
certifi==2018.11.29
Expand Down
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