Example #1
0
 def _post_clean(self):
     kwargs = {
         'amount': self.amount,
         'event': self.order.event,
         'order': self.order,
     }
     try:
         if self.cleaned_data.get('save_card'):
             self.card = self.add_card(self.cleaned_data['token'])
             self._charge = stripe_charge(self.card.id, customer=self.customer, **kwargs)
         else:
             self._charge = stripe_charge(self.cleaned_data['token'], **kwargs)
             self.card = self._charge.card
     except stripe.error.CardError, e:
         self.add_error(None, e.message)
    def test_charge__customer(self):
        event = EventFactory(api_type=Event.TEST,
                             application_fee_percent=Decimal('2.5'))
        order = OrderFactory(event=event)
        self.assertTrue(event.stripe_connected())
        stripe_prep(Event.TEST)

        token = stripe.Token.create(
            card={
                "number": '4242424242424242',
                "exp_month": 12,
                "exp_year": 2050,
                "cvc": '123'
            },
        )
        customer = stripe.Customer.create(
            card=token,
        )
        card = customer.default_card
        charge = stripe_charge(
            card,
            amount=42.15,
            event=event,
            order=order,
            customer=customer
        )
        self.assertIsInstance(charge.balance_transaction, stripe.StripeObject)
        self.assertEqual(charge.balance_transaction.object, "balance_transaction")
        self.assertEqual(len(charge.balance_transaction.fee_details), 2)
        self.assertEqual(charge.metadata, {'order': order.code, 'event': event.name})

        txn = Transaction.from_stripe_charge(charge, api_type=event.api_type, event=event)
        # 42.15 * 0.025 = 1.05
        self.assertEqual(txn.application_fee, Decimal('1.05'))
        # (42.15 * 0.029) + 0.30 = 1.52
        self.assertEqual(txn.processing_fee, Decimal('1.52'))

        refund = stripe_refund(
            order=order,
            event=event,
            payment_id=txn.remote_id,
            amount=txn.amount
        )
        self.assertEqual(refund['refund'].metadata, {'order': order.code, 'event': event.name})

        refund_txn = Transaction.from_stripe_refund(refund, api_type=event.api_type, related_transaction=txn, event=event)
        self.assertEqual(refund_txn.amount, -1 * txn.amount)
        self.assertEqual(refund_txn.application_fee, -1 * txn.application_fee)
        self.assertEqual(refund_txn.processing_fee, -1 * txn.processing_fee)
Example #3
0
 def _post_clean(self):
     self.card = self.cleaned_data.get('card')
     if not self.card:
         return
     if self.api_type == Event.LIVE:
         customer_id = self.order.person.stripe_customer_id
     else:
         customer_id = self.order.person.stripe_test_customer_id
     try:
         self._charge = stripe_charge(
             self.card.stripe_card_id,
             amount=self.amount,
             event=self.order.event,
             order=self.order,
             customer=customer_id
         )
     except stripe.error.CardError, e:
         self.add_error(None, e.message)