def make_exchange(self,
                   route,
                   amount,
                   fee,
                   participant,
                   status='succeeded',
                   error='',
                   vat=0):
     amount = amount if isinstance(amount, Money) else Money(amount, 'EUR')
     fee = fee if isinstance(fee, Money) else Money(fee, amount.currency)
     vat = vat if isinstance(vat, Money) else Money(vat, fee.currency)
     if not isinstance(route, ExchangeRoute):
         network = route
         currency = amount.currency if network == 'mango-cc' else None
         routes = ExchangeRoute.from_network(participant,
                                             network,
                                             currency=currency)
         if routes:
             route = routes[0]
         else:
             from .mangopay import MangopayHarness
             address = MangopayHarness.card_id if network == 'mango-cc' else -participant.id
             route = ExchangeRoute.insert(participant,
                                          network,
                                          address,
                                          'chargeable',
                                          currency=currency)
             assert route
     e_id = record_exchange(self.db, route, amount, fee, vat, participant,
                            'pre').id
     record_exchange_result(self.db, e_id, -e_id, status, error,
                            participant)
     return e_id
Пример #2
0
def fake_exchange(db, participant, amount, fee, vat, timestamp):
    routes = ExchangeRoute.from_network(participant, 'mango-cc')
    if routes:
        route = routes[0]
    else:
        route = _fake_thing(
            db,
            "exchange_routes",
            participant=participant.id,
            network='mango-cc',
            address='-1',
            error='',
            one_off=False,
            remote_user_id=participant.mangopay_user_id,
        )
    e = _fake_thing(
        db,
        "exchanges",
        timestamp=timestamp,
        participant=participant.id,
        amount=amount,
        fee=fee,
        vat=vat,
        status='pre',
        route=route.id,
        wallet_id=participant.mangopay_wallet_id,
    )
    record_exchange_result(db, e.id, -e.id, 'succeeded', '', participant)
    return e
Пример #3
0
def fake_exchange(db, participant, amount, fee, vat, timestamp):
    routes = ExchangeRoute.from_network(participant, 'mango-cc', currency='EUR')
    if routes:
        route = routes[0]
    else:
        route = _fake_thing(
            db,
            "exchange_routes",
            participant=participant.id,
            network='mango-cc',
            address='-1',
            status='chargeable',
            one_off=False,
            remote_user_id=participant.mangopay_user_id,
            currency='EUR',
        )
    e = _fake_thing(
        db,
        "exchanges",
        timestamp=timestamp,
        participant=participant.id,
        amount=amount,
        fee=fee,
        vat=vat,
        status='pre',
        route=route.id,
        wallet_id='-%i' % participant.id,
    )
    record_exchange_result(db, e.id, -e.id, 'succeeded', '', participant)
    return e
Пример #4
0
 def test_record_exchange_result_updates_balance_for_positive_amounts(self):
     janet, cc = self.janet, self.janet_route
     self.make_exchange('mango-cc', 4, 0, janet)
     e_id = record_exchange(self.db, cc, EUR('31.59'), EUR('0.01'), EUR(0), janet, 'pre').id
     assert janet.balance == EUR('4.00')
     record_exchange_result(self.db, e_id, -e_id, 'succeeded', None, janet)
     janet = Participant.from_username('janet')
     assert janet.balance == EUR('35.59')
Пример #5
0
 def test_record_exchange_result_doesnt_restore_balance_on_success(self):
     homer, ba = self.homer, self.homer_route
     self.make_exchange('mango-cc', 50, 0, homer)
     e_id = record_exchange(self.db, ba, EUR('-43.98'), EUR('1.60'), EUR(0), homer, 'pre').id
     assert homer.balance == EUR('4.42')
     record_exchange_result(self.db, e_id, -e_id, 'succeeded', None, homer)
     homer = Participant.from_username('homer')
     assert homer.balance == EUR('4.42')
Пример #6
0
 def test_record_exchange_result_restores_balance_on_error(self):
     homer, ba = self.homer, self.homer_route
     self.make_exchange('mango-cc', 30, 0, homer)
     e_id = record_exchange(self.db, ba, EUR('-27.06'), EUR('0.81'), EUR(0), homer, 'pre').id
     assert homer.balance == EUR('02.13')
     record_exchange_result(self.db, e_id, -e_id, 'failed', 'SOME ERROR', homer)
     homer = Participant.from_username('homer')
     assert homer.balance == EUR('30.00')
