def save(self, commit=True): if not self.campaign.is_free: is_success, code, text = self.payment_processor.result if not is_success: raise PaymentError( _('There was a system error in processing your payment.')) amount = D( self.payment_processor.transaction_data.payment.total_amount) else: amount = 0 UserProfileForm.save(self, commit=commit) if not self.campaign.is_free: tx_id = self.payment_processor.transaction_data.payment.invoice_num else: tx_id = make_invoice_num(self.campaign, self.user_profile.user) c = Contribution(campaign=self.campaign, contributor=self.user_profile.user, amount=amount, qty=self.qty, paid_on=datetime.now(), payment_mode='direct', transaction_id=tx_id) if commit: c.save() return c
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
def save(self, commit=True): if not self.campaign.is_free: is_success, code, text = self.payment_processor.result if not is_success: raise PaymentError(_('There was a system error in processing your payment.')) amount = D(self.payment_processor.transaction_data.payment.total_amount) else: amount = 0 UserProfileForm.save(self, commit=commit) if not self.campaign.is_free: tx_id = self.payment_processor.transaction_data.payment.invoice_num else: tx_id = make_invoice_num(self.campaign, self.user_profile.user) c = Contribution(campaign=self.campaign, contributor=self.user_profile.user, amount=amount, qty=self.qty, paid_on=datetime.now(), payment_mode='direct', transaction_id=tx_id) if commit: c.save() return c