def webhook(request): wh_secret = settings.STRIPE_WH_SECRET stripe.api_key = settings.STRIPE_SECRET_KEY payload = request.body sig_header = request.META['HTTP_STRIPE_SIGNATURE'] event = None try: event = stripe.Webhook.construct_event(payload, sig_header, wh_secret) except ValueError as e: # Invalid payload return HttpResponse(content=e, status=400) except stripe.error.SignatureVerificationError as e: # Invalid signature return HttpResponse(content=e, status=400) except Exception as e: return HttpResponse(content=e, status=400) # Handle the event handler = StripeWH_Handler(request) event_map = { 'payment_intent.succeeded': handler.handler_intent_succeeded, 'payment_intent.payment_failed': handler.handler_intent_failed, } event_type = event['type'] event_handler = event_map.get(event_type, handler.handler_event) response = event_handler(event) return response
def webhook(request): """ From Stripe Docs via Boutique Ado """ # Setup wh_secret = settings.STRIPE_WH_SECRET stripe.api_key = settings.STRIPE_SECRET_KEY # Retrieval payload = request.body sig_header = request.META['HTTP_STRIPE_SIGNATURE'] event = None try: event = stripe.Webhook.construct_event(payload, sig_header, wh_secret) except ValueError as e: return HttpResponse(content=e, status=400) except stripe.error.SignatureVerificationError as e: return HttpResponse(content=e, status=400) except Exception as e: return HttpResponse(content=e, status=400) handler = StripeWH_Handler(request) event_map = { 'payment_intent.succeeded': handler.handle_payment_intent_succeeded, 'payment_intent.payment_failed': handler.handle_payment_intent_payment_failed, } event_type = event["type"] event_handler = event_map.get(event_type, handler.handle_event) response = event_handler(event) return response
def webhook(request): """Listen for webhooks from Stripe""" wh_secret = settings.STRIPE_WH_SECRET stripe.api_key = settings.STRIPE_SECRET_KEY payload = request.body sig_header = request.META['HTTP_STRIPE_SIGNATURE'] event = None try: event = stripe.Webhook.construct_event( payload, sig_header, wh_secret ) except ValueError as e: return HttpResponse(status=400) except stripe.error.SignatureVerificationError as e: return HttpResponse(status=400) except Exception as e: return HttpResponse(content=e, status=400) # Set up webhook handler handler = StripeWH_Handler(request) event_map = { 'payment_intent.succeeded' : handler.handle_payment_intent_succeeded, 'payment_intent.payment_failed' : handler.handle_payment_intent_payment_failed, } # get the webhook type form Stripe event_type = event['type'] # If theres a handler for it, get it from the event map event_handler = event_map.get(event_type, handler.handle_event) # Call event handler with the event response = event_handler(event) return response
def webhook(request): """ Returns response from webhook in order to deal accordingly with the logic in the views in the rest of the app. """ wh_secret = settings.STRIPE_WH_SECRET stripe.api_key = settings.STRIPE_SECRET_KEY payload = request.body sig_header = request.META["HTTP_STRIPE_SIGNATURE"] event = None try: event = stripe.Webhook.construct_event(payload, sig_header, wh_secret) except ValueError as e: # Invalid payload return HttpResponse(status=400) except stripe.error.SignatureVerificationError as e: # Invalid signature return HttpResponse(status=400) except Exception as e: return HttpResponse(content=e, status=400) handler = StripeWH_Handler(request) event_map = { "payment_intent.succeeded": handler.handle_payment_intent_succeeded, "payment_intent.payment_failed": handler.handle_payment_intent_failed, } event_type = event["type"] event_handler = event_map.get(event_type, handler.handle_event) response = event_handler(event) return response
def my_webhook_view(request): request_data = json.loads(request.body) event = None data = request_data['data'] event_type = request_data['type'] if webhook_secret: # Retrieve the event by verifying the signature using the raw body and secret if webhook signing is configured. signature = request.META['HTTP_STRIPE_SIGNATURE'] #request.headers.get('stripe-signature') try: event = stripe.Webhook.construct_event( payload=request.body, sig_header=signature, secret=webhook_secret) except ValueError as e: # Invalid payload return HttpResponse(status=400) except stripe.error.SignatureVerificationError as e: # Invalid signature return HttpResponse(status=400) except Exception as e: return HttpResponse(content=e, status=400) # Get the type of webhook event sent - used to check the status of PaymentIntents. else: messages.error(request, 'the shopping will be cancel, please call support # not webhook key found!' ) return HttpResponse(status=400) handler = StripeWH_Handler(request) event_map = { 'payment_intet.succeeded': handler.handle_payment_intent_succeeded, 'payment_intet.payment_feiled': handler.handle_payment_intent_failed } event_hadler = event_map.get(event_type, handler.handle_event) Response = event_hadler(event) return Response
def webhook(request): wh_secret = settings.STRIPE_WH_SECRET stripe.api_key = settings.STRIPE_SECRET_KEY payload = request.body event = None try: event = stripe.Event.construct_from(json.loads(payload), stripe.api_key) except ValueError as e: # Invalid payload return HttpResponse(status=400) except stripe.error.SignatureVerificationError as e: # Invalid signature return HttpResponse(status=400) except Exception as e: return HttpResponse(content=e, status=400) handler = StripeWH_Handler(request) event_map = { 'payment_intent.succeeded': handler.handle_payment_intent_succeeded, 'payment_intent.payment_failed': handler.handle_payment_intent_payment_failed, } event_type = event['type'] event_handler = event_map.get(event_type, handler.handle_event) # Call the event handler with the event response = event_handler(event) return response
def webhook(request): """Listen for webhooks from Stripe the code for this mostly comes frm stripe """ # Setup # we use the wh_secret key n secrt key to verify that # the webhoock came frm stripe wh_secret = settings.STRIPE_WH_SECRET stripe.api_key = settings.STRIPE_SECRET_KEY # Get the webhook data and verify its signature payload = request.body sig_header = request.META['HTTP_STRIPE_SIGNATURE'] event = None try: event = stripe.Webhook.construct_event(payload, sig_header, wh_secret) except ValueError as e: # Invalid payload return HttpResponse(status=400) except stripe.error.SignatureVerificationError as e: # Invalid signature return HttpResponse(status=400) except Exception as e: return HttpResponse(content=e, status=400) # print('success') # return HttpResponse(status=200) # Set up a webhook handler handler = StripeWH_Handler(request) # Map webhook events to relevant handler functions # the dictionaries keys will be the names of the webhooks # coming from stripe.While its values will be the actual # methods inside the handler. event_map = { 'payment_intent.succeeded': handler.handle_payment_intent_succeeded, 'payment_intent.payment_failed': handler.handle_payment_intent_payment_failed, } # Get the webhook type from Stripe # gets the type of the event from stripe which will be stored # in a key called type.So this will return something like # payment intent.succeeded or payment intent.payment failed. event_type = event['type'] # If there's a handler for it, get it from the event map # Use the generic one by default event_handler = event_map.get(event_type, handler.handle_event) # Call the event handler with the event response = event_handler(event) return response
def webhook(request): """Listen for webhooks from Stripe""" print("1 - getting into the function.") # Setup wh_secret = settings.STRIPE_WH_SECRET stripe.api_key = settings.STRIPE_SECRET_KEY # Get the webhook data and verify its signature payload = request.body sig_header = request.META['HTTP_STRIPE_SIGNATURE'] event = None print("2") try: event = stripe.Webhook.construct_event(payload, sig_header, wh_secret) except ValueError as e: # Invalid payload print(e) return HttpResponse(status=400) except stripe.error.SignatureVerificationError as e: # Invalid signature print(e) return HttpResponse(status=400) except Exception as e: print(e) return HttpResponse(content=e, status=400) print("3") # Set up a webhook handler handler = StripeWH_Handler(request) # Map webhook events to relevant handler functions event_map = { 'payment_intent.succeeded': handler.handle_payment_intent_succeeded, 'payment_intent.payment_failed': handler.handle_payment_intent_payment_failed, } # Get the webhook type from Stripe event_type = event['type'] # If there's a handler for it, get it from the event map # Use the generic one by default event_handler = event_map.get(event_type, handler.handle_event) # Call the event handler with the event response = event_handler(event) return response
def webhook(request): """Listen for webhooks from Stripe""" # Setup wh_secret = settings.STRIPE_WH_SECRET stripe.api_key = settings.STRIPE_SECRET_KEY # Get the webhook data and verify its signature payload = request.body sig_header = request.META['HTTP_STRIPE_SIGNATURE'] event = None try: event = stripe.Webhook.construct_event( payload, sig_header, wh_secret ) except ValueError as e: # Invalid payload return HttpResponse(status=400) except stripe.error.SignatureVerificationError as e: # Invalid signature return HttpResponse(status=400) except Exception as e: return HttpResponse(content=e, status=400) # Set up a webhook handler handler = StripeWH_Handler(request) # create a dictionary. the dictionaries keys will be the names # of the webhooks coming from stripe.its values will be the actual # methods inside handler.Map webhook events to relevant handler functions event_map = { 'payment_intent.succeeded': handler.handle_payment_intent_succeeded, 'payment_intent.payment_failed': handler.handle_payment_intent_payment_failed, } # Get the webhook type from Stripe event_type = event['type'] # If there's a handler for it, get it from the event map # Use the generic one by default. # we'll look up the key in dictionary,assign its value to a variable # called event handler. event handler is nothing more than an alias for # whatever function we pulled out of the dictionary.we can call it just # like any other function event_handler = event_map.get(event_type, handler.handle_event) # Call the event handler with the event response = event_handler(event) return response
def webhook(request): # This listens to webhooks coming from Stripe # API and Secret Keys. These are placed in the environment for security wh_secret = settings.STRIPE_WH_SECRET stripe.api_key = settings.STRIPE_SECRET_KEY # Reads the data sent from the webhook and verifies the signature payload = request.body sig_header = request.META['HTTP_STRIPE_SIGNATURE'] event = None try: event = stripe.Webhook.construct_event(payload, sig_header, wh_secret) except ValueError as e: # Invalid payload return HttpResponse(status=400) except stripe.error.SignatureVerificationError as e: # Invalid signature return HttpResponse(status=400) # General exeptions not covered by payload or signature except Exception as e: return HttpResponse(content=e, status=400) print('success') return HttpResponse(status=200) # This sets up the webhook handler handler = StripeWH_Handler(request) # This maps webhook events to the relevant handler functions event_map = { 'payment_intent.succeeded': handler.handle_payment_intent_succeeded, 'payment_intent.payment_failed': handler.handle_payment_intent_payment_failed, } # Gets the type of webhook from Stripe event_type = event['type'] # If there's a handler for it, get it from the event map # Use the generic one by default event_handler = event_map.get(event_type, handler.handle_event) # Call the event handler with the event response = event_handler(event) return response
def webhook(request): """ listen for webhooks from stripe """ # setup wh_secret = settings.STRIPE_WH_SECRET stripe.api_key = settings.STRIPE_SECRET_KEY # get the webhook payload = request.body sig_header = request.META['HTTP_STRIPE_SIGNATURE'] event = None try: event = stripe.Webhook.construct_event( payload, sig_header, wh_secret ) except ValueError as e: # Invalid payload return HttpResponse(status=400) except stripe.error.SignatureVerificationError as e: # Invalid signature return HttpResponse(status=400) except Exception as e: return HttpResponse(content=e, status=400) # print('Success!') # return HttpResponse(status=200) # set up a webhook webhook handler handler = StripeWH_Handler(request) # map webhook events to relevant handler functions event_map = { 'payment_intent.succeeded': handler.handle_payment_intent_succeeded, 'payment_intent.payment_failed': handler.handle_payment_intent_payment_failed, } # get the webhook type from stripe event_type = event['type'] # if there's a handler for it, get it from the event map # use the generic one by default event_handler = event_map.get(event_type, handler.handle_event) # call the event handler with the event_type response = event_handler(event) return response
def webhook(request): """ Listen for Stripe webhooks """ # Set up wh_secret = settings.STRIPE_WH_SECRET stripe.api_key = settings.STRIPE_SECRET_KEY # Code from Stripe (Webhook data and signature) payload = request.body sig_header = request.META['HTTP_STRIPE_SIGNATURE'] event = None try: event = stripe.Webhook.construct_event(payload, sig_header, wh_secret) except ValueError as e: # Invalid payload return HttpResponse(content=e, status=400) # Tutorial additional code except stripe.error.SignatureVerificationError as e: # Invalid signature return HttpResponse(content=e, status=400) except Exception as e: return HttpResponse(content=e, status=400) # Handle the event # Create an instance of the webhook and pass in the request handler = StripeWH_Handler(request) # Map the webhook events to relevant handler functions event_map = { 'payment_intent.succeeded': handler.handle_payment_intent_succeeded, 'payment_intent.payment_failed': (handler.handle_payment_intent_payment_failed, ) } # Get the webhook type from Stripe event_type = event['type'] # If there's a handler for the webhook type, get it from the event map # Use the generic one by default event_handler = event_map.get(event_type, handler.handle_event) # Call the event handler with the event response = event_handler(event) return response
def webhook(request): """ Listen for webhooks and handle them """ # Setup wh_secret = settings.STRIPE_WH_SECRET stripe.api_key = settings.STRIPE_SECRET_KEY # Get the webhook data and verify the signature payload = request.body sig_header = request.META['HTTP_STRIPE_SIGNATURE'] event = None try: event = stripe.Webhook.construct_event(payload, sig_header, wh_secret) except ValueError as e: return HttpResponse(status=400) # Invalid payload except stripe.error.SignatureVerificationError as e: return HttpResponse(status=400) # Invalid signature except Exception as e: return HttpResponse(content=e, status=400) # Any other exception # Set up a webhook handler handler = StripeWH_Handler(request) # Map webhook events to relevent handler functions event_map = { 'payment_intent.succeeded': handler.handle_payment_intent_succeeded, 'payment_intent.payment_failed': handler.handle_payment_intent_payment_failed, } # Get the webhook type from Stripe event_type = event['type'] # If there's a handler for it, get it from the event map. # Otherwise use the generic one try: event_handler = event_map[event_type] except KeyError: event_handler = handler.handle_event # Call the event handler with the event, e.g. # handler.handle_payment_intent_succeeded(event) response = event_handler(event) return response
def webhook(request): """ Listen for webhooks from Stripe. Code taken from Code Institute Boutique Ado project. """ # Setup wh_secret = settings.STRIPE_WH_SECRET stripe.api_key = settings.STRIPE_SECRET_KEY # Get the webhook data and verify its signature payload = request.body sig_header = request.META["HTTP_STRIPE_SIGNATURE"] event = None try: event = stripe.Webhook.construct_event(payload, sig_header, wh_secret) except ValueError as e: # Invalid payload return HttpResponse(status=400) except stripe.error.SignatureVerificationError as e: # Invalid signature return HttpResponse(status=400) except Exception as e: return HttpResponse(content=e, status=400) # Set up a webhook handler handler = StripeWH_Handler(request) # Map webhook events to relevant handler functions event_map = { "payment_intent.succeeded": handler.handle_payment_intent_succeeded, "payment_intent.payment_failed": handler.handle_payment_intent_payment_failed, } # Get the webhook type from Stripe event_type = event["type"] # If there's a handler for it, get it from the event map # Use the generic one by default event_handler = event_map.get(event_type, handler.handle_event) # Call the event handler with the event response = event_handler(event) return response
def webhook(request): """This calls out webhooks from stripe""" # Stripe call creation endpoint_secret = settings.STRIPE_WH_SECRET stripe.api_key = settings.STRIPE_SECRET_KEY # Obtain the webhook data and confirm the signature payload = request.body sig_header = request.META['HTTP_STRIPE_SIGNATURE'] event = None try: event = stripe.Webhook.construct_event( payload, sig_header, endpoint_secret ) except ValueError as e: # Invalid payload return HttpResponse(status=400) except stripe.error.SignatureVerificationError as e: # Invalid signature return HttpResponse(status=400) except Exception as e: return HttpResponse(content=e, status=400) # This creates up a webhook handler handler = StripeWH_Handler(request) # This calls up a dictionary list from webhook events event_map = { 'payment_intent.succeeded': handler.webhook_payment_successful, 'payment_intent.payment_failed': handler.webhook_payment_failed, } # Obtains a webhook type from Stripe event_type = event['type'] event_handler = event_map.get( event_type, handler.webhook_event ) # This calls up the event of the handler response = event_handler(event) return response
def webhook(request): """Listen for webhooks from Stripe""" # Get enviroment key used by the handler wh_secret = settings.STRIPE_WH_SECRET stripe.api_key = settings.STRIPE_SECRET_KEY # Get webhook data payload = request.body # Verify signature of webhook sig_header = request.META['HTTP_STRIPE_SIGNATURE'] event = None try: event = stripe.Webhook.construct_event(payload, sig_header, wh_secret) except ValueError as e: # Invalid payload return HttpResponse(status=400) except stripe.error.SignatureVerificationError as e: # Invalid signature return HttpResponse(status=400) except Exception as e: return HttpResponse(content=e, status=400) # Webhook Handler Set Up handler = StripeWH_Handler(request) # Map webhook events to relevant handler functions event_map = { 'payment_intent.succeeded': handler.handle_payment_intent_succeeded, 'payment_intent.payment_failed': handler.handle_payment_intent_payment_failed, } # Retrieve webhook type from Stripe event_type = event['type'] # Set generic handler function as default # If the event has a handler get it from the event map event_handler = event_map.get(event_type, handler.handle_event) # Calls the event handler with the event response = event_handler(event) return response
def webhook(request): """ listener for webhooks from stripe """ # keys wh_prv = settings.STRIPE_WH_KEY stripe.api_key = settings.STRIPE_PRV_KEY # verify WH signature payload = request.body sig_header = request.META['HTTP_STRIPE_SIGNATURE'] event = None try: event = stripe.Webhook.construct_event( payload, sig_header, wh_prv ) except ValueError as e: # payload error return HttpResponse(status=400) except stripe.error.SignatureVerificationError as e: # signature error return HttpResponse(status=400) except Exception as e: return HttpResponse(content=e, status=400) # print('Success!') # return HttpResponse(status=200) # WH handler handler = StripeWH_Handler(request) # map events to handler functions event_map = { 'payment_intent.succeeded': handler.handle_payment_success, 'payment_intent.payment_failed': handler.handle_payment_fail } # get webhook type event_type = event["type"] # use generic handler if no specific one in the event_map event_handler = event_map.get(event_type, handler.handle_event) # call the handler response = event_handler(event) return response
def webhook(request): """ Listen for webhooks """ # Setup wh_secret = settings.STRIPE_WH_SECRET stripe.api_key = settings.STRIPE_SECRET_KEY # Get WH data, verify signature payload = request.body sig_header = request.META['HTTP_STRIPE_SIGNATURE'] event = None try: event = stripe.Webhook.construct_event(payload, sig_header, wh_secret) except ValueError as e: # Invalid payload return HttpResponse(status=400) except stripe.error.SignatureVerificationError as e: # Invalid signature return HttpResponse(status=400) except Exception as e: return HttpResponse(content=e, status=400) # Set up wh handler handler = StripeWH_Handler(request) # Map wh events to relevant handler function event_map = { 'payment_intent.succeeded': handler.handle_payment_intent_succeeded, 'payment_intent.payment_failed': handler.handle_payment_intent_payment_failed, } # Get webhook type from Stripe event_type = event['type'] # Get handler from event map if there is one # Use generic one by default event_handler = event_map.get(event_type, handler.handle_event) # Call event handler with event response = event_handler(event) return response
def webhook(request): """Listen for webhooks from Stripe""" # Setup wh_secret = settings.STRIPE_WH_SECRET stripe.api_key = settings.STRIPE_SECRET_KEY # Get the webhook data and verify its signature payload = request.body sig_header = request.META['HTTP_STRIPE_SIGNATURE'] event = None try: event = stripe.Webhook.construct_event( payload, sig_header, wh_secret ) except ValueError as e: # Invalid payload return HttpResponse(status=400) except stripe.error.SignatureVerificationError as e: # Invalid signature print(wh_secret) return HttpResponse(status=400) except Exception as e: return HttpResponse(content=e, status=400) # Code from Code Institute # Set up a webhook handler handler = StripeWH_Handler(request) # and the values will be the actual methods inside the handler event_map = { 'payment_intent.succeeded': handler.handle_payment_intent_succeeded, 'payment_intent.payment_failed': handler.handle_payment_intent_payment_failed, } # Get the webhook type from Stripe event_type = event['type'] # Once we get the type we look it up in the key dictionary and assign it to a variable # If there's a handler for it, get it from the event map # Use the generic one by default event_handler = event_map.get(event_type, handler.handle_event) # envent_handler is just an alias for whatever function we pulled out of the dictionary # Call the event handler with the event response = event_handler(event) return response
def webhook(request): """Listen for webhooks from Stripe""" # Setup wh_secret = settings.STRIPE_WH_SECRET stripe.api_key = settings.STRIPE_SECRET_KEY # Get the webhook data and verify its signature payload = request.body sig_header = request.META['HTTP_STRIPE_SIGNATURE'] event = None try: # listener for the webhook Stripe sends based on the event event = stripe.Webhook.construct_event(payload, sig_header, wh_secret) except ValueError as e: # Invalid payload return HttpResponse(status=400) except stripe.error.SignatureVerificationError as e: # Invalid signature return HttpResponse(status=400) except Exception as e: return HttpResponse(content=e, status=400) # Instantiate the webhook handler (from StripeWH_Handler class) handler = StripeWH_Handler(request) # Map webhook events to relevant handler functions # key = name of Stripe's webhook; value = method from StripeWH_Handler event_map = { 'payment_intent.succeeded': handler.handle_payment_intent_succeeded, 'payment_intent.payment_failed': handler.handle_payment_intent_payment_failed, } # Get the webhook type sent by Stripe event_type = event['type'] # If there's a handler for it, get it from the event map # Otherwise, use the generic one by default event_handler = event_map.get(event_type, handler.handle_event) # Call the event handler for the event received from Stripe response = event_handler(event) return response
def webhook(request): """ Listen for Webhooks from Stripe """ # Setup wh_secret = settings.STRIPE_WH_SECRET stripe_api_key = settings.STRIPE_SECRET_KEY # Get the webhook data and verify the signature payload = request.body event = None try: event = stripe.Event.construct_from( json.loads(payload), stripe.api_key, wh_secret ) except ValueError as e: # Invalid payload return HttpResponse(status=400) except stripe.error.SignatureVerificationError as e: # Invalid signature return HttpResponse(status=400) except Exception as e: return HttpResponse(content=e, status=400) # Set up a webhook handler handler = StripeWH_Handler(request) # Map webhook events to relevant handler functions event_map = { 'payment_intent.succeeded': handler.handle_payment_intent_succeeded, 'payment_intent.payment_failed': ( handler.handle_payment_intent_payment_failed), } # Get the webhook type from Stripe event_type = event['type'] # If there's a handler for it, get it from the event map # Use the generic one by default event_handler = event_map.get(event_type, handler.handle_event) # Call the event handler with the event response = event_handler(event) return response
def webhook(request): """ Listen for webhooks from Stripe""" # Setup wh_secret = settings.STRIPE_WH_SECRET stripe.api_key = settings.STRIPE_SECRET_KEY # Get the webhook data , the wh secret and verify its signature from Stripe # remember to export STRIPE_WH_SECRET from Stripe payload = request.body sig_header = request.META['HTTP_STRIPE_SIGNATURE'] event = None try: event = stripe.Webhook.construct_event(payload, sig_header, wh_secret) except ValueError as e: # Invalid payload return HttpResponse(status=400) except stripe.error.SignatureVerificationError as e: # Invalid signature return HttpResponse(status=400) except Exception as e: return HttpResponse(content=e, status=400) # Set up a webhook handler for import to other projects, work reusablility, open-source and share handler = StripeWH_Handler(request) # Map webhook events to relevant handler functions event_map = { 'payment_intent.succeeded': handler.handle_payment_intent_succeeded, 'payment_intent.payment_failed': handler.handle_payment_intent_payment_failed, } # Get webhook from stripe # event handler is an alias for whatever is pulld from dictionary event_type = event['type'] # If there's a handler for it, get it form the event map #Use the generic one by default event_handler = event_map.get(event_type, handler.handle_event) # Call the event handler with the event response = event_handler(event) return response
def webhook(request): """Listen for webhooks from Stripe""" # Setup wh_secret = settings.STRIPE_WH_SECRET stripe.api_key = settings.STRIPE_SECRET_KEY # Get the webhook data and verify its signature payload = request.body sig_header = request.META['HTTP_STRIPE_SIGNATURE'] event = None try: event = stripe.Webhook.construct_event(payload, sig_header, wh_secret) except ValueError as e: # When the payload is invalid return HttpResponse(status=400) except stripe.error.SignatureVerificationError as e: # When signature invalid return HttpResponse(status=400) except Exception as e: return HttpResponse(content=e, status=400) # Webhook handler setup handler = StripeWH_Handler(request) # Map webhook events to relevant handler functions event_map = { 'payment_intent.succeeded': handler.handle_payment_intent_succeeded, 'payment_intent.payment_failed': handler.handle_payment_intent_payment_failed, } # Get the type of webhook from Stripe event_type = event['type'] # If a handler exists get from event map # Use generic one by default event_handler = event_map.get(event_type, handler.handle_event) # Call the event handler with the event response = event_handler(event) return response
def webhook(request): """Listen for webhooks from Stripe and handle each according to StripeWH_Handler class """ # Setup secret key and API key wh_secret = settings.STRIPE_WH_SECRET_CH stripe.api_key = settings.STRIPE_SECRET_KEY # Get the webhook data and verify its signature payload = request.body sig_header = request.META['HTTP_STRIPE_SIGNATURE'] event = None try: event = stripe.Webhook.construct_event(payload, sig_header, wh_secret) except ValueError: # Invalid payload return HttpResponse(status=400) except stripe.error.SignatureVerificationError: # Invalid signature return HttpResponse(status=400) except Exception as e: return HttpResponse(content=e, status=400) # Set up a webhook handler handler = StripeWH_Handler(request) # Map webhook events to relevant handler functions event_map = { 'payment_intent.succeeded': (handler.handle_payment_intent_succeeded), 'payment_intent.payment_failed': (handler.handle_payment_intent_payment_failed), } # Get the webhook type from Stripe event_type = event['type'] # Get webhook handler or use generic event_handler = event_map.get(event_type, handler.handle_event) # Call the webhook handler obtained above response = event_handler(event) return response
def webhook(request): """ Listen for Stripe webhooks """ # Internal setup wh_secret = settings.STRIPE_WH_SECRET stripe.api_key = settings.STRIPE_SECRET_KEY # Get webhook data and verify signature payload = request.body sig_header = request.META['HTTP_STRIPE_SIGNATURE'] event = None try: event = stripe.Webhook.construct_event(payload, sig_header, wh_secret) except ValueError as e: # Invalid payload return HttpResponse(status=400) except stripe.error.SignatureVerificationError as e: # Invalid signature return HttpResponse(status=400) except Exception as e: return HttpResponse(content=e, status=400) # setup a webhook handler handler = StripeWH_Handler(request) # map webhook events to relevant handler functions event_map = { 'payment_intent.succeeded': handler.handle_payment_intent_succeeded, 'payment_intent.payment_failed': handler.handle_payment_intent_payment_failed, } # get webhook type from Stripe event_type = event['type'] # if the event has a handler, use that from # the map, if not use the generic one event_handler = event_map.get(event_type, handler.handle_event) # call the event handler with the event response = event_handler(event) return response
def webhook(request): """Listen for webhooks from Stripe""" # Setup wh_secret = settings.STRIPE_WH_SECRET stripe.api_key = settings.STRIPE_SECRET_KEY # Get the webhook data and verify its signature payload = request.body sig_header = request.META['HTTP_STRIPE_SIGNATURE'] event = None try: event = stripe.Webhook.construct_event(payload, sig_header, wh_secret) except ValueError as e: # Invalid payload return HttpResponse(status=400) except stripe.error.SignatureVerificationError as e: # Invalid signature return HttpResponse(status=400) except Exception as e: return HttpResponse(content=e, status=400) # create an instance of the webhook handler handler = StripeWH_Handler(request) # this maps webhook events to their relevant handler functions event_map = { 'payment_intent.succeeded': handler.handle_payment_intent_succeeded, 'payment_intent.payment_failed': handler.handle_payment_intent_payment_failed, # noqa } # retrieves the webhook type from Stripe event_type = event['type'] # Get handler from the event map if available # Use the generic one by default event_handler = event_map.get(event_type, handler.handle_event) # Call the event handler with event response = event_handler(event) return response
def webhook(request): """Listen for webhooks from Stripe""" # Setup wh_secret = settings.STRIPE_WH_SECRET stripe.api_key = settings.STRIPE_SECRET_KEY # Get the webhook data and verify its signature payload = request.body sig_header = request.META['HTTP_STRIPE_SIGNATURE'] event = None try: event = stripe.Webhook.construct_event(payload, sig_header, wh_secret) except ValueError as e: # Invalid payload return HttpResponse(status=400) except stripe.error.SignatureVerificationError as e: # Invalid signature return HttpResponse(status=400) except Exception as e: return HttpResponse(content=e, status=400) # Webhook Handler handler = StripeWH_Handler(request) # Map Which Event Is Happening event_map = { 'payment_intent.succeeded': handler.handle_payment_intent_succeeded, 'payment_intent.payment_failed': handler.handle_payment_intent_payment_failed, } # Get the webhook type from Stripe: event_type = event["type"] # Use generic one by default, or personal handlers if we have one. event_handler = event_map.get(event_type, handler.handle_event) # Call the event handler with the event: response = event_handler(event) return response
def webhook(request): """Listen for webhooks from Stripe.""" # Setup variables wh_secret = settings.STRIPE_WH_SECRET stripe.api_key = settings.STRIPE_SECRET_KEY # Get the webhook data and verify its signature payload = request.body sig_header = request.META['HTTP_STRIPE_SIGNATURE'] event = None try: event = stripe.Webhook.construct_event(payload, sig_header, wh_secret) except ValueError as e: # Invalid payload return HttpResponse(content=e, status=400) except stripe.error.SignatureVerificationError as e: # Invalid signature return HttpResponse(content=e, status=400) except Exception as e: return HttpResponse(content=e, status=400) # Custom webhook handler for this app handler = StripeWH_Handler(request) # Map webhook events to relevant handler functions event_map = { 'payment_intent.succeeded': handler.handle_payment_intent_succeeded, 'payment_intent.payment_failed': handler.handle_payment_intent_failed, } # Get the event type from Stripe webhook event_type = event['type'] # If there's a handler for it, get it from the event map. # If not in event map, then use default handle_event event_handler = event_map.get(event_type, handler.handle_event) # Call the event handler with the event response = event_handler(event) return response
def webhook(request): wh_secret = settings.STRIPE_WH_SECRET stripe.api_key = settings.STRIPE_SECRET_KEY payload = request.body sig_header = request.META['HTTP_STRIPE_SIGNATURE'] event = None try: event = stripe.Webhook.construct_event(payload, sig_header, wh_secret) except ValueError as e: # Invalid payload return HttpResponse(content=e, status=400) except stripe.error.SignatureVerificationError as e: # Invalid signature return HttpResponse(content=e, status=400) except Exception as e: return HttpResponse(content=e, status=400) # Set up webhook handler handler = StripeWH_Handler(request) # Map webhook events to handler functions event_map = { 'payment_intent.succeeded': handler.handle_payment_intent_succeeded, 'payment_intent.payment_failed': handler.handle_payment_intent_payment_failed, } # Get webhook event type from Stripe event_type = event['type'] # If handler exists, get it from map otherwise use default event_handler = event_map.get(event_type, handler.handle_event) # Call event handler with event response = event_handler(event) return response
def webhook(request): endpoint_secret = settings.STRIPE_WH_SECRET stripe.api_key = settings.STRIPE_SECRET_KEY # Ensure webhook's signature is coming from stripe payload = request.body sig_header = request.META['HTTP_STRIPE_SIGNATURE'] event = None try: event = stripe.Webhook.construct_event(payload, sig_header, endpoint_secret) except ValueError as e: # Invalid payload print(f'payload error: {e}') return HttpResponse(status=400) except stripe.error.SignatureVerificationError as e: # Invalid signature print(f'invalid signature error: {e}') return HttpResponse(status=400) except Exception as e: print(f'error: {e} ') return HttpResponse(content=e, status=400) handler = StripeWH_Handler(request) event_map = { 'payment_intent.succeeded': handler.handle_successful_payment_intent, 'payment_intent.payment_failed': handler.handles_failed_payment_intent, } event_type = event['type'] # retrieves event from dictionary above event_handler = event_map.get(event_type, handler.handle_event) response = event_handler(event) return response