<commit_before># -*- coding: utf-8 -*-

import json
from datetime import datetime

from django.conf import settings
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.core.mail import send_mail
from django.http import HttpResponseRedirect, JsonResponse
from django.shortcuts import get_object_or_404, redirect, render
from django.template.loader import render_to_string
from django.urls import reverse
from django.utils.translation import ugettext as _

import stripe

from apps.lan.models import Ticket, TicketType


def payment_info_static(request):
    return render(request, 'payment/info.html')


def payment_info(request, ticket_type_id):
    return render(
        request,
        'payment/info.html',
        {
            'ticket_type_id': ticket_type_id,
        })


@login_required()
def payment(request, ticket_type_id):
    stripe.api_key = settings.STRIPE_PRIVATE_KEY

    ticket_type = get_object_or_404(TicketType, pk=ticket_type_id)

    if request.user not in ticket_type.lan.attendees:
        messages.info(request, _(u'You must be an attendee to buy a ticket for this event'))
        return redirect('lan_details', lan_id=ticket_type.lan_id)

    if ticket_type.lan.has_ticket(request.user):
        messages.info(request, _(u'You have already have a ticket for this event'))
        return redirect('lan_details', lan_id=ticket_type.lan_id)

    if request.method == 'GET':
        return render(
            request,
            'payment/checkout.html',
            {
                'ticket_type': ticket_type,
                'lan': ticket_type.lan,
            })

    if request.method == 'POST':
        json_data = json.loads(request.body)
        intent = None
        try:
            if 'payment_method_id' in request.body:
                # Create the PaymentIntent
                intent = stripe.PaymentIntent.create(
                    payment_method=str(json_data['payment_method_id']),
                    amount=ticket_type.price * 100,
                    currency='nok',
                    confirmation_method='manual',
                    confirm=True,
                )
            elif 'payment_intent_id' in request.body:
                intent = stripe.PaymentIntent.confirm(str(json_data['payment_intent_id']))
        except stripe.error.CardError as e:
            messages.error(request, _(e.user_message))
            return JsonResponse({'error': e.user_message})

        return generate_payment_response(request, ticket_type, intent)

    return HttpResponseRedirect('/')


def generate_payment_response(request, ticket_type, intent):
    # Instruct Stripe.js to handle SCA if required
    if intent.status == 'requires_action' and intent.next_action.type == 'use_stripe_sdk':
        return JsonResponse({
            'requires_action': True,
            'payment_intent_client_secret': intent.client_secret,
        })
    elif intent.status == 'succeeded':

        ticket = Ticket()
        ticket.user = request.user
        ticket.ticket_type = ticket_type
        ticket.bought_date = datetime.now()
        ticket.save()

        lan = ticket.ticket_type.lan
        lan_link = request.build_absolute_uri(reverse('lan_details', kwargs={'lan_id': lan.id}))
        context = {
            'ticket': ticket,
            'lan': lan,
            'lan_link': lan_link,
        }
        txt_message = render_to_string('payment/email/ticket_receipt.txt', context, request).strip()
        html_message = render_to_string('payment/email/ticket_receipt.html', context, request).strip()
        send_mail(
            subject=_(u'Ticket confirmation'),
            from_email=settings.STUDLAN_FROM_MAIL,
            recipient_list=[ticket.user.email],
            message=txt_message,
            html_message=html_message,
        )

            messages.success(request, _(u'Payment complete. Confirmation sent to {email}.').format(email=request.user.email))

        return JsonResponse({'success': True})
    else:
        messages.error(request, _(u'Payment unsuccessful - please contact support'))
        return JsonResponse({'error': 'Invalid PaymentIntent status'})<commit_msg>Fix merge error<commit_after># -*- coding: utf-8 -*-

import json
from datetime import datetime

from django.conf import settings
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.core.mail import send_mail
from django.http import HttpResponseRedirect, JsonResponse
from django.shortcuts import get_object_or_404, redirect, render
from django.template.loader import render_to_string
from django.urls import reverse
from django.utils.translation import ugettext as _

import stripe

from apps.lan.models import Ticket, TicketType


def payment_info_static(request):
    return render(request, 'payment/info.html')


def payment_info(request, ticket_type_id):
    return render(
        request,
        'payment/info.html',
        {
            'ticket_type_id': ticket_type_id,
        })


@login_required()
def payment(request, ticket_type_id):
    stripe.api_key = settings.STRIPE_PRIVATE_KEY

    ticket_type = get_object_or_404(TicketType, pk=ticket_type_id)

    if request.user not in ticket_type.lan.attendees:
        messages.info(request, _(u'You must be an attendee to buy a ticket for this event'))
        return redirect('lan_details', lan_id=ticket_type.lan_id)

    if ticket_type.lan.has_ticket(request.user):
        messages.info(request, _(u'You have already have a ticket for this event'))
        return redirect('lan_details', lan_id=ticket_type.lan_id)

    if request.method == 'GET':
        return render(
            request,
            'payment/checkout.html',
            {
                'ticket_type': ticket_type,
                'lan': ticket_type.lan,
            })

    if request.method == 'POST':
        json_data = json.loads(request.body)
        intent = None
        try:
            if 'payment_method_id' in request.body:
                # Create the PaymentIntent
                intent = stripe.PaymentIntent.create(
                    payment_method=str(json_data['payment_method_id']),
                    amount=ticket_type.price * 100,
                    currency='nok',
                    confirmation_method='manual',
                    confirm=True,
                )
            elif 'payment_intent_id' in request.body:
                intent = stripe.PaymentIntent.confirm(str(json_data['payment_intent_id']))
        except stripe.error.CardError as e:
            messages.error(request, _(e.user_message))
            return JsonResponse({'error': e.user_message})

        return generate_payment_response(request, ticket_type, intent)

    return HttpResponseRedirect('/')


def generate_payment_response(request, ticket_type, intent):
    # Instruct Stripe.js to handle SCA if required
    if intent.status == 'requires_action' and intent.next_action.type == 'use_stripe_sdk':
        return JsonResponse({
            'requires_action': True,
            'payment_intent_client_secret': intent.client_secret,
        })
    elif intent.status == 'succeeded':

        ticket = Ticket()
        ticket.user = request.user
        ticket.ticket_type = ticket_type
        ticket.bought_date = datetime.now()
        ticket.save()

        lan = ticket.ticket_type.lan
        lan_link = request.build_absolute_uri(reverse('lan_details', kwargs={'lan_id': lan.id}))
        context = {
            'ticket': ticket,
            'lan': lan,
            'lan_link': lan_link,
        }
        txt_message = render_to_string('payment/email/ticket_receipt.txt', context, request).strip()
        html_message = render_to_string('payment/email/ticket_receipt.html', context, request).strip()
        send_mail(
            subject=_(u'Ticket confirmation'),
            from_email=settings.STUDLAN_FROM_MAIL,
            recipient_list=[ticket.user.email],
            message=txt_message,
            html_message=html_message,
        )

        messages.success(
            request,
            _(u'Payment complete. Confirmation sent to {email}.').format(email=request.user.email)
        )

        return JsonResponse({'success': True})
    else:
        messages.error(request, _(u'Payment unsuccessful - please contact support'))
        return JsonResponse({'error': 'Invalid PaymentIntent status'})
{'fraction_non_alphanumeric': 0.0963226571767497, 'fraction_numerical': 0.002372479240806643, 'mean_word_length': 2.616902616902617, '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': '1738518', 'n_tokens_mistral': 2392, 'n_tokens_neox': 2285, 'n_words': 512}