From a9f11f4f769237c26d7066d60d0d91b40803e83a Mon Sep 17 00:00:00 2001 From: Sander Roosendaal Date: Thu, 20 Dec 2018 15:29:21 +0100 Subject: [PATCH] confirmation emails to buyers --- rowers/braintreestuff.py | 80 +++++++++++- rowers/forms.py | 5 +- rowers/tasks.py | 117 +++++++++++++++++- .../cancel_subscription_fail_email.html | 21 ++++ .../templates/paymentconfirmationemail.html | 19 +++ rowers/templates/rower_form.html | 2 +- .../templates/subscription_create_email.html | 60 +++++++++ .../templates/subscription_update_email.html | 63 ++++++++++ rowers/templates/upgradeconfirm.html | 2 +- rowers/views.py | 4 + 10 files changed, 364 insertions(+), 9 deletions(-) create mode 100644 rowers/templates/cancel_subscription_fail_email.html create mode 100644 rowers/templates/paymentconfirmationemail.html create mode 100644 rowers/templates/subscription_create_email.html create mode 100644 rowers/templates/subscription_update_email.html diff --git a/rowers/braintreestuff.py b/rowers/braintreestuff.py index 2015891c..4a2bc46e 100644 --- a/rowers/braintreestuff.py +++ b/rowers/braintreestuff.py @@ -2,6 +2,20 @@ import braintree from django.utils import timezone import datetime +import django_rq +queue = django_rq.get_queue('default') +queuelow = django_rq.get_queue('low') +queuehigh = django_rq.get_queue('low') + +from rowers.utils import myqueue +from rowers.tasks import ( + handle_send_email_transaction, + handle_send_email_subscription_update, + handle_send_email_subscription_create, + handle_send_email_failed_cancel, + ) + + from rowsandall_app.settings import ( BRAINTREE_MERCHANT_ID,BRAINTREE_PUBLIC_KEY,BRAINTREE_PRIVATE_KEY ) @@ -68,7 +82,7 @@ def get_plans_costs(): def make_payment(rower,data): nonce_from_the_client = data['payment_method_nonce'] amount = data['amount'] - amount = str(amount) + amount = '{amount:.f2}'.format(amount=amount) result = gateway.transaction.sale({ "amount": amount, @@ -80,6 +94,14 @@ def make_payment(rower,data): if result.is_success: transaction = result.transaction amount = transaction.amount + name = '{f} {l}'.format( + f = rower.user.first_name, + l = rower.user.last_name, + ) + + + job = myqueue(queuehigh,handle_send_email_transaction, + name, rower.user.email, amount) return amount else: @@ -91,7 +113,6 @@ def update_subscription(rower,data): nonce_from_the_client = data['payment_method_nonce'] amount = data['amount'] amount = '{amount:.2f}'.format(amount=amount) - print amount,'aap' gatewaydata = { "price": amount, @@ -122,6 +143,29 @@ def update_subscription(rower,data): rower.rowerplan = plan.shortname rower.subscription_id = result.subscription.id rower.save() + name = '{f} {l}'.format( + f = rower.user.first_name, + l = rower.user.last_name, + ) + + + transactions = result.subscription.transactions + + if transactions: + amount = transactions[0].amount + else: + amount = 0 + + + job = myqueue(queuehigh, + handle_send_email_subscription_update, + name, rower.user.email, + plan.name, + plan.paymenttype == 'recurring', + plan.price, + amount, + result.subscription.billing_period_end_date.strftime('%Y-%m-%d')) + return True else: return False @@ -153,13 +197,31 @@ def create_subscription(rower,data): if result.is_success: rower.paidplan = plan - rower.planexpires = timezone.now()+datetime.timedelta(days=365) - rower.teamplanexpires = timezone.now()+datetime.timedelta(days=365) + rower.planexpires = result.subscription.billing_period_end_date + rower.teamplanexpires = result.subscription.billing_period_end_date rower.clubsize = plan.clubsize rower.paymenttype = plan.paymenttype rower.rowerplan = plan.shortname rower.subscription_id = result.subscription.id rower.save() + name = '{f} {l}'.format( + f = rower.user.first_name, + l = rower.user.last_name, + ) + + + recurring = plan.paymenttype == 'recurring', + + job = myqueue( + queuehigh, + handle_send_email_subscription_create, + name, rower.user.email, + plan.name, + recurring, + plan.price, + plan.price, + result.subscription.billing_period_end_date.strftime('%Y-%m-%d') + ) return True else: return False @@ -174,7 +236,15 @@ def cancel_subscription(rower,id): result = gateway.subscription.cancel(id) messages.append("Subscription canceled") except: - errormessages.append("We could not find the subscription record in our customer database") + errormessages.append("We could not find the subscription record in our customer database. We have notified the site owner, who will contact you.") + + name = '{f} {l}'.format(f = rower.user.first_name, l = rower.user.last_name) + + + job = myqueue(queuehigh, + handle_send_email_failed_cancel, + name, rower.user.email,rower.user.username,id) + return False, themessages, errormessages rower.paidplan = None diff --git a/rowers/forms.py b/rowers/forms.py index 84b0536d..31d292de 100644 --- a/rowers/forms.py +++ b/rowers/forms.py @@ -732,7 +732,10 @@ class PlanSelectForm(forms.Form): "price","clubsize","shortname" ) if rower: - amount = rower.paidplan.price + try: + amount = rower.paidplan.price + except AttributeError: + amount = 0 self.fields['plan'].queryset = PaidPlan.objects.filter( paymentprocessor=rower.paymentprocessor ).exclude( diff --git a/rowers/tasks.py b/rowers/tasks.py index 8601baa0..1d51ac0f 100644 --- a/rowers/tasks.py +++ b/rowers/tasks.py @@ -739,7 +739,122 @@ def handle_updatedps(useremail, workoutids, debug=False,**kwargs): return 1 -# send email when a breakthrough workout is uploaded +@app.task +def handle_send_email_transaction( + username, useremail, amount, **kwargs): + + if 'debug' in kwargs: + debug = kwargs['debug'] + else: + debug = True + + subject = "Rowsandall Payment Confirmation" + + from_email = 'Rowsandall ' + + d = { + 'name': username, + 'siteurl': siteurl, + 'amount': amount + } + + res = send_template_email(from_email,[useremail], + subject, + 'paymentconfirmationemail.html', + d, **kwargs) + + return 1 + +@app.task +def handle_send_email_failed_cancel( + name, email, username, id, **kwargs): + + if 'debug' in kwargs: + debug = kwargs['debug'] + else: + debug = True + + subject = "Rowsandall Subscription Cancellation Error" + + from_email = 'Rowsandall ' + + d = { + 'name': name, + 'siteurl': siteurl, + 'email': email, + 'username': username, + 'id': id, + } + + res = send_template_email(from_email,["support@rowsandall.com"], + subject, + 'cancel_subscription_fail_email.html', + d, **kwargs) + + return 1 + + +@app.task +def handle_send_email_subscription_update( + username, useremail, planname, recurring, price, amount, + end_of_billing_period, **kwargs): + + if 'debug' in kwargs: + debug = kwargs['debug'] + else: + debug = True + + subject = "Rowsandall Payment Confirmation" + + from_email = 'Rowsandall ' + + d = { + 'name': username, + 'siteurl': siteurl, + 'amount': amount, + 'price':price, + 'planname': planname, + 'recurring': recurring, + 'end_of_billing_period': end_of_billing_period, + } + + res = send_template_email(from_email,[useremail], + subject, + 'subscription_update_email.html', + d, **kwargs) + + return 1 + +@app.task +def handle_send_email_subscription_create( + username, useremail, planname, recurring, price, amount, + end_of_billing_period, **kwargs): + + if 'debug' in kwargs: + debug = kwargs['debug'] + else: + debug = True + + subject = "Rowsandall Payment Confirmation" + + from_email = 'Rowsandall ' + + d = { + 'name': username, + 'siteurl': siteurl, + 'amount': amount, + 'price':price, + 'planname': planname, + 'end_of_billing_period': end_of_billing_period, + 'recurring': recurring, + } + + res = send_template_email(from_email,[useremail], + subject, + 'subscription_create_email.html', + d, **kwargs) + + return 1 @app.task def handle_sendemail_raceregistration( diff --git a/rowers/templates/cancel_subscription_fail_email.html b/rowers/templates/cancel_subscription_fail_email.html new file mode 100644 index 00000000..cd4cac32 --- /dev/null +++ b/rowers/templates/cancel_subscription_fail_email.html @@ -0,0 +1,21 @@ +{% extends "emailbase.html" %} + +{% block body %} +

+ User {{ name }} tried to cancel his subscription with id "{{ id }}" on {{ siteurl }} but failed. +

+ +

+ User name: {{ username }} +

+ +

+ User email: {{ email }} +

+ + +

+ Best Regards, the Rowsandall Team +

+{% endblock %} + diff --git a/rowers/templates/paymentconfirmationemail.html b/rowers/templates/paymentconfirmationemail.html new file mode 100644 index 00000000..0bc62f45 --- /dev/null +++ b/rowers/templates/paymentconfirmationemail.html @@ -0,0 +1,19 @@ +{% extends "emailbase.html" %} + +{% block body %} +

Dear {{ name }},

+ +

+ Thank you. We have received the payment of € {{ amount }} for Rowsandall related services. +

+ +

+ Please contact our customer service by replying to this email if you have any further + questions regarding the payment. +

+ +

+ Best Regards, the Rowsandall Team +

+{% endblock %} + diff --git a/rowers/templates/rower_form.html b/rowers/templates/rower_form.html index ce5aea0c..b58f18fb 100644 --- a/rowers/templates/rower_form.html +++ b/rowers/templates/rower_form.html @@ -52,7 +52,7 @@ {% endif %} {% csrf_token %} - {% if rower.rowerplan != 'coach' and rower.user == user %} + {% if rower.clubsize < 100 and rower.user == user %}

Upgrade

diff --git a/rowers/templates/subscription_create_email.html b/rowers/templates/subscription_create_email.html new file mode 100644 index 00000000..c312a6b1 --- /dev/null +++ b/rowers/templates/subscription_create_email.html @@ -0,0 +1,60 @@ +{% extends "emailbase.html" %} + +{% block body %} +

Dear {{ name }},

+ +

+ Thank you. We have received the payment of € {{ amount }} for your new + subscription to the Rowsandall paid plan "{{ planname }}". +

+ +{% if recurring %} +

+ Your next charge is due on {{ end_of_billing_period }}. We will charge your {{ paymentmethod }} + on that date. +

+ +

+ The subscription will keep running until you change or stop it. At any point in time you + can change the automatically renewing subscription to a "one year only" subscription through + the upgrade page. On this page, you can also + upgrade your subscription. +

+ +{% else %} +

+ This one year subscription will automatically end on {{ end_of_billing_period }}. You can + renew your subscription after that. +

+ +

+ At any point in time, you can change your subscription to an automatically renewing subscription. + You can do this on the upgrade page. + Here, you can also upgrade your subscription. +

+ +{% endif %} + +

+ Upgrades in the middle of a billing cycle will be charged pro-rated. For the current billing + cycle, you will only be charged for the price difference for the remaining fraction of the + billing cycle. If you downgrade to a lower cost subscription, the pro-rated difference will be + used as a credit, lowering the amount charged on the next billing cycle. +

+ +

+ You can stop the subscription through + the subscription management page. The + subscription will be stopped immediately without a refund. +

+ +

+ Please contact our customer service by replying to this email if you have any further + questions regarding your subscription. +

+ +

+ Best Regards, the Rowsandall Team +

+{% endblock %} + diff --git a/rowers/templates/subscription_update_email.html b/rowers/templates/subscription_update_email.html new file mode 100644 index 00000000..339c00fc --- /dev/null +++ b/rowers/templates/subscription_update_email.html @@ -0,0 +1,63 @@ +{% extends "emailbase.html" %} + +{% block body %} +

Dear {{ name }},

+ +

+ Thank you. We have received the payment of € {{ amount }} for + your updated Rowsandall subscription. + You are now on the Rowsandall paid plan "{{ planname }}". +

+ +{% if recurring %} +

+ The subscription cost is €{{ price }} per year. + Your next charge is due on {{ end_of_billing_period }}. We will charge your {{ paymentmethod }} + on that date. +

+ +

+ The subscription will keep running until you change or stop it. At any point in time you + can change the automatically renewing subscription to a "one year only" subscription through + the upgrade page. On this page, you can also + upgrade your subscription. +

+ +{% else %} +

+ The price of the subscription is €{{ price }}. You have paid €{{ amount }} as a + prorated cost of your upgrade. + This one year subscription will automatically end on {{ end_of_billing_period }}. You can + renew your subscription after that. +

+ +

+ At any point in time, you can change your subscription to an automatically renewing subscription. + You can do this on the upgrade page. + Here, you can also upgrade your subscription. +

+{% endif %} + +

+ Upgrades in the middle of a billing cycle are charged pro-rated. For the current billing + cycle, you have only been charged for the price difference for the remaining fraction of the + billing cycle. If you downgraded to a lower cost subscription, the pro-rated difference will be + used as a credit, lowering the amount charged on the next billing cycle. +

+ +

+ You can stop the subscription through + the subscription management page. The + subscription will be stopped immediately without a refund. +

+ +

+ Please contact our customer service by replying to this email if you have any further + questions regarding your subscription. +

+ +

+ Best Regards, the Rowsandall Team +

+{% endblock %} + diff --git a/rowers/templates/upgradeconfirm.html b/rowers/templates/upgradeconfirm.html index d064c78b..d4cc67ac 100644 --- a/rowers/templates/upgradeconfirm.html +++ b/rowers/templates/upgradeconfirm.html @@ -19,7 +19,7 @@ Payment Type{{ plan.paymenttype }} - Plan Duration1 year starting today + Billing Cycle1 year Total€ {{ plan.price|currency }} diff --git a/rowers/views.py b/rowers/views.py index d384d6ba..a4ee2992 100644 --- a/rowers/views.py +++ b/rowers/views.py @@ -1113,6 +1113,10 @@ def billing_view(request): def upgrade_view(request): r = getrequestrower(request) + if r.subscription_id is None or r.subscription_id == '': + url = reverse(billing_view) + return HttpResponseRedirect(url) + if request.method == 'POST': billingaddressform = RowerBillingAddressForm(request.POST) planselectform = PlanSelectForm(request.POST,paymentprocessor='braintree')