diff --git a/CHANGELOG.md b/CHANGELOG.md index 5359ff3..a4d3947 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,10 @@ Given a version number MAJOR.MINOR.PATCH, increment: ## [Unreleased] +### Added +- PixKeyHolmes resource +- PixInternalTransactionReport resource +- log subresource to PixFraud ## [0.25.0] - 2026-04-08 ### Added diff --git a/README.md b/README.md index d5fb6fd..2c01ab7 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,7 @@ This SDK version is compatible with the Stark Infra API v2. - [PixBalance](#get-your-pixbalance): View your account balance - [PixStatement](#create-a-pixstatement): Request your account statement - [PixKey](#create-a-pixkey): Create a Pix Key + - [PixKeyHolmes](#create-pixkeyholmes): Investigate the registration of a Pix Key in the DICT - [PixClaim](#create-a-pixclaim): Claim a Pix Key - [PixDirector](#create-a-pixdirector): Create a Pix Director - [PixInfraction](#create-pixinfractions): Create Pix Infraction reports @@ -59,6 +60,7 @@ This SDK version is compatible with the Stark Infra API v2. - [DynamicBrcode](#create-dynamicbrcodes): Create dynamic Pix BR codes - [BrcodePreview](#create-brcodepreviews): Read data from BR Codes before paying them - [PixDispute](#create-pixdisputes): Create Pix Disputes + - [PixInternalTransactionReport](#create-pixinternaltransactionreports): Report internal transactions to the Central Bank - [Lending](#lending) - [CreditNote](#create-creditnotes): Create credit notes - [CreditPreview](#create-creditpreviews): Create credit previews @@ -1866,6 +1868,46 @@ log = starkinfra.pixkey.log.get("5155165527080960") print(log) ``` +### Create PixKeyHolmes + +To investigate whether a Pix Key is registered in the Central Bank's DICT, +open up a PixKeyHolmes for it: + +```python +import starkinfra + +holmes = starkinfra.pixkeyholmes.create([ + starkinfra.PixKeyHolmes( + key_id="+5511989898989" + ), + starkinfra.PixKeyHolmes( + key_id="valid@sandbox.com", + tags=["sherlock"] + ) +]) + +for sherlock in holmes: + print(sherlock) +``` + +### Query PixKeyHolmes + +You can query multiple PixKeyHolmes according to filters. + +```python +import starkinfra +from datetime import date + +holmes = starkinfra.pixkeyholmes.query( + after=date(2022, 6, 1), + before=date(2022, 10, 30), + status="solved" +) + +for sherlock in holmes: + print(sherlock) +``` + ### Create a PixClaim You can create a Pix claim to request the transfer of a Pix key from another bank to one of your accounts: @@ -2182,6 +2224,36 @@ fraud = starkinfra.pixfraud.cancel("5155165527080960") print(fraud) ``` +### Query PixFraud logs + +You can query PixFraud logs to better understand their life cycles. + +```python +import starkinfra +from datetime import date + +logs = starkinfra.pixfraud.log.query( + limit=50, + after=date(2022, 1, 1), + before=date(2022, 1, 20), +) + +for log in logs: + print(log) +``` + +### Get a PixFraud log + +You can also get a specific log by its id. + +```python +import starkinfra + +log = starkinfra.pixfraud.log.get("5155165527080960") + +print(log) +``` + ### Get a PixUser You can get a specific fraud statistics of a user with his taxId. @@ -2649,6 +2721,101 @@ log = starkinfra.pixdispute.log.get("5155165527080960") print(log) ``` +### Create PixInternalTransactionReports + +Transactions that happen internally, outside of the SPI, must be reported to the +Central Bank so they are reflected in your statements. You can do so by creating +PixInternalTransactionReports: + +```python +import starkinfra +from datetime import datetime + +reports = starkinfra.pixinternaltransactionreport.create([ + starkinfra.PixInternalTransactionReport( + amount=10000, + created=datetime(2024, 1, 1, 12, 0, 0), + end_to_end_id="E12345678202401011234567890123456", + method="manual", + reference_type="request", + sender_account_number="12345", + sender_branch_code="0001", + sender_account_type="checking", + sender_bank_code="12345678", + sender_tax_id="123.456.789-01", + receiver_account_number="67890", + receiver_branch_code="0001", + receiver_account_type="savings", + receiver_bank_code="87654321", + receiver_tax_id="987.654.321-00", + receiver_key_id="user@example.com" + ) +]) + +for report in reports: + print(report) +``` + +### Query PixInternalTransactionReports + +You can query multiple PixInternalTransactionReports according to filters. + +```python +import starkinfra +from datetime import date + +reports = starkinfra.pixinternaltransactionreport.query( + after=date(2024, 1, 1), + before=date(2024, 1, 30), + status="success" +) + +for report in reports: + print(report) +``` + +### Get a PixInternalTransactionReport + +After its creation, information on a PixInternalTransactionReport may be retrieved by its id. + +```python +import starkinfra + +report = starkinfra.pixinternaltransactionreport.get("5656565656565656") + +print(report) +``` + +### Query PixInternalTransactionReport logs + +You can query PixInternalTransactionReport logs to better understand their life cycles. + +```python +import starkinfra +from datetime import date + +logs = starkinfra.pixinternaltransactionreport.log.query( + limit=50, + after=date(2024, 1, 1), + before=date(2024, 1, 20), +) + +for log in logs: + print(log) +``` + +### Get a PixInternalTransactionReport log + +You can also get a specific log by its id. + +```python +import starkinfra + +log = starkinfra.pixinternaltransactionreport.log.get("5155165527080960") + +print(log) +``` + ## Lending If you want to establish a lending operation, you can use Stark Infra to create a CCB contract. This will enable your business to lend money without diff --git a/run-tests.sh b/run-tests.sh index 3e485d3..1e63e55 100755 --- a/run-tests.sh +++ b/run-tests.sh @@ -41,6 +41,10 @@ commands=( "{ python -m unittest tests.sdk.testPixReversal; }" "{ python -m unittest tests.sdk.testPixReversalLog; }" "{ python -m unittest tests.sdk.testPixStatement; }" + "{ python -m unittest tests.sdk.testPixKeyHolmes; }" + "{ python -m unittest tests.sdk.testPixInternalTransactionReport; }" + "{ python -m unittest tests.sdk.testPixInternalTransactionReportLog; }" + "{ python -m unittest tests.sdk.testPixFraudLog; }" ) clen=`expr "${#commands[@]}" - 1` # get length of commands - 1 diff --git a/starkinfra/__init__.py b/starkinfra/__init__.py index e77ce55..6990478 100644 --- a/starkinfra/__init__.py +++ b/starkinfra/__init__.py @@ -51,6 +51,12 @@ from . import pixuser from .pixuser.__pixuser import PixUser +from . import pixkeyholmes +from .pixkeyholmes.__pixkeyholmes import PixKeyHolmes + +from . import pixinternaltransactionreport +from .pixinternaltransactionreport.__pixinternaltransactionreport import PixInternalTransactionReport + from . import issuingbalance from .issuingbalance.__issuingbalance import IssuingBalance diff --git a/starkinfra/pixfraud/__init__.py b/starkinfra/pixfraud/__init__.py index d091d05..3cd1156 100644 --- a/starkinfra/pixfraud/__init__.py +++ b/starkinfra/pixfraud/__init__.py @@ -1 +1,3 @@ from .__pixfraud import create, get, query, page, cancel +from .log.__log import Log +from . import log diff --git a/starkinfra/pixfraud/log/__init__.py b/starkinfra/pixfraud/log/__init__.py new file mode 100644 index 0000000..a8a69ba --- /dev/null +++ b/starkinfra/pixfraud/log/__init__.py @@ -0,0 +1 @@ +from .__log import query, page, get diff --git a/starkinfra/pixfraud/log/__log.py b/starkinfra/pixfraud/log/__log.py new file mode 100644 index 0000000..699fef6 --- /dev/null +++ b/starkinfra/pixfraud/log/__log.py @@ -0,0 +1,97 @@ +from starkcore.utils.checks import check_datetime, check_date +from ..__pixfraud import _resource as _pixfraud_resource +from starkcore.utils.resource import Resource +from starkcore.utils.api import from_api_json +from ...utils import rest + + +class Log(Resource): + """# pixfraud.Log object + Every time a PixFraud entity is modified, a corresponding PixFraud.Log + is generated for the entity. This log is never generated by the user. + ## Attributes (return-only): + - id [string]: unique id returned when the log is created. ex: "5656565656565656" + - fraud [PixFraud]: PixFraud entity to which the log refers to. + - type [string]: type of the PixFraud event which triggered the log creation. ex: "created", "failed", "registered", "canceled" + - errors [list of strings]: list of errors linked to this PixFraud event + - created [datetime.datetime]: creation datetime for the log. ex: datetime.datetime(2020, 3, 10, 10, 30, 0, 0) + """ + def __init__(self, id, fraud, type, errors, created): + Resource.__init__(self, id=id) + + self.fraud = from_api_json(_pixfraud_resource, fraud) + self.type = type + self.errors = errors + self.created = check_datetime(created) + + +_resource = {"class": Log, "name": "PixFraudLog"} + + +def get(id, user=None): + """# Retrieve a specific PixFraud.Log + Receive a single PixFraud.Log object previously created by the Stark Infra API by its id + ## Parameters (required): + - id [string]: object unique id. ex: "5656565656565656" + ## Parameters (optional): + - user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkinfra.user was set before function call. + ## Return: + - PixFraud.Log object with updated attributes + """ + return rest.get_id(resource=_resource, id=id, user=user) + + +def query(ids=None, limit=None, after=None, before=None, types=None, fraud_ids=None, user=None): + """# Retrieve PixFraud.Logs + Receive a generator of PixFraud.Log objects previously created in the Stark Infra API + ## Parameters (optional): + - limit [integer, default None]: maximum number of objects to be retrieved. Unlimited if None. ex: 35 + - after [datetime.date or string, default None]: date filter for objects created after specified date. ex: datetime.date(2020, 3, 10) + - before [datetime.date or string, default None]: date filter for objects created before a specified date. ex: datetime.date(2020, 3, 10) + - types [list of strings, default None]: filter retrieved objects by types. ex: ["created", "failed", "registered", "canceled"] + - fraud_ids [list of strings, default None]: list of PixFraud IDs to filter retrieved objects. ex: ["5656565656565656", "4545454545454545"] + - ids [list of strings, default None]: Log ids to filter PixFraud Logs. ex: ["5656565656565656"] + - user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkinfra.user was set before function call + ## Return: + - generator of PixFraud.Log objects with updated attributes + """ + return rest.get_stream( + resource=_resource, + ids=ids, + limit=limit, + after=check_date(after), + before=check_date(before), + types=types, + fraud_ids=fraud_ids, + user=user, + ) + + +def page(cursor=None, ids=None, limit=None, after=None, before=None, types=None, fraud_ids=None, user=None): + """# Retrieve paged PixFraud.Logs + Receive a list of up to 100 PixFraud.Log objects previously created in the Stark Infra API and the cursor to the next page. + Use this function instead of query if you want to manually page your frauds. + ## Parameters (optional): + - cursor [string, default None]: cursor returned on the previous page function call + - limit [integer, default 100]: maximum number of objects to be retrieved. Max = 100. ex: 35 + - after [datetime.date or string, default None]: date filter for objects created after a specified date. ex: datetime.date(2020, 3, 10) + - before [datetime.date or string, default None]: date filter for objects created before a specified date. ex: datetime.date(2020, 3, 10) + - types [list of strings, default None]: filter retrieved objects by types. ex: ["created", "failed", "registered", "canceled"] + - fraud_ids [list of strings, default None]: list of PixFraud ids to filter retrieved objects. ex: ["5656565656565656", "4545454545454545"] + - ids [list of strings, default None]: Log ids to filter PixFraud Logs. ex: ["5656565656565656"] + - user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkinfra.user was set before function call + ## Return: + - list of PixFraud.Log objects with updated attributes + - cursor to retrieve the next page of PixFraud.Log objects + """ + return rest.get_page( + resource=_resource, + cursor=cursor, + ids=ids, + limit=limit, + after=check_date(after), + before=check_date(before), + types=types, + fraud_ids=fraud_ids, + user=user, + ) diff --git a/starkinfra/pixinternaltransactionreport/__init__.py b/starkinfra/pixinternaltransactionreport/__init__.py new file mode 100644 index 0000000..3b3eb09 --- /dev/null +++ b/starkinfra/pixinternaltransactionreport/__init__.py @@ -0,0 +1,3 @@ +from . import log +from .log.__log import Log +from .__pixinternaltransactionreport import create, get, query, page diff --git a/starkinfra/pixinternaltransactionreport/__pixinternaltransactionreport.py b/starkinfra/pixinternaltransactionreport/__pixinternaltransactionreport.py new file mode 100644 index 0000000..ccc388b --- /dev/null +++ b/starkinfra/pixinternaltransactionreport/__pixinternaltransactionreport.py @@ -0,0 +1,144 @@ +from ..utils import rest +from starkcore.utils.resource import Resource +from starkcore.utils.checks import check_datetime, check_date + + +class PixInternalTransactionReport(Resource): + """# PixInternalTransactionReport object + PixInternalTransactionReports are used to report transactions that happened + internally, outside of the SPI, to the Central Bank so they are reflected in + the participant's statements. + When you initialize a PixInternalTransactionReport, the entity will not be + automatically created in the Stark Infra API. The 'create' function sends the + objects to the Stark Infra API and returns the list of created objects. + ## Parameters (required): + - amount [integer]: amount of the reported transaction in cents. ex: 1234 (= R$ 12.34) + - created [datetime.datetime or string]: datetime when the reported transaction occurred. ex: datetime.datetime(2020, 3, 10, 10, 30, 0, 0) + - end_to_end_id [string]: central bank's unique transaction id. ex: "E20018183202201201213u34sav898j" + - method [string]: method used to process the reported transaction. ex: "manual", "key", "staticQrcode", "dynamicQrcode" + - reference_type [string]: type of the reported transaction. Options: "request", "reversal" + - sender_account_number [string]: sender's bank account number. ex: "76543" + - sender_branch_code [string]: sender's bank account branch code. ex: "1234" + - sender_account_type [string]: sender's bank account type. Options: "checking", "savings", "salary" or "payment" + - sender_bank_code [string]: sender's participant code (ISPB). ex: "20018183" + - sender_tax_id [string]: sender's tax ID (CPF/CNPJ) with or without formatting. ex: "01234567890" or "20.018.183/0001-80" + - receiver_account_number [string]: receiver's bank account number. ex: "76543" + - receiver_branch_code [string]: receiver's bank account branch code. ex: "1234" + - receiver_account_type [string]: receiver's bank account type. Options: "checking", "savings", "salary" or "payment" + - receiver_bank_code [string]: receiver's participant code (ISPB). ex: "20018183" + - receiver_tax_id [string]: receiver's tax ID (CPF/CNPJ) with or without formatting. ex: "01234567890" or "20.018.183/0001-80" + ## Parameters (optional): + - receiver_key_id [string, default None]: receiver's Pix Key used in the reported transaction. ex: "+5511989898989" + - return_id [string, default None]: central bank's unique reversal transaction id. Required when reference_type is "reversal". ex: "D20018183202201201213u34sav898j" + ## Attributes (return-only): + - id [string]: unique id returned when the PixInternalTransactionReport is created. ex: "5656565656565656" + - status [string]: current PixInternalTransactionReport status. ex: "created", "processing", "success", "failed" + - updated [datetime.datetime]: latest update datetime for the PixInternalTransactionReport. ex: datetime.datetime(2020, 3, 10, 10, 30, 0, 0) + """ + + def __init__(self, amount, created, end_to_end_id, method, reference_type, sender_account_number, + sender_branch_code, sender_account_type, sender_bank_code, sender_tax_id, receiver_account_number, + receiver_branch_code, receiver_account_type, receiver_bank_code, receiver_tax_id, receiver_key_id=None, + return_id=None, id=None, status=None, updated=None): + Resource.__init__(self, id=id) + + self.amount = amount + self.created = check_datetime(created) + self.end_to_end_id = end_to_end_id + self.method = method + self.reference_type = reference_type + self.sender_account_number = sender_account_number + self.sender_branch_code = sender_branch_code + self.sender_account_type = sender_account_type + self.sender_bank_code = sender_bank_code + self.sender_tax_id = sender_tax_id + self.receiver_account_number = receiver_account_number + self.receiver_branch_code = receiver_branch_code + self.receiver_account_type = receiver_account_type + self.receiver_bank_code = receiver_bank_code + self.receiver_tax_id = receiver_tax_id + self.receiver_key_id = receiver_key_id + self.return_id = return_id + self.status = status + self.updated = check_datetime(updated) + + +_resource = {"class": PixInternalTransactionReport, "name": "PixInternalTransactionReport"} + + +def create(reports, user=None): + """# Create PixInternalTransactionReports + Send a list of PixInternalTransactionReport objects for creation at the Stark Infra API + ## Parameters (required): + - reports [list of PixInternalTransactionReport objects]: list of PixInternalTransactionReport objects to be created in the API + ## Parameters (optional): + - user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkinfra.user was set before function call. + ## Return: + - list of PixInternalTransactionReport objects with updated attributes + """ + return rest.post_multi(resource=_resource, entities=reports, user=user) + + +def get(id, user=None): + """# Retrieve a specific PixInternalTransactionReport + Receive a single PixInternalTransactionReport object previously created in the Stark Infra API by its id + ## Parameters (required): + - id [string]: object unique id. ex: "5656565656565656" + ## Parameters (optional): + - user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkinfra.user was set before function call. + ## Return: + - PixInternalTransactionReport object with updated attributes + """ + return rest.get_id(resource=_resource, id=id, user=user) + + +def query(limit=None, after=None, before=None, status=None, ids=None, user=None): + """# Retrieve PixInternalTransactionReports + Receive a generator of PixInternalTransactionReport objects previously created in the Stark Infra API + ## Parameters (optional): + - limit [integer, default None]: maximum number of objects to be retrieved. Unlimited if None. ex: 35 + - after [datetime.date or string, default None]: date filter for objects created only after specified date. ex: datetime.date(2020, 3, 10) + - before [datetime.date or string, default None]: date filter for objects created only before specified date. ex: datetime.date(2020, 3, 10) + - status [list of strings, default None]: filter for status of retrieved objects. ex: ["created", "processing", "success", "failed"] + - ids [list of strings, default None]: list of ids to filter retrieved objects. ex: ["5656565656565656", "4545454545454545"] + - user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkinfra.user was set before function call. + ## Return: + - generator of PixInternalTransactionReport objects with updated attributes + """ + return rest.get_stream( + resource=_resource, + limit=limit, + after=check_date(after), + before=check_date(before), + status=status, + ids=ids, + user=user, + ) + + +def page(cursor=None, limit=None, after=None, before=None, status=None, ids=None, user=None): + """# Retrieve paged PixInternalTransactionReports + Receive a list of up to 100 PixInternalTransactionReport objects previously created in the Stark Infra API and the cursor to the next page. + Use this function instead of query if you want to manually page your requests. + ## Parameters (optional): + - cursor [string, default None]: cursor returned on the previous page function call + - limit [integer, default 100]: maximum number of objects to be retrieved. It must be an integer between 1 and 100. ex: 50 + - after [datetime.date or string, default None]: date filter for objects created only after specified date. ex: datetime.date(2020, 3, 10) + - before [datetime.date or string, default None]: date filter for objects created only before specified date. ex: datetime.date(2020, 3, 10) + - status [list of strings, default None]: filter for status of retrieved objects. ex: ["created", "processing", "success", "failed"] + - ids [list of strings, default None]: list of ids to filter retrieved objects. ex: ["5656565656565656", "4545454545454545"] + - user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkinfra.user was set before function call. + ## Return: + - list of PixInternalTransactionReport objects with updated attributes + - cursor to retrieve the next page of PixInternalTransactionReport objects + """ + return rest.get_page( + resource=_resource, + cursor=cursor, + limit=limit, + after=check_date(after), + before=check_date(before), + status=status, + ids=ids, + user=user, + ) diff --git a/starkinfra/pixinternaltransactionreport/log/__init__.py b/starkinfra/pixinternaltransactionreport/log/__init__.py new file mode 100644 index 0000000..a8a69ba --- /dev/null +++ b/starkinfra/pixinternaltransactionreport/log/__init__.py @@ -0,0 +1 @@ +from .__log import query, page, get diff --git a/starkinfra/pixinternaltransactionreport/log/__log.py b/starkinfra/pixinternaltransactionreport/log/__log.py new file mode 100644 index 0000000..47d1410 --- /dev/null +++ b/starkinfra/pixinternaltransactionreport/log/__log.py @@ -0,0 +1,96 @@ +from ...utils import rest +from starkcore.utils.resource import Resource +from starkcore.utils.checks import check_datetime, check_date +from starkcore.utils.api import from_api_json +from ..__pixinternaltransactionreport import _resource as _report_resource + + +class Log(Resource): + """# pixinternaltransactionreport.Log object + Every time a PixInternalTransactionReport entity is updated, a corresponding + pixinternaltransactionreport.Log is generated for the entity. This log is never + generated by the user, but it can be retrieved to check additional information + on the PixInternalTransactionReport. + ## Attributes (return-only): + - id [string]: unique id returned when the log is created. ex: "5656565656565656" + - report [PixInternalTransactionReport]: PixInternalTransactionReport entity to which the log refers to. + - errors [list of strings]: list of errors linked to this PixInternalTransactionReport event + - type [string]: type of the PixInternalTransactionReport event which triggered the log creation. ex: "created", "processing", "success", "failed" + - created [datetime.datetime]: creation datetime for the log. ex: datetime.datetime(2020, 3, 10, 10, 30, 0, 0) + """ + + def __init__(self, id, created, type, errors, report): + Resource.__init__(self, id=id) + + self.created = check_datetime(created) + self.type = type + self.errors = errors + self.report = from_api_json(_report_resource, report) + + +_resource = {"class": Log, "name": "PixInternalTransactionReportLog"} + + +def get(id, user=None): + """# Retrieve a specific pixinternaltransactionreport.Log + Receive a single pixinternaltransactionreport.Log object previously created by the Stark Infra API by its id + ## Parameters (required): + - id [string]: object unique id. ex: "5656565656565656" + ## Parameters (optional): + - user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkinfra.user was set before function call. + ## Return: + - pixinternaltransactionreport.Log object with updated attributes + """ + return rest.get_id(resource=_resource, id=id, user=user) + + +def query(limit=None, after=None, before=None, types=None, report_ids=None, user=None): + """# Retrieve pixinternaltransactionreport.Logs + Receive a generator of pixinternaltransactionreport.Log objects previously created in the Stark Infra API + ## Parameters (optional): + - limit [integer, default None]: maximum number of objects to be retrieved. Unlimited if None. ex: 35 + - after [datetime.date or string, default None]: date filter for objects created only after specified date. ex: datetime.date(2020, 3, 10) + - before [datetime.date or string, default None]: date filter for objects created only before specified date. ex: datetime.date(2020, 3, 10) + - types [list of strings, default None]: filter for log event types. ex: ["created", "processing", "success", "failed"] + - report_ids [list of strings, default None]: list of PixInternalTransactionReport ids to filter logs. ex: ["5656565656565656", "4545454545454545"] + - user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkinfra.user was set before function call. + ## Return: + - generator of pixinternaltransactionreport.Log objects with updated attributes + """ + return rest.get_stream( + resource=_resource, + limit=limit, + after=check_date(after), + before=check_date(before), + types=types, + report_ids=report_ids, + user=user, + ) + + +def page(cursor=None, limit=None, after=None, before=None, types=None, report_ids=None, user=None): + """# Retrieve paged pixinternaltransactionreport.Logs + Receive a list of up to 100 pixinternaltransactionreport.Log objects previously created in the Stark Infra API and the cursor to the next page. + Use this function instead of query if you want to manually page your requests. + ## Parameters (optional): + - cursor [string, default None]: cursor returned on the previous page function call + - limit [integer, default 100]: maximum number of objects to be retrieved. It must be an integer between 1 and 100. ex: 50 + - after [datetime.date or string, default None]: date filter for objects created only after specified date. ex: datetime.date(2020, 3, 10) + - before [datetime.date or string, default None]: date filter for objects created only before specified date. ex: datetime.date(2020, 3, 10) + - types [list of strings, default None]: filter for log event types. ex: ["created", "processing", "success", "failed"] + - report_ids [list of strings, default None]: list of PixInternalTransactionReport ids to filter logs. ex: ["5656565656565656", "4545454545454545"] + - user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkinfra.user was set before function call. + ## Return: + - list of pixinternaltransactionreport.Log objects with updated attributes + - cursor to retrieve the next page of pixinternaltransactionreport.Log objects + """ + return rest.get_page( + resource=_resource, + cursor=cursor, + limit=limit, + after=check_date(after), + before=check_date(before), + types=types, + report_ids=report_ids, + user=user, + ) diff --git a/starkinfra/pixkeyholmes/__init__.py b/starkinfra/pixkeyholmes/__init__.py new file mode 100644 index 0000000..0ede07e --- /dev/null +++ b/starkinfra/pixkeyholmes/__init__.py @@ -0,0 +1 @@ +from .__pixkeyholmes import create, query, page diff --git a/starkinfra/pixkeyholmes/__pixkeyholmes.py b/starkinfra/pixkeyholmes/__pixkeyholmes.py new file mode 100644 index 0000000..e0108a7 --- /dev/null +++ b/starkinfra/pixkeyholmes/__pixkeyholmes.py @@ -0,0 +1,105 @@ +from ..utils import rest +from starkcore.utils.resource import Resource +from starkcore.utils.checks import check_datetime, check_date + + +class PixKeyHolmes(Resource): + """# PixKeyHolmes object + PixKeyHolmes are used to investigate the registration status of a Pix Key + in the Central Bank's DICT. + When you initialize a PixKeyHolmes, the entity will not be automatically + created in the Stark Infra API. The 'create' function sends the objects + to the Stark Infra API and returns the list of created objects. + ## Parameters (required): + - key_id [string]: Pix Key to be investigated. ex: "+5511989898989", "11.222.333/0001-00", "valid@sandbox.com" + ## Parameters (optional): + - tags [list of strings, default []]: list of strings for reference when searching for PixKeyHolmes. ex: ["employees", "monthly"] + ## Attributes (return-only): + - id [string]: unique id returned when the PixKeyHolmes is created. ex: "5656565656565656" + - result [string]: result of the investigation after the case is solved. ex: "registered", "unregistered" + - status [string]: current PixKeyHolmes status. ex: "created", "solving", "solved", "failed" + - created [datetime.datetime]: creation datetime for the PixKeyHolmes. ex: datetime.datetime(2020, 3, 10, 10, 30, 0, 0) + - updated [datetime.datetime]: latest update datetime for the PixKeyHolmes. ex: datetime.datetime(2020, 3, 10, 10, 30, 0, 0) + """ + + def __init__(self, key_id, tags=None, id=None, result=None, status=None, created=None, updated=None): + Resource.__init__(self, id=id) + + self.key_id = key_id + self.tags = tags + self.result = result + self.status = status + self.created = check_datetime(created) + self.updated = check_datetime(updated) + + +_resource = {"class": PixKeyHolmes, "name": "PixKeyHolmes"} + + +def create(holmes, user=None): + """# Create PixKeyHolmes + Send a list of PixKeyHolmes objects for creation at the Stark Infra API + ## Parameters (required): + - holmes [list of PixKeyHolmes objects]: list of PixKeyHolmes objects to be created in the API + ## Parameters (optional): + - user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkinfra.user was set before function call. + ## Return: + - list of PixKeyHolmes objects with updated attributes + """ + return rest.post_multi(resource=_resource, entities=holmes, user=user) + + +def query(limit=None, after=None, before=None, status=None, tags=None, ids=None, user=None): + """# Retrieve PixKeyHolmes + Receive a generator of PixKeyHolmes objects previously created in the Stark Infra API + ## Parameters (optional): + - limit [integer, default None]: maximum number of objects to be retrieved. Unlimited if None. ex: 35 + - after [datetime.date or string, default None]: date filter for objects created only after specified date. ex: datetime.date(2020, 3, 10) + - before [datetime.date or string, default None]: date filter for objects created only before specified date. ex: datetime.date(2020, 3, 10) + - status [list of strings, default None]: filter for status of retrieved objects. ex: ["created", "solving", "solved", "failed"] + - tags [list of strings, default None]: tags to filter retrieved objects. ex: ["tony", "stark"] + - ids [list of strings, default None]: list of ids to filter retrieved objects. ex: ["5656565656565656", "4545454545454545"] + - user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkinfra.user was set before function call. + ## Return: + - generator of PixKeyHolmes objects with updated attributes + """ + return rest.get_stream( + resource=_resource, + limit=limit, + after=check_date(after), + before=check_date(before), + status=status, + tags=tags, + ids=ids, + user=user, + ) + + +def page(cursor=None, limit=None, after=None, before=None, status=None, tags=None, ids=None, user=None): + """# Retrieve paged PixKeyHolmes + Receive a list of up to 100 PixKeyHolmes objects previously created in the Stark Infra API and the cursor to the next page. + Use this function instead of query if you want to manually page your requests. + ## Parameters (optional): + - cursor [string, default None]: cursor returned on the previous page function call + - limit [integer, default 100]: maximum number of objects to be retrieved. It must be an integer between 1 and 100. ex: 50 + - after [datetime.date or string, default None]: date filter for objects created only after specified date. ex: datetime.date(2020, 3, 10) + - before [datetime.date or string, default None]: date filter for objects created only before specified date. ex: datetime.date(2020, 3, 10) + - status [list of strings, default None]: filter for status of retrieved objects. ex: ["created", "solving", "solved", "failed"] + - tags [list of strings, default None]: tags to filter retrieved objects. ex: ["tony", "stark"] + - ids [list of strings, default None]: list of ids to filter retrieved objects. ex: ["5656565656565656", "4545454545454545"] + - user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkinfra.user was set before function call. + ## Return: + - list of PixKeyHolmes objects with updated attributes + - cursor to retrieve the next page of PixKeyHolmes objects + """ + return rest.get_page( + resource=_resource, + cursor=cursor, + limit=limit, + after=check_date(after), + before=check_date(before), + status=status, + tags=tags, + ids=ids, + user=user, + ) diff --git a/tests/sdk/testPixFraudLog.py b/tests/sdk/testPixFraudLog.py new file mode 100644 index 0000000..3557a8a --- /dev/null +++ b/tests/sdk/testPixFraudLog.py @@ -0,0 +1,59 @@ +import starkinfra +from unittest import TestCase, main +from tests.utils.user import exampleProject + + +starkinfra.user = exampleProject + + +class TestPixFraudLogQuery(TestCase): + + def test_success(self): + logs = list(starkinfra.pixfraud.log.query(limit=10)) + logs = list(starkinfra.pixfraud.log.query( + limit=10, + fraud_ids={log.fraud.id for log in logs}, + types={log.type for log in logs} + )) + self.assertEqual(10, len(logs)) + print("Number of logs:", len(logs)) + + def test_success_with_params(self): + frauds = starkinfra.pixfraud.log.query( + limit=2, + ) + self.assertEqual(len(list(frauds)), 2) + + +class TestPixFraudLogPage(TestCase): + + def test_success(self): + cursor = None + ids = [] + for _ in range(2): + logs, cursor = starkinfra.pixfraud.log.page(limit=2, cursor=cursor) + for log in logs: + print(log) + self.assertFalse(log.id in ids) + ids.append(log.id) + if cursor is None: + break + self.assertTrue(len(ids) == 4) + + def test_success_with_params(self): + frauds = starkinfra.pixfraud.log.page( + limit=2, + ) + self.assertEqual(len(list(frauds)), 2) + + +class TestPixFraudLogInfoGet(TestCase): + def test_success(self): + logs = starkinfra.pixfraud.log.query(limit=10) + log_id = next(logs).id + log = starkinfra.pixfraud.log.get(id=log_id) + self.assertEqual(log_id, log.id) + + +if __name__ == '__main__': + main() diff --git a/tests/sdk/testPixInternalTransactionReport.py b/tests/sdk/testPixInternalTransactionReport.py new file mode 100644 index 0000000..177fc3a --- /dev/null +++ b/tests/sdk/testPixInternalTransactionReport.py @@ -0,0 +1,53 @@ +import starkinfra +from unittest import TestCase, main +from tests.utils.pixInternalTransactionReport import generateExamplePixInternalTransactionReportJson +from tests.utils.user import exampleProject + +starkinfra.user = exampleProject + + +class TestPixInternalTransactionReportPost(TestCase): + + def test_success(self): + reports = generateExamplePixInternalTransactionReportJson(n=3) + reports = starkinfra.pixinternaltransactionreport.create(reports) + for report in reports: + self.assertIsNotNone(report.id) + + +class TestPixInternalTransactionReportQuery(TestCase): + + def test_success(self): + reports = list(starkinfra.pixinternaltransactionreport.query(limit=10)) + for report in reports: + self.assertIsNotNone(report.id) + print("Number of pix internal transaction reports:", len(reports)) + + +class TestPixInternalTransactionReportPage(TestCase): + + def test_success(self): + cursor = None + ids = [] + for _ in range(2): + reports, cursor = starkinfra.pixinternaltransactionreport.page(limit=2, cursor=cursor) + for report in reports: + self.assertFalse(report.id in ids) + ids.append(report.id) + if cursor is None: + break + self.assertTrue(len(ids) == 4) + + +class TestPixInternalTransactionReportGet(TestCase): + + def test_success(self): + reports = starkinfra.pixinternaltransactionreport.query(limit=10) + for report in reports: + report_id = report.id + report = starkinfra.pixinternaltransactionreport.get(report_id) + self.assertEqual(report.id, report_id) + + +if __name__ == '__main__': + main() diff --git a/tests/sdk/testPixInternalTransactionReportLog.py b/tests/sdk/testPixInternalTransactionReportLog.py new file mode 100644 index 0000000..e12f944 --- /dev/null +++ b/tests/sdk/testPixInternalTransactionReportLog.py @@ -0,0 +1,42 @@ +import starkinfra +from unittest import TestCase, main +from tests.utils.user import exampleProject + +starkinfra.user = exampleProject + + +class TestPixInternalTransactionReportLogQuery(TestCase): + + def test_success(self): + logs = list(starkinfra.pixinternaltransactionreport.log.query(limit=10)) + for log in logs: + self.assertIsNotNone(log.id) + print("Number of pix internal transaction report logs:", len(logs)) + + +class TestPixInternalTransactionReportLogPage(TestCase): + + def test_success(self): + cursor = None + ids = [] + for _ in range(2): + logs, cursor = starkinfra.pixinternaltransactionreport.log.page(limit=2, cursor=cursor) + for log in logs: + self.assertFalse(log.id in ids) + ids.append(log.id) + if cursor is None: + break + self.assertTrue(len(ids) == 4) + + +class TestPixInternalTransactionReportLogInfoGet(TestCase): + + def test_success(self): + logs = starkinfra.pixinternaltransactionreport.log.query(limit=1) + log_id = next(logs).id + log = starkinfra.pixinternaltransactionreport.log.get(id=log_id) + self.assertEqual(log.id, log_id) + + +if __name__ == '__main__': + main() diff --git a/tests/sdk/testPixKeyHolmes.py b/tests/sdk/testPixKeyHolmes.py new file mode 100644 index 0000000..6142725 --- /dev/null +++ b/tests/sdk/testPixKeyHolmes.py @@ -0,0 +1,43 @@ +import starkinfra +from unittest import TestCase, main +from tests.utils.pixKeyHolmes import generateExamplePixKeyHolmesJson +from tests.utils.user import exampleProject + +starkinfra.user = exampleProject + + +class TestPixKeyHolmesPost(TestCase): + + def test_success(self): + holmes = generateExamplePixKeyHolmesJson(n=3) + holmes = starkinfra.pixkeyholmes.create(holmes) + for sherlock in holmes: + self.assertIsNotNone(sherlock.id) + + +class TestPixKeyHolmesQuery(TestCase): + + def test_success(self): + holmes = list(starkinfra.pixkeyholmes.query(limit=10)) + for sherlock in holmes: + self.assertIsNotNone(sherlock.id) + print("Number of pix key holmes:", len(holmes)) + + +class TestPixKeyHolmesPage(TestCase): + + def test_success(self): + cursor = None + ids = [] + for _ in range(2): + holmes, cursor = starkinfra.pixkeyholmes.page(limit=2, cursor=cursor) + for sherlock in holmes: + self.assertFalse(sherlock.id in ids) + ids.append(sherlock.id) + if cursor is None: + break + self.assertTrue(len(ids) == 4) + + +if __name__ == '__main__': + main() diff --git a/tests/utils/pixInternalTransactionReport.py b/tests/utils/pixInternalTransactionReport.py new file mode 100644 index 0000000..2a63059 --- /dev/null +++ b/tests/utils/pixInternalTransactionReport.py @@ -0,0 +1,31 @@ +from random import randint, choice +from starkinfra import PixInternalTransactionReport, endtoendid, returnid +from .user import bank_code +from .date import randomPastDatetime +from .taxIdGenerator import TaxIdGenerator + + +def generateExamplePixInternalTransactionReportJson(n=1): + reports = [] + for _ in range(n): + reference_type = choice(["request", "reversal"]) + report = PixInternalTransactionReport( + amount=randint(100, 1000000), + created=randomPastDatetime(days=30), + end_to_end_id=endtoendid.create(bank_code), + method="manual", + reference_type=reference_type, + sender_account_number=str(randint(10000, 99999999)), + sender_branch_code=str(randint(1, 999)), + sender_account_type=choice(["checking", "savings", "salary", "payment"]), + sender_bank_code=str(bank_code), + sender_tax_id=TaxIdGenerator.taxId(), + receiver_account_number=str(randint(10000, 99999999)), + receiver_branch_code=str(randint(1, 999)), + receiver_account_type=choice(["checking", "savings", "salary", "payment"]), + receiver_bank_code=choice(["18236120", "60701190", "20018183"]), + receiver_tax_id=TaxIdGenerator.taxId(), + return_id=returnid.create(bank_code) if reference_type == "reversal" else None, + ) + reports.append(report) + return reports diff --git a/tests/utils/pixKeyHolmes.py b/tests/utils/pixKeyHolmes.py new file mode 100644 index 0000000..d0d1469 --- /dev/null +++ b/tests/utils/pixKeyHolmes.py @@ -0,0 +1,29 @@ +from copy import deepcopy +from uuid import uuid4 +from random import choice, randint +from starkinfra import PixKeyHolmes +from tests.utils.taxIdGenerator import TaxIdGenerator + + +example_pix_key_holmes = PixKeyHolmes( + key_id="valid@sandbox.com", + tags=["test"], +) + + +def _random_key_id(): + return choice([ + "{}@sandbox.com".format(uuid4().hex[:12]), + "+55{}".format(randint(10000000000, 99999999999)), + TaxIdGenerator.taxId(), + ]) + + +def generateExamplePixKeyHolmesJson(n=1): + holmes_list = [] + for _ in range(n): + holmes = deepcopy(example_pix_key_holmes) + holmes.key_id = _random_key_id() + holmes.tags = ["test"] + holmes_list.append(holmes) + return holmes_list