Example #1
0
def charge_stripe_customer(user, amount_cents, description):
    stripe.api_key = settings.STRIPE_API_KEY
    stripe_customer = None
    try:
        stripe_customer = user.stripecustomer
    except StripeCustomer.DoesNotExist:
        logger.error("Stripe customer does not exist", kwargs={
            'user_id': user.pk,
            'amount_cents': amount_cents,
            'description': description
        })
        return False

    charge = stripe.Charge.create(
        amount=amount_cents,
        currency="aud",
        customer=stripe_customer.customer_id,
        description=description,
        capture=True        # Charge immediately
    )
    if charge['piad'] == True:
        Transaction.create(user, amount_cents, description)
    else:
        raise CardChargeFailedException({
            "stripe_customer_id": stripe_customer.customer_id,
            "amount_cents": amount_cents,
            "description": description
        })

    return True
Example #2
0
def _make_move(wallet_from: Wallet, wallet_to: Wallet, amount: float, description=None) -> Transaction:
    try:
        with t.atomic():
            _check_moving(wallet_from, amount)

            to_amount = amount
            if wallet_from.currency != wallet_to.currency:
                currency_rate = CurrencyRate.objects \
                    .filter(currency_from=wallet_from.currency, currency_to=wallet_to.currency) \
                    .first()
                if not currency_rate:
                    currency_rate = force_load_currency_rate(wallet_from.currency, wallet_to.currency)
                to_amount = amount * currency_rate.rate

            transaction = Transaction(wallet_from=wallet_from, wallet_to=wallet_to, amount=amount,
                                      description=description)

            wallet_from.amount -= amount
            wallet_to.amount += to_amount

            wallet_from.save()
            wallet_to.save()
            transaction.save()

            return transaction
    except DatabaseError as e:
        log.exception(e)
        t.rollback()
Example #3
0
    def post(self, request, *args, **kwargs):
        # TODO: add credit card processing

        user = request.user
        cart = user.get_cart()

        if cart.num_items() == 0:
            data = {'success': False, 'errMsg': 'Your cart is empty'}
            return JsonResponse(data=data)

        processed_items = []
        for item in cart.all_items():
            # TODO: how to handle restricted?
            transaction = Transaction(
                buyer=request.user,
                seller=item.product.seller,
                product=item.product,
                price=item.product.get_price * item.quantity,
                quantity=item.quantity,
            )
            transaction.save()

            item_count = ItemCount(
                buyer=request.user,
                product=item.product,
                order=item.quantity,
                quantity=item.quantity,
            )
            item_count.save()

            try:
                self._sendAdminEmail(user, item.product.seller,
                                     item.product.title, item.quantity,
                                     item.intention, item.attachment,
                                     item.product.restricted_active, request,
                                     item.product)
                processed_items.append(item)
            except:
                # TODO: log error, recover, try again?
                pass

        links = []
        for item in processed_items:
            download_link = item.product.get_download()
            preview_link = download_link + "?preview=True"
            link = {
                "download": download_link,
                "preview": preview_link,
            }
            links.append(link)
            item.delete()

        data = {'success': True, 'links': links}

        return JsonResponse(data=data)
Example #4
0
    def test_cron_transaction_repair(self):
        value = Currency.us_dollars("99999999.99123459", "30.12342")

        account1 = self.user1_account
        account2 = self.user2_account

        transaction = Transaction({'source' : account1._id, 'dest' : account2._id, 'amount' : value})
        transaction.save()

        amount_rub = transaction.amount.db_value()
        Transaction.objects.update({'_id': transaction._id}, {'$set': {'state': Transaction.STATE_PENDING}})
        Account.objects.update({'_id': account1._id, 'pending_transactions': {'$ne': transaction._id}},
                {'$inc': {'balance': -amount_rub}, '$push': {'pending_transactions': transaction._id}})

        transaction2 = Transaction({'source' : account1._id, 'dest' : account2._id, 'amount' : -value})
        transaction2.save()

        amount_rub2 = transaction2.amount.db_value()
        Transaction.objects.update({'_id': transaction2._id}, {'$set': {'state': Transaction.STATE_PENDING}})
        Account.objects.update({'_id': account1._id, 'pending_transactions': {'$ne': transaction2._id}},
                {'$inc': {'balance': -amount_rub2}, '$push': {'pending_transactions': transaction2._id}})

        bad_transactions = Transaction.objects.get({'state' : {'$nin' : [Transaction.STATE_INITIAL, Transaction.STATE_DONE]}})
        self.assertTrue(len(bad_transactions) > 0)

        task = RepairTransactionsTask()
        task.execute()

        bad_transactions = Transaction.objects.get({'state' : {'$nin' : [Transaction.STATE_INITIAL, Transaction.STATE_DONE]}})
        self.assertEquals(len(bad_transactions), 0)
