From 937620e86ca2f5e4d1ca632d61a1bd4b5315d796 Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Thu, 24 Aug 2023 16:13:24 +1000 Subject: [PATCH 1/9] main: Initial stripe test --- master/main.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/master/main.py b/master/main.py index edbca2b..8072c5b 100644 --- a/master/main.py +++ b/master/main.py @@ -232,6 +232,26 @@ def tlsa(): return resp.json() + +@app.route('stripe', methods=['POST']) +def stripe(): + # Get API header + api_key = request.headers.get('key') + if api_key == None: + return jsonify({'error': 'Invalid API key', 'success': 'false'}) + if api_key != os.getenv('STRIPE_KEY'): + return jsonify({'error': 'Invalid API key', 'success': 'false'}) + + # Log all requests + print(request.json) + # Log for docker + print(request.json, flush=True) + + return jsonify({'success': 'true'}) + + + + def get_sites_count(): # If file doesn't exist, create it try: From 5f56c1d0c2d499e475213a2715535e0f0d5af395 Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Thu, 24 Aug 2023 16:17:27 +1000 Subject: [PATCH 2/9] main: Initial stripe test fix path --- master/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/master/main.py b/master/main.py index 8072c5b..9451555 100644 --- a/master/main.py +++ b/master/main.py @@ -233,7 +233,7 @@ def tlsa(): return resp.json() -@app.route('stripe', methods=['POST']) +@app.route('/stripe', methods=['POST']) def stripe(): # Get API header api_key = request.headers.get('key') From d4fd4705644c4afc37be57e5dbe1f0637d0ab24d Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Thu, 24 Aug 2023 16:25:55 +1000 Subject: [PATCH 3/9] main: Stripe log all requests --- master/main.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/master/main.py b/master/main.py index 9451555..4ae4b03 100644 --- a/master/main.py +++ b/master/main.py @@ -235,6 +235,10 @@ def tlsa(): @app.route('/stripe', methods=['POST']) def stripe(): + # Log all requests + print(request.json) + # Log for docker + print(request.json, flush=True) # Get API header api_key = request.headers.get('key') if api_key == None: @@ -242,10 +246,7 @@ def stripe(): if api_key != os.getenv('STRIPE_KEY'): return jsonify({'error': 'Invalid API key', 'success': 'false'}) - # Log all requests - print(request.json) - # Log for docker - print(request.json, flush=True) + return jsonify({'success': 'true'}) From 230dc2a12a9758844ab636e7bca17b0ecae9ad97 Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Thu, 24 Aug 2023 16:41:38 +1000 Subject: [PATCH 4/9] main: Added test stripe verification --- master/main.py | 38 ++++++++++++++++++++++++++------------ master/requirements.txt | 3 ++- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/master/main.py b/master/main.py index 4ae4b03..355063e 100644 --- a/master/main.py +++ b/master/main.py @@ -234,20 +234,34 @@ def tlsa(): @app.route('/stripe', methods=['POST']) -def stripe(): - # Log all requests - print(request.json) - # Log for docker +def stripeapi(): print(request.json, flush=True) - # Get API header - api_key = request.headers.get('key') - if api_key == None: - return jsonify({'error': 'Invalid API key', 'success': 'false'}) - if api_key != os.getenv('STRIPE_KEY'): - return jsonify({'error': 'Invalid API key', 'success': 'false'}) - - + print(request.headers, flush=True) + payload = request.data + stripe.api_key = os.getenv('STRIPE_SECRET') + endpoint_secret = os.getenv('STRIPE_ENDPOINT_SECRET') + sig_header = request.headers.get('HTTP_STRIPE_SIGNATURE') + events = None + try: + event = stripe.Webhook.construct_event( + payload, sig_header, endpoint_secret + ) + except ValueError as e: + # Invalid payload + return jsonify({'success': 'false'}) + except stripe.error.SignatureVerificationError as e: + return jsonify({'success': 'false'}) + # Handle the event + if event.type == 'payment_intent.succeeded': + payment_intent = event.data.object # contains a stripe.PaymentIntent + print('PaymentIntent was successful!', flush=True) + elif event.type == 'payment_method.attached': + payment_method = event.data.object # contains a stripe.PaymentMethod + print('PaymentMethod was attached to a Customer!', flush=True) + # ... handle other event types + else: + print('Unhandled event type {}'.format(event.type)) return jsonify({'success': 'true'}) diff --git a/master/requirements.txt b/master/requirements.txt index 01ad091..9a8f17f 100644 --- a/master/requirements.txt +++ b/master/requirements.txt @@ -1,4 +1,5 @@ python-dotenv requests flask -jsonify \ No newline at end of file +jsonify +stripe \ No newline at end of file From 969e5e17a54cac2d9284288d84d8cbb3530c8917 Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Thu, 24 Aug 2023 16:44:17 +1000 Subject: [PATCH 5/9] main: Import stripe python module --- master/main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/master/main.py b/master/main.py index 355063e..8a5cad0 100644 --- a/master/main.py +++ b/master/main.py @@ -2,6 +2,7 @@ from flask import Flask, request, jsonify import dotenv import os import requests +import stripe dotenv.load_dotenv() @@ -240,7 +241,7 @@ def stripeapi(): payload = request.data stripe.api_key = os.getenv('STRIPE_SECRET') endpoint_secret = os.getenv('STRIPE_ENDPOINT_SECRET') - sig_header = request.headers.get('HTTP_STRIPE_SIGNATURE') + sig_header = request.headers.get('Stripe-Signature') events = None try: event = stripe.Webhook.construct_event( From eb137b85d32465b15f43fe442bc4ade91c2f5323 Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Thu, 24 Aug 2023 16:53:39 +1000 Subject: [PATCH 6/9] main: Added email test for stripe --- README.md | 13 +++++++++++++ master/main.py | 35 +++++++++++++++++++++++++++-------- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index e00aad3..a3364ac 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,19 @@ Then to start the master api server screen -dmS hnshosting-master python3 main.py ``` +## Stripe webhook +Create a new webhook endpoint on stripe and set the url to https://master-server-domain:5000/stripe-webhook (note: you need to add a https proxy to your master server if you want to use stripe) +Add these environment variables to your master server +```yaml +STRIPE_SECRET: your-stripe-secret-key +STRIPE_ENDPOINT_SECRET: your-stripe-endpoint-secret +SMTP_HOST: smtp-server +SMTP_PORT: smtp-port +SMTP_USER: smtp-user +SMTP_PASS: smtp-pass +SMTP_FROM: smtp-from +``` + ## Worker server install diff --git a/master/main.py b/master/main.py index 8a5cad0..f314b0b 100644 --- a/master/main.py +++ b/master/main.py @@ -2,7 +2,8 @@ from flask import Flask, request, jsonify import dotenv import os import requests -import stripe +import stripe # For stripe payments +import smtplib, ssl # For sending emails dotenv.load_dotenv() @@ -236,8 +237,6 @@ def tlsa(): @app.route('/stripe', methods=['POST']) def stripeapi(): - print(request.json, flush=True) - print(request.headers, flush=True) payload = request.data stripe.api_key = os.getenv('STRIPE_SECRET') endpoint_secret = os.getenv('STRIPE_ENDPOINT_SECRET') @@ -255,12 +254,32 @@ def stripeapi(): # Handle the event if event.type == 'payment_intent.succeeded': - payment_intent = event.data.object # contains a stripe.PaymentIntent + payment_intent = event.data.object + # Get email + email = payment_intent['receipt_email'] + # Create licence key + licence_key = os.urandom(16).hex() + # Add licence key to file + key_file = open('/data/licence_key.txt', 'a') + key_file.write(licence_key + '\n') + key_file.close() + # Send email + host = os.getenv('SMTP_HOST') + port = os.getenv('SMTP_PORT') + user = os.getenv('SMTP_USER') + password = os.getenv('SMTP_PASS') + from_email = os.getenv('SMTP_FROM') + if from_email == None: + from_email = user + + context = ssl.create_default_context() + with smtplib.SMTP_SSL(host, port, context=context) as server: + server.login(user, password) + server.sendmail(from_email, email, "Your licence key is: " + licence_key) + + + print('PaymentIntent was successful!', flush=True) - elif event.type == 'payment_method.attached': - payment_method = event.data.object # contains a stripe.PaymentMethod - print('PaymentMethod was attached to a Customer!', flush=True) - # ... handle other event types else: print('Unhandled event type {}'.format(event.type)) return jsonify({'success': 'true'}) From 93333eed26cffcf2d7f52d8e0cc1b9091a9f1843 Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Thu, 24 Aug 2023 16:57:05 +1000 Subject: [PATCH 7/9] main: Cleaned up email --- master/main.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/master/main.py b/master/main.py index f314b0b..833ec47 100644 --- a/master/main.py +++ b/master/main.py @@ -275,7 +275,12 @@ def stripeapi(): context = ssl.create_default_context() with smtplib.SMTP_SSL(host, port, context=context) as server: server.login(user, password) - server.sendmail(from_email, email, "Your licence key is: " + licence_key) + message = """\ + Subject: Your Licence key + + Your licence key is: """ + licence_key + + server.sendmail(from_email, email, message) From cb849004b7923e64579d90461a59b8964f2dc359 Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Thu, 24 Aug 2023 17:05:01 +1000 Subject: [PATCH 8/9] main: Added info to email --- master/main.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/master/main.py b/master/main.py index 833ec47..b3feea6 100644 --- a/master/main.py +++ b/master/main.py @@ -275,10 +275,20 @@ def stripeapi(): context = ssl.create_default_context() with smtplib.SMTP_SSL(host, port, context=context) as server: server.login(user, password) - message = """\ + message = """From: Hosting <""" + from_email + """> + To: """ + email + """ Subject: Your Licence key - Your licence key is: """ + licence_key + Hello, + This email contains your licence key for your new wordpress site. + You can redeem this key via the discord bot or api. + + Your licence key is: """ + licence_key + """ + + Thanks, + HNSHosting + + """ server.sendmail(from_email, email, message) From 2209e03158461c6ff2cc27da4e59cc1eea9c13f3 Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Thu, 24 Aug 2023 17:11:42 +1000 Subject: [PATCH 9/9] main: Email test 3 --- master/main.py | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/master/main.py b/master/main.py index b3feea6..9384027 100644 --- a/master/main.py +++ b/master/main.py @@ -275,20 +275,11 @@ def stripeapi(): context = ssl.create_default_context() with smtplib.SMTP_SSL(host, port, context=context) as server: server.login(user, password) - message = """From: Hosting <""" + from_email + """> - To: """ + email + """ - Subject: Your Licence key - - Hello, - This email contains your licence key for your new wordpress site. - You can redeem this key via the discord bot or api. - - Your licence key is: """ + licence_key + """ - - Thanks, - HNSHosting - - """ + message = "From: Hosting <" + from_email + ">\nTo: " + email + \ + "\nSubject: Your Licence key\n\nHello,\n\n"\ + +"This email contains your licence key for your new wordpress site.\n" \ + +"You can redeem this key via the discord bot or api.\n\n"\ + +"Your licence key is: " + licence_key +"\nThanks,\nHNSHosting" server.sendmail(from_email, email, message)