From 8348458870b79668eb195565bd8339ec77a65148 Mon Sep 17 00:00:00 2001 From: Manager Bot Date: Wed, 30 Nov 2022 09:17:14 +0000 Subject: [PATCH] Now with subscriptions. --- Web/lib/MJB/Web.pm | 11 +-- Web/lib/MJB/Web/Controller/UserSettings.pm | 10 +-- Web/script/stripe-backend | 71 +++++++++++++++++++ .../06_user_settings/05_subscription.t | 17 +++++ .../06_user_settings/06_do_subscription.t | 16 +++++ .../07_do_subscription_manage.t | 16 +++++ Web/templates/layouts/standard.html.ep | 8 +++ .../user_settings/subscription.html.ep | 27 +++++++ 8 files changed, 165 insertions(+), 11 deletions(-) create mode 100644 Web/script/stripe-backend create mode 100644 Web/t/01_endpoints/06_user_settings/05_subscription.t create mode 100644 Web/t/01_endpoints/06_user_settings/06_do_subscription.t create mode 100644 Web/t/01_endpoints/06_user_settings/07_do_subscription_manage.t create mode 100644 Web/templates/user_settings/subscription.html.ep diff --git a/Web/lib/MJB/Web.pm b/Web/lib/MJB/Web.pm index 2740a27..66fcb9d 100644 --- a/Web/lib/MJB/Web.pm +++ b/Web/lib/MJB/Web.pm @@ -187,10 +187,13 @@ sub startup ($self) { $r->post( '/reset/:token' )->to('Auth#do_reset' )->name('do_reset' ); # User setting changes when logged in - $auth->get ( '/profile' )->to('UserSettings#profile' )->name('show_profile' ); - $auth->post( '/profile' )->to('UserSettings#do_profile' )->name('do_profile' ); - $auth->get ( '/password' )->to('UserSettings#change_password' )->name('show_change_password' ); - $auth->post( '/password' )->to('UserSettings#do_change_password' )->name('do_change_password' ); + $auth->get ( '/profile' )->to('UserSettings#profile' )->name('show_profile' ); + $auth->post( '/profile' )->to('UserSettings#do_profile' )->name('do_profile' ); + $auth->get ( '/password' )->to('UserSettings#change_password' )->name('show_change_password' ); + $auth->post( '/password' )->to('UserSettings#do_change_password' )->name('do_change_password' ); + $auth->get ( '/subscription' )->to('UserSettings#subscription' )->name('show_subscription' ); + $auth->post( '/subscription' )->to('UserSettings#do_subscription' )->name('do_subscription' ); + $auth->post( '/manage' )->to('UserSettings#do_subscription_manage' )->name('do_subscription_manage' ); # Dashboard / Blog Management $auth->get ( '/dashboard' )->to('Dashboard#index' )->name('show_dashboard' ); diff --git a/Web/lib/MJB/Web/Controller/UserSettings.pm b/Web/lib/MJB/Web/Controller/UserSettings.pm index 37bb1f4..471e655 100644 --- a/Web/lib/MJB/Web/Controller/UserSettings.pm +++ b/Web/lib/MJB/Web/Controller/UserSettings.pm @@ -111,10 +111,6 @@ sub do_change_password ( $c ) { $c->redirect_to( $c->url_for( 'show_change_password' ) ); } -1; - -__END__ - sub subscription ($c) { my $status = $c->param('status'); @@ -131,7 +127,7 @@ sub subscription ($c) { my $session_id = $c->param('session_id'); - my $customer_id = $c->ua->get( $c->config->{stripe_backend} . '/stripe/session-to-customer?session_id=' . $session_id )->result->json->{customer_id}; + my $customer_id = $c->ua->get( $c->config->{stripe}->{backend} . '/stripe/session-to-customer?session_id=' . $session_id )->result->json->{customer_id}; # Store the customer id along side the user in the DB. if ( $customer_id ) { @@ -151,14 +147,14 @@ sub subscription ($c) { # Send to stripe to signup for the subscription sub do_subscription ($c) { my $lookup_key = $c->param('lookup_key'); - my $url = $c->ua->get( $c->config->{stripe_backend} . '/stripe/get-checkout-link?lookup_key=' . $lookup_key )->result->json->{url}; + my $url = $c->ua->get( $c->config->{stripe}->{backend} . '/stripe/get-checkout-link?lookup_key=' . $lookup_key )->result->json->{url}; $c->redirect_to( $url ); } # Send to stripe to manage the subscription sub do_subscription_manage ($c) { - my $url = $c->ua->get( $c->config->{stripe_backend} . '/stripe/get-portal-link?customer_id=' . $c->stash->{person}->stripe_customer_id )->result->json->{url}; + my $url = $c->ua->get( $c->config->{stripe}->{backend} . '/stripe/get-portal-link?customer_id=' . $c->stash->{person}->stripe_customer_id )->result->json->{url}; $c->redirect_to( $url ); } diff --git a/Web/script/stripe-backend b/Web/script/stripe-backend new file mode 100644 index 0000000..fca68f3 --- /dev/null +++ b/Web/script/stripe-backend @@ -0,0 +1,71 @@ +#! /usr/bin/env python3.6 + +""" +server.py +Stripe Sample. +Python 3.6 or newer required. +""" +import os +from flask import Flask, redirect, jsonify, json, request, current_app +import yaml + +import stripe + +with open('/etc/mjb.yml') as file: + config = yaml.safe_load(file) + +# This is your test secret API key. +stripe.api_key = config.['stripe']['api_key'] + +app = Flask(__name__, + static_url_path='', + static_folder='public') + +YOUR_DOMAIN = config['stripe']['return_domain'] + +@app.route('/stripe/get-checkout-link', methods=['GET']) +def create_checkout_session(): + try: + + checkout_session = stripe.checkout.Session.create( + line_items=[ + { + 'price': request.args.get('lookup_key'), + 'quantity': 1, + }, + ], + mode='subscription', + success_url=YOUR_DOMAIN + '/subscription?status=success&session_id={CHECKOUT_SESSION_ID}', + cancel_url=YOUR_DOMAIN + '/subscription?status=error', + ) + + return jsonify({'status': 'success', 'url' : checkout_session.url }) + + except Exception as e: + return jsonify({'status': 'failure', 'error': e }) + +@app.route('/stripe/get-portal-link', methods=['GET']) +def customer_portal(): + customer_id = request.args.get('customer_id') + + # This is the URL to which the customer will be redirected after they are + # done managing their billing with the portal. + return_url = YOUR_DOMAIN + '/dashboard' + + portalSession = stripe.billing_portal.Session.create( + customer=customer_id, + return_url=return_url, + ) + + return jsonify({'status': 'success', 'url' : portalSession.url }) + +@app.route('/stripe/session-to-customer', methods=['GET']) +def session_to_customer(): + checkout_session_id = request.args.get('session_id') + checkout_session = stripe.checkout.Session.retrieve(checkout_session_id) + + return jsonify({ 'customer_id' : checkout_session.customer }) + + +if __name__ == '__main__': + app.run() diff --git a/Web/t/01_endpoints/06_user_settings/05_subscription.t b/Web/t/01_endpoints/06_user_settings/05_subscription.t new file mode 100644 index 0000000..1f8acb9 --- /dev/null +++ b/Web/t/01_endpoints/06_user_settings/05_subscription.t @@ -0,0 +1,17 @@ +#!/usr/bin/env perl +use MJB::Web::Test; + +#== +# This test ensures that the subscription page works. +# +# 1. Create user and login. +# 2. Go to the subscription page +#== + +my $t = Test::Mojo::MJB->new('MJB::Web'); + +my $blog_id = $t->create_user + ->get_ok( '/subscription' ) + ->status_is( 200 ); + +done_testing; diff --git a/Web/t/01_endpoints/06_user_settings/06_do_subscription.t b/Web/t/01_endpoints/06_user_settings/06_do_subscription.t new file mode 100644 index 0000000..4daa223 --- /dev/null +++ b/Web/t/01_endpoints/06_user_settings/06_do_subscription.t @@ -0,0 +1,16 @@ +#!/usr/bin/env perl +use MJB::Web::Test; + +#== +# Ensure the handler exists for do_subscription. +# +# Don't actually run it, since it depends on the stripe-backend, and +# that is outside of the scope of these tests. +#== + +my $t = Test::Mojo::MJB->new('MJB::Web'); + +ok $t->app->routes->find( 'do_subscription' ), 'Have a route for do_subscription'; +is $t->app->routes->find( 'do_subscription' )->methods->[0], 'POST', 'Is a post handler.'; + +done_testing(); diff --git a/Web/t/01_endpoints/06_user_settings/07_do_subscription_manage.t b/Web/t/01_endpoints/06_user_settings/07_do_subscription_manage.t new file mode 100644 index 0000000..e0d90e9 --- /dev/null +++ b/Web/t/01_endpoints/06_user_settings/07_do_subscription_manage.t @@ -0,0 +1,16 @@ +#!/usr/bin/env perl +use MJB::Web::Test; + +#== +# Ensure the handler exists for do_subscription_manage. +# +# Don't actually run it, since it depends on the stripe-backend, and +# that is outside of the scope of these tests. +#== + +my $t = Test::Mojo::MJB->new('MJB::Web'); + +ok $t->app->routes->find( 'do_subscription_manage' ), 'Have a route for do_subscription_manage'; +is $t->app->routes->find( 'do_subscription_manage' )->methods->[0], 'POST', 'Is a post handler.'; + +done_testing(); diff --git a/Web/templates/layouts/standard.html.ep b/Web/templates/layouts/standard.html.ep index d8774da..73c082f 100644 --- a/Web/templates/layouts/standard.html.ep +++ b/Web/templates/layouts/standard.html.ep @@ -148,6 +148,14 @@ Change Password + % if ( $c->config->{stripe}->{enable} ) { + + % } % } else {