<commit_before>###############################################################################
# Copyright (c) 2011, <NAME> <<EMAIL>>
# 
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
# 
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
############################################################################### 

import datetime
import logging
from lib import mollie

from django.utils.translation import ugettext as _
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response, get_object_or_404
from django.core.context_processors import csrf
from django.contrib.auth.decorators import login_required
from django.db.transaction import commit_on_success

from subscribe.models import Event, EventQuestion
from subscribe.forms import Registration, SubscribeForm, fill_subscription

from django.conf import settings


def _safe_string(s, max_len=32):
    safe = set("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ")
    return filter(lambda c: c in safe, s)[:max_len]

def register(request, slug):
    event = get_object_or_404(Event, slug=slug)
    now = datetime.datetime.now()
    if event.start_registration > now or event.end_registration < now:
        return HttpResponse(_("Inschrijving gesloten."))
    #SubscribeForm = SubscribeFormBuilder(event)
    if request.method == "POST":
        form = SubscribeForm(event, request.POST)
        if form.is_valid():
            # Store the data
            subscription = fill_subscription(form, event)
            if not subscription:
                # Error Filling subscription
                return HttpResponse(_("Error in saving form."))
            if subscription.event_option.price <= 0:
                subscription.payed = True
                subscription.send_confirmation_email()
                subscription.save()
                return HttpResponse(_("Dank voor uw inschrijving"))
            
            # You need to pay
            response = mollie.fetch(
                settings.MOLLIE['partner_id'],          # Partner id
                subscription.event_option.price,        # Amount
                form.cleaned_data["issuer"].safe_id(),  # Bank ID
                subscription.event_option.name,         # Description
                settings.MOLLIE['report_url'],          # Report url
                settings.MOLLIE['return_url']           # Return url
            )
            
            err = mollie.get_error(response)
            if err:
                return HttpResponse(_("Technische fout, probeer later opnieuw.") + "\n\n%d: %s" % (
                    err[0], err[1]
                ))
            
            subscription.trxid = response.order.transaction_id
            subscription.save()
            
            return HttpResponseRedirect(response.order.URL)

    else:
        form = SubscribeForm(event)
    c = {
        "event": event,
        "request": request,
        "form": form,
    }
    c.update(csrf(request))
    return render_to_response("form.html", c)

# called when the user returns from iDeal, is set as MERCHANTRETURNURL.
def returnPage(request):
    transaction_id = request.GET['transaction_id']
    
    try:
        subscription = Registration.objects.get(trxid=transaction_id)
    except:
        return HttpResponse(_("iDEAL error (onbekende inschrijving): Neem contact op met <EMAIL>. Controleer of uw betaling is afgeschreven alvorens de betaling opnieuw uit te voeren."))
    
    if subscription.status == "":
        check(request)
        subscription = Registration.objects.get(trxid=transaction_id)
        
    print "view::returnPage() : " + subscription.status
    
    if subscription.payed and subscription.status == "Success":
        return HttpResponse(_("Betaling geslaagd. Ter bevestiging is een e-mail verstuurd."))
    elif subscription.status == "Cancelled":
        return HttpResponse(_("Je betaling is geannuleerd."))
    elif subscription.status == "Failure":
        return HttpResponse(_("Je betaling is om onbekende reden niet gelukt. Er is geen geld afgeschreven. Neem contact op met <EMAIL> of probeer het nogmaals."))
    elif subscription.status == "Expired":
        return HttpResponse(_("Je betaling is geannuleerd."))
    elif subscription.status == "Open":
        return HttpResponse(_("Je betaling wordt nog verwerkt. Als je binnen 15 minuten geen bevestigingsmail ontvangen hebt, is er waarschijnlijk iets fout gegaan met de betaling. Neem in dat geval contact op met <EMAIL>."))
    else:
        return HttpResponse(_("Er is een fout opgetreden bij het verwerken van je iDEAL transactie. Neem contact op met <EMAIL> of probeer het later nogmaals. Controleer of je betaling is afgeschreven alvorens de betaling opnieuw uit te voeren."))

         
def check(request):
    transaction_id = request.GET['transaction_id']
    
    response = mollie.check(settings.MOLLIE['partner_id'], transaction_id)  

    try:
        subscription = Registration.objects.get(trxid=transaction_id)
    except:
        logging.error("views::check() - cannot find subscription with transaction id: " + str(transaction_id))
        return HttpResponse(_("NOT OK"))
        
    if response.order.status == "CheckedBefore":
        return HttpResponse(_("OK"))
    elif response.order.payed and response.order.status == "Success": # Mollie gives payed=true and status=Success only once
        subscription.payed = True
        subscription.status = "Success"
        subscription.send_confirmation_email()
        subscription.save()
        return HttpResponse(_("OK"))
    else:
        subscription.payed = False
        subscription.status = response.order.status
        subscription.save() 
    
    return HttpResponse(_("OK"))
    
@login_required<commit_msg>Improved user message on Open transaction status.<commit_after>###############################################################################
# Copyright (c) 2011, <NAME> <<EMAIL>>
# 
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
# 
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
############################################################################### 

import datetime
import logging
from lib import mollie

