Пример #1
0
    def test_parent(self):
        tree_gen = wac.URIGen('trees', '{tree}')
        apple_gen = wac.URIGen('apples', '{apple}', parent=tree_gen)

        for expected, kwargs in [
            ('/trees/1/apples', dict(tree=1)),
        ]:
            self.assertEqual(apple_gen.collection_uri(**kwargs), expected)

        for expected, kwargs in [
            ('/trees/1/apples/2', dict(tree=1, apple=2)),
        ]:
            self.assertEqual(apple_gen.member_uri(**kwargs), expected)
Пример #2
0
class Marketplace(Resource):
    """
    A Marketplace represents your central broker for all operations on the
    Balanced API.

    A Marketplace has a single `owner_customer` which represents your person or
    business.

    All Resources apart from APIKeys are associated with a Marketplace.

    A Marketplace has an escrow account which receives all funds from Debits
    that are not associated with Orders. The sum of the escrow (`in_escrow`) is
    (Debits - Refunds + Reversals - Credits).
    """

    type = 'marketplaces'

    uri_gen = wac.URIGen('/marketplaces', '{marketplace}')

    @utils.classproperty
    def mine(cls):
        """
        Returns an instance representing the marketplace associated with the
        current API key used for this request.
        """
        return cls.query.one()

    my_marketplace = mine
Пример #3
0
class Resource(JSONSchemaResource):

    client = config.client

    registry = registry

    uri_gen = wac.URIGen('/resources', '{resource}')

    def unstore(self):
        return self.delete()

    @classmethod
    def fetch(cls, href):
        return cls.get(href)

    @classmethod
    def get(cls, href):
        if href.startswith('/resources'):
            # hackety hack hax
            # resource is an abstract type, we shouldn't have it comeing back itself
            # instead we need to figure out the type based off the api response
            resp = cls.client.get(href)
            resource = [
                k for k in resp.data.keys() if k != 'links' and k != 'meta'
            ]
            if resource:
                return Resource.registry.get(resource[0], cls)(**resp.data)
            return cls(**resp.data)
        return super(Resource, cls).get(href)
Пример #4
0
    def test_composite_member(self):
        gen = wac.URIGen('as', '{a}/{b}')

        for expected, kwargs in [
            ('/as/1/ababa', dict(a=1, b='ababa')),
        ]:
            self.assertEqual(gen.member_uri(**kwargs), expected)
Пример #5
0
class Dispute(Resource):
    """
    A dispute occurs when a customer disputes a transaction that
    occurred on their funding instrument.
    """
    type = 'disputes'

    uri_gen = wac.URIGen('/disputes', '{dispute}')
Пример #6
0
class Callback(Resource):
    """
    A Callback is a publicly accessible location that can receive POSTed JSON
    data whenever an Event is generated.
    """

    type = 'callbacks'

    uri_gen = wac.URIGen('/callbacks', '{callback}')
Пример #7
0
class ExternalAccount(FundingInstrument):
    """
    An External Account represents a source of funds provided by an external, 3rd
    party processor. You may Debit funds from the account if can_debit is true.
    """

    type = 'external_accounts'

    uri_gen = wac.URIGen('/external_accounts', '{external_account}')
Пример #8
0
class Settlement(Transaction):
    """
    A Settlement is the action of moving money out of an Account to a
    bank account.
    """

    type = 'settlements'

    uri_gen = wac.URIGen('/settlements', '{settlements}')
Пример #9
0
    def test_single_member(self):
        gen = wac.URIGen('as', '{a}')

        class Resource(object):
            pass

        for expected, kwargs in [
            ('/as/1', dict(a=1)),
        ]:
            self.assertEqual(gen.member_uri(**kwargs), expected)
Пример #10
0
class Reversal(Transaction):
    """
    A Reversal represents a reversal of funds from a Credit. A Credit can have
    many Reversal associated with it up to the total amount of the original
    Credit. Funds are returned to your Marketplace's escrow account
    proportional to the amount of the Reversal.
    """

    type = 'reversals'

    uri_gen = wac.URIGen('/reversals', '{reversal}')
Пример #11
0
class Event(Resource):
    """
    An Event is a snapshot of another resource at a point in time when
    something significant occurred. Events are created when resources are
    created, updated, deleted or otherwise change state such as a Credit being
    marked as failed.
    """

    type = 'events'

    uri_gen = wac.URIGen('/events', '{event}')
Пример #12
0
class APIKey(Resource):
    """
    Your APIKey is used to authenticate when performing operations on the
    Balanced API. You must create an APIKey before you create a Marketplace.

    **NOTE:** Never give out or expose your APIKey. You may POST to this
    endpoint to create new APIKeys and then DELETE any old keys.
    """
    type = 'api_keys'

    uri_gen = wac.URIGen('/api_keys', '{api_key}')
Пример #13
0
class CardHold(Resource):

    type = 'card_holds'

    uri_gen = wac.URIGen('/card_holds', '{card_hold}')

    def cancel(self):
        self.is_void = True
        return self.save()

    def capture(self, **kwargs):
        return Debit(href=self.debits.href, **kwargs).save()
