Пример #1
0
def redeem(order_number, user, allocations):
    """
    Settle payment for the passed set of account allocations

    Will raise UnableToTakePayment if any of the transfers is invalid
    """
    # First, we need to check if the allocations are still valid.  The accounts
    # may have changed status since the allocations were written to the
    # session.
    transfers = []
    destination = core.redemptions_account()
    for code, amount in allocations.items():
        try:
            account = Account.active.get(code=code)
        except Account.DoesNotExist:
            raise UnableToTakePayment(
                _("No active account found with code %s") % code)

        # We verify each transaction
        try:
            Transfer.objects.verify_transfer(
                account, destination, amount, user)
        except exceptions.AccountException as e:
            raise UnableToTakePayment(str(e))

        transfers.append((account, destination, amount))

    # All transfers verified, now redeem
    for account, destination, amount in transfers:
        facade.transfer(account, destination, amount,
                        user=user, merchant_reference=order_number,
                        description="Redeemed to pay for order %s" % order_number)
Пример #2
0
def create_giftcard(order_number, user, amount):
    source = core.paid_source_account()
    code = codes.generate()
    destination = Account.objects.create(
        code=code
    )
    facade.transfer(source, destination, amount, user,
                    "Create new account")
 def test_account_exception_raised_for_runtime_error(self):
     user = G(User)
     source = G(Account, credit_limit=None)
     destination = G(Account)
     with mock.patch("accounts.abstract_models.PostingManager._wrap") as mock_method:
         mock_method.side_effect = RuntimeError()
         with self.assertRaises(exceptions.AccountException):
             facade.transfer(source, destination, D("100"), user)
 def test_account_exception_raised_for_runtime_error(self):
     user = G(User)
     source = G(Account, credit_limit=None)
     destination = G(Account)
     with mock.patch('accounts.abstract_models.PostingManager._wrap'
                     ) as mock_method:
         mock_method.side_effect = RuntimeError()
         with self.assertRaises(exceptions.AccountException):
             facade.transfer(source, destination, D('100'), user)
Пример #5
0
 def form_valid(self, form):
     account = self.object
     amount = form.cleaned_data['amount']
     try:
         facade.transfer(account, form.get_source_account(), amount,
                         user=self.request.user,
                         description=_("Return funds to source account"))
     except exceptions.AccountException, e:
         messages.error(self.request,
                        _("Unable to withdraw funds from account: %s") % e)
Пример #6
0
 def form_valid(self, form):
     account = self.object
     amount = form.cleaned_data['amount']
     try:
         facade.transfer(form.get_source_account(), account, amount,
                         user=self.request.user,
                         description=_("Top-up account"))
     except exceptions.AccountException, e:
         messages.error(self.request,
                        _("Unable to top-up account: %s") % e)
Пример #7
0
 def form_valid(self, form):
     account = self.object
     amount = form.cleaned_data['amount']
     try:
         facade.transfer(form.get_source_account(), account, amount,
                         user=self.request.user,
                         description=_("Top-up account"))
     except exceptions.AccountException, e:
         messages.error(self.request,
                        _("Unable to top-up account: %s") % e)
 def test_no_transaction_created_when_exception_raised(self):
     user = G(User)
     source = G(Account, credit_limit=None)
     destination = G(Account)
     with mock.patch("accounts.abstract_models.PostingManager._wrap") as mock_method:
         mock_method.side_effect = RuntimeError()
         try:
             facade.transfer(source, destination, D("100"), user)
         except Exception:
             pass
     self.assertEqual(0, Transfer.objects.all().count())
     self.assertEqual(0, Transaction.objects.all().count())
Пример #9
0
 def form_valid(self, form):
     account = self.object
     amount = form.cleaned_data['amount']
     try:
         facade.transfer(account,
                         form.get_source_account(),
                         amount,
                         user=self.request.user,
                         description=_("Return funds to source account"))
     except exceptions.AccountException, e:
         messages.error(self.request,
                        _("Unable to withdraw funds from account: %s") % e)
 def test_no_transaction_created_when_exception_raised(self):
     user = G(User)
     source = G(Account, credit_limit=None)
     destination = G(Account)
     with mock.patch('accounts.abstract_models.PostingManager._wrap'
                     ) as mock_method:
         mock_method.side_effect = RuntimeError()
         try:
             facade.transfer(source, destination, D('100'), user)
         except Exception:
             pass
     self.assertEqual(0, Transfer.objects.all().count())
     self.assertEqual(0, Transaction.objects.all().count())
