示例#1
0
 def create(self):
     author = self.mangopay_user.get_user()
     payin = self.mangopay_pay_in.get_pay_in()
     payin_refund = PayInRefund(
         author=author,
         payin=payin,
     )
     payin_refund.save()
     self.mangopay_id = payin_refund.get_pk()
     self.status = payin_refund.status
     self.result_code = payin_refund.result_code
     self.execution_date = get_execution_date_as_datetime(payin_refund)
     self.save()
     return self
示例#2
0
def refund_payin(db, exchange, amount, participant):
    """Refund a specific payin.
    """
    assert participant.id == exchange.participant

    # Record the refund attempt
    fee = vat = amount.zero()
    with db.get_cursor() as cursor:
        cursor.run("LOCK TABLE cash_bundles IN EXCLUSIVE MODE")
        bundles = cursor.all(
            """
            SELECT b.id
              FROM cash_bundles b
             WHERE b.owner = %s
        """, (participant.id, ))
        e_refund = cursor.one(
            """
            INSERT INTO exchanges
                        (participant, amount, fee, vat, route, status, refund_ref, wallet_id)
                 VALUES (%s, %s, %s, %s, %s, 'pre', %s, %s)
              RETURNING *
        """, (participant.id, -amount, fee, vat, exchange.route, exchange.id,
              exchange.wallet_id))
        cursor.run(
            """
            INSERT INTO exchange_events
                        (timestamp, exchange, status, wallet_delta)
                 VALUES (%s, %s, 'pre', %s)
        """, (e_refund.timestamp, e_refund.id, e_refund.amount - e_refund.fee))
        propagate_exchange(cursor,
                           participant,
                           e_refund,
                           None,
                           e_refund.amount,
                           bundles=bundles)

    # Submit the refund
    wallet = db.one("SELECT * FROM wallets WHERE remote_id = %s",
                    (exchange.wallet_id, ))
    m_refund = PayInRefund(payin_id=exchange.remote_id)
    m_refund.AuthorId = wallet.remote_owner_id
    m_refund.Tag = str(e_refund.id)
    m_refund.DebitedFunds = amount.int()
    m_refund.Fees = -fee.int()
    try:
        m_refund.save()
    except Exception as e:
        error = repr_exception(e)
        e_refund = record_exchange_result(db, e_refund.id, '', 'failed', error,
                                          participant)
        return 'exception', e_refund
    e_refund = record_exchange_result(db, e_refund.id, m_refund.Id,
                                      m_refund.Status.lower(),
                                      repr_error(m_refund), participant)
    return e_refund.status, e_refund
示例#3
0
def refund_payin(db, exchange, amount, participant):
    """Refund a specific payin.
    """
    assert participant.id == exchange.participant

    # Record the refund attempt
    fee = vat = amount.zero()
    with db.get_cursor() as cursor:
        cursor.run("LOCK TABLE cash_bundles IN EXCLUSIVE MODE")
        bundles = cursor.all("""
            SELECT b.id
              FROM cash_bundles b
             WHERE b.owner = %s
        """, (participant.id,))
        e_refund = cursor.one("""
            INSERT INTO exchanges
                        (participant, amount, fee, vat, route, status, refund_ref, wallet_id)
                 VALUES (%s, %s, %s, %s, %s, 'pre', %s, %s)
              RETURNING *
        """, (participant.id, -amount, fee, vat, exchange.route, exchange.id, exchange.wallet_id))
        cursor.run("""
            INSERT INTO exchange_events
                        (timestamp, exchange, status, wallet_delta)
                 VALUES (%s, %s, 'pre', %s)
        """, (e_refund.timestamp, e_refund.id, e_refund.amount - e_refund.fee))
        propagate_exchange(cursor, participant, e_refund, None, e_refund.amount, bundles=bundles)

    # Submit the refund
    wallet = db.one("SELECT * FROM wallets WHERE remote_id = %s", (exchange.wallet_id,))
    m_refund = PayInRefund(payin_id=exchange.remote_id)
    m_refund.AuthorId = wallet.remote_owner_id
    m_refund.Tag = str(e_refund.id)
    m_refund.DebitedFunds = Money_to_cents(amount)
    m_refund.Fees = -Money_to_cents(fee)
    try:
        m_refund.save()
    except Exception as e:
        error = repr_exception(e)
        e_refund = record_exchange_result(db, e_refund.id, '', 'failed', error, participant)
        return 'exception', e_refund
    e_refund = record_exchange_result(
        db, e_refund.id, m_refund.Id, m_refund.Status.lower(), repr_error(m_refund), participant
    )
    return e_refund.status, e_refund
