<commit_before>import logging
import stripe

from django.http import HttpResponse
from django.views.generic import View

from bluebottle.payments.services import PaymentService
from bluebottle.payments_stripe.utils import get_webhook_secret

from .models import StripePayment


logger = logging.getLogger(__name__)


class WebHookView(View):
    def post(self, request, **kwargs):
        payload = request.body
        signature_header = request.META['HTTP_STRIPE_SIGNATURE']

        try:
            event = stripe.Webhook.construct_event(
                payload, signature_header, get_webhook_secret()
            )

            if event.type == 'source.chargeable':
                payment = StripePayment.objects.get(source_token=event.data.object.id)

                service = PaymentService(payment.order_payment)
                service.adapter.charge()
            elif event.type == 'charge.dispute.closed' and event.data.object.status == 'lost':
                payment = StripePayment.objects.get(charge_token=event.data.object.charge)
                service = PaymentService(payment.order_payment)
                charge = stripe.Charge.retrieve(event.data.object.charge)
                service.adapter.update_from_charge(charge)
            elif event.type in ('source.canceled', 'source.failed'):
                payment = StripePayment.objects.get(source_token=event.data.object.id)
                service = PaymentService(payment.order_payment)
                service.adapter.update_from_source(event.data.object)
            elif event.type in (
                'charge.succeeded', 'charge.pending', 'charge.failed', 'charge.refunded',
            ):
                payment = StripePayment.objects.get(charge_token=event.data.object.id)
                service = PaymentService(payment.order_payment)
                service.adapter.update_from_charge(event.data.object)
        except stripe.error.SignatureVerificationError:
            # Invalid signature
            return HttpResponse(status=400)
        except StripePayment.DoesNotExist:
            # StripePayment not found
            return HttpResponse(status=400)
        return HttpResponse(status=200)<commit_msg>Don't throw charge errors back at Stripe.<commit_after>import logging
import stripe

from django.http import HttpResponse
from django.views.generic import View

from bluebottle.payments.exception import PaymentException
from bluebottle.payments.services import PaymentService
from bluebottle.payments_stripe.utils import get_webhook_secret

from .models import StripePayment


logger = logging.getLogger(__name__)


class WebHookView(View):
    def post(self, request, **kwargs):
        payload = request.body
        signature_header = request.META['HTTP_STRIPE_SIGNATURE']

        try:
            event = stripe.Webhook.construct_event(
                payload, signature_header, get_webhook_secret()
            )

            if event.type == 'source.chargeable':
                payment = StripePayment.objects.get(source_token=event.data.object.id)

                service = PaymentService(payment.order_payment)
                service.adapter.charge()
            elif event.type == 'charge.dispute.closed' and event.data.object.status == 'lost':
                payment = StripePayment.objects.get(charge_token=event.data.object.charge)
                service = PaymentService(payment.order_payment)
                charge = stripe.Charge.retrieve(event.data.object.charge)
                service.adapter.update_from_charge(charge)
            elif event.type in ('source.canceled', 'source.failed'):
                payment = StripePayment.objects.get(source_token=event.data.object.id)
                service = PaymentService(payment.order_payment)
                service.adapter.update_from_source(event.data.object)
            elif event.type in (
                'charge.succeeded', 'charge.pending', 'charge.failed', 'charge.refunded',
            ):
                payment = StripePayment.objects.get(charge_token=event.data.object.id)
                service = PaymentService(payment.order_payment)
                service.adapter.update_from_charge(event.data.object)
        except stripe.error.SignatureVerificationError:
            # Invalid signature
            return HttpResponse(status=400)
        except StripePayment.DoesNotExist:
            # StripePayment not found
            return HttpResponse(status=400)
        except PaymentException:
            # Payment error due to failed charge e.g.
            return HttpResponse(status=200)
        return HttpResponse(status=200)
{'fraction_non_alphanumeric': 0.08535004321521175, 'fraction_numerical': 0.004537597234226448, 'mean_word_length': 2.5228310502283104, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '109915', 'n_tokens_mistral': 1250, 'n_tokens_neox': 1166, 'n_words': 220}