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)
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)
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 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())
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())
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)
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}))
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" )
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")
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")
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)
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}))
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)
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)
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)
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)
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)
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'))
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")
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")
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 load_account(self, account, payload): bank = Account.objects.get(name=names.BANK) facade.transfer(bank, account, payload['amount'], description="Load from bank")
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")
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")