Example #1
0
def payday():

    # Wire things up.
    # ===============

    env = wireup.env()
    db = wireup.db(env)

    wireup.billing(env)

    # Lazily import the billing module.
    # =================================

    from liberapay.billing.exchanges import sync_with_mangopay
    from liberapay.billing.payday import Payday

    try:
        sync_with_mangopay(db)
        Payday.start().run()
    except KeyboardInterrupt:
        pass
    except:
        import aspen
        import traceback

        aspen.log(traceback.format_exc())
Example #2
0
 def test_sync_with_mangopay(self):
     with mock.patch('liberapay.billing.exchanges.record_exchange_result') as rer:
         rer.side_effect = Foobar()
         with self.assertRaises(Foobar):
             charge(self.db, self.janet, PAYIN_CARD_MIN, 'http://localhost/')
     exchange = self.db.one("SELECT * FROM exchanges")
     assert exchange.status == 'pre'
     sync_with_mangopay(self.db)
     exchange = self.db.one("SELECT * FROM exchanges")
     assert exchange.status == 'succeeded'
     assert Participant.from_username('janet').balance == PAYIN_CARD_MIN
Example #3
0
 def test_sync_with_mangopay_deletes_charges_that_didnt_happen(self):
     with mock.patch('liberapay.billing.exchanges.record_exchange_result') as rer \
        , mock.patch('liberapay.billing.mangoapi.payIns.Create') as Create:
         rer.side_effect = Create.side_effect = Foobar
         with self.assertRaises(Foobar):
             charge(self.db, self.janet, D('33.67'), 'http://localhost/')
     exchange = self.db.one("SELECT * FROM exchanges")
     assert exchange.status == 'pre'
     sync_with_mangopay(self.db)
     exchanges = self.db.all("SELECT * FROM exchanges")
     assert not exchanges
     assert Participant.from_username('janet').balance == 0
Example #4
0
 def test_sync_with_mangopay_deletes_charges_that_didnt_happen(self):
     with mock.patch('liberapay.billing.exchanges.record_exchange_result') as rer \
        , mock.patch('liberapay.billing.mangoapi.payIns.Create') as Create:
         rer.side_effect = Create.side_effect = Foobar
         with self.assertRaises(Foobar):
             charge(self.db, self.janet, D('33.67'), 'http://localhost/')
     exchange = self.db.one("SELECT * FROM exchanges")
     assert exchange.status == 'pre'
     sync_with_mangopay(self.db)
     exchanges = self.db.all("SELECT * FROM exchanges")
     assert not exchanges
     assert Participant.from_username('janet').balance == 0
Example #5
0
 def test_sync_with_mangopay(self):
     with mock.patch(
             'liberapay.billing.exchanges.record_exchange_result') as rer:
         rer.side_effect = Foobar()
         with self.assertRaises(Foobar):
             charge(self.db, self.janet, CHARGE_MIN, 'http://localhost/')
     exchange = self.db.one("SELECT * FROM exchanges")
     assert exchange.status == 'pre'
     sync_with_mangopay(self.db)
     exchange = self.db.one("SELECT * FROM exchanges")
     assert exchange.status == 'succeeded'
     assert Participant.from_username('janet').balance == CHARGE_MIN
 def test_sync_with_mangopay_reverts_credits_that_didnt_happen(self):
     self.make_exchange('mango-cc', 41, 0, self.homer)
     with mock.patch('liberapay.billing.exchanges.record_exchange_result') as rer \
        , mock.patch('liberapay.billing.exchanges.test_hook') as test_hook:
         rer.side_effect = test_hook.side_effect = Foobar
         with self.assertRaises(Foobar):
             payout(self.db, self.homer, D('35.00'))
     exchange = self.db.one("SELECT * FROM exchanges WHERE amount < 0")
     assert exchange.status == 'pre'
     sync_with_mangopay(self.db)
     exchanges = self.db.all("SELECT * FROM exchanges WHERE amount < 0")
     assert not exchanges
     assert Participant.from_username('homer').balance == 41
Example #7
0
 def test_sync_with_mangopay_deletes_charges_that_didnt_happen(self):
     pass  # this is for pep8
     with mock.patch('liberapay.billing.exchanges.record_exchange_result') as rer, \
          mock.patch('liberapay.billing.exchanges.DirectPayIn.save', autospec=True) as save:
         rer.side_effect = save.side_effect = Foobar
         with self.assertRaises(Foobar):
             charge(self.db, self.janet, D('33.67'), 'http://localhost/')
     exchange = self.db.one("SELECT * FROM exchanges")
     assert exchange.status == 'pre'
     sync_with_mangopay(self.db)
     exchanges = self.db.all("SELECT * FROM exchanges")
     assert not exchanges
     assert Participant.from_username('janet').balance == 0