Пример #11
0
    def form_valid(self, form):
        account = form.save()

        # Load transaction
        source = form.get_source_account()
        amount = form.cleaned_data['initial_amount']
        try:
            facade.transfer(source, account, amount,
                            user=self.request.user,
                            description=_("Creation of account"))
        except exceptions.AccountException, e:
            messages.error(self.request,
                _("Account created but unable to load funds onto new "
                  "account: %s") % e)
Пример #12
0
    def form_valid(self, form):
        account = form.save()

        # Load transaction
        source = form.get_source_account()
        amount = form.cleaned_data['initial_amount']
        try:
            facade.transfer(source, account, amount,
                            user=self.request.user,
                            description=_("Creation of account"))
        except exceptions.AccountException, e:
            messages.error(self.request,
                _("Account created but unable to load funds onto new "
                  "account: %s") % e)
Пример #13
0
 def form_valid(self, form):
     account = self.object
     amount = form.cleaned_data['amount']
     try:
         facade.transfer(form.get_source_account(), account, amount,
                         user=self.request.user,
                         description=_("Top-up account"))
     except exceptions.AccountException as e:
         messages.error(self.request,
                        _("Unable to top-up account: %s") % e)
     else:
         messages.success(
             self.request, _("%s added to account") % currency(amount))
     return http.HttpResponseRedirect(reverse('accounts-detail',
                                              kwargs={'pk': account.id}))
Пример #14
0
 def form_valid(self, form):
     account = self.object
     amount = form.cleaned_data['amount']
     try:
         facade.transfer(form.get_source_account(), account, amount,
                         user=self.request.user,
                         description=_("Top-up account"))
     except exceptions.AccountException as e:
         messages.error(self.request,
                        _("Unable to top-up account: %s") % e)
     else:
         messages.success(
             self.request, _("%s added to account") % currency(amount))
     return http.HttpResponseRedirect(reverse('accounts-detail',
                                              kwargs={'pk': account.id}))
Пример #15
0
 def form_valid(self, form):
     account = self.object
     amount = form.cleaned_data['amount']
     try:
         facade.transfer(account, form.get_source_account(), amount,
                         user=self.request.user,
                         description=_("Return funds to source account"))
     except exceptions.AccountException as e:
         messages.error(self.request,
                        _("Unable to withdraw funds from account: %s") % e)
     else:
         messages.success(
             self.request,
             _("%s withdrawn from account") % currency(amount))
     return http.HttpResponseRedirect(reverse('dashboard:accounts-detail',
                                              kwargs={'pk': account.id}))
 def setUp(self):
     self.user = G(User)
     self.source = G(Account, credit_limit=None, primary_user=None)
     self.destination = G(Account)
     self.transfer = facade.transfer(
         self.source, self.destination, D("100"), self.user, description="Give money to customer"
     )
Пример #17
0
 def setUp(self):
     self.user = G(User)
     self.source = G(Account, primary_user=None, credit_limit=None)
     self.destination = G(Account, primary_user=None)
     self.transfer = facade.transfer(self.source, self.destination,
                                     D('100'), self.user, "Give money to customer")
     self.reverse = facade.reverse(self.transfer, self.user,
                                   "Oops! Return money")
 def setUp(self):
     self.user = G(User)
     self.source = G(Account, credit_limit=None, primary_user=None)
     self.destination = G(Account)
     self.transfer = facade.transfer(self.source,
                                     self.destination,
                                     D('100'),
                                     user=self.user,
                                     description="Give money to customer")
Пример #19
0
 def setUp(self):
     self.user = G(User)
     self.source = G(Account, primary_user=None, credit_limit=None)
     self.destination = G(Account, primary_user=None)
     self.transfer = facade.transfer(self.source, self.destination,
                                     D('100'), user=self.user,
                                     description="Give money to customer")
     self.reverse = facade.reverse(self.transfer, self.user,
                                   description="Oops! Return money")