Example #5
0
    def test_complete_after_committed3(self):
        value = Currency.us_dollars("99999999.99123459", "30.12342")

        account1 = self.user1_account
        account2 = self.user2_account

        transaction = Transaction({'source' : account1._id, 'dest' : account2._id, 'amount' : value})
        transaction.save()

        # partial transaction.apply() - up to committed
        amount_rub = transaction.amount.db_value()
        Transaction.objects.update({'_id': transaction._id}, {'$set': {'state': Transaction.STATE_PENDING}})
        Account.objects.update({'_id': account1._id, 'pending_transactions': {'$ne': transaction._id}},
                {'$inc': {'balance': -amount_rub}, '$push': {'pending_transactions': transaction._id}})
        Account.objects.update({'_id': account2._id, 'pending_transactions': {'$ne': transaction._id}},
                {'$inc': {'balance': amount_rub}, '$push': {'pending_transactions': transaction._id}})
        Transaction.objects.update({'_id': transaction._id}, {'$set': {'state': Transaction.STATE_COMMITTED}})
        Account.objects.update({'_id': account1._id},
                {'$pull': {'pending_transactions': transaction._id}})
        Account.objects.update({'_id': account2._id},
                {'$pull': {'pending_transactions': transaction._id}})

        transaction = Transaction.objects.get_one({'_id' : transaction._id})

        transaction.complete()

        account1 = Account.objects.get_one({'_id' : account1._id})
        account2 = Account.objects.get_one({'_id' : account2._id})
        trans = Transaction.objects.get_one({'_id' : transaction._id})

        self.assertEqual(trans.state, Transaction.STATE_DONE)
        self.assertEqual(account1.balance.amount, -value.amount * value.rate)
        self.assertEqual(account2.balance.amount, value.amount * value.rate)
Example #6
0
    def test_money_transfer(self):
        value = Currency.russian_roubles("123.43")
        account1 = self.user1_account
        account2 = self.user2_account

        transaction = Transaction({'source' : account1._id, 'dest' : account2._id, 'amount' : value})
        transaction.save()
        transaction.apply()

        account1 = Account.objects.get_one({'_id' : account1._id})
        account2 = Account.objects.get_one({'_id' : account2._id})

        self.assertEqual(account1.balance, -value)
        self.assertEqual(account2.balance, value)
Example #7
0
    def test_complete_after_pending1(self):
        value = Currency.us_dollars("99999999.99123459", "30.12342")

        account1 = self.user1_account
        account2 = self.user2_account

        transaction = Transaction({'source' : account1._id, 'dest' : account2._id, 'amount' : value})
        transaction.save()

        # partial transaction.apply() - up to pending
        Transaction.objects.update({'_id': transaction._id}, {'$set': {'state': Transaction.STATE_PENDING}})
        transaction = Transaction.objects.get_one({'_id' : transaction._id})

        transaction.complete()

        account1 = Account.objects.get_one({'_id' : account1._id})
        account2 = Account.objects.get_one({'_id' : account2._id})
        trans = Transaction.objects.get_one({'_id' : transaction._id})

        self.assertEqual(trans.state, Transaction.STATE_DONE)
        self.assertEqual(account1.balance.amount, -value.amount * value.rate)
        self.assertEqual(account2.balance.amount, value.amount * value.rate)
Example #8
0
    def test_floats_usd(self):
        value = Currency.us_dollars("99999999.99123459", "30.12342")

        account1 = self.user1_account
        account2 = self.user2_account

        transaction = Transaction({'source' : account1._id, 'dest' : account2._id, 'amount' : value})
        transaction.save()
        transaction.apply()

        account1 = Account.objects.get_one({'_id' : account1._id})
        account2 = Account.objects.get_one({'_id' : account2._id})

        self.assertEqual(account1.balance.amount, -value.amount * value.rate)
        self.assertEqual(account2.balance.amount, value.amount * value.rate)

        new_value = Currency.russian_roubles("-10.99122459")
        value += new_value

        transaction = Transaction({'source' : account1._id, 'dest' : account2._id, 'amount' : new_value})
        transaction.save()
        transaction.apply()

        account1 = Account.objects.get_one({'_id' : account1._id})
        account2 = Account.objects.get_one({'_id' : account2._id})

        self.assertEqual(account1.balance, -value)
        self.assertEqual(account2.balance, value)