<commit_before>from decimal import Decimal
import logging
import paypalrestsdk

from django.contrib import messages
from django.http.response import Http404
from django.shortcuts import render, get_object_or_404, redirect
from django.views.generic import TemplateView
from django.utils.translation import ugettext as _

from konfera.models import Order
from konfera.utils import update_order_status_context, update_event_context
from konfera.event.forms import ReceiptForm

from payments import settings
from payments.utils import _process_payment


logger = logging.getLogger(__name__)


paypalrestsdk.configure({
    "mode": settings.PAYPAL_MODE,
    "client_id": settings.PAYPAL_CLIENT_ID,
    "client_secret": settings.PAYPAL_CLIENT_SECRET,
})


def order_payment(request, order_uuid):
    order = get_object_or_404(Order, uuid=order_uuid)
    context = dict()

    if order.status == Order.PAID:
        return redirect('order_details', order_uuid=str(order.uuid))

    if order.status == Order.AWAITING:
        context['form'] = form = ReceiptForm(request.POST or None, instance=order.receipt_of)

        if form.is_valid():
            form.save()
            messages.success(request, _('Your order details has been updated.'))

    if order.event:
        update_event_context(order.event, context, show_sponsors=False)

    context['PAYPAL_ADDITIONAL_CHARGE'] = settings.PAYPAL_ADDITIONAL_CHARGE
    context['order'] = order
    update_order_status_context(order.status, context)

    return render(request=request, template_name='payments/order_payment.html', context=context)


class PayOrderByPaypal(TemplateView):

    @staticmethod
    def get_paypal_price(order):
        """ Calculate the total price for order when paid by paypal """
        additional_charge = order.left_to_pay * Decimal(settings.PAYPAL_ADDITIONAL_CHARGE) / Decimal('100')
        return order.left_to_pay + additional_charge

    @staticmethod
    def get_paypal_url(payment):
        """ Return the PayPal URL where the user can pay for the order """
        for link in payment.links:
            if link.method == 'REDIRECT':
                return str(link.href)

        raise Exception("REDIRECT url not found when creating payment {id}".format(id=payment.id))

    @staticmethod
    def pay(request, order):
        """ Create the payment and redirect to PayPal or back to the order with an error message """
        payment = paypalrestsdk.Payment({
            "intent": "sale",
            "payer": {"payment_method": "paypal"},
            "redirect_urls": {<commit_msg>Add 2place rounding for paypal payments (#238)<commit_after>from decimal import Decimal, ROUND_UP
import logging
import paypalrestsdk

from django.contrib import messages
from django.http.response import Http404
from django.shortcuts import render, get_object_or_404, redirect
from django.views.generic import TemplateView
from django.utils.translation import ugettext as _

from konfera.models import Order
from konfera.utils import update_order_status_context, update_event_context
from konfera.event.forms import ReceiptForm

from payments import settings
from payments.utils import _process_payment


logger = logging.getLogger(__name__)


paypalrestsdk.configure({
    "mode": settings.PAYPAL_MODE,
    "client_id": settings.PAYPAL_CLIENT_ID,
    "client_secret": settings.PAYPAL_CLIENT_SECRET,
})


def order_payment(request, order_uuid):
    order = get_object_or_404(Order, uuid=order_uuid)
    context = dict()

    if order.status == Order.PAID:
        return redirect('order_details', order_uuid=str(order.uuid))

    if order.status == Order.AWAITING:
        context['form'] = form = ReceiptForm(request.POST or None, instance=order.receipt_of)

        if form.is_valid():
            form.save()
            messages.success(request, _('Your order details has been updated.'))

    if order.event:
        update_event_context(order.event, context, show_sponsors=False)

    context['PAYPAL_ADDITIONAL_CHARGE'] = settings.PAYPAL_ADDITIONAL_CHARGE
    context['order'] = order
    update_order_status_context(order.status, context)

    return render(request=request, template_name='payments/order_payment.html', context=context)


class PayOrderByPaypal(TemplateView):

    @staticmethod
    def get_paypal_price(order):
        """ Calculate the total price for order when paid by paypal """
        additional_charge = order.left_to_pay * Decimal(settings.PAYPAL_ADDITIONAL_CHARGE) / Decimal('100')
        return (order.left_to_pay + additional_charge).quantize(Decimal('1.00'), rounding=ROUND_UP)

    @staticmethod
    def get_paypal_url(payment):
        """ Return the PayPal URL where the user can pay for the order """
        for link in payment.links:
            if link.method == 'REDIRECT':
                return str(link.href)

        raise Exception("REDIRECT url not found when creating payment {id}".format(id=payment.id))

    @staticmethod
    def pay(request, order):
        """ Create the payment and redirect to PayPal or back to the order with an error message """
        payment = paypalrestsdk.Payment({
            "intent": "sale",
            "payer": {"payment_method": "paypal"},
            "redirect_urls": {
{'fraction_non_alphanumeric': 0.09097859327217125, 'fraction_numerical': 0.005925076452599388, 'mean_word_length': 3.823041474654378, 'pattern_counts': {'":': 14, '<': 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': '4898422', 'n_tokens_mistral': 1555, 'n_tokens_neox': 1482, 'n_words': 392}