from django.utils.translation import ugettext as _
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response, get_object_or_404
from django.core.context_processors import csrf
from django.contrib.auth.decorators import login_required
from django.db.transaction import commit_on_success

from subscribe.models import Event, EventQuestion
from subscribe.forms import Registration, SubscribeForm, fill_subscription

from django.conf import settings


def _safe_string(s, max_len=32):
    safe = set("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ")
    return filter(lambda c: c in safe, s)[:max_len]

def register(request, slug):
    event = get_object_or_404(Event, slug=slug)
    now = datetime.datetime.now()
    if event.start_registration > now or event.end_registration < now:
        return HttpResponse(_("Inschrijving gesloten."))
    #SubscribeForm = SubscribeFormBuilder(event)
    if request.method == "POST":
        form = SubscribeForm(event, request.POST)
        if form.is_valid():
            # Store the data
            subscription = fill_subscription(form, event)
            if not subscription:
                # Error Filling subscription
                return HttpResponse(_("Error in saving form."))
            if subscription.event_option.price <= 0:
                subscription.payed = True
                subscription.send_confirmation_email()
                subscription.save()
                return HttpResponse(_("Dank voor uw inschrijving"))
            
            # You need to pay
            response = mollie.fetch(
                settings.MOLLIE['partner_id'],          # Partner id
                subscription.event_option.price,        # Amount
                form.cleaned_data["issuer"].safe_id(),  # Bank ID
                subscription.event_option.name,         # Description
                settings.MOLLIE['report_url'],          # Report url
                settings.MOLLIE['return_url']           # Return url
            )
            
            err = mollie.get_error(response)
            if err:
                return HttpResponse(_("Technische fout, probeer later opnieuw.") + "\n\n%d: %s" % (
                    err[0], err[1]
                ))
            
            subscription.trxid = response.order.transaction_id
            subscription.save()
            
            return HttpResponseRedirect(response.order.URL)

    else:
        form = SubscribeForm(event)
    c = {
        "event": event,
        "request": request,
        "form": form,
    }
    c.update(csrf(request))
    return render_to_response("form.html", c)

# called when the user returns from iDeal, is set as MERCHANTRETURNURL.
def returnPage(request):
    transaction_id = request.GET['transaction_id']
    
    try:
        subscription = Registration.objects.get(trxid=transaction_id)
    except:
        return HttpResponse(_("iDEAL error (onbekende inschrijving): Neem contact op met <EMAIL>. Controleer of uw betaling is afgeschreven alvorens de betaling opnieuw uit te voeren."))
    
    if subscription.status == "":
        check(request)
        subscription = Registration.objects.get(trxid=transaction_id)
        
    print "view::returnPage() : " + subscription.status
    
    if subscription.payed and subscription.status == "Success":
        return HttpResponse(_("Betaling geslaagd. Ter bevestiging is een e-mail verstuurd."))
    elif subscription.status == "Cancelled":
        return HttpResponse(_("Je betaling is geannuleerd."))
    elif subscription.status == "Failure":
        return HttpResponse(_("Je betaling is om onbekende reden niet gelukt. Er is geen geld afgeschreven. Neem contact op met <EMAIL> of probeer het nogmaals."))
    elif subscription.status == "Expired":
        return HttpResponse(_("Je betaling is geannuleerd."))
    elif subscription.status == "Open":
        return HttpResponse(_("Je betaling staat geregistreerd in ons systeem, maar wordt nog verwerkt door onze bank. Als je binnen een uur geen bevestigingsmail ontvangt, is er mogelijk iets fout gegaan met de betaling. Neem in dat geval contact op met <EMAIL>."))
    else:
        return HttpResponse(_("Er is een fout opgetreden bij het verwerken van je iDEAL transactie. Neem contact op met <EMAIL> of probeer het later nogmaals. Controleer of je betaling is afgeschreven alvorens de betaling opnieuw uit te voeren."))

         
def check(request):
    transaction_id = request.GET['transaction_id']
    
    response = mollie.check(settings.MOLLIE['partner_id'], transaction_id)  

    try:
        subscription = Registration.objects.get(trxid=transaction_id)
    except:
        logging.error("views::check() - cannot find subscription with transaction id: " + str(transaction_id))
        return HttpResponse(_("NOT OK"))
        
    if response.order.status == "CheckedBefore":
        return HttpResponse(_("OK"))
    elif response.order.payed and response.order.status == "Success": # Mollie gives payed=true and status=Success only once
        subscription.payed = True
        subscription.status = "Success"
        subscription.send_confirmation_email()
        subscription.save()
        return HttpResponse(_("OK"))
    else:
        subscription.payed = False
        subscription.status = response.order.status
        subscription.save() 
    
    return HttpResponse(_("OK"))
    
@login_required
{'fraction_non_alphanumeric': 0.10543452980851228, 'fraction_numerical': 0.004031320257384293, 'mean_word_length': 2.9643515673017826, 'pattern_counts': {'":': 24, '<': 21, '<?xml version=': 0, '>': 19, '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': '68078', 'n_tokens_mistral': 3542, 'n_tokens_neox': 3421, 'n_words': 1063}