Example #1
0
File: models.py Project: ojr9/g39
 def create(self):
     # mpu = Saver.objects.get(self.author)
     ba = MangoBankAccount.objects.get(owner=self.saver.mid)
     po = BankWirePayOut(author=self.saver.mid, debited_funds=Money(amount=self.amount, currency=self.currency),
                         debited_wallet=self.dwid, bank_account=ba.bid, bank_wire_ref=self.bank_wire_ref)
     po.save()
     self.poid = po.get_pk()
     # get other stuff from the api call and add accordingly
     self.save()
Example #2
0
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)
Example #3
0
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)
Example #4
0
 def create(self, user):
     # call the _check here
     mid = MangoUser.objects.get(user=user).mid
     bid = MangoBankAccount.objects.get(mid=mid)
     dwid = MangoWallet.objects.get(mid=mid)
     payout = BankWirePayOut(AuthorId=mid,
                             DebitedFunds=Money(self.amount, self.currency),
                             Fees=Money(self.fees, self.currency),
                             BankAccountId=bid,
                             DebitedWalletId=dwid,
                             BankWireRef=self.reference)
     payout.save()
     self.poid = payout.get_pk()
     self.author_id = mid
     self.bid = bid
     self.dwid = dwid
     self.creation_date = payout['CreationDate']  # is this form ok?
     self.save()
Example #5
0
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)
    def test_Idempotency(self):
        key = str(int(time.time())) + 'abcdefg'
        pay_out = None

        #create bankwire
        try:
            pay_out_post = BankWirePayOut()
            pay_out_post.author = BaseTestLive.get_john()
            pay_out_post.debited_wallet = BaseTestLive.get_johns_wallet()
            debited_funds = Money()
            debited_funds.amount = 10
            debited_funds.currency = 'EUR'
            pay_out_post.debited_funds = debited_funds
            fees = Money()
            fees.amount = 5
            fees.currency = 'EUR'
            pay_out_post.fees = fees
            pay_out_post.bank_account = BaseTestLive.get_johns_account()
            pay_out_post.bank_wire_ref = "Johns bank wire ref"
            pay_out_post.tag = "DefaultTag"
            pay_out_post.credited_user = BaseTestLive.get_john()
            pay_out = pay_out_post.save(idempotency_key=key)
        except Exception as e:
            self.assertFalse(True, str(e))

        self.assertIsNotNone(pay_out)

        #test existing key
        result = None
        try:
            result = IdempotencyResponse.get(key)
        except Exception as e:
            self.assertFalse(True, str(e))

        self.assertIsNotNone(result)

        #test non existing key
        try:
            IdempotencyResponse.get(key+'_no')

            #expecting a APIError to be thrown
            self.assertFalse(True, 'Api Error should have been thrown')
        except APIError as e:
            self.assertEqual(e.content['Type'], 'correlationid_not_found')
            self.assertTrue(e.code == 400)
        except Exception as ex:
            self.assertFalse(True, str(ex))