Пример #14
0
class Card(FundingInstrument):
    """
    A card represents a source of funds. You may Debit funds from the Card.
    """

    type = 'cards'

    uri_gen = wac.URIGen('/cards', '{card}')

    def hold(self, amount, **kwargs):
        return CardHold(href=self.card_holds.href, amount=amount,
                        **kwargs).save()
Пример #15
0
class Account(FundingInstrument):
    """
    An Account is a way to transfer funds from multiple Orders into one place,
    which can later be bulk credited out.
    """

    type = 'accounts'

    uri_gen = wac.URIGen('/accounts', '{account}')

    def settle(self, funding_instrument, **kwargs):
        return Settlement(href=self.settlements.href,
                          funding_instrument=funding_instrument,
                          **kwargs).save()
Пример #16
0
class Order(Resource):
    """
    An Order is a logical construct for grouping Transactions.

    An Order may have 0:n Transactions associated with it so long as the sum
    (`amount_escrowed`) which is calculated as
    (Debits - Refunds - Credits + Reversals), is always >= 0.
    """

    type = 'orders'

    uri_gen = wac.URIGen('/orders', '{order}')

    def credit_to(self, destination, amount, **kwargs):
        return destination.credit(order=self.href, amount=amount, **kwargs)

    def debit_from(self, source, amount, **kwargs):
        return source.debit(order=self.href, amount=amount, **kwargs)
Пример #17
0
class Customer(Resource):
    """
    A Customer represents a business or person within your Marketplace. A
    Customer can have many funding instruments such as cards and bank accounts
    associated to them. Customers are logical grouping constructs for
    associating many Transactions and FundingInstruments.
    """

    type = 'customers'

    uri_gen = wac.URIGen('/customers', '{customer}')

    def create_order(self, **kwargs):
        return Order(href=self.orders.href, **kwargs).save()

    @property
    def payable_account(self):
        return self.accounts.filter(type="payable").first()
Пример #18
0
class BankAccount(FundingInstrument):
    """
    A BankAccount is both a source, and a destination of, funds. You may
    create Debits and Credits to and from, this funding instrument.
    """

    type = 'bank_accounts'

    uri_gen = wac.URIGen('/bank_accounts', '{bank_account}')

    def verify(self):
        """
        Creates a verification of the associated BankAccount so it can
        perform verified operations (debits).

        :rtype: BankAccountVerification
        """
        return BankAccountVerification(
            href=self.bank_account_verifications.href).save()
Пример #19
0
class Credit(Transaction):
    """
    A Credit represents a transfer of funds from your Marketplace's
    escrow account to a FundingInstrument.

    Credits are created by calling the `credit` method on a FundingInstrument.
    """

    type = 'credits'

    uri_gen = wac.URIGen('/credits', '{credit}')

    def reverse(self, **kwargs):
        """
        Reverse a Credit.  If no amount is specified it will reverse the entire
        amount of the Credit, you may create many Reversals up to the sum of
        the total amount of the original Credit.

        :rtype: Reversal
        """
        return Reversal(href=self.reversals.href, **kwargs).save()
Пример #20
0
class Debit(Transaction):
    """
    A Debit represents a transfer of funds from a FundingInstrument to your
    Marketplace's escrow account.

    A Debit may be created directly, or it will be created as a side-effect
    of capturing a CardHold. If you create a Debit directly it will implicitly
    create the associated CardHold if the FundingInstrument supports this.
    """

    type = 'debits'

    uri_gen = wac.URIGen('/debits', '{debit}')

    def refund(self, **kwargs):
        """
        Refunds this Debit. If no amount is specified it will refund the entire
        amount of the Debit, you may create many Refunds up to the sum total
        of the original Debit's amount.

        :rtype: Refund
        """
        return Refund(href=self.refunds.href, **kwargs).save()
Пример #21
0
class Playlist(Resource):

    type = 'playlist'

    uri_gen = wac.URIGen('/v1/playlists', '{playlist}')
Пример #22
0
    def test_root(self):
        tree_gen = wac.URIGen('trees', '{tree}')
        self.assertIsNotNone(tree_gen.root_uri)

        apple_spec = wac.URIGen('trees/{tree}/apples', '{apple}')
        self.assertIsNone(apple_spec.root_uri)
Пример #23
0
        class Resource4(Resource):

            type = 'four'
            uri_gen = wac.URIGen('/v1/4s', '{fours}')
Пример #24
0
class Resource1(Resource):

    type = 'one'
    uri_gen = wac.URIGen('/v2/1s', '{one}')
Пример #25
0
class Resource2(Resource):

    type = 'two'
    uri_gen = wac.URIGen('/v2/2s', '{two}')
Пример #26
0
class Resource3(Resource):

    type = 'three'
    uri_gen = wac.URIGen('/v2/3s', '{three}')
Пример #27
0
class Song(Resource):

    type = 'song'

    uri_gen = wac.URIGen('/v1/songs', '{song}')