Beispiel #1
0
    def test_basic_transaction(self):
        t = Transact()
        t.transaction(self.user_a,
                      'test-party',
                      'test-reference-number-test_basic_transaction',
                      self.currency,
                      credit=Decimal('25.0000'))
        t.external_ledger(self.user_a,
                          'test-party',
                          'test-reference-number-test_basic_transaction',
                          self.currency,
                          credit=Decimal('25.0000'))
        assert_true(t.verify())

        t.execute()

        txn = TransactionModel.query.filter_by(
            balance_id=self.user_a_balance.id).all()
        assert_equal(1, len(txn))
        txn = txn[0]
        assert_equal(Decimal('25.0000'), txn.credit)
        assert_true(txn.debit is None)

        ldgr = ExternalLedgerModel.query.filter_by(record_id=t.id).all()
        assert_equal(1, len(ldgr))
        ldgr = ldgr[0]
        assert_equal(Decimal('25.0000'), ldgr.credit)
        assert_equal('test-party', ldgr.processor)

        assert_equal(Decimal('75.0000'), self.user_a_balance.amount)
Beispiel #2
0
    def test_basic_transaction(self):
        t = Transact()
        t.transaction(self.user_a,
                      'test-party',
                      'test-reference-number-test_basic_transaction',
                      self.currency,
                      credit=Decimal('25.0000'))
        t.external_ledger(self.user_a,
                          'test-party',
                          'test-reference-number-test_basic_transaction',
                          self.currency,
                          credit=Decimal('25.0000'))
        assert_true(t.verify())

        t.execute()

        txn = TransactionModel.query.filter_by(balance_id=self.user_a_balance.id).all()
        assert_equal(1, len(txn))
        txn = txn[0]
        assert_equal(Decimal('25.0000'), txn.credit)
        assert_true(txn.debit is None)

        ldgr = ExternalLedgerModel.query.filter_by(record_id=t.id).all()
        assert_equal(1, len(ldgr))
        ldgr = ldgr[0]
        assert_equal(Decimal('25.0000'), ldgr.credit)
        assert_equal('test-party', ldgr.processor)

        assert_equal(Decimal('75.0000'), self.user_a_balance.amount)
Beispiel #3
0
 def test_insufficient_funds_transaction(self):
     t = Transact()
     t.transaction(self.user_a,
                   'test-party',
                   'test-reference-number-test_test_insufficient_funds_transaction',
                   self.currency,
                   debit=Decimal('55.0000'))
     t.execute()
Beispiel #4
0
 def test_insufficient_funds_transaction(self):
     t = Transact()
     t.transaction(
         self.user_a,
         'test-party',
         'test-reference-number-test_test_insufficient_funds_transaction',
         self.currency,
         debit=Decimal('55.0000'))
     t.execute()
