def test_Idempotency(self): key = random.randrange(100000000000000000, 999999999999999999) wallet = self.getJohnsWallet() user = self.getJohn() account = self.getJohnsAccount() payOutPost = PayOut() payOutPost.AuthorId = user.Id payOutPost.DebitedWalletId = wallet.Id payOutPost.DebitedFunds = Money(10, 'EUR') payOutPost.Fees = Money(5, 'EUR') payOutPost.MeanOfPaymentDetails = PayOutPaymentDetailsBankWire() payOutPost.MeanOfPaymentDetails.BankAccountId = account.Id payOutPost.MeanOfPaymentDetails.BankWireRef = 'Johns bank wire ref' payOutPost.Tag = 'DefaultTag' payOutPost.CreditedUserId = user.Id payOut = self.sdk.payOuts.CreateIdempotent(key, payOutPost) self.assertIsNotNone(payOut) # test existing key result = self.sdk.idempotency.Get(key) self.assertIsNotNone(result) # test non-existing key try: result = self.sdk.idempotency.Get(key + '_no') # expecting a response error self.assertTrue(1 == 0) except: result = None
def getJohnsPayOutBankWire(self): """Creates Pay-Out Bank Wire object""" if self._johnsPayOutBankWire == None: wallet = self.getJohnsWallet() user = self.getJohn() account = self.getJohnsAccount() payOut = PayOut() payOut.Tag = 'DefaultTag' payOut.AuthorId = user.Id payOut.CreditedUserId = user.Id payOut.DebitedFunds = Money() payOut.DebitedFunds.Currency = 'EUR' payOut.DebitedFunds.Amount = 10 payOut.Fees = Money() payOut.Fees.Currency = 'EUR' payOut.Fees.Amount = 5 payOut.DebitedWalletId = wallet.Id payOut.MeanOfPaymentDetails = PayOutPaymentDetailsBankWire() payOut.MeanOfPaymentDetails.BankAccountId = account.Id payOut.MeanOfPaymentDetails.Communication = 'Communication text' self._johnsPayOutBankWire = self.sdk.payOuts.Create(payOut) return self._johnsPayOutBankWire
def payout(db, participant, amount): if participant.is_suspicious: raise UserIsSuspicious route = ExchangeRoute.from_network(participant, 'mango-ba') assert route ba = mangoapi.users.GetBankAccount(participant.mangopay_user_id, route.address) # Do final calculations credit_amount, fee = skim_credit(amount, ba) if credit_amount <= 0 or fee / credit_amount > 0.1: raise TransactionFeeTooHigh # Try to dance with MangoPay e_id = record_exchange(db, route, -credit_amount, fee, participant, 'pre') payout = PayOut() payout.AuthorId = participant.mangopay_user_id payout.DebitedFunds = Money(int(credit_amount * 100), 'EUR') payout.DebitedWalletId = participant.mangopay_wallet_id payout.Fees = Money(int(fee * 100), 'EUR') payout.MeanOfPaymentDetails = PayOutPaymentDetailsBankWire( BankAccountId=route.address, BankWireRef=str(e_id), ) payout.Tag = str(e_id) try: test_hook() mangoapi.payOuts.Create(payout) return record_exchange_result(db, e_id, 'created', None, participant) except Exception as e: error = repr_exception(e) return record_exchange_result(db, e_id, 'failed', error, participant)
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, -10, 0, 0, homer, 'pre') assert homer.balance == 0 homer.close(None) assert homer.status == 'closed' qs = "EventType=PAYOUT_NORMAL_" + status_up + "&RessourceId=123456790" payout = PayOut() 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'] 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, -10, 0, homer, 'pre') assert homer.balance == 0 homer.close(None) assert homer.status == 'closed' qs = "EventType=PAYOUT_NORMAL_"+status_up+"&RessourceId=123456790" payout = PayOut() 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, csrf_token=False) assert b'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' homer.update_status('active') # reset for next loop run
def create(self, tag=''): pay_out = PayOut() pay_out.Tag = tag pay_out.AuthorId = self.mangopay_user.mangopay_id pay_out.DebitedFunds = python_money_to_mangopay_money( self.debited_funds) pay_out.Fees = python_money_to_mangopay_money(self.fees) pay_out.DebitedWalletId = self.mangopay_wallet.mangopay_id details = PayOutPaymentDetailsBankWire() details.BankAccountId = self.mangopay_bank_account.mangopay_id pay_out.MeanOfPaymentDetails = details client = get_mangopay_api_client() created_pay_out = client.payOuts.Create(pay_out) self.mangopay_id = created_pay_out.Id return self._update(created_pay_out)
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 = mangoapi.users.GetBankAccount(participant.mangopay_user_id, route.address) # 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 = PayOut() 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.MeanOfPaymentDetails = PayOutPaymentDetailsBankWire( BankAccountId=route.address, BankWireRef=str(e_id), ) payout.Tag = str(e_id) try: test_hook() payout = mangoapi.payOuts.Create(payout) 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)
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, -9, 1, 0, homer, 'pre') assert homer.balance == 0 homer.close(None) assert homer.status == 'closed' payout = PayOut() 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() refund.DebitedFunds = Money(900, 'EUR') refund.Fees = Money(-100, 'EUR') refund.Status = status_up refund.ResultCode = '000001' if error else '000000' refund.ResultMessage = error reason = refund.RefundReason = RefundReason() reason.RefundReasonMessage = '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'] homer.update_status('active') # reset for next loop run
def payout(db, participant, amount, ignore_high_fee=False): assert amount > 0 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 = mangoapi.users.GetBankAccount(participant.mangopay_user_id, route.address) # 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_CREDIT_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 = PayOut() 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.MeanOfPaymentDetails = PayOutPaymentDetailsBankWire( BankAccountId=route.address, BankWireRef=str(e_id), ) payout.Tag = str(e_id) try: test_hook() mangoapi.payOuts.Create(payout) return record_exchange_result(db, e_id, 'created', None, participant) except Exception as e: error = repr_exception(e) return record_exchange_result(db, e_id, 'failed', error, participant)
def Get(self, pay_out_id): pay_out = PayOut() pay_out.Id = pay_out_id pay_out.ExecutionDate = 12312312 pay_out.Status = "CREATED" return pay_out