コード例 #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")
コード例 #3
0
 def test_account_exception_raised_for_runtime_error(self):
     user = UserFactory()
     source = AccountFactory(credit_limit=None)
     destination = AccountFactory()
     with mock.patch('izi_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)
コード例 #4
0
 def test_no_transaction_created_when_exception_raised(self):
     user = UserFactory()
     source = AccountFactory(credit_limit=None)
     destination = AccountFactory()
     with mock.patch('izi_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())
コード例 #5
0
ファイル: views.py プロジェクト: izi-ecommerce/izi-accounts
 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}))
コード例 #6
0
 def setUp(self):
     self.user = UserFactory()
     self.source = AccountFactory(credit_limit=None, primary_user=None)
     self.destination = AccountFactory()
     self.transfer = facade.transfer(self.source,
                                     self.destination,
                                     D('100'),
                                     user=self.user,
                                     description="Give money to customer")
コード例 #7
0
ファイル: views.py プロジェクト: izi-ecommerce/izi-accounts
    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}))
コード例 #8
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 as e:
         return self.forbidden(code=errors.CANNOT_CREATE_TRANSFER,
                               msg=e.message)
     return self.created(
         reverse('transfer', kwargs={'reference': transfer.reference}),
         transfer.as_dict())
コード例 #9
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 as e:
         return self.forbidden(code=errors.CANNOT_CREATE_TRANSFER,
                               msg=e.message)
     return self.created(
         reverse('transfer', kwargs={'reference': transfer.reference}),
         transfer.as_dict())
コード例 #10
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 as e:
            return self.forbidden(code=errors.CANNOT_CREATE_TRANSFER,
                                  msg=e.message)
        return self.created(
            reverse('transfer', kwargs={'reference': transfer.reference}),
            transfer.as_dict())
コード例 #11
0
 def test_doesnt_explode(self):
     source = AccountFactory(credit_limit=None)
     destination = AccountFactory()
     facade.transfer(source, destination, D('1'))
コード例 #12
0
 def test_account_exception_raised_for_invalid_transfer(self):
     user = UserFactory()
     source = AccountFactory(credit_limit=D('0.00'))
     destination = AccountFactory()
     with self.assertRaises(exceptions.AccountException):
         facade.transfer(source, destination, D('100'), user)
コード例 #13
0
 def load_account(self, account, payload):
     bank = Account.objects.get(name=names.BANK)
     facade.transfer(bank,
                     account,
                     payload['amount'],
                     description="Load from bank")