Example #8
0
 def test_sync_with_mangopay_transfers(self):
     self.make_exchange('mango-cc', 10, 0, self.janet)
     with mock.patch('liberapay.billing.exchanges.record_transfer_result') as rtr:
         rtr.side_effect = Foobar()
         with self.assertRaises(Foobar):
             transfer(self.db, self.janet.id, self.david.id, D('10.00'), 'tip')
     t = self.db.one("SELECT * FROM transfers")
     assert t.status == 'pre'
     sync_with_mangopay(self.db)
     t = self.db.one("SELECT * FROM transfers")
     assert t.status == 'succeeded'
     assert Participant.from_username('david').balance == 10
     assert Participant.from_username('janet').balance == 0
Example #9
0
 def test_sync_with_mangopay_reverts_credits_that_didnt_happen(self):
     self.make_exchange('mango-cc', 41, 0, self.homer)
     with mock.patch('liberapay.billing.exchanges.record_exchange_result') as rer \
        , mock.patch('liberapay.billing.exchanges.test_hook') as test_hook:
         rer.side_effect = test_hook.side_effect = Foobar
         with self.assertRaises(Foobar):
             payout(self.db, self.homer, D('35.00'))
     exchange = self.db.one("SELECT * FROM exchanges WHERE amount < 0")
     assert exchange.status == 'pre'
     sync_with_mangopay(self.db)
     exchange = self.db.one("SELECT * FROM exchanges WHERE amount < 0")
     assert exchange.status == 'failed'
     homer = self.homer.refetch()
     assert homer.balance == homer.withdrawable_balance == 41
Example #10
0
 def test_sync_with_mangopay_deletes_transfers_that_didnt_happen(self):
     self.make_exchange('mango-cc', 10, 0, self.janet)
     with mock.patch('liberapay.billing.exchanges.record_transfer_result') as rtr \
        , mock.patch('liberapay.billing.mangoapi.transfers.Create') as Create:
         rtr.side_effect = Create.side_effect = Foobar
         with self.assertRaises(Foobar):
             transfer(self.db, self.janet.id, self.david.id, D('10.00'), 'tip')
     t = self.db.one("SELECT * FROM transfers")
     assert t.status == 'pre'
     sync_with_mangopay(self.db)
     transfers = self.db.all("SELECT * FROM transfers")
     assert not transfers
     assert Participant.from_username('david').balance == 0
     assert Participant.from_username('janet').balance == 10
Example #11
0
 def test_sync_with_mangopay_reverts_credits_that_didnt_happen(self):
     self.make_exchange('mango-cc', 41, 0, self.homer)
     with mock.patch('liberapay.billing.exchanges.record_exchange_result') as rer, \
          mock.patch('liberapay.billing.exchanges.test_hook') as test_hook:
         rer.side_effect = test_hook.side_effect = Foobar
         with self.assertRaises(Foobar):
             payout(self.db, self.homer, D('35.00'))
     exchange = self.db.one("SELECT * FROM exchanges WHERE amount < 0")
     assert exchange.status == 'pre'
     sync_with_mangopay(self.db)
     exchange = self.db.one("SELECT * FROM exchanges WHERE amount < 0")
     assert exchange.status == 'failed'
     homer = self.homer.refetch()
     assert homer.balance == homer.withdrawable_balance == 41
Example #12
0
 def test_sync_with_mangopay_transfers(self):
     self.make_exchange('mango-cc', 10, 0, self.janet)
     with mock.patch(
             'liberapay.billing.exchanges.record_transfer_result') as rtr:
         rtr.side_effect = Foobar()
         with self.assertRaises(Foobar):
             transfer(self.db, self.janet.id, self.david.id, D('10.00'),
                      'tip')
     t = self.db.one("SELECT * FROM transfers")
     assert t.status == 'pre'
     sync_with_mangopay(self.db)
     t = self.db.one("SELECT * FROM transfers")
     assert t.status == 'succeeded'
     assert Participant.from_username('david').balance == 10
     assert Participant.from_username('janet').balance == 0
Example #13
0
 def test_sync_with_mangopay_deletes_transfers_that_didnt_happen(self):
     self.make_exchange('mango-cc', 10, 0, self.janet)
     with mock.patch('liberapay.billing.exchanges.record_transfer_result') as rtr, \
          mock.patch('liberapay.billing.mangoapi.transfers.Create') as Create:
         rtr.side_effect = Create.side_effect = Foobar
         with self.assertRaises(Foobar):
             transfer(self.db, self.janet.id, self.david.id, D('10.00'),
                      'tip')
     t = self.db.one("SELECT * FROM transfers")
     assert t.status == 'pre'
     sync_with_mangopay(self.db)
     transfers = self.db.all("SELECT * FROM transfers")
     assert not transfers
     assert Participant.from_username('david').balance == 0
     assert Participant.from_username('janet').balance == 10