Пример #20
0
def redeem(order_number, user, allocations):
    """
    Is a variation of accounts.checkout.gateway.redeem, which uses pk instead of code in allocations.
    Also validates that the type is 6 :: deferred

    Settle payment for the passed set of account allocations

    Will raise UnableToTakePayment if any of the transfers is invalid
    """
    # First, we need to check if the allocations are still valid.  The accounts
    # may have changed status since the allocations were written to the
    # session.
    transfers = []
    destination = core.redemptions_account()

    for id, amount in allocations.items():
        try:
            account = Account.active.get(pk=id)
        except Account.DoesNotExist:
            raise JSONAPIException(status='422', code='ACCOUNT_REDEMPTION_DNE',
                                   title='Account Does Not Exist',
                                   detail="Account "+str(id)+" does not exist")

        if account.account_type.id != 6:
            raise JSONAPIException(status='422', code='ACCOUNT_REDEMPTION_MISTYPE',
                                   title='Account Type Is Invalid',
                                   detail="Account "+str(id)+" is not a valid type for redemption")

        # We verify each transaction
        try:
            Transfer.objects.verify_transfer(
                account, destination, amount, user)
        except exceptions.AccountException as e:
            raise JSONAPIException(status='422', code='ACCOUNT_REDEMPTION_FAIL',
                                   title='Account Redemption Failed',
                                   detail="Account redemption failed: "+str(e))

        transfers.append((account, destination, amount))

    # All transfers verified, now redeem
    for account, destination, amount in transfers:
        facade.transfer(account, destination, amount,
                        user=user, merchant_reference=order_number,
                        description="Redeemed to pay for order %s" % order_number)
Пример #21
0
    def form_valid(self, form):
        account = form.save()

        # Load transaction
        source = form.get_source_account()
        amount = form.cleaned_data['initial_amount']
        try:
            facade.transfer(source, account, amount,
                            user=self.request.user,
                            description=_("Creation of account"))
        except exceptions.AccountException as e:
            messages.error(
                self.request,
                _("Account created but unable to load funds onto new "
                  "account: %s") % e)
        else:
            messages.success(
                self.request,
                _("New account created with code '%s'") % account.code)
        return http.HttpResponseRedirect(
            reverse('accounts-detail', kwargs={'pk': account.id}))
Пример #22
0
    def form_valid(self, form):
        account = form.save()

        # Load transaction
        source = form.get_source_account()
        amount = form.cleaned_data['initial_amount']
        try:
            facade.transfer(source, account, amount,
                            user=self.request.user,
                            description=_("Creation of account"))
        except exceptions.AccountException as e:
            messages.error(
                self.request,
                _("Account created but unable to load funds onto new "
                  "account: %s") % e)
        else:
            messages.success(
                self.request,
                _("New account created with code '%s'") % account.code)
        return http.HttpResponseRedirect(
            reverse('accounts-detail', kwargs={'pk': account.id}))
Пример #23
0
 def valid_payload(self, payload):
     account = get_object_or_404(Account, code=self.kwargs['code'])
     if not account.is_active():
         raise ValidationError(errors.ACCOUNT_INACTIVE)
     redemptions = Account.objects.get(name=names.REDEMPTIONS)
     try:
         transfer = facade.transfer(
             redemptions, account, payload['amount'],
             merchant_reference=payload.get('merchant_reference', None))
     except exceptions.AccountException, e:
         return self.forbidden(
             code=errors.CANNOT_CREATE_TRANSFER,
             msg=e.message)
Пример #24
0
 def valid_payload(self, payload):
     account = get_object_or_404(Account, code=self.kwargs['code'])
     if not account.is_active():
         raise ValidationError(errors.ACCOUNT_INACTIVE)
     redemptions = Account.objects.get(name=names.REDEMPTIONS)
     try:
         transfer = facade.transfer(
             redemptions, account, payload['amount'],
             merchant_reference=payload.get('merchant_reference', None))
     except exceptions.AccountException, e:
         return self.forbidden(
             code=errors.CANNOT_CREATE_TRANSFER,
             msg=e.message)
Пример #25
0
def redeem(order_number, user, allocations):
    """
    Settle payment for the passed set of account allocations

    Will raise UnableToTakePayment if any of the transfers is invalid
    """
    # First, we need to check if the allocations are still valid.  The accounts
    # may have changed status since the allocations were written to the
    # session.
    transfers = []
    destination = core.redemptions_account()
    for code, amount in allocations.items():
        try:
            account = Account.active.get(code=code)
        except Account.DoesNotExist:
            raise UnableToTakePayment(
                _("No active account found with code %s") % code)

        # We verify each transaction
        try:
            Transfer.objects.verify_transfer(account, destination, amount,
                                             user)
        except exceptions.AccountException as e:
            raise UnableToTakePayment(str(e))

        transfers.append((account, destination, amount))

    # All transfers verified, now redeem
    for account, destination, amount in transfers:
        facade.transfer(account,
                        destination,
                        amount,
                        user=user,
                        merchant_reference=order_number,
                        description="Redeemed to pay for order %s" %
                        order_number)
