def test_simple_transfer_to(self): t = Transact() t.transfer_to_campaign_goal(Decimal('25.0000'), self.currency, self.campaign_goal, self.user_a) assert_true(t.verify()) t.execute() xfers = TransferModel.query.filter_by(record_id=t.id).all() check_balance = self.user_a.balance_for_currency(self.currency) debit_xfer = [x for x in xfers if x.balance == check_balance] assert_equal(1, len(debit_xfer)) assert_equal(Decimal('25.0000'), check_balance.amount) assert_equal(Decimal('25.0000'), debit_xfer[0].debit) assert_equal(None, debit_xfer[0].credit) check_balance = self.campaign.balance_for_currency(self.currency) credit_xfer = [x for x in xfers if x.balance == check_balance] assert_equal(1, len(credit_xfer)) assert_equal(Decimal('75.0000'), check_balance.amount) assert_equal(Decimal('25.0000'), credit_xfer[0].credit) assert_equal(None, credit_xfer[0].debit) comm_ledger = CampaignGoalLedgerModel.query.filter_by(campaign_goal=self.campaign_goal).all() assert_equal(1, len(comm_ledger)) comm_ledger = comm_ledger[0] assert_equal(Decimal('25.0000'), comm_ledger.credit) assert_equal(None, comm_ledger.debit) assert_equal(self.campaign.id, comm_ledger.campaign_id) assert_equal(self.campaign_goal.id, comm_ledger.campaign_goal_id) assert_equal(self.user_a.id, comm_ledger.party_id) assert_equal('user', comm_ledger.party_type)
def test_simple_transfer_to(self): t = Transact() t.transfer_to_campaign_goal(Decimal('25.0000'), self.currency, self.campaign_goal, self.user_a) assert_true(t.verify()) t.execute() xfers = TransferModel.query.filter_by(record_id=t.id).all() check_balance = self.user_a.balance_for_currency(self.currency) debit_xfer = [x for x in xfers if x.balance == check_balance] assert_equal(1, len(debit_xfer)) assert_equal(Decimal('25.0000'), check_balance.amount) assert_equal(Decimal('25.0000'), debit_xfer[0].debit) assert_equal(None, debit_xfer[0].credit) check_balance = self.campaign.balance_for_currency(self.currency) credit_xfer = [x for x in xfers if x.balance == check_balance] assert_equal(1, len(credit_xfer)) assert_equal(Decimal('75.0000'), check_balance.amount) assert_equal(Decimal('25.0000'), credit_xfer[0].credit) assert_equal(None, credit_xfer[0].debit) comm_ledger = CampaignGoalLedgerModel.query.filter_by( campaign_goal=self.campaign_goal).all() assert_equal(1, len(comm_ledger)) comm_ledger = comm_ledger[0] assert_equal(Decimal('25.0000'), comm_ledger.credit) assert_equal(None, comm_ledger.debit) assert_equal(self.campaign.id, comm_ledger.campaign_id) assert_equal(self.campaign_goal.id, comm_ledger.campaign_goal_id) assert_equal(self.user_a.id, comm_ledger.party_id) assert_equal('user', comm_ledger.party_type)
def test_basic_transfer(self): t = Transact() t.transfer(Decimal('25.0000'), self.currency, destination=self.campaign_a, origin=self.user_a) assert_true(t.verify()) t.execute() xfers = TransferModel.query.filter_by(record_id=t.id).all() assert_equal(2, len(xfers)) debit_xfer = [ x for x in xfers if x.balance == self.user_a.balance_for_currency(self.currency) ] assert_equal(1, len(debit_xfer)) assert_equal(Decimal('25.0000'), debit_xfer[0].debit) credit_xfer = [ x for x in xfers if x.balance == self.campaign_a.balance_for_currency(self.currency) ] assert_equal(1, len(credit_xfer)) assert_equal(Decimal('25.0000'), credit_xfer[0].credit)
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)
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)
def test_insufficient_funds_transfer(self): t = Transact() t.transfer(Decimal('55.0000'), self.currency, destination=self.campaign_a, origin=self.user_a) t.execute()
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()
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()
def test_transfer_with_fee(self): t = Transact() t.transfer(Decimal('25.0000'), self.currency, destination=self.campaign_a, origin=self.user_a) t.transfer(Decimal('5.0000'), self.currency, destination=self.user_b, origin=self.user_a, fee=self.fee) assert_true(t.verify()) t.execute() xfers = TransferModel.query.filter_by(record_id=t.id).all() assert_equal(3, len(xfers)) check_balance = self.user_a.balance_for_currency(self.currency) debit_xfer = [x for x in xfers if x.balance == check_balance] assert_equal(1, len(debit_xfer)) assert_equal(Decimal('20.0000'), check_balance.amount) assert_equal(Decimal('30.0000'), debit_xfer[0].debit) assert_equal(None, debit_xfer[0].credit) check_balance = self.campaign_a.balance_for_currency(self.currency) credit_xfer = [x for x in xfers if x.balance == check_balance] assert_equal(1, len(credit_xfer)) assert_equal(Decimal('75.0000'), check_balance.amount) assert_equal(Decimal('25.0000'), credit_xfer[0].credit) assert_equal(None, credit_xfer[0].debit) check_balance = self.user_b.balance_for_currency(self.currency) debit_xfer = [x for x in xfers if x.balance == check_balance] assert_equal(1, len(debit_xfer)) assert_equal(Decimal('55.0000'), check_balance.amount) assert_equal(Decimal('5.0000'), debit_xfer[0].credit) assert_equal(None, debit_xfer[0].debit) iledgers = InternalLedgerModel.query.filter_by(record_id=t.id).all() assert_equal(2, len(iledgers)) credit_iledger = [ l for l in iledgers if l.party == self.user_b.username ] assert_equal(1, len(credit_iledger)) assert_equal(Decimal('5.0000'), credit_iledger[0].credit) assert_equal(None, credit_iledger[0].debit) debit_iledger = [ l for l in iledgers if l.party == self.user_a.username ] assert_equal(1, len(debit_iledger)) assert_equal(Decimal('5.0000'), debit_iledger[0].debit) assert_equal(None, debit_iledger[0].credit)
def test_external_entry_no_name(self): t = Transact() t.external_ledger(self.user, 'test-party', 'test-reference-number-test_external_entry_no_name', self.currency, credit=Decimal('25.0000')) t.execute() txns = ExternalLedgerModel.query.filter_by(record_id=t.id).all() assert_equal(1, len(txns)) txn = txns[0] assert_true(txn.full_name is None)
def test_basic_transfer(self): t = Transact() t.transfer(Decimal('25.0000'), self.currency, destination=self.campaign_a, origin=self.user_a) assert_true(t.verify()) t.execute() xfers = TransferModel.query.filter_by(record_id=t.id).all() assert_equal(2, len(xfers)) debit_xfer = [x for x in xfers if x.balance == self.user_a.balance_for_currency(self.currency)] assert_equal(1, len(debit_xfer)) assert_equal(Decimal('25.0000'), debit_xfer[0].debit) credit_xfer = [x for x in xfers if x.balance == self.campaign_a.balance_for_currency(self.currency)] assert_equal(1, len(credit_xfer)) assert_equal(Decimal('25.0000'), credit_xfer[0].credit)
def test_transfer_with_fee(self): t = Transact() t.transfer(Decimal('25.0000'), self.currency, destination=self.campaign_a, origin=self.user_a) t.transfer(Decimal('5.0000'), self.currency, destination=self.user_b, origin=self.user_a, fee=self.fee) assert_true(t.verify()) t.execute() xfers = TransferModel.query.filter_by(record_id=t.id).all() assert_equal(3, len(xfers)) check_balance = self.user_a.balance_for_currency(self.currency) debit_xfer = [x for x in xfers if x.balance == check_balance] assert_equal(1, len(debit_xfer)) assert_equal(Decimal('20.0000'), check_balance.amount) assert_equal(Decimal('30.0000'), debit_xfer[0].debit) assert_equal(None, debit_xfer[0].credit) check_balance = self.campaign_a.balance_for_currency(self.currency) credit_xfer = [x for x in xfers if x.balance == check_balance] assert_equal(1, len(credit_xfer)) assert_equal(Decimal('75.0000'), check_balance.amount) assert_equal(Decimal('25.0000'), credit_xfer[0].credit) assert_equal(None, credit_xfer[0].debit) check_balance = self.user_b.balance_for_currency(self.currency) debit_xfer = [x for x in xfers if x.balance == check_balance] assert_equal(1, len(debit_xfer)) assert_equal(Decimal('55.0000'), check_balance.amount) assert_equal(Decimal('5.0000'), debit_xfer[0].credit) assert_equal(None, debit_xfer[0].debit) iledgers = InternalLedgerModel.query.filter_by(record_id=t.id).all() assert_equal(2, len(iledgers)) credit_iledger = [l for l in iledgers if l.party == self.user_b.username] assert_equal(1, len(credit_iledger)) assert_equal(Decimal('5.0000'), credit_iledger[0].credit) assert_equal(None, credit_iledger[0].debit) debit_iledger = [l for l in iledgers if l.party == self.user_a.username] assert_equal(1, len(debit_iledger)) assert_equal(Decimal('5.0000'), debit_iledger[0].debit) assert_equal(None, debit_iledger[0].credit)
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)
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)
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)