Example #14
0
def main(override_payday_checks=False):
    from liberapay.billing.exchanges import sync_with_mangopay
    from liberapay.main import website

    # https://github.com/liberapay/salon/issues/19#issuecomment-191230689
    from liberapay.billing.payday import Payday

    if not website.env.override_payday_checks and not override_payday_checks:
        # Check that payday hasn't already been run today
        r = website.db.one("""
            SELECT id
              FROM paydays
             WHERE ts_start >= now() - INTERVAL '6 days'
               AND ts_end >= ts_start
        """)
        assert not r, "payday has already been run this week"
        # Check that today is Wednesday
        wd = date.today().isoweekday()
        assert wd == 3, "today is not Wednesday (%s != 3)" % wd

    # Prevent a race condition, by acquiring a DB lock
    conn = website.db.get_connection().__enter__()
    cursor = conn.cursor()
    lock = cursor.one("SELECT pg_try_advisory_lock(1)")
    assert lock, "failed to acquire the payday lock"

    try:
        sync_with_mangopay(website.db)
        Payday.start().run(website.env.log_dir, website.env.keep_payday_logs)
    except KeyboardInterrupt:  # pragma: no cover
        pass
    except Exception as e:  # pragma: no cover
        website.tell_sentry(e, {})
        raise
    finally:
        conn.close()
Example #15
0
def main():
    from os import environ

    from liberapay.billing.exchanges import sync_with_mangopay
    from liberapay.main import website

    # https://github.com/liberapay/salon/issues/19#issuecomment-191230689
    from liberapay.billing.payday import Payday

    if website.env.canonical_host == 'liberapay.com':
        log_dir = environ['OPENSHIFT_DATA_DIR']
        keep_log = True
    else:
        log_dir = ''
        keep_log = False

    try:
        sync_with_mangopay(website.db)
        Payday.start().run(log_dir, keep_log)
    except KeyboardInterrupt:
        pass
    except:
        import traceback
        traceback.print_exc()
Example #16
0
def main(override_payday_checks=False):
    from liberapay.billing.exchanges import sync_with_mangopay
    from liberapay.main import website

    # https://github.com/liberapay/salon/issues/19#issuecomment-191230689
    from liberapay.billing.payday import Payday

    if not website.env.override_payday_checks and not override_payday_checks:
        # Check that payday hasn't already been run today
        r = website.db.one("""
            SELECT id
              FROM paydays
             WHERE ts_start >= now() - INTERVAL '6 days'
               AND ts_end >= ts_start
        """)
        assert not r, "payday has already been run this week"
        # Check that today is Wednesday
        wd = date.today().isoweekday()
        assert wd == 3, "today is not Wednesday (%s != 3)" % wd

    # Prevent a race condition, by acquiring a DB lock
    conn = website.db.get_connection().__enter__()
    cursor = conn.cursor()
    lock = cursor.one("SELECT pg_try_advisory_lock(1)")
    assert lock, "failed to acquire the payday lock"

    try:
        sync_with_mangopay(website.db)
        Payday.start().run(website.env.log_dir, website.env.keep_payday_logs)
    except KeyboardInterrupt:  # pragma: no cover
        pass
    except Exception as e:  # pragma: no cover
        website.tell_sentry(e, {}, allow_reraise=False)
        raise
    finally:
        conn.close()
Example #17
0
def main():
    from os import environ

    from liberapay.billing.exchanges import sync_with_mangopay
    from liberapay.main import website

    # https://github.com/liberapay/salon/issues/19#issuecomment-191230689
    from liberapay.billing.payday import Payday

    if website.env.canonical_host == 'liberapay.com':
        log_dir = environ['OPENSHIFT_DATA_DIR']
        keep_log = True
    else:
        log_dir = ''
        keep_log = False

    try:
        sync_with_mangopay(website.db)
        Payday.start().run(log_dir, keep_log)
    except KeyboardInterrupt:
        pass
    except:
        import traceback
        traceback.print_exc()
Example #18
0
        for p in participants:
            p.notify('low_balance')


if __name__ == '__main__':  # pragma: no cover
    from os import environ

    from liberapay import wireup
    from liberapay.billing.exchanges import sync_with_mangopay

    env = wireup.env()
    db = wireup.db(env)
    Payday.db = db
    wireup.billing(env)

    if env.mangopay_client_id == 'liberapay':
        log_dir = environ['OPENSHIFT_DATA_DIR']
        keep_log = True
    else:
        log_dir = ''
        keep_log = False

    try:
        sync_with_mangopay(db)
        Payday.start().run(log_dir, keep_log)
    except KeyboardInterrupt:
        pass
    except:
        import traceback
        traceback.print_exc()
Example #19
0
                      WHERE t.tipper = p.id
                        AND t.timestamp > %s
                        AND t.timestamp <= %s
                        AND t.status = 'succeeded'
                   )
        """, (previous_ts_end, self.ts_end))
        for p in participants:
            p.notify('low_balance')


if __name__ == '__main__':  # pragma: no cover
    from liberapay import wireup
    from liberapay.billing.exchanges import sync_with_mangopay

    # Wire things up.
    # ===============

    env = wireup.env()
    db = wireup.db(env)
    Payday.db = db
    wireup.billing(env)

    try:
        sync_with_mangopay(db)
        Payday.start().run()
    except KeyboardInterrupt:
        pass
    except:
        import traceback
        traceback.print_exc()