Beispiel #5
0
    def test_transfer_with_fee(self):
        raise SkipTest()
        t = Transact()
        t.transaction(self.user_a,
                      'test-party',
                      'test-reference-number-test_transfer_with_fee',
                      self.currency,
                      debit=Decimal('25.0000'))
        t.external_ledger(self.user_a,
                          'test-party',
                          'test-reference-number-test_transfer_with_fee',
                          self.currency,
                          debit=Decimal('5.0000'),
                          fee=self.fee)
        t.transaction(self.user_a,
                      'test-party',
                      'test-reference-number-test_transfer_with_fee',
                      self.currency,
                      debit=Decimal('5.0000'),
                      fee=self.fee)
        t.external_ledger(self.user_a,
                          'test-party',
                          'test-reference-number-test_transfer_with_fee',
                          self.currency,
                          debit=Decimal('5.0000'),
                          fee=self.fee)
        t.transaction(self.user_a,
                      'poold',
                      'test-reference-number-test_transfer_with_fee',
                      self.currency,
                      debit=Decimal('5.0000'),
                      fee=self.fee)
        t.external_ledger(self.user_a,
                          'poold',
                          'test-reference-number-test_transfer_with_fee',
                          self.currency,
                          debit=Decimal('5.0000'),
                          fee=self.fee)

        assert_true(t.verify())

        t.execute()

        txn = TransactionModel.query.filter_by(
            balance_id=self.user_a_balance.id).all()
        assert_equal(1, len(txn))
        txn = txn[0]
        assert_equal(Decimal('35.0000'), txn.debit)
        assert_true(txn.credit is None)

        ldgr = ExternalLedgerModel.query.filter_by(record_id=t.id).all()
        assert_equal(3, len(ldgr))

        party_ldgr = [
            l for l in ldgr if l.party == 'test-party' and l.fee is None
        ]
        assert_equal(1, len(party_ldgr))
        party_ldgr = party_ldgr[0]
        assert_equal(Decimal('25.0000'), party_ldgr.debit)
        assert_equal('test-party', party_ldgr.party)
        assert_true(party_ldgr.credit is None)

        party_fee_ldgr = [
            l for l in ldgr if l.party == 'test-party' and l.fee is not None
        ]
        assert_equal(1, len(party_fee_ldgr))
        party_fee_ldgr = party_fee_ldgr[0]
        assert_equal(Decimal('5.0000'), party_fee_ldgr.debit)
        assert_equal('test-party', party_fee_ldgr.party)
        assert_true(party_fee_ldgr.credit is None)

        poold_ldgr = [l for l in ldgr if l.party == 'poold']
        assert_equal(1, len(poold_ldgr))
        poold_ldgr = poold_ldgr[0]
        assert_equal(Decimal('5.0000'), poold_ldgr.debit)
        assert_equal('poold', poold_ldgr.party)
        assert_true(poold_ldgr.credit is None)

        assert_equal(Decimal('15.0000'), self.user_a_balance.amount)
Beispiel #6
0
    def test_transfer_with_fee(self):
        raise SkipTest()
        t = Transact()
        t.transaction(self.user_a,
                      'test-party',
                      'test-reference-number-test_transfer_with_fee',
                      self.currency,
                      debit=Decimal('25.0000'))
        t.external_ledger(self.user_a,
                          'test-party',
                          'test-reference-number-test_transfer_with_fee',
                          self.currency,
                          debit=Decimal('5.0000'),
                          fee=self.fee)
        t.transaction(self.user_a,
                      'test-party',
                      'test-reference-number-test_transfer_with_fee',
                      self.currency,
                      debit=Decimal('5.0000'),
                      fee=self.fee)
        t.external_ledger(self.user_a,
                          'test-party',
                          'test-reference-number-test_transfer_with_fee',
                          self.currency,
                          debit=Decimal('5.0000'),
                          fee=self.fee)
        t.transaction(self.user_a,
                      'poold',
                      'test-reference-number-test_transfer_with_fee',
                      self.currency,
                      debit=Decimal('5.0000'),
                      fee=self.fee)
        t.external_ledger(self.user_a,
                          'poold',
                          'test-reference-number-test_transfer_with_fee',
                          self.currency,
                          debit=Decimal('5.0000'),
                          fee=self.fee)

        assert_true(t.verify())

        t.execute()

        txn = TransactionModel.query.filter_by(balance_id=self.user_a_balance.id).all()
        assert_equal(1, len(txn))
        txn = txn[0]
        assert_equal(Decimal('35.0000'), txn.debit)
        assert_true(txn.credit is None)

        ldgr = ExternalLedgerModel.query.filter_by(record_id=t.id).all()
        assert_equal(3, len(ldgr))

        party_ldgr = [l for l in ldgr if l.party == 'test-party' and l.fee is None]
        assert_equal(1, len(party_ldgr))
        party_ldgr = party_ldgr[0]
        assert_equal(Decimal('25.0000'), party_ldgr.debit)
        assert_equal('test-party', party_ldgr.party)
        assert_true(party_ldgr.credit is None)

        party_fee_ldgr = [l for l in ldgr if l.party == 'test-party' and l.fee is not None]
        assert_equal(1, len(party_fee_ldgr))
        party_fee_ldgr = party_fee_ldgr[0]
        assert_equal(Decimal('5.0000'), party_fee_ldgr.debit)
        assert_equal('test-party', party_fee_ldgr.party)
        assert_true(party_fee_ldgr.credit is None)

        poold_ldgr = [l for l in ldgr if l.party == 'poold']
        assert_equal(1, len(poold_ldgr))
        poold_ldgr = poold_ldgr[0]
        assert_equal(Decimal('5.0000'), poold_ldgr.debit)
        assert_equal('poold', poold_ldgr.party)
        assert_true(poold_ldgr.credit is None)

        assert_equal(Decimal('15.0000'), self.user_a_balance.amount)