示例#4
0
def refund_payin(db, exchange, create_debts=False, refund_fee=False, dry_run=False):
    """Refund a specific payin.
    """
    assert exchange.status == 'succeeded' and exchange.remote_id, exchange
    e_refund = db.one("SELECT e.* FROM exchanges e WHERE e.refund_ref = %s", (exchange.id,))
    if e_refund and e_refund.status == 'succeeded':
        return 'already done', e_refund

    # Lock the bundles and try to swap them
    with db.get_cursor() as cursor:
        cursor.run("LOCK TABLE cash_bundles IN EXCLUSIVE MODE")
        bundles = [NS(d._asdict()) for d in cursor.all("""
            UPDATE cash_bundles
               SET disputed = true
             WHERE origin = %s
         RETURNING *
        """, (exchange.id,))]
        bundles_sum = sum(b.amount for b in bundles)
        assert bundles_sum == exchange.amount
        original_owner = exchange.participant
        for b in bundles:
            if b.owner == original_owner:
                continue
            try_to_swap_bundle(cursor, b, original_owner)

    # Move the funds back to the original wallet
    LiberapayOrg = Participant.from_username('LiberapayOrg')
    assert LiberapayOrg
    return_payin_bundles_to_origin(db, exchange, LiberapayOrg, create_debts)

    # Add a debt for the fee
    if create_debts and refund_fee:
        create_debt(db, original_owner, LiberapayOrg.id, exchange.fee, exchange.id)

    # Compute and check the amount
    wallet = db.one("SELECT * FROM wallets WHERE remote_id = %s", (exchange.wallet_id,))
    if e_refund and e_refund.status == 'pre':
        amount = -e_refund.amount
    else:
        amount = min(wallet.balance, exchange.amount)
        if amount <= 0:
            return ('not enough money: wallet balance = %s' % wallet.balance), None

    # Stop here if this is a dry run
    zero = exchange.fee.zero()
    fee, vat = (exchange.fee, exchange.vat) if refund_fee else (zero, zero)
    if dry_run:
        msg = (
            '[dry run] full refund of payin #%s (liberapay id %s): amount = %s, fee = %s' %
            (exchange.remote_id, exchange.id, exchange.amount, exchange.fee)
        ) if amount + fee == exchange.amount + exchange.fee else (
            '[dry run] partial refund of payin #%s (liberapay id %s): %s of %s, fee %s of %s' %
            (exchange.remote_id, exchange.id, amount, exchange.amount, fee, exchange.fee)
        )
        return msg, None

    # Record the refund attempt
    participant = Participant.from_id(exchange.participant)
    if not (e_refund and e_refund.status == 'pre'):
        with db.get_cursor() as cursor:
            cursor.run("LOCK TABLE cash_bundles IN EXCLUSIVE MODE")
            bundles = [NS(d._asdict()) for d in cursor.all("""
                SELECT *
                  FROM cash_bundles
                 WHERE origin = %s
                   AND wallet_id = %s
                   AND disputed = true
            """, (exchange.id, exchange.wallet_id))]
            e_refund = cursor.one("""
                INSERT INTO exchanges
                            (participant, amount, fee, vat, route, status, refund_ref, wallet_id)
                     VALUES (%s, %s, %s, %s, %s, 'pre', %s, %s)
                  RETURNING *
            """, (participant.id, -amount, -fee, -vat, exchange.route, exchange.id, exchange.wallet_id))
            propagate_exchange(cursor, participant, e_refund, None, e_refund.amount, bundles=bundles)

    # Submit the refund
    m_refund = PayInRefund(payin_id=exchange.remote_id)
    m_refund.AuthorId = wallet.remote_owner_id
    m_refund.Tag = str(e_refund.id)
    m_refund.DebitedFunds = amount.int()
    m_refund.Fees = -fee.int()
    try:
        m_refund.save()
    except Exception as e:
        error = repr_exception(e)
        e_refund = record_exchange_result(db, e_refund.id, '', 'failed', error, participant)
        return 'exception', e_refund
    e_refund = record_exchange_result(
        db, e_refund.id, m_refund.Id, m_refund.Status.lower(), repr_error(m_refund), participant
    )
    return e_refund.status, e_refund
