#πŸ”’ Trying to integrate Stripe payment with my database

35 messages Β· Page 1 of 1 (latest)

lapis ruin
#

Im confused on the path i need to take. I got a simple website set up with a stripe checkout. Then i have my working database. What needs to connect the two and handle the webhooks and such? All i need to do is transfer data that is given at checkout to my db. Like 5 columns. Just not sure how to get started.

dull galleonBOT
#

@lapis ruin

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

tight pumice
#

What's your code look like so far?

lapis ruin
#

I have a discord bot, a database, and a website with a checkout through Stripe

#

the discord bot and database are good. It checks to see if the current server ID is inside of the column 'premium_server_id' after every command. tested and it works great

#

i just need stripe to get the server_id from the user at checkout and insert that into my database so its fully automated

#

i think what i need is a "webhook endpoint" in my server? Both the database and discord bot are hosted on the same server. So they interact through localhost

tight pumice
#

Your website is flask Django FastAPI?

lapis ruin
#

huh?

#

i dont think so

tight pumice
#

Plain HTML?

lapis ruin
#

ohhh

#

i just used wordpress that redirects you to my stripe checkout URL if you press the button

#

figured that was probably safer than any low-end version of wordpress

tight pumice
#

So you'll need to make your own webhook handler with Django/FastAPI/flask/quart etc

lapis ruin
#

how will i host it?

#

is it just a python script i leave running?

tight pumice
#

How do you run your bot?

lapis ruin
#

a python script i leave running

tight pumice
#

Probably you want systemd or supervisord to run both

lapis ruin
#

i used to have a .service file that started everything

tight pumice
#

If you're running quart/FastAPI you can integrate your bot into the same process

lapis ruin
#

i dont think i am

tight pumice
#

You'll need something to handle web requests

#

If you use an asgi server you can run your bot in your lifespan task

lapis ruin
#
# app.py
#
# Use this sample code to handle webhook events in your integration.
#
# 1) Paste this code into a new file (app.py)
#
# 2) Install dependencies
#   pip3 install flask
#   pip3 install stripe
#
# 3) Run the server on http://localhost:4242
#   python3 -m flask run --port=4242

import json
import os
import stripe

from flask import Flask, jsonify, request

# The library needs to be configured with your account's secret key.
# Ensure the key is kept out of any version control system you might be using.
stripe.api_key = "sk_test_..."

# This is your Stripe CLI webhook secret for testing your endpoint locally.
endpoint_secret = '******'

app = Flask(__name__)

@app.route('/webhook', methods=['POST'])
def webhook():
    event = None
    payload = request.data
    sig_header = request.headers['STRIPE_SIGNATURE']

    try:
        event = stripe.Webhook.construct_event(
            payload, sig_header, endpoint_secret
        )
    except ValueError as e:
        # Invalid payload
        raise e
    except stripe.error.SignatureVerificationError as e:
        # Invalid signature
        raise e

    # Handle the event
    if event['type'] == 'checkout.session.completed':
      session = event['data']['object']
    # ... handle other event types
    else:
      print('Unhandled event type {}'.format(event['type']))

    return jsonify(success=True)
``` I got this off the stripe website
#

so i think it wants me to use flask

tight pumice
#

You can just use any server tech

#

There's just docs for flask

lapis ruin
#

is that the only thing i need to run then?

dull galleonBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.