<commit_before>#-*- coding: utf-8 -*-
from datetime import datetime
from suds.client import Client
import hashlib
import logging
import traceback
from django.conf import settings
from django.conf.urls.defaults import patterns, url
from django.contrib.sites.models import get_current_site
from django.core.urlresolvers import reverse
from django.core.exceptions import SuspiciousOperation
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponse, HttpResponseRedirect, \
    HttpResponseBadRequest, HttpResponseServerError
from django.views.decorators.csrf import csrf_exempt
from forms import SessionIPaymentForm, SensibleIPaymentForm, ConfirmationForm
from models import Confirmation

class OffsiteIPaymentBackend(object):
    '''
    Glue code to let django-SHOP talk to the ipayment backend.
    '''
    backend_name = "IPayment"
    url_namespace = "ipayment"
    ALLOWED_CONFIRMERS = ('192.168.127.12', '192.168.3.11', '192.168.127.12')
    
    #===========================================================================
    # Defined by the backends API
    #===========================================================================
    
    def __init__(self, shop):
        self.shop = shop
        self.logger = logging.getLogger(__name__)
        assert type(settings.IPAYMENT).__name__=='dict', \
            "You need to configure an IPAYMENT dictionary in settings"
        assert settings.IPAYMENT['useSessionId'] or \
            settings.IPAYMENT.has_key('securityKey') and len(settings.IPAYMENT['securityKey'])>=6, \
            "In IPAYMENT, useSessionId must be True, or a securityKey must contain at least 6 characters" 
        
    def get_urls(self):
        urlpatterns = patterns('',
            url(r'^$', self.view_that_asks_for_money, name='ipayment'),
            url(r'^hidden$', self.payment_was_successful, name='ipayment_hidden'),
            url(r'^success$', self.ipayment_return_success_view, name='ipayment_success'),
            url(r'^error$', self.view_that_asks_for_money, name='ipayment_error'),
        )
        return urlpatterns
    
    #===========================================================================
    # Views
    #===========================================================================
    
    def view_that_asks_for_money(self, request):
        '''
        Show this form to ask for the customers credit cards content. This content MUST never be
        posted to the local server, because we are not allowed to "see" the credit card numbers 
        without a PCI DSS certification. Instead these numbers are sent directly to the IPayment server.
        '''
        if request.method != 'GET':
            return HttpResponseBadRequest()

        order = self.shop.get_order(request)
        ipaymentData = {
            'silent': 1,
            'shopper_id': self.shop.get_order_unique_id(order),
            'advanced_strict_id_check': 1,
            'invoice_text': settings.IPAYMENT['invoiceText'] % self.shop.get_order_short_name(order),
            'error_lang': 'en', # TODO: determine this value from language settings
        }
        extra = { 'accountId': settings.IPAYMENT['accountId'], 'isError': False }
        if request.GET.has_key('ret_errorcode') and int(request.GET['ret_errorcode'])>0:
            extra['isError'] = True
            extra['errorMessage'] = request.GET['ret_errormsg']
            ipaymentData['addr_name'] = request.GET['addr_name']

        # Fill the form content
        if settings.IPAYMENT['useSessionId']:
            # sensible data is send to IPayment in a separate SOAP call
            ipaymentData['ipayment_session_id'] = self.getSessionID(request, order)
            form = SessionIPaymentForm(ipaymentData)
        else:
            # sensible data is send using this form, but signed to detect manipulation attempts
            ipaymentData.update(self.getHiddenContext())
            ipaymentData['trx_securityhash'] = self.calcTrxSecurityHash(ipaymentData)
            form = SensibleIPaymentForm(ipaymentData)
        rc = RequestContext(request, { 'form': form, 'extra': extra, })
        return render_to_response("payment.html", rc)

    def getHiddenContext(self, request, order):<commit_msg>Moved post data initialisation into method getHiddenContext<commit_after>#-*- coding: utf-8 -*-