Beispiel #7
0
def payment_to_campaign(user, campaign, amount, currency, fees, note=None, goal=None, full_name=None):
    """Use this function to make a payment to the 'organizer' of 'campaign'.
    While we are actively not holding money, this method should be used for any
    and all money related transactions in which funds are being directed to a
    campaign. Records will be written to all appropriate ledger tables, and the
    transaction executed with stripe.

    :raises: :class:`pooldlib.exception.StripeCustomerAccountError`
             :class:`pooldlib.exception.StripeUserAccountError`
             :class:`pooldlib.exception.CampaignConfigurationError`
    """
    transact_ledger = Transact()
    deposit_id, withdrawal_id = [uuid() for i in range(2)]

    if user.stripe_customer_id is None:
        msg = 'User does not have an associated Stripe customer account!'
        data = dict(user=str(user))
        logger.error(msg, data=data)
        raise StripeCustomerAccountError(msg)

    organizer = get_campaign_organizer(campaign)
    if organizer is None:
        msg = 'No organizer was found for campaign!'
        data = dict(campaign=str(campaign))
        logger.critical(msg, data=data)
        raise CampaignConfigurationError(msg)
    if organizer.stripe_user_id is None or organizer.stripe_user_token is None:
        msg = 'User does not have an associated Stripe user account, need to complete this transaction!'
        data = dict(user=str(user))
        logger.error(msg, data=data)
        raise StripeUserAccountError(msg)

    txn_dict, amount_cents, fee_cents = _calculate_transaction_amounts(amount, fees)

    description = 'User: %s, paying towards campaign: %s.' % (user.id, campaign.id)
    if note is not None:
        description += ' %s' % note

    ret = _execute_charge(organizer.stripe_user_token, amount_cents, fee_cents, currency, user, description)

    msg = 'Transaction successfully completed.'
    data = dict(sub_total=amount,
                currency=currency.code,
                total=txn_dict['charge']['final'])
    meta = dict(stripe_response=ret)
    logger.transaction(msg, data=data, **meta)

    stripe_ref_number = ret['id']
    # Transaction for ``user`` with credit=``amount``
    transact_ledger.transaction(user,
                                'stripe',
                                stripe_ref_number,
                                currency,
                                credit=amount,
                                fee=None,
                                id=deposit_id)
    # External Ledger for ``user`` for ``credit=final_charge_amount``
    transact_ledger.external_ledger(user,
                                    'stripe',
                                    stripe_ref_number,
                                    currency,
                                    credit=txn_dict['charge']['final'],
                                    id=deposit_id,
                                    full_name=full_name)
    for fee in fees:
        fee_amount = [f['fee'] for f in txn_dict['fees'] if f['name'] == fee.name][0]
        # External Ledger for ``user`` for ``debit=fee_amount``
        transact_ledger.external_ledger(user,
                                        'stripe',
                                        stripe_ref_number,
                                        currency,
                                        debit=fee_amount,
                                        fee=fee,
                                        id=deposit_id)

    if not goal:
        transact_ledger.transfer(amount,
                                 currency,
                                 origin=user,
                                 destination=campaign,
                                 id=uuid())
        transact_ledger.transfer(amount,
                                 currency,
                                 origin=campaign,
                                 destination=organizer,
                                 id=uuid())
    else:
        transact_ledger.transfer_to_campaign_goal(amount,
                                                  currency,
                                                  goal,
                                                  user,
                                                  id=uuid())
        transact_ledger.transfer_from_campaign_goal(amount,
                                                    currency,
                                                    goal,
                                                    organizer,
                                                    id=uuid())

    transact_ledger.transaction(organizer,
                                'stripe',
                                stripe_ref_number,
                                currency,
                                debit=amount,
                                id=withdrawal_id)
    # External Ledger for ``user`` for ``debit=fee_amount``
    transact_ledger.external_ledger(organizer,
                                    'stripe',
                                    stripe_ref_number,
                                    currency,
                                    debit=amount,
                                    id=withdrawal_id)
    transact_ledger.execute()
    return (deposit_id, withdrawal_id)