<commit_before>"""
Utility methods to aid usage of Skipjack.

Included utility functions:
    create_transaction(data)

    get_transaction_status(order_number, transaction_id=None)

    change_transaction_status(transaction_id, desired_status, amount=None)

"""
from django.conf import settings

from skipjack.helpers import PaymentHelper, StatusHelper, ChangeStatusHelper
from skipjack.models import Transaction, Status, StatusChange
from skipjack.signals import payment_was_successful, payment_was_flagged


DEFAULT_LIST = [
    ('SerialNumber', settings.SKIPJACK_SERIAL_NUMBER),
    ('DeveloperSerialNumber', settings.SKIPJACK_DEVELOPER_SERIAL_NUMBER)
]

SZ_DEFAULT_LIST = [
    ('szSerialNumber', settings.SKIPJACK_SERIAL_NUMBER),
    ('szDeveloperSerialNumber', settings.SKIPJACK_DEVELOPER_SERIAL_NUMBER)
]


def create_transaction(data):
    """
    Creates a Transaction in the database based on the returned data from
    Skipjack to an authorize request.
    Sends signals payment_was_successful or payment_was_flagged.
    
    """
    helper = PaymentHelper(defaults=DEFAULT_LIST)
    response_dict = helper.get_response(data)
    response_dict['is_live'] = not settings.SKIPJACK_DEBUG
    response = Transaction.objects.create_from_dict(response_dict)
    if response.is_approved:
        payment_was_successful.send(sender=response)
    else:
        payment_was_flagged.send(sender=response)
    return response


def get_transaction_status(order_number, transaction_id=None):
    """
    Returns a textual description of either the latest transaction associated
    with the request, or the status of the specified transaction_id.
    
    order_number is required because that's what Skipjack wants. You can't send
    the transaction_id to Skipjack for the transaction status method.
    
    We return two critical parts of the Skipjack response: a numeric code
    indicating the status, and a textual string interpreting the numeric code.
    
    Naturally, the SerialNumber and DeveloperSerialNumber fields are prefixed
    with 'sz' making them totally inconsistent with the authorize request above.
    
    """
    helper = StatusHelper(defaults=SZ_DEFAULT_LIST)
    response_dict = helper.get_response(order_number,
                                        transaction_id=transaction_id)
    response = Status(**response_dict)
    return response


def change_transaction_status(transaction_id, desired_status, amount=None):
    """
    Changes a specified transaction to the desired status if Skipjack can.
    
    Returns a textual description of the response from Skipjack.
    
    """
    helper = ChangeStatusHelper(defaults=SZ_DEFAULT_LIST)
    data = [('szTransactionId', transaction_id),
            ('szDesiredStatus', desired_status)]
    if amount:
        data.append(('szAmount', str(amount)))
    response_dict = helper.get_response(data)
    response = StatusChange(**response_dict)
    return response<commit_msg>Adding szForceSettlement=1 when we settle a transaction.<commit_after>"""
Utility methods to aid usage of Skipjack.

Included utility functions:
    create_transaction(data)

    get_transaction_status(order_number, transaction_id=None)

    change_transaction_status(transaction_id, desired_status, amount=None)

"""
from django.conf import settings

from skipjack.helpers import PaymentHelper, StatusHelper, ChangeStatusHelper
from skipjack.models import Transaction, Status, StatusChange
from skipjack.signals import payment_was_successful, payment_was_flagged


DEFAULT_LIST = [
    ('SerialNumber', settings.SKIPJACK_SERIAL_NUMBER),
    ('DeveloperSerialNumber', settings.SKIPJACK_DEVELOPER_SERIAL_NUMBER)
]

SZ_DEFAULT_LIST = [
    ('szSerialNumber', settings.SKIPJACK_SERIAL_NUMBER),
    ('szDeveloperSerialNumber', settings.SKIPJACK_DEVELOPER_SERIAL_NUMBER)
]


def create_transaction(data):
    """
    Creates a Transaction in the database based on the returned data from
    Skipjack to an authorize request.
    Sends signals payment_was_successful or payment_was_flagged.
    
    """
    helper = PaymentHelper(defaults=DEFAULT_LIST)
    response_dict = helper.get_response(data)
    response_dict['is_live'] = not settings.SKIPJACK_DEBUG
    response = Transaction.objects.create_from_dict(response_dict)
    if response.is_approved:
        payment_was_successful.send(sender=response)
    else:
        payment_was_flagged.send(sender=response)
    return response


def get_transaction_status(order_number, transaction_id=None):
    """
    Returns a textual description of either the latest transaction associated
    with the request, or the status of the specified transaction_id.
    
    order_number is required because that's what Skipjack wants. You can't send
    the transaction_id to Skipjack for the transaction status method.
    
    We return two critical parts of the Skipjack response: a numeric code
    indicating the status, and a textual string interpreting the numeric code.
    
    Naturally, the SerialNumber and DeveloperSerialNumber fields are prefixed
    with 'sz' making them totally inconsistent with the authorize request above.
    
    """
    helper = StatusHelper(defaults=SZ_DEFAULT_LIST)
    response_dict = helper.get_response(order_number,
                                        transaction_id=transaction_id)
    response = Status(**response_dict)
    return response


def change_transaction_status(transaction_id, desired_status, amount=None):
    """
    Changes a specified transaction to the desired status if Skipjack can.
    
    Returns a textual description of the response from Skipjack.
    
    """
    helper = ChangeStatusHelper(defaults=SZ_DEFAULT_LIST)
    data = [('szTransactionId', transaction_id),
            ('szDesiredStatus', desired_status)]
    if amount:
        data.append(('szAmount', str(amount)))
    if desired_status.lower() == 'settle':
        data.append(('szForceSettlement', '1'))
    response_dict = helper.get_response(data)
    response = StatusChange(**response_dict)
    return response
{'fraction_non_alphanumeric': 0.06779100529100529, 'fraction_numerical': 0.00033068783068783067, 'mean_word_length': 4.17008547008547, '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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7345666', 'n_tokens_mistral': 1638, 'n_tokens_neox': 1588, 'n_words': 456}