Exemple #1
0
def execute_direct_debit(db, exchange, route):
    """Execute a prepared direct debit.
    """
    assert exchange.route == route.id
    assert route
    assert route.network == 'mango-ba'
    assert route.mandate

    participant = route.participant
    assert exchange.participant == participant.id

    if exchange.status == 'pre-mandate':
        exchange = db.one(
            """
            UPDATE exchanges
               SET status = 'pre'
             WHERE id = %s
               AND status = %s
         RETURNING *
        """, (exchange.id, exchange.status))
        assert exchange, 'race condition'

    assert exchange.status == 'pre'

    amount, fee = exchange.amount, exchange.fee
    debit_amount = amount + fee

    e_id = exchange.id
    payin = DirectDebitDirectPayIn()
    payin.AuthorId = participant.mangopay_user_id
    payin.CreditedWalletId = exchange.wallet_id
    payin.DebitedFunds = debit_amount.int()
    payin.MandateId = route.mandate
    payin.Fees = fee.int()
    payin.Tag = str(e_id)
    try:
        test_hook()
        payin.save()
    except Exception as e:
        error = repr_exception(e)
        return record_exchange_result(db, e_id, '', 'failed', error,
                                      participant)

    return record_exchange_result(db, e_id, payin.Id, payin.Status.lower(),
                                  repr_error(payin), participant)
def execute_direct_debit(db, exchange, route):
    """Execute a prepared direct debit.
    """
    assert exchange.route == route.id
    assert route
    assert route.network == 'mango-ba'
    assert route.mandate

    participant = route.participant
    assert exchange.participant == participant.id

    if exchange.status == 'pre-mandate':
        exchange = db.one("""
            UPDATE exchanges
               SET status = 'pre'
             WHERE id = %s
               AND status = %s
         RETURNING *
        """, (exchange.id, exchange.status))
        assert exchange, 'race condition'

    assert exchange.status == 'pre'

    amount, fee = exchange.amount, exchange.fee
    debit_amount = amount + fee

    e_id = exchange.id
    payin = DirectDebitDirectPayIn()
    payin.AuthorId = participant.mangopay_user_id
    payin.CreditedWalletId = exchange.wallet_id
    payin.DebitedFunds = Money_to_cents(debit_amount)
    payin.MandateId = route.mandate
    payin.Fees = Money_to_cents(fee)
    payin.Tag = str(e_id)
    try:
        test_hook()
        payin.save()
    except Exception as e:
        error = repr_exception(e)
        return record_exchange_result(db, e_id, '', 'failed', error, participant)

    return record_exchange_result(
        db, e_id, payin.Id, payin.Status.lower(), repr_error(payin), participant
    )
    def test_PayIns_DirectDebitDirect_Create(self):
        # create wallet
        wallet = Wallet()
        wallet.owners = (BaseTestLive.get_john(), )
        wallet.currency = 'EUR'
        wallet.description = 'WALLET IN EUR'
        wallet = Wallet(**wallet.save())

        mandate = Mandate()
        mandate.bank_account_id = BaseTestLive.get_johns_account().id
        mandate.return_url = 'http://test.test'
        mandate.culture = 'FR'
        mandate = Mandate(**mandate.save())

        #       ! IMPORTANT NOTE !
        #       In order to make this test pass, at this place you have to set a breakpoint,
        #       navigate to URL the mandate.RedirectURL property points to and click "CONFIRM" button.

        post = DirectDebitDirectPayIn()
        post.author = BaseTestLive.get_john()
        post.credited_wallet = wallet
        post.debited_funds = Money('1000', 'EUR')
        post.fees = Money('0', 'EUR')
        post.mandate = mandate

        result = post.save()

        self.assertIsNotNone(result)
        self.assertFalse(
            'FAILED' == result['status'],
            'In order to make this test pass, after creating mandate and before creating the payin you have\
                          to navigate to URL the mandate.redirect_url property points to and click CONFIRM button.'
        )
        self.assertTrue(result['id'])
        self.assertEqual(wallet.id, result['credited_wallet_id'])
        self.assertEqual('DIRECT_DEBIT', result['payment_type'])
        self.assertEqual('DIRECT', result['execution_type'])
        self.assertEqual(BaseTestLive.get_john().id, result['author_id'])
        self.assertEqual('CREATED', result['status'])
        self.assertEqual('PAYIN', result['type'])
        self.assertIsNotNone(result['mandate_id'])
        self.assertEqual(mandate.id, result['mandate_id'])
    def test_PayIns_DirectDebitDirect_Create(self):
        # create wallet
        wallet = Wallet()
        wallet.owners = (BaseTestLive.get_john(),)
        wallet.currency = 'EUR'
        wallet.description = 'WALLET IN EUR'
        wallet = Wallet(**wallet.save())

        mandate = Mandate()
        mandate.bank_account_id = BaseTestLive.get_johns_account().id
        mandate.return_url = 'http://test.test'
        mandate.culture = 'FR'
        mandate = Mandate(**mandate.save())

        #       ! IMPORTANT NOTE !
        #       In order to make this test pass, at this place you have to set a breakpoint,
        #       navigate to URL the mandate.RedirectURL property points to and click "CONFIRM" button.

        post = DirectDebitDirectPayIn()
        post.author = BaseTestLive.get_john()
        post.credited_wallet = wallet
        post.debited_funds = Money('1000', 'EUR')
        post.fees = Money('0', 'EUR')
        post.mandate = mandate

        result = post.save()

        self.assertIsNotNone(result)
        self.assertFalse('FAILED' == result['status'],
                         'In order to make this test pass, after creating mandate and before creating the payin you have\
                          to navigate to URL the mandate.redirect_url property points to and click CONFIRM button.')
        self.assertTrue(result['id'])
        self.assertEqual(wallet.id, result['credited_wallet_id'])
        self.assertEqual('DIRECT_DEBIT', result['payment_type'])
        self.assertEqual('DIRECT', result['execution_type'])
        self.assertEqual(BaseTestLive.get_john().id, result['author_id'])
        self.assertEqual('CREATED', result['status'])
        self.assertEqual('PAYIN', result['type'])
        self.assertIsNotNone(result['mandate_id'])
        self.assertEqual(mandate.id, result['mandate_id'])