def delete(self, request, *args, **kwargs): if not request.is_ajax(): raise Http404 try: creditcard = CreditCard.objects.get(is_saved=True, pk=kwargs['pk']) except CreditCard.DoesNotExist: # Count it a success. return self.success() user = request.user if creditcard.person_id != user.id: # Maybe also just redirect? raise Http404 creditcard.is_saved = False creditcard.save() customer = None stripe_prep(creditcard.api_type) if creditcard.api_type == CreditCard.LIVE and creditcard.person.stripe_customer_id: customer = stripe.Customer.retrieve(creditcard.person.stripe_customer_id) if creditcard.api_type == CreditCard.TEST and creditcard.person.stripe_test_customer_id: customer = stripe.Customer.retrieve(creditcard.person.stripe_test_customer_id) if customer is not None: customer.cards.retrieve(creditcard.stripe_card_id).delete() return self.success()
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)
def add_card(self, token): user = self.user stripe_prep(self.api_type) if self.api_type == Event.LIVE: customer_attr = 'stripe_customer_id' else: customer_attr = 'stripe_test_customer_id' customer_id = getattr(user, customer_attr) if not customer_id: customer = stripe.Customer.create( email=user.email, description=user.get_full_name(), metadata={ 'brambling_id': user.id }, ) setattr(user, customer_attr, customer.id) user.save() else: customer = stripe.Customer.retrieve(customer_id) self.customer = customer return customer.cards.create(card=token)