def test_payout_callback(self, Get): homer, ba = self.homer, self.homer_route for status in ('succeeded', 'failed'): status_up = status.upper() error = 'FOO' if status == 'failed' else None self.make_exchange('mango-cc', 10, 0, homer) e_id = record_exchange(self.db, ba, EUR(-10), EUR(0), EUR(0), homer, 'pre').id assert homer.balance == 0 homer.close(None) assert homer.status == 'closed' qs = "EventType=PAYOUT_NORMAL_" + status_up + "&RessourceId=123456790" payout = BankWirePayOut(Id=-1) payout.Status = status_up payout.ResultCode = '000001' if error else '000000' payout.ResultMessage = error payout.AuthorId = homer.mangopay_user_id payout.Tag = str(e_id) Get.return_value = payout r = self.callback(qs) assert CSRF_TOKEN not in r.headers.cookie assert r.code == 200, r.text homer = homer.refetch() if status == 'succeeded': assert homer.balance == 0 assert homer.status == 'closed' else: assert homer.balance == 10 assert homer.status == 'active' emails = self.get_emails() assert len(emails) == 1 assert emails[0]['to'][0] == 'homer <%s>' % homer.email assert 'fail' in emails[0]['subject'] self.db.self_check() homer.update_status('active') # reset for next loop run
def test_payout_callback(self, Get): homer, ba = self.homer, self.homer_route for status in ('succeeded', 'failed'): status_up = status.upper() error = 'FOO' if status == 'failed' else None self.make_exchange('mango-cc', 10, 0, homer) e_id = record_exchange(self.db, ba, EUR(-10), EUR(0), EUR(0), homer, 'pre').id assert homer.balance == 0 homer.close(None) assert homer.status == 'closed' qs = "EventType=PAYOUT_NORMAL_"+status_up+"&RessourceId=123456790" payout = BankWirePayOut(Id=-1) payout.Status = status_up payout.ResultCode = '000001' if error else '000000' payout.ResultMessage = error payout.AuthorId = homer.mangopay_user_id payout.Tag = str(e_id) Get.return_value = payout r = self.callback(qs) assert CSRF_TOKEN not in r.headers.cookie assert r.code == 200, r.text homer = homer.refetch() if status == 'succeeded': assert homer.balance == 0 assert homer.status == 'closed' else: assert homer.balance == 10 assert homer.status == 'active' emails = self.get_emails() assert len(emails) == 1 assert emails[0]['to'][0] == 'homer <%s>' % homer.email assert 'fail' in emails[0]['subject'] self.db.self_check() homer.update_status('active') # reset for next loop run
def payout(db, route, amount, ignore_high_fee=False): """Withdraw money to the specified bank account (`route`). """ assert amount > 0 assert route assert route.network == 'mango-ba' participant = route.participant if participant.is_suspended: raise AccountSuspended() payday = db.one("SELECT * FROM paydays WHERE ts_start > ts_end") if payday: raise PaydayIsRunning ba = BankAccount.get(route.address, user_id=participant.mangopay_user_id) # Do final calculations amount = Money(amount, 'EUR') if isinstance(amount, Decimal) else amount credit_amount, fee, vat = skim_credit(amount, ba) if credit_amount <= 0 and fee > 0: raise FeeExceedsAmount fee_percent = fee / amount if fee_percent > FEE_PAYOUT_WARN and not ignore_high_fee: raise TransactionFeeTooHigh(fee_percent, fee, amount) # Try to dance with MangoPay e_id = record_exchange(db, route, -credit_amount, fee, vat, participant, 'pre').id payout = BankWirePayOut() payout.AuthorId = participant.mangopay_user_id payout.DebitedFunds = amount.int() payout.DebitedWalletId = participant.get_current_wallet( amount.currency).remote_id payout.Fees = fee.int() payout.BankAccountId = route.address payout.BankWireRef = str(e_id) payout.Tag = str(e_id) try: test_hook() payout.save() return record_exchange_result(db, e_id, payout.Id, payout.Status.lower(), repr_error(payout), participant) except Exception as e: error = repr_exception(e) return record_exchange_result(db, e_id, '', 'failed', error, participant)
def test_payout_refund_callback(self, R_Get, PO_Get): homer, ba = self.homer, self.homer_route for status in ('failed', 'succeeded'): # Create the payout self.make_exchange('mango-cc', 10, 0, homer) e_id = record_exchange(self.db, ba, EUR(-9), EUR(1), EUR(0), homer, 'pre').id assert homer.balance == 0 homer.close(None) assert homer.status == 'closed' payout = BankWirePayOut(Id=-1) payout.Status = 'SUCCEEDED' payout.ResultCode = '000000' payout.AuthorId = homer.mangopay_user_id payout.Tag = str(e_id) PO_Get.return_value = payout # Create the refund status_up = status.upper() error = 'FOO' if status == 'failed' else None refund = Refund(Id=-1) refund.DebitedFunds = Money(900, 'EUR') refund.Fees = Money(-100, 'EUR') refund.Status = status_up refund.ResultCode = '000001' if error else '000000' refund.ResultMessage = error refund.RefundReason = Reason(message='BECAUSE 42') refund.AuthorId = homer.mangopay_user_id R_Get.return_value = refund # Call back qs = "EventType=PAYOUT_REFUND_" + status_up + "&RessourceId=123456790" r = self.callback(qs) assert r.code == 200, r.text homer = homer.refetch() if status == 'failed': assert homer.balance == 0 assert homer.status == 'closed' else: assert homer.balance == 10 assert homer.status == 'active' emails = self.get_emails() assert len(emails) == 1 assert emails[0]['to'][0] == 'homer <%s>' % homer.email assert 'fail' in emails[0]['subject'] assert 'BECAUSE 42' in emails[0]['text'] self.db.self_check() homer.update_status('active') # reset for next loop run
def payout(db, route, amount, ignore_high_fee=False): """Withdraw money to the specified bank account (`route`). """ assert amount > 0 assert route assert route.network == 'mango-ba' participant = route.participant if participant.is_suspended: raise AccountSuspended() payday = db.one("SELECT * FROM paydays WHERE ts_start > ts_end") if payday: raise PaydayIsRunning ba = BankAccount.get(route.address, user_id=participant.mangopay_user_id) # Do final calculations amount = Money(amount, 'EUR') if isinstance(amount, Decimal) else amount credit_amount, fee, vat = skim_credit(amount, ba) if credit_amount <= 0 and fee > 0: raise FeeExceedsAmount fee_percent = fee / amount if fee_percent > FEE_PAYOUT_WARN and not ignore_high_fee: raise TransactionFeeTooHigh(fee_percent, fee, amount) # Try to dance with MangoPay e_id = record_exchange(db, route, -credit_amount, fee, vat, participant, 'pre').id payout = BankWirePayOut() payout.AuthorId = participant.mangopay_user_id payout.DebitedFunds = Money_to_cents(amount) payout.DebitedWalletId = participant.get_current_wallet(amount.currency).remote_id payout.Fees = Money_to_cents(fee) payout.BankAccountId = route.address payout.BankWireRef = str(e_id) payout.Tag = str(e_id) try: test_hook() payout.save() return record_exchange_result( db, e_id, payout.Id, payout.Status.lower(), repr_error(payout), participant ) except Exception as e: error = repr_exception(e) return record_exchange_result(db, e_id, '', 'failed', error, participant)
def test_payout_refund_callback(self, R_Get, PO_Get): homer, ba = self.homer, self.homer_route for status in ('failed', 'succeeded'): # Create the payout self.make_exchange('mango-cc', 10, 0, homer) e_id = record_exchange(self.db, ba, EUR(-9), EUR(1), EUR(0), homer, 'pre').id assert homer.balance == 0 homer.close(None) assert homer.status == 'closed' payout = BankWirePayOut(Id=-1) payout.Status = 'SUCCEEDED' payout.ResultCode = '000000' payout.AuthorId = homer.mangopay_user_id payout.Tag = str(e_id) PO_Get.return_value = payout # Create the refund status_up = status.upper() error = 'FOO' if status == 'failed' else None refund = Refund(Id=-1) refund.DebitedFunds = Money(900, 'EUR') refund.Fees = Money(-100, 'EUR') refund.Status = status_up refund.ResultCode = '000001' if error else '000000' refund.ResultMessage = error refund.RefundReason = Reason(message='BECAUSE 42') refund.AuthorId = homer.mangopay_user_id R_Get.return_value = refund # Call back qs = "EventType=PAYOUT_REFUND_"+status_up+"&RessourceId=123456790" r = self.callback(qs) assert r.code == 200, r.text homer = homer.refetch() if status == 'failed': assert homer.balance == 0 assert homer.status == 'closed' else: assert homer.balance == 10 assert homer.status == 'active' emails = self.get_emails() assert len(emails) == 1 assert emails[0]['to'][0] == 'homer <%s>' % homer.email assert 'fail' in emails[0]['subject'] assert 'BECAUSE 42' in emails[0]['text'] self.db.self_check() homer.update_status('active') # reset for next loop run
def payout(db, participant, amount, ignore_high_fee=False): assert amount > 0 if participant.is_suspended: raise AccountSuspended() payday = db.one("SELECT * FROM paydays WHERE ts_start > ts_end") if payday: raise PaydayIsRunning route = ExchangeRoute.from_network(participant, 'mango-ba') assert route ba = BankAccount.get(route.address, user_id=participant.mangopay_user_id) # Do final calculations credit_amount, fee, vat = skim_credit(amount, ba) if credit_amount <= 0 and fee > 0: raise FeeExceedsAmount fee_percent = fee / amount if fee_percent > FEE_PAYOUT_WARN and not ignore_high_fee: raise TransactionFeeTooHigh(fee_percent, fee, amount) # Try to dance with MangoPay e_id = record_exchange(db, route, -credit_amount, fee, vat, participant, 'pre') payout = BankWirePayOut() payout.AuthorId = participant.mangopay_user_id payout.DebitedFunds = Money(int(amount * 100), 'EUR') payout.DebitedWalletId = participant.mangopay_wallet_id payout.Fees = Money(int(fee * 100), 'EUR') payout.BankAccountId = route.address payout.BankWireRef = str(e_id) payout.Tag = str(e_id) try: test_hook() payout.save() return record_exchange_result(db, e_id, payout.Status.lower(), repr_error(payout), participant) except Exception as e: error = repr_exception(e) return record_exchange_result(db, e_id, 'failed', error, participant)