def clean(self): super(DirectContributionForm, self).clean() if not self._errors and not self.campaign.is_free: # # ---- IMPORTANT ----- # # Ideally, we would verify the CC here and post the transaction # later in save(). However, there isn't an efficient way to merely # verify a credit card without actually posting the CC transaction; # All that the ``CreditCardField`` validates that the CC number # abides by the Luhn-checksum but this could still not be a # legitimately issued CC number. # # Therefore, post the transaction here and produce validation errors # based on what the transaction processor returns. # # The end result is that if this method succeeds, the transaction has # already been posted. # # Save ``UserProfile`` and ``User`` fields to the database # because ``TransactionData`` needs to extract the payer's # name and address from the user instance. UserProfileForm.save(self, commit=True) data = TransactionData() data.user = self.user_profile.user payment = Payment() data.payment = payment payment.invoice_num = make_invoice_num(self.campaign, data.user) payment.total_amount = '%s' % self.campaign.contribution_amount * self.qty payment.cc_num = self.cleaned_data['cc_num'] payment.expiration_date = self.cleaned_data['expiration_date'] payment.ccv = self.cleaned_data['ccv'] self.payment_processor = PaymentProcessor() extra = dict(x_description=self.campaign.title) self.payment_processor.prepare_data(data, extra_dict=extra) is_success, code, text = self.payment_processor.process() if not is_success: raise forms.ValidationError(escape(text)) return self.cleaned_data
if __name__ == "__main__": if not settings.DEV_MODE: raise Exception("Payment testing must be run in DEV_MODE") # Configure logging. if not logging.getLogger().handlers: import threading import logging.config threading.currentThread().setName('Pay') # Used by the logger logging.config.fileConfig('./logging.conf') _x_root = logging.getLogger() _x_root.setLevel(settings.DEBUG and logging.DEBUG or logging.INFO) _x = logging.getLogger('payment.processor.authorizedotnet') from django.contrib.auth.models import User from payment import TransactionData, Payment data = TransactionData() data.user = User.objects.get(username='******') data.payment = Payment( invoice_num='12345', total_amount='0.01', cc_num='4007000000027', expiration_date='0509', ccv='144') processor = PaymentProcessor() processor.prepare_data(data) results, reason_code, msg = processor.process() _x.info(results, "::", reason_code, "::", msg)
return self.result if __name__ == "__main__": if not settings.DEV_MODE: raise Exception("Payment testing must be run in DEV_MODE") # Configure logging. if not logging.getLogger().handlers: import threading import logging.config threading.currentThread().setName('Pay') # Used by the logger logging.config.fileConfig('./logging.conf') _x_root = logging.getLogger() _x_root.setLevel(settings.DEBUG and logging.DEBUG or logging.INFO) _x = logging.getLogger('payment.processor.authorizedotnet') from django.contrib.auth.models import User from payment import TransactionData, Payment data = TransactionData() data.user = User.objects.get(username='******') data.payment = Payment(invoice_num='12345', total_amount='0.01', cc_num='4007000000027', expiration_date='0509', ccv='144') processor = PaymentProcessor() processor.prepare_data(data) results, reason_code, msg = processor.process() _x.info(results, "::", reason_code, "::", msg)