Пример #7
0
 def test_record_exchange_result_updates_balance_for_positive_amounts(self):
     janet, cc = self.janet, self.janet_route
     self.make_exchange('mango-cc', 4, 0, janet)
     e_id = record_exchange(self.db, cc, EUR('31.59'), EUR('0.01'), EUR(0), janet, 'pre').id
     assert janet.balance == D('4.00')
     record_exchange_result(self.db, e_id, -e_id, 'succeeded', None, janet)
     janet = Participant.from_username('janet')
     assert janet.balance == D('35.59')
Пример #8
0
 def test_record_exchange_result_doesnt_restore_balance_on_success(self):
     homer, ba = self.homer, self.homer_route
     self.make_exchange('mango-cc', 50, 0, homer)
     e_id = record_exchange(self.db, ba, EUR('-43.98'), EUR('1.60'), EUR(0), homer, 'pre').id
     assert homer.balance == D('4.42')
     record_exchange_result(self.db, e_id, -e_id, 'succeeded', None, homer)
     homer = Participant.from_username('homer')
     assert homer.balance == D('4.42')
Пример #9
0
 def test_record_exchange_result_restores_balance_on_error(self):
     homer, ba = self.homer, self.homer_route
     self.make_exchange('mango-cc', 30, 0, homer)
     e_id = record_exchange(self.db, ba, EUR('-27.06'), EUR('0.81'), EUR(0), homer, 'pre').id
     assert homer.balance == D('02.13')
     record_exchange_result(self.db, e_id, -e_id, 'failed', 'SOME ERROR', homer)
     homer = Participant.from_username('homer')
     assert homer.balance == D('30.00')
Пример #10
0
 def test_record_exchange_result_restores_balance_on_error_with_invalidated_route(self):
     homer, ba = self.homer, self.homer_route
     self.make_exchange('mango-cc', 37, 0, homer)
     e_id = record_exchange(self.db, ba, EUR('-32.45'), EUR('0.86'), EUR(0), homer, 'pre').id
     assert homer.balance == EUR('3.69')
     ba.update_error('invalidated')
     record_exchange_result(self.db, e_id, -e_id, 'failed', 'oops', homer)
     homer = Participant.from_username('homer')
     assert homer.balance == EUR('37.00')
     assert ba.error == 'invalidated'
Пример #11
0
 def test_record_exchange_result_restores_balance_on_error_with_invalidated_route(self):
     homer, ba = self.homer, self.homer_route
     self.make_exchange('mango-cc', 37, 0, homer)
     e_id = record_exchange(self.db, ba, EUR('-32.45'), EUR('0.86'), EUR(0), homer, 'pre').id
     assert homer.balance == D('3.69')
     ba.update_error('invalidated')
     record_exchange_result(self.db, e_id, -e_id, 'failed', 'oops', homer)
     homer = Participant.from_username('homer')
     assert homer.balance == D('37.00')
     assert ba.error == 'invalidated'
Пример #12
0
 def make_exchange(self, route, amount, fee, participant, status='succeeded', error='', vat=0):
     amount = amount if isinstance(amount, Money) else Money(amount, 'EUR')
     fee = fee if isinstance(fee, Money) else Money(fee, amount.currency)
     vat = vat if isinstance(vat, Money) else Money(vat, fee.currency)
     if not isinstance(route, ExchangeRoute):
         network = route
         currency = amount.currency if network == 'mango-cc' else None
         routes = ExchangeRoute.from_network(participant, network, currency=currency)
         if routes:
             route = routes[0]
         else:
             from .mangopay import MangopayHarness
             address = MangopayHarness.card_id if network == 'mango-cc' else -participant.id
             route = ExchangeRoute.insert(participant, network, address, currency=currency)
             assert route
     e_id = record_exchange(self.db, route, amount, fee, vat, participant, 'pre').id
     record_exchange_result(self.db, e_id, -e_id, status, error, participant)
     return e_id
Пример #13
0
 def make_exchange(self,
                   route,
                   amount,
                   fee,
                   participant,
                   status='succeeded',
                   error='',
                   vat=0):
     if not isinstance(route, ExchangeRoute):
         network = route
         routes = ExchangeRoute.from_network(participant, network)
         if routes:
             route = routes[0]
         else:
             from .mangopay import MangopayHarness
             route = ExchangeRoute.insert(participant, network,
                                          MangopayHarness.card_id)
             assert route
     e_id = record_exchange(self.db, route, amount, fee, vat, participant,
                            'pre').id
     record_exchange_result(self.db, e_id, -e_id, status, error,
                            participant)
     return e_id