示例#5
0
def refund_disputed_payin(db, exchange, create_debts=False, refund_fee=False, dry_run=False):
    """Refund a specific disputed payin.
    """
    assert exchange.status == 'succeeded' and exchange.remote_id, exchange
    e_refund = db.one("SELECT e.* FROM exchanges e WHERE e.refund_ref = %s", (exchange.id,))
    if e_refund and e_refund.status == 'succeeded':
        return 'already done', e_refund

    # Lock the bundles and try to swap them
    with db.get_cursor() as cursor:
        cursor.run("LOCK TABLE cash_bundles IN EXCLUSIVE MODE")
        bundles = [NS(d._asdict()) for d in cursor.all("""
            UPDATE cash_bundles
               SET disputed = true
             WHERE origin = %s
         RETURNING *
        """, (exchange.id,))]
        bundles_sum = sum(b.amount for b in bundles)
        assert bundles_sum == exchange.amount
        original_owner = exchange.participant
        for b in bundles:
            if b.owner == original_owner:
                continue
            try_to_swap_bundle(cursor, b, original_owner)

    # Move the funds back to the original wallet
    LiberapayOrg = Participant.from_username('LiberapayOrg')
    assert LiberapayOrg
    return_payin_bundles_to_origin(db, exchange, LiberapayOrg, create_debts)

    # Add a debt for the fee
    if create_debts and refund_fee:
        create_debt(db, original_owner, LiberapayOrg.id, exchange.fee, exchange.id)

    # Compute and check the amount
    wallet = db.one("SELECT * FROM wallets WHERE remote_id = %s", (exchange.wallet_id,))
    if e_refund and e_refund.status == 'pre':
        amount = -e_refund.amount
    else:
        amount = min(wallet.balance, exchange.amount)
        if amount <= 0:
            return ('not enough money: wallet balance = %s' % wallet.balance), None

    # Stop here if this is a dry run
    zero = exchange.fee.zero()
    fee, vat = (exchange.fee, exchange.vat) if refund_fee else (zero, zero)
    if dry_run:
        msg = (
            '[dry run] full refund of payin #%s (liberapay id %s): amount = %s, fee = %s' %
            (exchange.remote_id, exchange.id, exchange.amount, exchange.fee)
        ) if amount + fee == exchange.amount + exchange.fee else (
            '[dry run] partial refund of payin #%s (liberapay id %s): %s of %s, fee %s of %s' %
            (exchange.remote_id, exchange.id, amount, exchange.amount, fee, exchange.fee)
        )
        return msg, None

    # Record the refund attempt
    participant = Participant.from_id(exchange.participant)
    if not (e_refund and e_refund.status == 'pre'):
        with db.get_cursor() as cursor:
            cursor.run("LOCK TABLE cash_bundles IN EXCLUSIVE MODE")
            bundles = cursor.all("""
                SELECT id
                  FROM cash_bundles
                 WHERE origin = %s
                   AND wallet_id = %s
                   AND disputed = true
            """, (exchange.id, exchange.wallet_id))
            e_refund = cursor.one("""
                INSERT INTO exchanges
                            (participant, amount, fee, vat, route, status, refund_ref, wallet_id)
                     VALUES (%s, %s, %s, %s, %s, 'pre', %s, %s)
                  RETURNING *
            """, (participant.id, -amount, -fee, -vat, exchange.route, exchange.id, exchange.wallet_id))
            cursor.run("""
                INSERT INTO exchange_events
                            (timestamp, exchange, status, wallet_delta)
                     VALUES (%s, %s, 'pre', %s)
            """, (e_refund.timestamp, e_refund.id, e_refund.amount - e_refund.fee))
            propagate_exchange(cursor, participant, e_refund, None, e_refund.amount, bundles=bundles)

    # Submit the refund
    m_refund = PayInRefund(payin_id=exchange.remote_id)
    m_refund.AuthorId = wallet.remote_owner_id
    m_refund.Tag = str(e_refund.id)
    m_refund.DebitedFunds = Money_to_cents(amount)
    m_refund.Fees = -Money_to_cents(fee)
    try:
        m_refund.save()
    except Exception as e:
        error = repr_exception(e)
        e_refund = record_exchange_result(db, e_refund.id, '', 'failed', error, participant)
        return 'exception', e_refund
    e_refund = record_exchange_result(
        db, e_refund.id, m_refund.Id, m_refund.Status.lower(), repr_error(m_refund), participant
    )
    return e_refund.status, e_refund
    def test_RecurringPayment(self):
        user = self.get_john(True)
        wallet = self.get_johns_wallet(True)
        card = BaseTestLive.get_johns_card_3dsecure(True)

        recurring = RecurringPayInRegistration()
        recurring.author = user
        recurring.card = card
        recurring.user = user
        recurring.credited_wallet = wallet
        recurring.first_transaction_fees = Money(1, "EUR")
        recurring.first_transaction_debited_funds = Money(12, "EUR")
        address = Address()
        address.address_line_1 = "Big Street"
        address.address_line_2 = "no 2 ap 6"
        address.country = "FR"
        address.city = "Lyon"
        address.postal_code = "68400"
        recurring.billing = Billing(first_name="John",
                                    last_name="Doe",
                                    address=address)
        recurring.shipping = Shipping(first_name="John",
                                      last_name="Doe",
                                      address=address)
        recurring.end_date = 1768656033
        recurring.migration = True
        recurring.next_transaction_fees = Money(1, "EUR")
        recurring.next_transaction_debited_funds = Money(12, "EUR")
        result = recurring.save()
        self.assertIsNotNone(result)

        created_recurring = RecurringPayInRegistration.get(result.get('id'))
        self.assertIsNotNone(created_recurring)
        print(created_recurring.id)
        cit = RecurringPayInCIT()
        cit.recurring_payin_registration_id = created_recurring.id
        cit.tag = "custom meta"
        cit.statement_descriptor = "lorem"
        cit.secure_mode_return_url = "http://www.my-site.com/returnurl"
        cit.ip_address = "2001:0620:0000:0000:0211:24FF:FE80:C12C"
        browser = BrowserInfo()
        browser.accept_header = "text/html, application/xhtml+xml, application/xml;q=0.9, /;q=0.8"
        browser.java_enabled = True
        browser.language = "FR-FR"
        browser.color_depth = 4
        browser.screen_width = 400
        browser.screen_height = 1800
        browser.javascript_enabled = True
        browser.timezone_offset = "+60"
        browser.user_agent = "Mozilla/5.0 (iPhone; CPU iPhone OS 13_6_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148"
        cit.browser_info = browser
        cit.debited_funds = Money(12, "EUR")
        cit.fees = Money(1, "EUR")

        created_cit = cit.save()
        self.assertIsNotNone(created_cit)
        cit_id = created_cit.get('id')

        got_cit = RecurringPayInCIT.get(cit_id)
        self.assertIsNotNone(got_cit)
        self.assertIsInstance(got_cit, RecurringPayInCIT)

        mit = RecurringPayInMIT()
        mit.recurring_payin_registration_id = created_recurring.id
        mit.statement_descriptor = "lorem"
        mit.tag = "custom meta"
        mit.debited_funds = Money(10, "EUR")
        mit.fees = Money(1, "EUR")
        created_mit = mit.save()
        self.assertIsNotNone(created_mit)

        got_cit = RecurringPayInCIT.get(cit_id)
        self.assertIsNotNone(got_cit)
        #self.assertIsInstance(got_cit, RecurringPayInCIT)

        params = {"author": user, "payin": got_cit}

        payin_refund = PayInRefund(**params)

        self.assertIsNotNone(payin_refund)
        self.assertIsNone(payin_refund.get_pk())
        payin_refund.save()
        self.assertIsInstance(payin_refund, PayInRefund)
        self.assertEqual(payin_refund.status, 'SUCCEEDED')

        mit_id = created_mit.get('id')
        got_mit = RecurringPayInMIT.get(mit_id)
        self.assertIsNotNone(got_mit)
        self.assertIsInstance(got_mit, RecurringPayInMIT)

        params = {"author": user, "payin": got_mit}

        payin_refund_mit = PayInRefund(**params)

        self.assertIsNotNone(payin_refund_mit)
        self.assertIsNone(payin_refund_mit.get_pk())
        payin_refund_mit.save()
        self.assertIsInstance(payin_refund_mit, PayInRefund)
        self.assertEqual(payin_refund_mit.status, 'SUCCEEDED')