Пример #26
0
 def valid_payload(self, payload):
     to_refund = get_object_or_404(Transfer,
                                   reference=self.kwargs['reference'])
     amount = payload['amount']
     max_refund = to_refund.max_refund()
     if amount > max_refund:
         return self.forbidden(
             ("Refund not permitted: maximum refund permitted "
              "is %.2f") % max_refund)
     if not to_refund.source.is_active():
         raise ValidationError(errors.ACCOUNT_INACTIVE)
     try:
         transfer = facade.transfer(
             to_refund.destination, to_refund.source,
             payload['amount'], parent=to_refund,
             merchant_reference=payload.get('merchant_reference', None))
     except exceptions.AccountException, e:
         return self.forbidden(
             code=errors.CANNOT_CREATE_TRANSFER,
             msg=e.message)
Пример #27
0
 def valid_payload(self, payload):
     to_refund = get_object_or_404(Transfer,
                                   reference=self.kwargs['reference'])
     amount = payload['amount']
     max_refund = to_refund.max_refund()
     if amount > max_refund:
         return self.forbidden(
             ("Refund not permitted: maximum refund permitted "
              "is %.2f") % max_refund)
     if not to_refund.source.is_active():
         raise ValidationError(errors.ACCOUNT_INACTIVE)
     try:
         transfer = facade.transfer(
             to_refund.destination, to_refund.source,
             payload['amount'], parent=to_refund,
             merchant_reference=payload.get('merchant_reference', None))
     except exceptions.AccountException, e:
         return self.forbidden(
             code=errors.CANNOT_CREATE_TRANSFER,
             msg=e.message)
Пример #28
0
    def valid_payload(self, payload):
        """
        Redeem an amount from the selected giftcard
        """
        account = get_object_or_404(Account, code=self.kwargs['code'])
        if not account.is_active():
            raise ValidationError(errors.ACCOUNT_INACTIVE)
        amt = payload['amount']
        if not account.is_debit_permitted(amt):
            raise ValidationError(errors.INSUFFICIENT_FUNDS)

        redemptions = Account.objects.get(name=names.REDEMPTIONS)
        try:
            transfer = facade.transfer(
                account, redemptions, amt,
                merchant_reference=payload.get('merchant_reference', None))
        except exceptions.AccountException, e:
            return self.forbidden(
                code=errors.CANNOT_CREATE_TRANSFER,
                msg=e.message)
Пример #29
0
    def valid_payload(self, payload):
        """
        Redeem an amount from the selected giftcard
        """
        account = get_object_or_404(Account, code=self.kwargs['code'])
        if not account.is_active():
            raise ValidationError(errors.ACCOUNT_INACTIVE)
        amt = payload['amount']
        if not account.is_debit_permitted(amt):
            raise ValidationError(errors.INSUFFICIENT_FUNDS)

        redemptions = Account.objects.get(name=names.REDEMPTIONS)
        try:
            transfer = facade.transfer(
                account, redemptions, amt,
                merchant_reference=payload.get('merchant_reference', None))
        except exceptions.AccountException, e:
            return self.forbidden(
                code=errors.CANNOT_CREATE_TRANSFER,
                msg=e.message)
Пример #30
0
def redeem(order_number, user, allocations):
    """
    Settle payment for the passed set of account allocations.

    An error will be raised in case that any transaction can't got through.
    """
    # First, we need to check if the allocations are still valid
    transfers = _get_and_verify_transfers(order_number, user, allocations)
    transactions = []
    # All transfers verified, now redeem atomicaly
    redeem_transaction = transaction.savepoint()
    try:
        for account, destination, amount in transfers:
            transactions.append(facade.transfer(
                account, destination, amount, user=user,
                merchant_reference=order_number,
                description="Redeemed to pay for order %s" % order_number))
        transaction.savepoint_commit(redeem_transaction)
    except Exception, exception:
        logger.error("Rollbacking transaction because exception: '%s'",
                     unicode(exception))
        transaction.savepoint_rollback(redeem_transaction)
        raise exception
 def test_account_exception_raised_for_invalid_transfer(self):
     user = G(User)
     source = G(Account)
     destination = G(Account)
     with self.assertRaises(exceptions.AccountException):
         facade.transfer(source, destination, D("100"), user)
Пример #32
0
 def test_account_exception_raised_for_invalid_transfer(self):
     user = G(User)
     source = G(Account, credit_limit=D('0.00'))
     destination = G(Account)
     with self.assertRaises(exceptions.AccountException):
         facade.transfer(source, destination, D('100'), user)
 def test_doesnt_explode(self):
     source = G(Account, credit_limit=None)
     destination = G(Account)
     facade.transfer(source, destination, D('1'))
