def test_dwolla_charge__user(self): event = EventFactory(api_type=Event.TEST, application_fee_percent=Decimal('2.5')) self.assertTrue(event.dwolla_connected()) dwolla_prep(Event.TEST) person = PersonFactory() order = OrderFactory(person=person, event=event) charge = dwolla_charge( sender=person, amount=42.15, order=order, event=event, pin=settings.DWOLLA_TEST_USER_PIN, source='Balance', ) self.assertIsInstance(charge, dict) self.assertEqual(charge["Type"], "money_received") self.assertEqual(len(charge['Fees']), 2) self.assertEqual(charge["Notes"], "Order {} for {}".format(order.code, event.name)) txn = Transaction.from_dwolla_charge(charge, event=event) # 42.15 * 0.025 = 1.05 self.assertEqual(Decimal(txn.application_fee), Decimal('1.05')) # 0.25 self.assertEqual(Decimal(txn.processing_fee), Decimal('0.25')) refund = dwolla_refund( order=order, event=event, payment_id=txn.remote_id, amount=txn.amount, pin=settings.DWOLLA_TEST_ORGANIZATION_PIN ) self.assertIsInstance(refund, dict) self.assertEqual(refund["Amount"], txn.amount) refund_info = transactions.info( tid=str(refund['TransactionId']), alternate_token=dwolla_get_token(event.organization, event.api_type) ) self.assertEqual(refund_info["Notes"], "Order {} for {}".format(order.code, event.name)) refund_txn = Transaction.from_dwolla_refund(refund, txn, event=event) self.assertEqual(refund_txn.amount, -1 * txn.amount) self.assertEqual(refund_txn.application_fee, 0) self.assertEqual(refund_txn.processing_fee, 0)
def _post_clean(self): if 'dwolla_pin' in self.cleaned_data and 'source' in self.cleaned_data: self._charge = None if self.amount > 0: try: self._charge = dwolla_charge( sender=self.user if self.user.is_authenticated() else self.order, amount=float(self.amount), order=self.order, event=self.order.event, pin=self.cleaned_data['dwolla_pin'], source=self.cleaned_data['source'] ) except DwollaAPIException as e: self.add_error(None, getattr(e, 'response', e.message))