Ejemplo n.º 1
0
    def test_simple_transfer_from(self):
        t = Transact()
        t.transfer_from_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('75.0000'), check_balance.amount)
        assert_equal(Decimal('25.0000'), debit_xfer[0].credit)
        assert_equal(None, debit_xfer[0].debit)

        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('25.0000'), check_balance.amount)
        assert_equal(Decimal('25.0000'), credit_xfer[0].debit)
        assert_equal(None, credit_xfer[0].credit)

        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.debit)
        assert_equal(None, comm_ledger.credit)
        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)
Ejemplo n.º 2
0
    def test_simple_transfer_from(self):
        t = Transact()
        t.transfer_from_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('75.0000'), check_balance.amount)
        assert_equal(Decimal('25.0000'), debit_xfer[0].credit)
        assert_equal(None, debit_xfer[0].debit)

        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('25.0000'), check_balance.amount)
        assert_equal(Decimal('25.0000'), credit_xfer[0].debit)
        assert_equal(None, credit_xfer[0].credit)

        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.debit)
        assert_equal(None, comm_ledger.credit)
        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)
Ejemplo n.º 3
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)