Skip to content

Commit

Permalink
New cards (#132)
Browse files Browse the repository at this point in the history
* This is a way to solve it...

* This one is clearer

* Lint and test

* Another lint

* Fix test

* Add another test just in case and its corresponding fix

* To be clear

* Add customized session

* Bump versions

* Adding issuer and funding_type

* Adding card_activations

* Rebase

* Cassettes for everyone

* Upgrade version

* Upgrade version

* Allow cuenca-validations

* Conflict dependencies

* card_activations model and version

* Fix testt

* Version

* Adapting card_activations

* Lint

* Missing file

* Version

* cassettes and user_id optional

* from_dict

* Missing assert

* Adding pin

* Adding dacite as requirement

* card_activations

* Correct version

* Revert file

* Using card_uri instead of the card

* Remove dacite

* Fix cassette

* Alex comments and cuenca-validations
  • Loading branch information
Ricardo authored Mar 24, 2021
1 parent cabf724 commit e26630d
Show file tree
Hide file tree
Showing 19 changed files with 814 additions and 187 deletions.
2 changes: 2 additions & 0 deletions cuenca/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
'BalanceEntry',
'BillPayment',
'Card',
'CardActivation',
'CardTransaction',
'Commission',
'Deposit',
Expand All @@ -26,6 +27,7 @@
BalanceEntry,
BillPayment,
Card,
CardActivation,
CardTransaction,
Commission,
Deposit,
Expand Down
3 changes: 3 additions & 0 deletions cuenca/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
'BalanceEntry',
'BillPayment',
'Card',
'CardActivation',
'CardTransaction',
'Commission',
'Deposit',
Expand All @@ -19,6 +20,7 @@
from .api_keys import ApiKey
from .balance_entries import BalanceEntry
from .bill_payments import BillPayment
from .card_activations import CardActivation
from .card_transactions import CardTransaction
from .cards import Card
from .commissions import Commission
Expand All @@ -39,6 +41,7 @@
BalanceEntry,
BillPayment,
Card,
CardActivation,
CardTransaction,
Commission,
Deposit,
Expand Down
56 changes: 56 additions & 0 deletions cuenca/resources/card_activations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import datetime as dt
from dataclasses import dataclass
from typing import ClassVar, Optional, cast

from cuenca_validations.types.requests import CardActivationRequest

from ..http import Session, session as global_session
from .base import Creatable
from .cards import Card
from .resources import retrieve_uri


@dataclass
class CardActivation(Creatable):
_resource: ClassVar = 'card_activations'

created_at: dt.datetime
user_id: str
ip_address: str
card_uri: Optional[str]
succeeded: bool

@classmethod
def create(
cls,
number: str,
exp_month: int,
exp_year: int,
cvv2: str,
*,
session: Session = global_session,
) -> 'CardActivation':
"""
Associates a physical card with the current user
:param number: Card number
:param exp_month:
:param exp_year:
:param cvv2:
"""
req = CardActivationRequest(
number=number,
exp_month=exp_month,
exp_year=exp_year,
cvv2=cvv2,
)
return cast(
'CardActivation', cls._create(session=session, **req.dict())
)

@property
def card(self) -> Optional[Card]:
result = None
if self.card_uri:
result = cast(Card, retrieve_uri(self.card_uri))
return result
40 changes: 26 additions & 14 deletions cuenca/resources/cards.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from typing import ClassVar, Optional, cast

from cuenca_validations.types import CardStatus, CardType
from cuenca_validations.types import (
CardFundingType,
CardIssuer,
CardStatus,
CardType,
)
from cuenca_validations.types.queries import CardQuery
from cuenca_validations.types.requests import CardRequest, CardUpdateRequest
from pydantic.dataclasses import dataclass
Expand All @@ -15,38 +20,49 @@ class Card(Retrievable, Queryable, Creatable, Updateable):
_resource: ClassVar = 'cards'
_query_params: ClassVar = CardQuery

user_id: str
user_id: Optional[str]
number: str
exp_month: int
exp_year: int
cvv2: str
pin: Optional[str]
type: CardType
status: CardStatus
issuer: CardIssuer
funding_type: CardFundingType

@property
def last_4_digits(self):
return self.number[-4:]

@classmethod
def create(
cls,
ledger_account_id: str,
user_id: str,
issuer: CardIssuer,
funding_type: CardFundingType,
user_id: str = 'me',
*,
session: Session = global_session,
) -> 'Card':
"""
Assigns user_id and ledger_account_id to a existing card
Assigns user_id and ledger_account_id to a existing virtual card
:param ledger_account_id: associated ledger account id
:param user_id: associated user id
:param funding_type: debit or credit
:param issuer:
:return: New assigned card
"""
req = CardRequest(ledger_account_id=ledger_account_id, user_id=user_id)
req = CardRequest(
user_id=user_id,
issuer=issuer,
funding_type=funding_type,
)
return cast('Card', cls._create(session=session, **req.dict()))

@classmethod
def update(
cls,
card_id: str,
user_id: Optional[str] = None,
ledger_account_id: Optional[str] = None,
status: Optional[CardStatus] = None,
*,
session: Session = global_session,
Expand All @@ -56,14 +72,10 @@ def update(
reconfigure properties like status, and manufacturer.
:param card_id: existing card_id
:param user_id: owner user id
:param ledger_account_id: owner ledger account
:param status:
:return: Updated card object
"""
req = CardUpdateRequest(
user_id=user_id, ledger_account_id=ledger_account_id, status=status
)
req = CardUpdateRequest(status=status)
resp = cls._update(card_id, session=session, **req.dict())
return cast('Card', resp)

Expand Down
2 changes: 1 addition & 1 deletion cuenca/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__version__ = '0.7.0'
__version__ = '0.7.1'
CLIENT_VERSION = __version__
API_VERSION = '2020-03-19'
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
requests==2.25.1
cuenca-validations==0.8.2
cuenca-validations==0.9.0
dataclasses>=0.7;python_version<"3.7"
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
python_requires='>=3.6',
install_requires=[
'requests>=2.24,<2.26',
'cuenca-validations>=0.8.0,<0.9.0',
'cuenca-validations>=0.9.0,<0.10.0',
'dataclasses>=0.7;python_version<"3.7"',
],
classifiers=[
Expand Down
Loading

0 comments on commit e26630d

Please sign in to comment.