Stark Infra Elixir SDK Beta
Welcome to the Stark Infra Elixir SDK! This tool is made for Elixir developers who want to easily integrate with our API. This SDK version is compatible with the Stark Infra API v2.
Introduction
Index
- Introduction
- Setup
- Testing in Sandbox
- Usage
- Issuing
- BINs: View available sub-issuer BINs (a.k.a. card number ranges)
- Holders: Manage card holders
- Cards: Create virtual and/or physical cards
- Purchases: Authorize and view your past purchases
- Invoices: Add money to your issuing balance
- Withdrawals: Send money back to your Workspace from your issuing balance
- Balance: View your issuing balance
- Transactions: View the transactions that have affected your issuing balance
- Pix
- PixRequests: Create Pix transactions
- PixReversals: Reverse Pix transactions
- PixBalance: View your account balance
- PixStatement: Request your account statement
- PixKey: Create a Pix Key
- PixClaim: Claim a Pix Key
- PixDirector: Create a Pix Director
- PixInfraction: Create Pix Infraction reports
- PixChargeback: Create Pix Chargeback requests
- PixDomain: View registered SPI participants certificates
- Credit Note
- CreditNote: Create credit notes
- Webhook:
- Webhook: Configure your webhook endpoints and subscriptions
- Webhook Events:
- WebhookEvents: Manage Webhook events
- WebhookEventAttempts: Query failed webhook event deliveries
- Handling errors
- Help and Feedback
Supported Elixir Versions
This library supports Elixir versions 1.9+.
Stark Infra API documentation
Feel free to take a look at our API docs.
Versioning
This project adheres to the following versioning pattern:
Given a version number MAJOR.MINOR.PATCH, increment:
- MAJOR version when the API version is incremented. This may include backwards incompatible changes;
- MINOR version when breaking changes are introduced OR new functionalities are added in a backwards compatible manner;
- PATCH version when backwards compatible bug fixes are implemented.
Setup
1. Install our SDK
To install the package with mix, add this to your deps and run mix deps.get:
def deps do
[
{:starkinfra, "~> 0.1.0"}
]
end2. Create your Private and Public Keys
We use ECDSA. That means you need to generate a secp256k1 private key to sign your requests to our API, and register your public key with us so we can validate those requests.
You can use one of following methods:
2.1. Check out the options in our tutorial.
2.2. Use our SDK:
{private_key, public_key} = StarkInfra.Key.create()
# or, to also save .pem files in a specific path
{private_key, public_key} = StarkInfra.Key.create("file/keys/")Note: When you are creating a new Project, it is recommended that you create the keys inside the infrastructure that will use it, in order to avoid risky internet transmissions of your private-key. Then you can export the public-key alone to the computer where it will be used in the new Project creation.
3. Register your user credentials
You can interact directly with our API using two types of users: Projects and Organizations.
- Projects are workspace-specific users, that is, they are bound to the workspaces they are created in. One workspace can have multiple Projects.
- Organizations are general users that control your entire organization. They can control all your Workspaces and even create new ones. The Organization is bound to your company's tax ID only. Since this user is unique in your entire organization, only one credential can be linked to it.
3.1. To create a Project in Sandbox:
3.1.1. Log into StarkInfra Sandbox
3.1.2. Go to Menu > Integrations
3.1.3. Click on the "New Project" button
3.1.4. Create a Project: Give it a name and upload the public key you created in section 2
3.1.5. After creating the Project, get its Project ID
3.1.6. Use the Project ID and private key to create the struct below:
# Get your private key from an environment variable or an encrypted database.
# This is only an example of a private key content. You should use your own key.
private_key_content = "
-----BEGIN EC PARAMETERS-----
BgUrgQQACg==
-----END EC PARAMETERS-----
-----BEGIN EC PRIVATE KEY-----
MHQCAQEEIMCwW74H6egQkTiz87WDvLNm7fK/cA+ctA2vg/bbHx3woAcGBSuBBAAK
oUQDQgAE0iaeEHEgr3oTbCfh8U2L+r7zoaeOX964xaAnND5jATGpD/tHec6Oe9U1
IF16ZoTVt1FzZ8WkYQ3XomRD4HS13A==
-----END EC PRIVATE KEY-----
"
project = StarkInfra.project(
id: "5671398416568321",
environment: :sandbox,
private_key: private_key_content
)3.2. To create Organization credentials in Sandbox:
3.2.1. Log into Starkinfra Sandbox
3.2.2. Go to Menu > Integrations
3.2.3. Click on the "Organization public key" button
3.2.4. Upload the public key you created in section 2 (only a legal representative of the organization can upload the public key)
3.2.5. Click on your profile picture and then on the "Organization" menu to get the Organization ID
3.2.6. Use the Organization ID and private key to create the struct below:
# Get your private key from an environment variable or an encrypted database.
# This is only an example of a private key content. You should use your own key.
private_key_content = """
-----BEGIN EC PARAMETERS-----
BgUrgQQACg==
-----END EC PARAMETERS-----
-----BEGIN EC PRIVATE KEY-----
MHQCAQEEIMCwW74H6egQkTiz87WDvLNm7fK/cA+ctA2vg/bbHx3woAcGBSuBBAAK
oUQDQgAE0iaeEHEgr3oTbCfh8U2L+r7zoaeOX964xaAnND5jATGpD/tHec6Oe9U1
IF16ZoTVt1FzZ8WkYQ3XomRD4HS13A==
-----END EC PRIVATE KEY-----
"""
organization = StarkInfra.Organization(
environment: "sandbox",
id: "5656565656565656",
private_key: private_key_content,
workspace_id: nil, # You only need to set the workspace_id when you are operating a specific workspace_id
)NOTE 1: Never hard-code your private key. Get it from an environment variable or an encrypted database.
NOTE 2: We support 'sandbox' and 'production' as environments.
NOTE 3: The credentials you registered in sandbox do not exist in production and vice versa.
4. Setting up the user
There are three kinds of users that can access our API: Organization, Project and Member.
ProjectandOrganizationare designed for integrations and are the ones meant for our SDKs.Memberis the one you use when you log into our webpage with your e-mail.
There are two ways to inform the user to the SDK:
4.1 Passing the user as argument in all functions using the user keyword:
balance = StarkInfra.PixBalance.get!(user: project) # or organization
4.2 Set it as a default user in the config/config.exs file:
import Config
config :starkinfra,
project: [
environment: :sandbox,
id: "9999999999999999",
private_key: private_key_content
]or
import Config
config :starkinfra,
organization: [
environment: :sandbox,
id: "9999999999999999",
private_key: private_key_content,
workspace_id: "8888888888888888" # or nil
]Just select the way of passing the user that is more convenient to you. On all following examples we will assume a default user has been set in the configs.
5. Setting up the error language
The error language can also be set in the config/config.exs file:
import Config
config :starkinfra,
language: "en-US"Language options are "en-US" for english and "pt-BR" for brazilian portuguese. English is default
6. Resource listing and manual pagination
Almost all SDK resources provide a query and a page function.
-
The
queryfunction provides a straight forward way to efficiently iterate through all results that match the filters you inform, seamlessly retrieving the next batch of elements from the API only when you reach the end of the current batch. If you are not worried about data volume or processing time, this is the way to go.
notes = StarkInfra.CreditNote.query!(
after: Date.utc_today |> Date.add(-30),
before: Date.utc_today |> Date.add(-1)
)
|> Enum.take(10)
|> IO.inspect-
The
pagefunction gives you full control over the API pagination. With each function call, you receive up to 100 results and the cursor to retrieve the next batch of elements. This allows you to stop your queries and pick up from where you left off whenever it is convenient. When there are no more elements to be retrieved, the returned cursor will benil.
defmodule CursorRecursion do
def get!(iterations \\ 1, cursor \\ nil)
def get!(iterations, cursor) when iterations > 0 do
{new_cursor, new_entities} = StarkInfra.CreditNote.page!(cursor: cursor)
new_entities ++ get!(
iterations - 1,
new_cursor
)
end
def get!(iterations, _cursor) do
[]
end
end
transactions = CursorRecursion.get!(3)
|> IO.inspect
To simplify the following SDK examples, we will only use the query function, but feel free to use page instead.
Testing in Sandbox
Your initial balance is zero. For many operations in Stark Infra, you'll need funds in your account, which can be added to your balance by creating an StarkBank.Invoice or a StarkBank.Boleto.
In the Sandbox environment, most of the created Invoices and Boletos will be automatically paid, so there's nothing else you need to do to add funds to your account. Just create a few Invoices and wait around a bit.
In Production, you (or one of your clients) will need to actually pay this Invoice or Boleto for the value to be credited to your account.
Usage
Here are a few examples on how to use the SDK.
Here are a few examples on how to use the SDK. If you have any doubts, use the built-in
h() function to get more info on the desired functionality
(for example: StarkInfra.CreditNote |> h)
Note: Almost all SDK functions also provide a bang (!) version. To simplify the examples, they will be used the most throughout this README.
Issuing
Query IssuingBins
To take a look at the sub-issuer BINs available to you, just run the following:
StarkInfra.IssuingBin.query!()
|> Enum.take(10)
|> IO.inspectThis will tell which card products and card number prefixes you have at your disposal.
Create IssuingHolders
You can create card holders to which your cards will be bound. They support spending rules that will apply to all underlying cards.
StarkInfra.IssuingHolder.create!([
%StarkInfra.IssuingHolder{
name: "Iron Bank S.A.",
taxId: "012.345.678-90"
external_id: "1234",
tags: ["Traveler Employee"],
rules: [
%StarkInfra.IssuingRule{
name: "General USD",
interval: "day"
amount: "100000",
currencyCode: "USD"
}
]
}
]) |> IO.inspectNote: Instead of using IssuingHolder structs, you can also pass each transfer element in dictionary format
Query IssuingHolders
You can query multiple holders according to filters.
StarkInfra.IssuingHolder.query!()
|> Enum.take(10)
|> IO.inspectCancel an IssuingHolder
To cancel a single Issuing Holder by its id, run:
StarkInfra.IssuingHolder.cancel!("5155165527080960")
|> IO.inspectGet an IssuingHolder
To get a single Issuing Holder by its id, run:
StarkInfra.IssuingHolder.get!("5155165527080960")
|> IO.inspectQuery IssuingHolder logs
You can query holder logs to better understand holder life cycles.
StarkInfra.IssuingHolder.Log.query!(
limit: 10,
after: Date.utc_today |> Date.add(-30),
before: Date.utc_today |> Date.add(-1)
)
|> Enum.take(10)
|> IO.inspectGet an IssuingHolder log
You can also get a specific log by its id.
StarkInfra.IssuingHolder.Log.get!("5155165527080960")
|> IO.inspectCreate IssuingCards
You can issue cards with specific spending rules.
StarkInfra.IssuingCard.create!([
%StarkInfra.IssuingCard{
holder_name: "Developers",
holder_tax_id: "012.345.678-90"
holder_external_id: "1234",
rules: [
%StarkInfra.IssuingRule{
name: "General",
interval: "week"
amount: "100000",
currencyCode: "USD"
}
]
}
]) |> IO.inspectQuery IssuingCards
You can get a list of created cards given some filters.
StarkInfra.IssuingCard.query!(
limit: 10,
after: Date.utc_today |> Date.add(-30),
before: Date.utc_today |> Date.add(-1),
)
|> Enum.take(10)
|> IO.inspectGet an IssuingCard
After its creation, information on a card may be retrieved by its id.
StarkInfra.IssuingCard.get!("5155165527080960")
|> IO.inspectUpdate an IssuingCard
You can update a specific card by its id.
StarkInfra.IssuingCard.update!(
"5155165527080960",
status: "blocked"
) |> IO.inspectCancel an IssuingCard
You can also cancel a card by its id.
StarkInfra.IssuingCard.cancel!("5155165527080960")
|> IO.inspectQuery IssuingCard logs
Logs are pretty important to understand the life cycle of a card.
StarkInfra.IssuingCard.Log.query!(
limit: 10,
after: Date.utc_today |> Date.add(-30),
before: Date.utc_today |> Date.add(-1)
)
|> Enum.take(10)
|> IO.inspectGet an IssuingCard log
You can get a single log by its id.
StarkInfra.IssuingCard.Log.get!("5155165527080960")
|> IO.inspectProcess Purchase authorizations
It's easy to process purchase authorizations delivered to your endpoint. If you do not approve or decline the authorization within 2 seconds, the authorization will be denied.
request = listen() # this is the method you made to get the events posted to your webhook
{authorization, _cache_pid} = StarkInfra.IssuingAuthorization.parse!(
content: request.content,
signature: request.headers["Digital-Signature"]
)
send_response( # you should also implement this method
StarkInfra.IssuingAuthorization.response!(
"accepted",
amount: authorization.amount,
tags: ["my-purchase-id/123"]
)
)
# or
send_response(
StarkInfra.IssuingAuthorization.response!(
"denied",
reason: "other",
tags: ["other-id/456"]
)
)Query IssuingPurchases
You can get a list of created purchases given some filters.
StarkInfra.IssuingPurchase.query!(
limit: 10,
after: Date.utc_today |> Date.add(-30),
before: Date.utc_today |> Date.add(-1),
)
|> Enum.take(10)
|> IO.inspectGet an IssuingPurchase
After its creation, information on a purchase may be retrieved by its id.
StarkInfra.IssuingPurchase.get!("5155165527080960")
|> IO.inspectQuery IssuingPurchase logs
Logs are pretty important to understand the life cycle of a purchase.
StarkInfra.IssuingPurchase.Log.query!(
limit: 50,
after: Date.utc_today |> Date.add(-30),
before: Date.utc_today |> Date.add(-1)
)
|> Enum.take(50)
|> IO.inspectGet an IssuingPurchase log
You can get a single log by its id.
StarkInfra.IssuingPurchase.Log.get!("5155165527080960")
|> IO.inspectCreate IssuingInvoices
Issuing invoices are requests to transfer money to your Issuing Balance. When an Issuing Invoice you created is paid, the amount will be added to your Issuing Balance.
StarkInfra.IssuingInvoice.create!([
%StarkInfra.IssuingInvoice{
amount: 1000
}
]) |> IO.inspectNote: Instead of using Invoice structs, you can also pass each invoice element in dictionary format
Get an IssuingInvoice
After its creation, information on an invoice may be retrieved by its id. Its status indicates whether it's been paid.
StarkInfra.IssuingInvoice.get!("5155165527080960")
|> IO.inspectQuery IssuingInvoices
You can get a list of created invoices given some filters.
StarkInfra.IssuingInvoice.query!(
limit: 10,
after: Date.utc_today |> Date.add(-30),
before: Date.utc_today |> Date.add(-1),
)
|> Enum.take(10)
|> IO.inspectQuery IssuingInvoice logs
Logs are pretty important to understand the life cycle of an invoice.
StarkInfra.IssuingInvoice.Log.query!(
limit: 50,
after: Date.utc_today |> Date.add(-30),
before: Date.utc_today |> Date.add(-1)
)
|> Enum.take(10)
|> IO.inspectGet an IssuingInvoice log
You can get a single log by its id.
StarkInfra.IssuingInvoice.Log.get!("5155165527080960")
|> IO.inspectCreate IssuingWithdrawals
You can create withdrawals to send cash back from your Issuing balance to your Banking balance by using the Withdrawal resource.
StarkInfra.IssuingWithdrawal.create!(
%StarkInfra.IssuingWithdrawal{
amount: 10000,
external_id: "123"
description: "Sending back",
}
) |> IO.inspectNote: Instead of using Withdrawal structs, you can also pass each withdrawal element in dictionary format
Get an IssuingWithdrawal
After its creation, information on a withdrawal may be retrieved by its id.
StarkInfra.IssuingWithdrawal.get!("5155165527080960")
|> IO.inspectQuery IssuingWithdrawals
You can get a list of created invoices given some filters.
StarkInfra.IssuingWithdrawal.query!(
limit: 10,
after: Date.utc_today |> Date.add(-30),
before: Date.utc_today |> Date.add(-1),
)
|> Enum.take(10)
|> IO.inspectGet your IssuingBalance
To know how much money you have available to run authorizations, run:
StarkInfra.IssuingBalance.get!()
|> IO.inspectQuery IssuingTransactions
To understand your balance changes (issuing statement), you can query transactions. Note that our system creates transactions for you when you make purchases, withdrawals, receive issuing invoice payments, for example.
StarkInfra.IssuingTransaction.query!(
limit: 10,
after: Date.utc_today |> Date.add(-30),
before: Date.utc_today |> Date.add(-1),
)
|> Enum.take(10)
|> IO.inspectGet an IssuingTransaction
You can get a specific transaction by its id:
StarkInfra.IssuingTransaction.get!("5155165527080960")
|> IO.inspectPix
Create PixRequests
You can create a Pix request to transfer money from one of your users to anyone else:
StarkInfra.PixRequest.create!([
%StarkInfra.PixRequest{
amount: 100,
external_id: "141234121",
sender_account_number: "00000-0",
sender_branch_code: "0001",
sender_account_type: "checking",
sender_name: "Tyrion Lannister",
sender_tax_id: "012.345.678-90",
receiver_bank_code: "00000001",
receiver_account_number: "00000-0",
receiver_branch_code: "0001",
receiver_account_type: "checking",
receiver_name: "Jamie Lannister",
receiver_tax_id: "012.345.678-90",
end_to_end_id: StarkInfra.Utils.EndToEndId.generate("00000001") # Pass your bank code to create an end to end ID
}
]) |> IO.inspectNote: Instead of using PixRequest structs, you can also pass each element in dictionary format
Query PixRequests
You can query multiple Pix requests according to filters.
StarkInfra.PixRequest.query!(
after: Date.utc_today |> Date.add(-30),
before: Date.utc_today |> Date.add(-1),
status: ["success"],
tags: ["iron", "suit"],
end_to_end_ids: ["E79457883202101262140HHX553UPqeq"]
)
|> Enum.take(10)
|> IO.inspectGet a PixRequest
After its creation, information on a Pix request may be retrieved by its id. Its status indicates whether it has been paid.
StarkInfra.PixRequest.get!("5155165527080960")
|> IO.inspectQuery PixRequest logs
You can query Pix request logs to better understand Pix request life cycles.
logs = StarkInfra.PixRequest.Log.query!(
after: Date.utc_today |> Date.add(-30),
before: Date.utc_today |> Date.add(-1)
)
|> Enum.take(10)
|> IO.inspectGet a PixRequest log
You can also get a specific log by its id.
StarkInfra.PixRequest.Log.get!("5155165527080960") |> IO.inspectCreate PixReversals
You can reverse a PixRequest either partially or totally using a PixReversal.
StarkInfra.PixReversal.create!([
%StarkInfra.PixReversal{
amount: 100,
external_id: "my_unique_id",
end_to_end_id: "E00000000202201060100rzsJzG9PzMg",
}
]) |> IO.inspectQuery PixReversals
You can query multiple Pix reversals according to filters.
StarkInfra.PixReversal.query!(
limit: 10,
after: Date.utc_today |> Date.add(-30),
before: Date.utc_today |> Date.add(-1),
status: ["created"],
tags: ["iron", "suit"],
return_ids: ["D20018183202202030109X3OoBHG74wo"]
)
|> Enum.take(10)
|> IO.inspectGet a PixReversal
After its creation, information on a Pix reversal may be retrieved by its id. Its status indicates whether it has been successfully processed.
StarkInfra.PixReversal.get!("5155165527080960")
|> IO.inspectQuery PixReversal logs
You can query Pix reversal logs to better understand their life cycles.
StarkInfra.PixReversal.Log.query!(
limit: 50,
after: Date.utc_today |> Date.add(-30),
before: Date.utc_today |> Date.add(-1)
)
|> Enum.take(10)
|> IO.inspectGet a PixReversal log
You can also get a specific log by its id.
StarkInfra.PixReversal.Log.get!("5155165527080960")
|> IO.inspectGet your PixBalance
To see how much money you have in your account, run:
StarkInfra.PixBalance.get!()
|> IO.inspectCreate a PixStatement
Statements are generated directly by the Central Bank and are only available for direct participants. To create a statement of all the transactions that happened on your account during a specific day, run:
statement = StarkInfra.PixStatement.create!(
after: Date.utc_today |> Date.add(-1), # This is the date that you want to create a statement.
before: Date.utc_today |> Date.add(-1), # After and before must be the same date.
type: "transaction" # Options are "interchange", "interchangeTotal", "transaction".
) |> IO.inspectQuery PixStatements
You can query multiple Pix statements according to filters.
statements = StarkInfra.PixStatement.query!(
limit: 50,
)
|> Enum.take(10)
|> IO.inspectGet a PixStatement
Statements are only available for direct participants. To get a Pix statement by its id:
statement = StarkInfra.PixStatement.get!("5674087007387648")
|> IO.inspectGet a PixStatement .csv file
To get the .csv file corresponding to a Pix statement using its id, run:
statement_csv = StarkInfra.PixStatement.get_csv!("5674087007387648")
file = File.open!("statement.zip", [:write])
IO.binwrite(file, statement_csv)
File.close(file)Create a PixKey
You can create a Pix Key to link a bank account information to a key id:
key = StarkInfra.PixKey.create!(
%StarkInfra.PixKey{
key_id: "+5511989898989",
bank_code: "34052649",
account_number: "052649",
branch_code: "0001",
account_type: "checking",
name: "Tyrion Lannister",
tax_id: "012.345.678-90",
}
) |> IO.inspectQuery PixKeys
You can query multiple Pix keys you own according to filters.
keys = StarkInfra.PixKey.query!(
limit: 1,
after: Date.utc_today |> Date.add(-30),
before: Date.utc_today |> Date.add(-1),
status: ["registered"],
tags: ["iron", "suit"],
ids: ["+5511989898989"],
)
|> Enum.take(10)
|> IO.inspectGet a PixKey
Information on a Pix key may be retrieved by its id and the tax ID of the consulting agent. An endToEndId must be informed so you can link any resulting purchases to this query, avoiding sweep blocks by the Central Bank.
StarkInfra.PixKey.get!("+5511989898989", "012.345.678-90")
|> IO.inspectPatch a PixKey
Update the account information linked to a Pix Key.
StarkInfra.PixKey.update!(
"+5511989898989",
"reconciliation",
name: "Tyrion Lannister"
) |> IO.inspectCencel a PixKey
Cancel a specific Pix Key using its id.
StarkInfra.PixKey.cancel!("+5511989898989")
|> IO.inspectQuery PixKey logs
You can query Pix key logs to better understand a Pix key life cycle.
StarkInfra.PixKey.Log.query!(
limit: 50,
ids: ["5729405850615808"],
after: Date.utc_today |> Date.add(-30),
before: Date.utc_today |> Date.add(-1),
types: ["created"],
key_ids: ["+5511989898989"]
)
|> Enum.take(10)
|> IO.inspectGet a PixKey log
You can also get a specific log by its id.
StarkInfra.PixKey.Log.get!("5729405850615808")
|> IO.inspectCreate a PixClaim
You can create a Pix claim to request the transfer of a Pix key from another bank to one of your accounts:
StarkInfra.PixClaim.create!(
%StarkInfra.PixClaim{
account_created: DateTime.utc_now(),
account_number: "123456789",
account_type: "checking",
branch_code: "0001",
name: "Jamie Lanister",
tax_id: "012.345.678-90",
key_id: "+5511933571793",
}
) |> IO.inspectQuery PixClaims
You can query multiple Pix claims according to filters.
StarkInfra.PixClaim.query!(
limit: 1,
after: Date.utc_today |> Date.add(-30),
before: Date.utc_today |> Date.add(-1),
status: ["registered"],
type: "ownership",
agent: "claimed",
key_type: "phone",
key_id: "+5511989898989"
)
|> Enum.take(10)
|> IO.inspectGet a PixClaim
After its creation, information on a Pix claim may be retrieved by its id.
StarkInfra.PixClaim.get!("5729405850615808")
|> IO.inspectPatch a PixClaim
A Pix Claim can be confirmed or canceled by patching its status. A received Pix Claim must be confirmed by the donor to be completed. Ownership Pix Claims can only be canceled by the donor if the reason is "fraud". A sent Pix Claim can also be canceled.
StarkInfra.PixClaim.update!(
"5729405850615808",
status: "confirmed"
) |> IO.inspectQuery PixClaim logs
You can query Pix claim logs to better understand Pix claim life cycles.
logs = StarkInfra.PixClaim.Log.query!(
limit: 50,
after: Date.utc_today |> Date.add(-30),
before: Date.utc_today |> Date.add(-1),
types: ["registered"],
claim_ids: ["5719405850615809"]
)
|> Enum.take(10)
|> IO.inspectGet a PixClaim log
You can also get a specific log by its id.
StarkInfra.PixClaim.Log.get!("5719405850615809")
|> IO.inspectCreate a PixDirector
To register the Pix director contact information at the Central Bank, run the following:
StarkInfra.PixDirector.create!(
%StarkInfra.PixDirector{
name: "Tyrion Lannister",
email: "tyrion@lannister.com",
phone: "+5511989898989",
tax_id: "012.345.678-90",
}
) |> IO.inspectCreate PixInfractions
Pix infractions are used to report transactions that raise fraud suspicion, to request a refund or to reverse a refund. Pix infractions can be created by either participant of a transaction.
StarkInfra.PixInfraction.create!([
%StarkInfra.PixInfraction{
reference_id: "E20018183202201201450u34sDGd19lz",
type: "fraud",
}
]) |> IO.inspectQuery PixInfractions
You can query multiple Pix infractions according to filters.
StarkInfra.PixInfraction.query!(
limit: 10,
after: "2022-01-01",
before: "2022-01-12",
status: ["delivered"],
ids: ["5729405850615808"],
)
|> Enum.take(10)
|> IO.inspectGet a PixInfraction
After its creation, information on a Pix Infraction may be retrieved by its id.
StarkInfra.PixInfraction.get!("5155165527080960") |> IO.inspectUpdate a PixInfraction
A received Pix Infraction can be confirmed or declined by patching its status. After a Pix Infraction is patched, its status changes to closed.
StarkInfra.PixInfraction.update!(
"5155165527080960",
"agreed"
) |> IO.inspectCancel a PixInfraction
Cancel a specific Pix Infraction using its id.
StarkInfra.PixInfraction.cancel!("5155165527080960")
|> IO.inspectQuery PixInfraction logs
You can query Pix infractions logs to better understand their life cycles.
StarkInfra.PixInfraction.Log.query!(
limit: 50,
ids: ["5729405850615808"],
after: "2022-01-01",
before: "2022-01-20",
types: ["created"],
infraction_ids: ["5155165527080960"]
)
|> Enum.take(50)
|> IO.inspectGet a PixInfraction log
You can also get a specific log by its id.
StarkInfra.PixInfraction.Log.get!("5155165527080960")
|> IO.inspectCreate PixChargebacks
A Pix chargeback can be created when fraud is detected on a transaction or a system malfunction results in an erroneous transaction.
StarkInfra.PixChargeback.create!(
%StarkInfra.PixChargeback{
amount: 100,
reference_id: "E20018183202201201450u34sDGd19lz",
reason: "fraud",
}
)
|> Enum.take(100)
|> IO.inspectQuery PixChargebacks
You can query multiple Pix chargebacks according to filters.
chargebacks = StarkInfra.PixChargeback.query!(
limit: 1,
after: "2022-01-01",
before: "2022-01-12",
status: ["registered"],
ids: ["5155165527080960"]
)
|> Enum.take(1)
|> IO.inspectGet a PixChargeback
After its creation, information on a Pix Chargeback may be retrieved by its.
StarkInfra.PixChargeback.get!("5155165527080960")
|> IO.inspectUpdate a PixChargeback
A received Pix Chargeback can be accepted or rejected by patching its status. After a Pix Chargeback is patched, its status changes to closed.
StarkInfra.PixChargeback.update!(
"5155165527080960",
"accepted",
reversal_reference_id: StarkInfra.Utils.ReturnId.create!("20018183")
) |> IO.inspectCancel a PixChargeback
Cancel a specific Pix Chargeback using its id.
StarkInfra.PixChargeback.cancel!("5155165527080960")
|> IO.inspectQuery PixChargeback logs
You can query Pix chargeback logs to better understand Pix chargeback life cycles.
StarkInfra.PixChargeback.Log.query!(
limit: 50,
ids: ["5729405850615808"],
after: "2022-01-01",
before: "2022-01-20",
types: ["created"],
chargeback_ids: ["5155165527080960"]
)
|> Enum.take(50)
|> IO.inspectGet a PixChargeback log
You can also get a specific log by its id.
StarkInfra.PixChargeback.Log.get!("5155165527080960")
|> IO.inspectQuery PixDomains
You can query for domains of registered SPI participants able to issue dynamic QR Codes.
StarkInfra.PixDomain.query!()
|> IO.inspectCredit Note
Create CreditNotes
You can create a Credit Note to generate a CCB contract:
StarkInfra.CreditNote.create!([
%StarkInfra.CreditNote{
template_id: "5686220801703936",
name: "Jamie Lannister",
tax_id: "012.345.678-90",
nominal_amount: 100000,
scheduled: "2022-04-28",
invoices: [
%StarkInfra.CreditNote.Invoice{
due: "2023-06-25",
amount: 120000,
fine: 10,
interest: 2
}
],
tags: ["test", "testing"],
payment: %StarkInfra.CreditNote.Transfer{
due: "2023-06-25",
amount: 120000,
fine: 10,
interest: 2
}
signers: [
%StarkInfra.CreditNote.Transfer{
due: "2023-06-25",
amount: 120000,
fine: 10,
interest: 2
}
],
external_id: "my_unique_123",
street_line_1: "Av. Paulista, 200",
street_line_2: "10 andar",
district: "Bela Vista",
city: "Sao Paulo",
state_code: "SP",
zip_code: "01310-000",
}
]) |> IO.inspectNote: Instead of using CreditNote structs, you can also pass each CreditNote element in map format
Query CreditNotes
You can query multiple credit notes according to filters.
StarkInfra.CreditNote.query!(
limit: 10,
after: Date.utc_today |> Date.add(-30),
before: Date.utc_today |> Date.add(-1),
status: ["success"],
tags: ["iron", "suit"]
)
|> Enum.take(10)
|> IO.inspectGet a CreditNote
After its creation, information on a credit note may be retrieved by its id.
StarkInfra.CreditNote.get!("5155165527080960")
|> IO.inspectCancel a CreditNote
You can cancel a credit note if it has not been signed yet.
StarkInfra.CreditNote.cancel!("5155165527080960")
|> IO.inspectQuery CreditNote logs
You can query credit note logs to better understand credit note life cycles.
StarkInfra.CreditNote.Log.query!(
limit: 10,
after: "2020-11-01",
before: "2020-11-02"
)
|> Enum.take(10)
|> IO.inspectGet a CreditNote log
You can also get a specific log by its id.
StarkInfra.CreditNote.log.get!("5155165527080960")
|> IO.inspectWebhook
Create a Webhook subscription
To create a webhook subscription and be notified whenever an event occurs, run:
StarkInfra.Webhook.create!(
url: "https://webhook.site/dd784f26-1d6a-4ca6-81cb-fda0267761ec",
subscriptions: ["contract", "credit-note", "signer", "issuing-card", "issuing-invoice", "issuing-purchase", "pix-request.in", "pix-request.out", "pix-reversal.in", "pix-reversal.out", "pix-claim", "pix-key", "pix-chargeback", "pix-infraction"]
) |> IO.inspectQuery Webhooks
To search for registered webhooks, run:
for webhook <- StarkInfra.Webhook.query!() do
webhook |> IO.inspect
endGet a Webhook subscription
You can get a specific webhook by its id.
StarkInfra.Webhook.get!("6178044066660352")
|> IO.inspectDelete a Webhook subscription
You can also delete a specific webhook by its id.
StarkInfra.Webhook.delete!("6178044066660352")
|> IO.inspectWebhook events
Process Webhook events
It's easy to process events delivered to your Webhook endpoint. Remember to pass the signature header so the SDK can make sure it was StarkInfra that sent you the event.
response = listen() # this is the function you made to get the events posted to your webhook
{event, _cache_pid} = StarkInfra.Event.parse!(
content: response.content,
signature: response.headers["Digital-Signature"]
) |> IO.inspect
To avoid making unnecessary requests to the API (/GET public-key), you can pass the cache_pid (returned on all requests)
on your next parse. The process referred by the PID cache_pid will store the latest Stark Bank public key
and automatically refresh it if an inconsistency is found between the content, signature and current public key.
Note: If you don't send the cache_pid to the parser, a new cache process will be generated.
{event, _cache_pid} = StarkInfra.Event.parse!(
content: response.content,
signature: response.headers["Digital-Signature"],
cache_pid: cache_pid
) |> IO.inspectIf the data does not check out with the Stark Bank public-key, the function will automatically request the key from the API and try to validate the signature once more. If it still does not check out, it will raise an error.
Query Webhook events
To search for webhooks events, run:
StarkInfra.Event.query!(
after: "2020-03-20",
is_delivered: false,
limit: 10
)
|> Enum.take(10)
|> IO.inspectGet a Webhook event
You can get a specific webhook event by its id.
StarkInfra.Event.get!("4568139664719872")
|> IO.inspectDelete a Webhook event
You can also delete a specific webhook event by its id.
StarkInfra.Event.delete!("4568139664719872")
|> IO.inspectSet Webhook events as delivered
This can be used in case you've lost events.
With this function, you can manually set events retrieved from the API as
"delivered" to help future event queries with is_delivered: false.
StarkInfra.Event.update!("5764442407043072", is_delivered: true)
|> IO.inspectQuery failed Webhook event delivery attempts information
You can also get information on failed webhook event delivery attempts.
for attempt <- StarkInfra.Event.Attempt.query!(after: "2020-03-20") do
attempt |> IO.inspect attempt
endGet a failed Webhook event delivery attempt information
To retrieve information on a single attempt, use the following function:
StarkInfra.Event.Attempt.get("1616161616161616")
|> IO.inspectHandling errors
The SDK may raise or return errors as the StarkInfra.Error struct, which contains the "code" and "message" attributes.
If you use bang functions, the list of errors will be converted into a string and raised. If you use normal functions, the list of error structs will be returned so you can better analyse them.
Help and Feedback
If you have any questions about our SDK, just send us an email. We will respond you quickly, pinky promise. We are here to help you integrate with us ASAP. We also love feedback, so don't be shy about sharing your thoughts with us.
Email: help@starkinfra.com