#mxd

1 messages · Page 1 of 1 (latest)

drifting pivotBOT
woven bloom
#

Hello there

blazing nacelle
#

Hi, i am getting a 302 error from webhook, and the application error message is AH00126: Invalid URI in request HEAD

woven bloom
#

Sounds like your Server is attempting to redirect the Webhook request to a different URL

#

We don't support following redirects for Webhook endpoints.

blazing nacelle
#

yeah it should redirect to my website

#

theres no code in there to redirect it anywhere else

#

the redirect was set up in the interactive webhook creator

woven bloom
#

Does the endpoint you have set up in your Dashboard match the exact endpoint on your server?

#

Does your server perform any sort of redirect by default when receiving requests?

blazing nacelle
#

so after I enter the dummy details: the 424242424.. thing It goes to /membership route

#

and i get redirected to that route successfully, but the webhooks look like this

woven bloom
#

Can you share your webhook code?

blazing nacelle
#

screenshot works?

woven bloom
#

Text is better

blazing nacelle
#

im over the limit

woven bloom
#

You can use the three backticks to format like: this

#

Break it up into a couple posts if needed

blazing nacelle
#
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

    #user_id = current_user.id
    #user_id = users.id
    user_id = User.query.filter_by(current_user.id)
    activemembership = ''
    productsubscription = ''

    # Handle the event
    # session
    if event['type'] == 'checkout.session.completed':
      # Payment is successful and the subscription is created.
      # You should provision the subscription and save the customer ID to your database.
      session = event['data']['object']
      # subscription: sub_1M735iLXb2BpINMFQ6C11vRF --> object>data>object>subscription
      # db.session.add(membership)
      # db.session.commit()
      activemembership = 'YES'

    # subscription
    if event['type'] == 'customer.subscription.created':
      subscription = event['data']['object']
      #activemembership = request['data']['object']
      activemembership = 'YES'
      # bronze: price_1M2H21LOEVMWAWDDt5NZ1Qki // object>data>object>items>data>0>plan>id
      productsubscription = request.get_json['data']['object']['items']['data']['plan']['id']

    if event['type'] == 'customer.subscription.deleted':
      subscription = event['data']['object']
      activemembership = 'NO'

    if event['type'] == 'customer.subscription.updated':
      subscription = event['data']['object']
      # object>data>object>canceled_at(webhook_response_customer-subscription-updated-canceled.txt)
      activemembership = 'YES'#change this to check if the user removed the subscription
#
    if event['type'] == 'invoice.paid':
      # Continue to provision the subscription as payments continue to be made.
      # Store the status in your database and check when a user accesses your service.
      # This approach helps you avoid hitting rate limits
      invoice = event['data']['object']
      # object>data>object>lines>data>0>price>id
      activemembership = 'YES'

    if event['type'] == 'invoice.payment_failed':
      # The payment failed or the customer does not have a valid payment method.
      # The subscription becomes past_due. Notify your customer and send them to the
      # customer portal to update their payment information.
      invoice = event['data']['object']
      # ... handle other event types
      activemembership = 'NO'

    else:
      print('Unhandled event type {}'.format(event['type']))

    # get current user thats logged in 
    # flask-login module of Flask, import function current_user with from flask_login import current_user
    #user_id = current_user.id
    #user_email = current_user.email
    #user_name = current_user.name

    membership = Membership(user_id=user_id, activemembership=activemembership, productsubscription=productsubscription)
    db.session.add(membership)
    db.session.commit()

    # get customer_id
    # subscription: sub_1M735iLXb2BpINMFQ6C11vRF --> object>data>object>subscription
    # membership = Membership(activemembership=activemembership, subscriptionproduct=subscriptionproduct, datecreated=datecreated, datepaid=datepaid, invoicepaid=invoicepaid)


    # canceled subscription triggers: customer subscription updated --> object>data>object>canceled_at/cancel_at_period_end (cancel_at_period_end : true & canceled_at :1669649092)

    # db.session.add(membership)
    # db.session.commit()

    return jsonify(success=True)

woven bloom
#

Are you using Flask?

blazing nacelle
#

Yes

woven bloom
#

Can you show me how you are initializing @app?

blazing nacelle
#

ummm like this? ```# Create Flask app load app.config
app = Flask(name)
app.config.from_object(name+'.ConfigClass')

Initialize Flask-BabelEx

babel = Babel(app)

Initialize Flask-SQLAlchemy

db = SQLAlchemy(app)```

#

or the wsgi? ```#!/usr/bin/python3.7
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApp/")

from FlaskApp import main as application

woven bloom
#

Can you add a log just after def webhook() to ensure we just aren't getting inside the route at all and instead seeing a redirect occur?

blazing nacelle
#

i dont know how to do that

woven bloom
#

Just:

 print("test test test")
...
#

Then trigger a new test event

blazing nacelle
#
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

    print("test test test")``` lol
woven bloom
#

No you want the print("test") in the line after def webhook()

#

We want to know if the request is even making it to this endpoint at all

#

I'm pretty sure it is not, and we are seeing a redirect of the request so then there is immediately an error response of a 302 on the request attempt but this is a good way to check

blazing nacelle
#

where would test test test print to?

woven bloom
#

Your server logs