<commit_before>import uuid
import zlib
import commands
from pprint import pformat

from django.conf import settings
from django.core.urlresolvers import reverse
from django.http import HttpResponse


def generate_order_id():
    return uuid.uuid4().get_hex()


def get_redirect_url(request):
    url = "%s%s" % (settings.DOMAIN, reverse("dcavenue-callback"))
    if request.is_secure():
        return "https://%s" % (url, )
    else:
        return "http://%s" % (url, )


def checksum(redirect_url, amount, order_id):
    # "$MerchantId|$OrderId|$Amount|$redirectUrl|$WorkingKey"

    data = "%s|%s|%s|%s|%s" % (
        settings.DCAVENUE["MERCHANT_ID"], order_id, amount,
        redirect_url, settings.DCAVENUE["WORKING_KEY"]
    )

    csum = zlib.adler32(data, 1)

    if csum < 0:
        csum += 2 ** 32

    return csum


def enc_request(request, order_id):
    amount = request.REQUEST["Amount"]

    redirect_url = get_redirect_url(request)

    cca_request = "Merchant_Id=%s&Order_Id=%s&Redirect_Url=%s" % (
        settings.DCAVENUE["MERCHANT_ID"], order_id, redirect_url,
    )

    cca_request = "%s&%s" % (
        cca_request, "&".join(
            "%s=%s" % (k, v) for k, v in request.REQUEST.items()
        )
    )

    cca_request = "%s&Checksum=%s" % (
        cca_request, checksum(redirect_url, amount, order_id)
    )

    return commands.getoutput(
        '%s -jar %s %s "%s" enc' % (
            settings.DCAVENUE.get("JAVA", "java"), settings.DCAVENUE["JAR"],
            settings.DCAVENUE["WORKING_KEY"], cca_request
        )
    )


#noinspection PyTypeChecker
def verify_checksum(data):
    # "$MerchantId|$OrderId|$Amount|$AuthDesc|$WorkingKey";
    data = "%s|%s|%s|%s|%s" % (<commit_msg>minor<commit_after>import uuid
import zlib
import commands
from pprint import pformat

from django.conf import settings
from django.core.urlresolvers import reverse
from django.http import HttpResponse


def generate_order_id():
    return uuid.uuid4().get_hex()


def get_redirect_url(request):
    url = "%s%s" % (settings.DOMAIN, reverse("dcavenue-callback"))
    if request.is_secure():
        return "https://%s" % (url, )
    else:
        return "http://%s" % (url, )


def checksum(redirect_url, amount, order_id):
    # "$MerchantId|$OrderId|$Amount|$redirectUrl|$WorkingKey"

    data = "%s|%s|%s|%s|%s" % (
        settings.DCAVENUE["MERCHANT_ID"], order_id, amount,
        redirect_url, settings.DCAVENUE["WORKING_KEY"]
    )

    csum = zlib.adler32(data, 1)

    if csum < 0:
        csum += 2 ** 32

    return csum


def enc_request(request, order_id):
    amount = request.REQUEST["Amount"]

    redirect_url = get_redirect_url(request)

    g = request.GET.get

    cca_request = "&".join(
        [
            "Merchant_Id=%s" % settings.DCAVENUE["MERCHANT_ID"],
            "Amount=%s" % amount,
            "Order_Id=%s" % order_id,
            "Redirect_Url=%s" % redirect_url,
            "billing_cust_name=%s" % g("billing_cust_name", ""),
            "billing_cust_address=%s" % g("billing_cust_address", ""),
            "billing_cust_country=%s" % g("billing_cust_country", ""),
            "billing_cust_state=%s" % g("billing_cust_state", ""),
            "billing_cust_city=%s" % g("billing_cust_city", ""),
            "billing_zip_code=%s" % g("billing_zip_code", ""),
            "billing_cust_tel=%s" % g("billing_cust_tel", ""),
            "billing_cust_email=%s" % g("billing_cust_email", ""),
            "delivery_cust_name=%s" % g("delivery_cust_name", ""),
            "delivery_cust_address=%s" % g("delivery_cust_address", ""),
            "delivery_cust_country=%s" % g("delivery_cust_country", ""),
            "delivery_cust_state=%s" % g("delivery_cust_state", ""),
            "delivery_cust_city=%s" % g("delivery_cust_city", ""),
            "delivery_zip_code=%s" % g("delivery_zip_code", ""),
            "delivery_cust_tel=%s" % g("delivery_cust_tel", ""),
            "billing_cust_notes=%s" % g("billing_cust_notes", ""),
            "Checksum=%s" % checksum(redirect_url, amount, order_id)
        ]
    )

    return commands.getoutput(
        '%s -jar %s %s "%s" enc' % (
            settings.DCAVENUE.get("JAVA", "java"), settings.DCAVENUE["JAR"],
            settings.DCAVENUE["WORKING_KEY"], cca_request
        )
    )


#noinspection PyTypeChecker
def verify_checksum(data):
    # "$MerchantId|$OrderId|$Amount|$AuthDesc|$WorkingKey";
    data = "%s|%s|%s|%s|%s" % (
{'fraction_non_alphanumeric': 0.1613192730536235, 'fraction_numerical': 0.0035898586493156833, 'mean_word_length': 3.1011959521619135, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '5677123', 'n_tokens_mistral': 1663, 'n_tokens_neox': 1588, 'n_words': 253}