Пример #34
0
        try:
            account = Account.active.get(code=code)
        except Account.DoesNotExist:
            raise UnableToTakePayment(
                _("No active account found with code %s") % code)

        # We verify each transaction
        try:
            Transfer.objects.verify_transfer(
                account, destination, amount, user)
        except exceptions.AccountException, e:
            raise UnableToTakePayment(str(e))

        transfers.append((account, destination, amount))

    # All transfers verified, now redeem
    for account, destination, amount in transfers:
        facade.transfer(account, destination, amount,
                        user=user, merchant_reference=order_number,
                        description="Redeemed to pay for order %s" % order_number)


def create_giftcard(order_number, user, amount):
    source = core.paid_source_account()
    code = codes.generate()
    destination = Account.objects.create(
        code=code
    )
    facade.transfer(source, destination, amount, user,
                    "Create new account")
Пример #35
0
 def test_doesnt_explode(self):
     source = G(Account, credit_limit=None)
     destination = G(Account)
     facade.transfer(source, destination, D('1'))
Пример #36
0
            account = Account.active.get(code=code)
        except Account.DoesNotExist:
            raise UnableToTakePayment(
                _("No active account found with code %s") % code)

        # We verify each transaction
        try:
            Transfer.objects.verify_transfer(account, destination, amount,
                                             user)
        except exceptions.AccountException, e:
            raise UnableToTakePayment(str(e))

        transfers.append((account, destination, amount))

    # All transfers verified, now redeem
    for account, destination, amount in transfers:
        facade.transfer(account,
                        destination,
                        amount,
                        user,
                        order_number,
                        description="Redeemed to pay for order %s" %
                        order_number)


def create_giftcard(order_number, user, amount):
    source = core.paid_source_account()
    code = codes.generate()
    destination = Account.objects.create(code=code)
    facade.transfer(source, destination, amount, user, "Create new account")
Пример #37
0
def create_giftcard(order_number, user, amount):
    source = core.paid_source_account()
    code = codes.generate()
    destination = Account.objects.create(code=code)
    facade.transfer(source, destination, amount, user, "Create new account")
Пример #38
0
 def load_account(self, account, payload):
     bank = Account.objects.get(name=names.BANK)
     facade.transfer(bank, account, payload['amount'],
                     description="Load from bank")
Пример #39
0
 def load_account(self, account, payload):
     bank = Account.objects.get(name=names.BANK)
     facade.transfer(bank,
                     account,
                     payload['amount'],
                     description="Load from bank")
 def test_account_exception_raised_for_invalid_transfer(self):
     user = G(User)
     source = G(Account, credit_limit=D('0.00'))
     destination = G(Account)
     with self.assertRaises(exceptions.AccountException):
         facade.transfer(source, destination, D('100'), user)
Пример #41
0
        try:
            account = Account.active.get(code=code)
        except Account.DoesNotExist:
            raise UnableToTakePayment(
                _("No active account found with code %s") % code)

        # We verify each transaction
        try:
            Transfer.objects.verify_transfer(
                account, destination, amount, user)
        except exceptions.AccountException, e:
            raise UnableToTakePayment(str(e))

        transfers.append((account, destination, amount))

    # All transfers verified, now redeem
    for account, destination, amount in transfers:
        facade.transfer(account, destination, amount,
                        user, order_number,
                        description="Redeemed to pay for order %s" % order_number)


def create_giftcard(order_number, user, amount):
    source = core.paid_source_account()
    code = codes.generate()
    destination = Account.objects.create(
        code=code
    )
    facade.transfer(source, destination, amount, user,
                    "Create new account")
Пример #42
0
            account = Account.active.get(code=code)
        except Account.DoesNotExist:
            raise UnableToTakePayment(
                _("No active account found with code %s") % code)

        # We verify each transaction
        try:
            Transfer.objects.verify_transfer(account, destination, amount,
                                             user)
        except exceptions.AccountException, e:
            raise UnableToTakePayment(str(e))

        transfers.append((account, destination, amount))

    # All transfers verified, now redeem
    for account, destination, amount in transfers:
        facade.transfer(account,
                        destination,
                        amount,
                        user=user,
                        merchant_reference=order_number,
                        description="Redeemed to pay for order %s" %
                        order_number)


def create_giftcard(order_number, user, amount):
    source = core.paid_source_account()
    code = codes.generate()
    destination = Account.objects.create(code=code)
    facade.transfer(source, destination, amount, user, "Create new account")