From 5ee4e785687f7bbcaa0e8c99ccdeceb2ed598e78 Mon Sep 17 00:00:00 2001 From: Chad Whitacre Date: Thu, 21 May 2015 08:38:09 -0400 Subject: [PATCH] Bring back Your Tip as Your Payment --- js/gratipay.js | 2 +- js/gratipay/payments.js | 86 ++++++++++++++++++++++++++++++++++++ js/gratipay/tips.js | 86 ------------------------------------ scss/components/cta.scss | 4 +- templates/your-payment.html | 38 ++++++++++++++++ templates/your-tip.html | 39 ---------------- www/%team/index.html.spt | 8 ++-- www/~/%username/tip.json.spt | 72 ------------------------------ 8 files changed, 132 insertions(+), 203 deletions(-) create mode 100644 js/gratipay/payments.js delete mode 100644 js/gratipay/tips.js create mode 100644 templates/your-payment.html delete mode 100644 templates/your-tip.html delete mode 100644 www/~/%username/tip.json.spt diff --git a/js/gratipay.js b/js/gratipay.js index 86940ac04c..129cd3fc81 100644 --- a/js/gratipay.js +++ b/js/gratipay.js @@ -14,7 +14,7 @@ Gratipay.init = function() { Gratipay.forms.initCSRF(); Gratipay.signIn(); Gratipay.signOut(); - Gratipay.tips.initSupportGratipay(); + Gratipay.payments.initSupportGratipay(); }; Gratipay.error = function(jqXHR, textStatus, errorThrown) { diff --git a/js/gratipay/payments.js b/js/gratipay/payments.js new file mode 100644 index 0000000000..b8cf3e5a5c --- /dev/null +++ b/js/gratipay/payments.js @@ -0,0 +1,86 @@ +Gratipay.payments = {}; + +Gratipay.payments.init = function() { + + Gratipay.forms.jsEdit({ + confirmBeforeUnload: true, + hideEditButton: true, + root: $('.your-payment.js-edit'), + success: function(data) { + Gratipay.notification(data.msg, 'success'); + Gratipay.payments.afterTipChange(data); + } + }); + + $('.your-payment button.edit').click(function() { + $('.your-payment input').focus(); + }); + + $('.your-payment button.stop').click(function() { + $('.your-payment input').val('0'); + $('.your-payment button.save').click(); + }); + + $('.your-payment button.cancel').click(function() { + $('.your-payment form').trigger('reset'); + }); + + // Cancel if the user presses the Escape key + $('.your-payment input').keyup(function(e) { + if (e.keyCode === 27) + $('.your-payment button.cancel').click(); + }); +}; + + +Gratipay.payments.initSupportGratipay = function() { + $('.support-gratipay button').click(function() { + var amount = parseFloat($(this).attr('data-amount'), 10); + Gratipay.payments.set('Gratipay', amount, function(data) { + Gratipay.notification(data.msg, 'success'); + $('.support-gratipay').slideUp(); + + // If you're on your own giving page ... + var payment_on_giving = $('.your-payment[data-team="Gratipay"]'); + if (payment_on_giving.length > 0) { + payment_on_giving[0].defaultValue = amount; + payment_on_giving.attr('value', amount.toFixed(2)); + } + }); + }); + + $('.support-gratipay .no-thanks').click(function(event) { + event.preventDefault(); + jQuery.post('/ride-free.json') + .success(function() { $('.support-gratipay').slideUp(); }) + .fail(Gratipay.error) + }); +}; + + +Gratipay.payments.afterTipChange = function(data) { + $('.my-total-giving').text(data.total_giving_l); + $('.total-receiving[data-team="'+data.team_id+'"]').text(data.total_receiving_team_l); + $('#payment-prompt').toggleClass('needed', data.amount > 0); + $('.nsupporters[data-team="'+data.team_id+'"]').text(data.nsupporters); + + var $your_payment = $('.your-payment[data-team="'+data.team_id+'"]'); + if ($your_payment) { + var $input = $your_payment.find('input'); + $input[0].defaultValue = $input.val(); + $your_payment.find('span.amount').text(data.amount_l); + $your_payment.find('.edit').toggleClass('not-zero', data.amount > 0); + $your_payment.find('.stop').toggleClass('zero', data.amount === 0); + } +}; + + +Gratipay.payments.set = function(team, amount, callback) { + + // send request to set up a recurring payment + $.post('/' + team + '/subscription.json', { amount: amount }, function(data) { + if (callback) callback(data); + Gratipay.payments.afterTipChange(data); + }) + .fail(Gratipay.error); +}; diff --git a/js/gratipay/tips.js b/js/gratipay/tips.js deleted file mode 100644 index 2d4e2114e2..0000000000 --- a/js/gratipay/tips.js +++ /dev/null @@ -1,86 +0,0 @@ -Gratipay.tips = {}; - -Gratipay.tips.init = function() { - - Gratipay.forms.jsEdit({ - confirmBeforeUnload: true, - hideEditButton: true, - root: $('.your-tip.js-edit'), - success: function(data) { - Gratipay.notification(data.msg, 'success'); - Gratipay.tips.afterTipChange(data); - } - }); - - $('.your-tip button.edit').click(function() { - $('.your-tip input').focus(); - }); - - $('.your-tip button.stop').click(function() { - $('.your-tip input').val('0'); - $('.your-tip button.save').click(); - }); - - $('.your-tip button.cancel').click(function() { - $('.your-tip form').trigger('reset'); - }); - - // Cancel if the user presses the Escape key - $('.your-tip input').keyup(function(e) { - if (e.keyCode === 27) - $('.your-tip button.cancel').click(); - }); -}; - - -Gratipay.tips.initSupportGratipay = function() { - $('.support-gratipay button').click(function() { - var amount = parseFloat($(this).attr('data-amount'), 10); - Gratipay.tips.set('Gratipay', amount, function(data) { - Gratipay.notification(data.msg, 'success'); - $('.support-gratipay').slideUp(); - - // If you're on your own giving page ... - var tip_on_giving = $('.your-tip[data-tippee="Gratipay"]'); - if (tip_on_giving.length > 0) { - tip_on_giving[0].defaultValue = amount; - tip_on_giving.attr('value', amount.toFixed(2)); - } - }); - }); - - $('.support-gratipay .no-thanks').click(function(event) { - event.preventDefault(); - jQuery.post('/ride-free.json') - .success(function() { $('.support-gratipay').slideUp(); }) - .fail(Gratipay.error) - }); -}; - - -Gratipay.tips.afterTipChange = function(data) { - $('.my-total-giving').text(data.total_giving_l); - $('.total-receiving[data-tippee="'+data.tippee_id+'"]').text(data.total_receiving_tippee_l); - $('#payment-prompt').toggleClass('needed', data.amount > 0); - $('.npatrons[data-tippee="'+data.tippee_id+'"]').text(data.npatrons); - - var $your_tip = $('.your-tip[data-tippee="'+data.tippee_id+'"]'); - if ($your_tip) { - var $input = $your_tip.find('input'); - $input[0].defaultValue = $input.val(); - $your_tip.find('span.amount').text(data.amount_l); - $your_tip.find('.edit').toggleClass('not-zero', data.amount > 0); - $your_tip.find('.stop').toggleClass('zero', data.amount === 0); - } -}; - - -Gratipay.tips.set = function(tippee, amount, callback) { - - // send request to change tip - $.post('/' + tippee + '/tip.json', { amount: amount }, function(data) { - if (callback) callback(data); - Gratipay.tips.afterTipChange(data); - }) - .fail(Gratipay.error); -}; diff --git a/scss/components/cta.scss b/scss/components/cta.scss index 1ba64443d4..bc351b5042 100644 --- a/scss/components/cta.scss +++ b/scss/components/cta.scss @@ -8,7 +8,7 @@ color: $white; } - .your-tip .amount { + .your-payment .amount { /* Match content h1 */ font: bold 28px/37px $Ideal; } @@ -17,7 +17,7 @@ font: normal 14px/14px $Ideal; } - .your-tip { + .your-payment { input { width: 120px; height: 33px; diff --git a/templates/your-payment.html b/templates/your-payment.html new file mode 100644 index 0000000000..74eb6b2955 --- /dev/null +++ b/templates/your-payment.html @@ -0,0 +1,38 @@ +{% if user.ANON %} +
+ {% include "templates/sign-in-using-to-give.html" %} +
+{% else %} +
+ {% set subscription = user.participant.get_subscription_to(team.slug) %} +

{{ _('Your Payment') }}

+
+
+ {{ format_currency(subscription.amount, 'USD') }} +
{{ _("per week") }}
+ +
+
+ $ + +
{{ _("per week") }}
+ + + +
+
+ + {% if not subscription.is_funded %} +
+ {{ _("Back your payment with a {0}credit card{1} to make sure it goes through!", + ""|safe % user.participant.username, + ""|safe) }} +
+ {% endif %} +
+{% endif %} diff --git a/templates/your-tip.html b/templates/your-tip.html deleted file mode 100644 index df29622b00..0000000000 --- a/templates/your-tip.html +++ /dev/null @@ -1,39 +0,0 @@ -{% if user.ANON %} -
- {% include "templates/sign-in-using-to-give.html" %} -
-{% else %} -
- {% set tippee = participant.username %} - {% set tip = user.participant.get_tip_to(tippee) %} -

{{ _('Your Tip') }}

-
-
- {{ format_currency(tip.amount, 'USD') }} -
{{ _("per week") }}
- -
-
- $ - -
{{ _("per week") }}
- - - -
-
- - {% if not tip.is_funded %} -
- {{ _("Back your payment with a {0}credit card{1} to make sure it goes through!", - ""|safe % user.participant.username, - ""|safe) }} -
- {% endif %} -
-{% endif %} diff --git a/www/%team/index.html.spt b/www/%team/index.html.spt index 6ca978b64f..2ef53576e4 100644 --- a/www/%team/index.html.spt +++ b/www/%team/index.html.spt @@ -29,6 +29,10 @@ title = name = team.name {% endblock %} +{% block sidebar %} +{% include "templates/your-payment.html" %} +{% endblock %} + {% block content %}
{% if team.is_approved in (None, False) %} @@ -58,8 +62,6 @@ title = name = team.name {% endblock %} {% block scripts %} -{% if user.participant == team.owner %} - -{% endif %} + {{ super() }} {% endblock %} diff --git a/www/~/%username/tip.json.spt b/www/~/%username/tip.json.spt deleted file mode 100644 index 1fc5005330..0000000000 --- a/www/~/%username/tip.json.spt +++ /dev/null @@ -1,72 +0,0 @@ -"""Get or change the authenticated user's tip to this person. -""" -from decimal import InvalidOperation - -from aspen import Response -from babel.numbers import NumberFormatError -from gratipay.exceptions import BadAmount -from gratipay.models.participant import Participant -from gratipay.utils import get_participant - -[-----------------------------------------------------------------------------] - -raise Response(404) # turn off while migrating to Teams (see #3399) - -if user.ANON: - raise Response(403, _("Please sign in first")) - -else: - out = {} - - # Get tipper and tippee. - # ====================== - - tipper = user.participant - tippee = get_participant(state, restrict=False, resolve_unclaimed=False) - - - # Get and maybe set amount. - # ========================= - - if request.method == 'POST' and 'amount' in request.body and tippee != tipper: - try: - out = tipper.set_tip_to(tippee, parse_decimal(request.body['amount'])) - except (InvalidOperation, ValueError, BadAmount, NumberFormatError): - raise Response(400, "bad amount") - else: - out = tipper.get_tip_to(tippee.username) - - amount = out['amount'] - total_giving = tipper.giving - total_receiving = tipper.receiving - - out["amount"] = str(amount) - out["amount_l"] = format_currency(amount, 'USD') - if tippee.username == 'Gratipay': - out["msg"] = _("Thank you so much for supporting Gratipay! :D") - else: - out["msg"] = _("Tip changed to {0} per week!", out["amount_l"]) - out["npatrons"] = tippee.npatrons - out["tippee_id"] = tippee.id - out["total_giving"] = str(total_giving) - out["total_giving_l"] = format_currency(total_giving, 'USD') - out["total_receiving"] = str(total_receiving) - out["total_receiving_l"] = format_currency(total_receiving, 'USD') - - if not tippee.anonymous_receiving: - total_receiving_tippee = tippee.receiving - out["total_receiving_tippee"] = str(total_receiving_tippee) - out["total_receiving_tippee_l"] = format_currency(total_receiving_tippee, 'USD') - else: - out["total_receiving_tippee"] = None - out["total_receiving_tippee_l"] = '[' + _("hidden") + ']' - - if 'ctime' in out: - out["ctime"] = str(out['ctime']) - out["mtime"] = str(out['mtime']) - else: - out["ctime"] = out["mtime"] = None - - -[---] application/json via json_dump -out