<commit_before>from ..forms import PaymentForm
from authorizenet.fields import CreditCardExpiryField
from django import forms
from django.conf import settings
from django.core import validators
from django.utils.translation import ugettext_lazy as _
from payment.fields import CreditCardNumberField
from re import match

CVV_VALIDATOR = validators.RegexValidator('^[0-9]{1,4}$',
                                          _('Enter a valid security number.'))


class PaymentForm(PaymentForm):

    VALID_TYPES = ['visa', 'mastercard', 'discover', 'amex']
    name = forms.CharField(label=_('Name on Credit Card'), max_length=128)
    number = CreditCardNumberField(label=_('Card Number'), max_length=32,
                                   required=True)
    expiration = CreditCardExpiryField()
    cvv2 = forms.CharField(validators=[CVV_VALIDATOR], required=False,
                           label=_('CVV2 Security Number'), max_length=4)
    default_error_messages = {
         'invalid_type': _(u'We accept only %(valid_types)s'),
     }

    def __init__(self, *args, **kwargs):
        super(PaymentForm, self).__init__(hidde_inputs=False, *args, **kwargs)

    def get_credit_card_type(self, number):
        if match('^4[0-9]{12}(?:[0-9]{3})?$', number):
            return 'visa'
        if match('^5[1-5][0-9]{14}$', number):
            return 'mastercard'
        if match('^6(?:011|5[0-9]{2})[0-9]{12}$', number):
            return 'discover'
        if match('^3[47][0-9]{13}$', number):
            return 'amex'

    def clean(self):
        cleaned_data = super(PaymentForm, self).clean()
        cleaned_data['next'] = self.payment.success_url
        if 'number' in cleaned_data.keys():
            type = self.get_credit_card_type(cleaned_data['number'])
            if type not in self.VALID_TYPES:
                message = (self.default_error_messages['invalid_type'] %
                           {'valid_types':', '.join(self.VALID_TYPES)})
                self._errors['number'] = self.error_class([message])
            else:
                cleaned_data['type'] = type
        if not self.errors:
            if not self.payment.transaction_id:
                response = self.provider.get_payment_response(cleaned_data)
                data = response.json()
                if response.ok:
                    self.payment.transaction_id = data['id']
                    self.payment.status = 'confirmed'
                else:
                    errors = [error['issue'] for error in data['details']]
                    self._errors['__all__'] = self.error_class(errors)
                    self.payment.status = 'error'
            self.payment.save()<commit_msg>Use status_changed in Paypal card<commit_after>from ..forms import PaymentForm
from authorizenet.fields import CreditCardExpiryField
from django import forms
from django.conf import settings
from django.core import validators
from django.utils.translation import ugettext_lazy as _
from payment.fields import CreditCardNumberField
from re import match

CVV_VALIDATOR = validators.RegexValidator('^[0-9]{1,4}$',
                                          _('Enter a valid security number.'))


class PaymentForm(PaymentForm):

    VALID_TYPES = ['visa', 'mastercard', 'discover', 'amex']
    name = forms.CharField(label=_('Name on Credit Card'), max_length=128)
    number = CreditCardNumberField(label=_('Card Number'), max_length=32,
                                   required=True)
    expiration = CreditCardExpiryField()
    cvv2 = forms.CharField(validators=[CVV_VALIDATOR], required=False,
                           label=_('CVV2 Security Number'), max_length=4)
    default_error_messages = {
         'invalid_type': _(u'We accept only %(valid_types)s'),
     }

    def __init__(self, *args, **kwargs):
        super(PaymentForm, self).__init__(hidde_inputs=False, *args, **kwargs)

    def get_credit_card_type(self, number):
        if match('^4[0-9]{12}(?:[0-9]{3})?$', number):
            return 'visa'
        if match('^5[1-5][0-9]{14}$', number):
            return 'mastercard'
        if match('^6(?:011|5[0-9]{2})[0-9]{12}$', number):
            return 'discover'
        if match('^3[47][0-9]{13}$', number):
            return 'amex'

    def clean(self):
        cleaned_data = super(PaymentForm, self).clean()
        cleaned_data['next'] = self.payment.success_url
        if 'number' in cleaned_data.keys():
            type = self.get_credit_card_type(cleaned_data['number'])
            if type not in self.VALID_TYPES:
                message = (self.default_error_messages['invalid_type'] %
                           {'valid_types':', '.join(self.VALID_TYPES)})
                self._errors['number'] = self.error_class([message])
            else:
                cleaned_data['type'] = type
        if not self.errors:
            if not self.payment.transaction_id:
                response = self.provider.get_payment_response(cleaned_data)
                data = response.json()
                if response.ok:
                    self.payment.transaction_id = data['id']
                    self.payment.status_changed('confirmed')
                else:
                    errors = [error['issue'] for error in data['details']]
                    self._errors['__all__'] = self.error_class(errors)
                    self.payment.status_changed('error')
            self.payment.save()
{'fraction_non_alphanumeric': 0.12689044632976762, 'fraction_numerical': 0.016967908520841016, 'mean_word_length': 2.341343191620456, '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': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4866207', 'n_tokens_mistral': 1580, 'n_tokens_neox': 1531, 'n_words': 319}