from datetime import datetime
from suds.client import Client
import hashlib
import logging
import traceback
from django.conf import settings
from django.conf.urls.defaults import patterns, url
from django.contrib.sites.models import get_current_site
from django.core.urlresolvers import reverse
from django.core.exceptions import SuspiciousOperation
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponse, HttpResponseRedirect, \
    HttpResponseBadRequest, HttpResponseServerError
from django.views.decorators.csrf import csrf_exempt
from forms import SessionIPaymentForm, SensibleIPaymentForm, ConfirmationForm
from models import Confirmation

class OffsiteIPaymentBackend(object):
    '''
    Glue code to let django-SHOP talk to the ipayment backend.
    '''
    backend_name = "IPayment"
    url_namespace = "ipayment"
    ALLOWED_CONFIRMERS = ('192.168.127.12', '192.168.3.11', '192.168.127.12')
    
    #===========================================================================
    # Defined by the backends API
    #===========================================================================
    
    def __init__(self, shop):
        self.shop = shop
        self.logger = logging.getLogger(__name__)
        assert type(settings.IPAYMENT).__name__=='dict', \
            "You need to configure an IPAYMENT dictionary in settings"
        assert settings.IPAYMENT['useSessionId'] or \
            settings.IPAYMENT.has_key('securityKey') and len(settings.IPAYMENT['securityKey'])>=6, \
            "In IPAYMENT, useSessionId must be True, or a securityKey must contain at least 6 characters" 
        
    def get_urls(self):
        urlpatterns = patterns('',
            url(r'^$', self.view_that_asks_for_money, name='ipayment'),
            url(r'^hidden$', self.payment_was_successful, name='ipayment_hidden'),
            url(r'^success$', self.ipayment_return_success_view, name='ipayment_success'),
            url(r'^error$', self.view_that_asks_for_money, name='ipayment_error'),
        )
        return urlpatterns
    
    #===========================================================================
    # Views
    #===========================================================================
    
    def view_that_asks_for_money(self, request):
        '''
        Show this form to ask for the customers credit cards content. This content MUST never be
        posted to the local server, because we are not allowed to "see" the credit card numbers 
        without a PCI DSS certification. Instead these numbers are sent directly to the IPayment server.
        '''
        if request.method != 'GET':
            return HttpResponseBadRequest()

        order = self.shop.get_order(request)
        ipaymentData = self.getHiddenContext(order)
        extra = { 'accountId': settings.IPAYMENT['accountId'], 'isError': False }
        if request.GET.has_key('ret_errorcode') and int(request.GET['ret_errorcode'])>0:
            extra['isError'] = True
            extra['errorMessage'] = request.GET['ret_errormsg']
            ipaymentData['addr_name'] = request.GET['addr_name']

        # Fill the form content
        if settings.IPAYMENT['useSessionId']:
            # sensible data is send to IPayment in a separate SOAP call
            ipaymentData['ipayment_session_id'] = self.getSessionID(request, order)
            form = SessionIPaymentForm(ipaymentData)
        else:
            # sensible data is send using this form, but signed to detect manipulation attempts
            ipaymentData.update(self.getSessionlessContext(request, order))
            ipaymentData['trx_securityhash'] = self.calcTrxSecurityHash(ipaymentData)
            form = SensibleIPaymentForm(ipaymentData)
        rc = RequestContext(request, { 'form': form, 'extra': extra, })
        return render_to_response("payment.html", rc)

    def getHiddenContext(self, order):
        return {
            'silent': 1,
            'shopper_id': self.shop.get_order_unique_id(order),
            'advanced_strict_id_check': 1,
            'invoice_text': settings.IPAYMENT['invoiceText'] % self.shop.get_order_short_name(order),
            'error_lang': 'en', # TODO: determine this value from language settings
        }

    def getSessionlessContext(self, request, order):
{'fraction_non_alphanumeric': 0.1564284083124001, 'fraction_numerical': 0.008449417675268326, 'mean_word_length': 3.4259727134916624, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 6, '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': '488973', 'n_tokens_mistral': 2434, 'n_tokens_neox': 2312, 'n_words': 601}