Welcome to the Stark Bank PHP SDK! This tool is made for PHP developers who want to easily integrate with our API. This SDK version is compatible with the Stark Bank API v2.
If you have no idea what Stark Bank is, check out our website and discover a world where receiving or making payments is as easy as sending a text message to your client!
- Introduction
- Setup
- Testing in Sandbox
- Usage
- Transactions: Account statement entries
- Balance: Account balance
- 10000 Transfers: Wire transfers (TED and manual Pix)
- DictKeys: Pix Key queries to use with Transfers
- Institutions: Instutitions recognized by the Central Bank
- Invoices: Reconciled receivables (dynamic Pix QR Codes)
- DynamicBrcode: Simplified reconciled receivables (dynamic Pix QR Codes)
- Deposits: Other cash-ins (static Pix QR Codes, manual Pix, etc)
- Boletos: Boleto receivables
- BoletoHolmes: Boleto receivables investigator
- BrcodePayments: Pay Pix QR Codes
- BoletoPayments: Pay Boletos
- UtilityPayments: Pay Utility bills (water, light, etc.)
- TaxPayments: Pay taxes
- DarfPayments: Pay DARFs
- PaymentPreviews: Preview all sorts of payments
- PaymentRequest: Request a payment approval to a cost center
- CorporateHolders: Manage cardholders
- CorporateCard: Create virtual and/or physical cards
- CorporateInvoices: Add money to your corporate balance
- CorporateWithdrawals: Send money back to your Workspace from your corporate balance
- CorporateBalance: View your corporate balance
- CorporateTransactions: View the transactions that have affected your corporate balance
- CorporateEnums: Query enums related to the corporate purchases, such as merchant categories, countries and card purchase methods
- Split: Split received Invoice or Boleto payments between different receivers
- SplitReceiver: Receiver of an Invoice or Boleto split
- SplitProfile: Configure your SplitProfile
- Webhooks: Configure your webhook endpoints and subscriptions
- WebhookEvents: Manage webhook events
- WebhookEventAttempts: Query failed webhook event deliveries
- Workspaces: Manage your accounts
- Handling errors
- Help and Feedback
This library supports the following PHP versions:
- PHP 7.1
- PHP 7.2
- PHP 7.3
- PHP 7.4
- PHP 8.0
- PHP 8.1
Feel free to take a look at our API docs.
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.
1.1 Composer: To install the package with Composer, run:
composer require starkbank/sdk
To use the bindings, use Composer's autoload:
require_once('vendor/autoload.php');
In manual installations, you will also need to get the following dependency:
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:
use StarkBank\Key;
list($privateKey, $publicKey) = Key::create();
# or, to also save .pem files in a specific path
list($privateKey, $publicKey) = Key::create("file/keys/");
NOTE: When you are creating new credentials, it is recommended that you create the keys inside the infrastructure that will use it, in order to avoid any 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.
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 Starkbank 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 object below:
use StarkBank\Project;
// 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.
$privateKeyContent = "
-----BEGIN EC PARAMETERS-----
BgUrgQQACg==
-----END EC PARAMETERS-----
-----BEGIN EC PRIVATE KEY-----
MHQCAQEEIMCwW74H6egQkTiz87WDvLNm7fK/cA+ctA2vg/bbHx3woAcGBSuBBAAK
oUQDQgAE0iaeEHEgr3oTbCfh8U2L+r7zoaeOX964xaAnND5jATGpD/tHec6Oe9U1
IF16ZoTVt1FzZ8WkYQ3XomRD4HS13A==
-----END EC PRIVATE KEY-----
";
$project = new Project([
"environment" => "sandbox",
"id" => "5656565656565656",
"privateKey" => $privateKeyContent
]);
3.2. To create Organization credentials in Sandbox:
3.2.1. Log into Starkbank 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 object below:
use StarkBank\Organization;
// 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.
privateKeyContent = "
-----BEGIN EC PARAMETERS-----
BgUrgQQACg==
-----END EC PARAMETERS-----
-----BEGIN EC PRIVATE KEY-----
MHQCAQEEIMCwW74H6egQkTiz87WDvLNm7fK/cA+ctA2vg/bbHx3woAcGBSuBBAAK
oUQDQgAE0iaeEHEgr3oTbCfh8U2L+r7zoaeOX964xaAnND5jATGpD/tHec6Oe9U1
IF16ZoTVt1FzZ8WkYQ3XomRD4HS13A==
-----END EC PRIVATE KEY-----
";
$organization = new Organization([
"environment" => "sandbox",
"id" => "5656565656565656",
"privateKey" => $privateKeyContent,
"workspaceId" => null // You only need to set the workspaceId when you are operating a specific workspaceId
]);
// To dynamically use your organization credentials in a specific workspaceId,
// you can use the Organization::replace() method:
$balance = Balance::get(Organization::replace($organization, "4848484848484848"));
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.
There are three kinds of users that can access our API: Organization, Project and Member.
Project
andOrganization
are designed for integrations and are the ones meant for our SDKs.Member
is 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:
use StarkBank\Balance;
$balance = Balance::get($project); # or organization
4.2 Set it as a default user in the SDK:
use StarkBank\Settings;
use StarkBank\Balance;
Settings::setUser($project); # or organization
$balance = Balance::get();
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.
The error language can also be set in the same way as the default user:
use StarkBank\Settings;
Settings::setLanguage("en-US");
Language options are "en-US" for english and "pt-BR" for brazilian portuguese. English is default.
Almost all SDK resources provide a query
and a page
function.
- The
query
function 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.
use StarkBank\Transaction;
$transactions = Transaction::query([
"after" => "2020-01-01",
"before" => "2020-03-01"
]);
foreach($transactions as $transaction){
print_r($transaction);
}
- The
page
function 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 benull
.
use StarkBank\Transaction;
$cursor = null;
while (true) {
list($page, $cursor) = Transaction::page($options = ["limit" => 5, "cursor" => $cursor]);
foreach ($page as $transaction) {
print_r($transaction);
}
if ($cursor == null) {
break;
}
}
To simplify the following SDK examples, we will only use the query
function, but feel free to use page
instead.
Your initial balance is zero. For many operations in Stark Bank, you'll need funds in your account, which can be added to your balance by creating an Invoice or a 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.
Here are a few examples on how to use the SDK. If you have any doubts, check out the function or class docstring to get more info or go straight to our [API docs].
To send money between Stark Bank accounts, you can create transactions:
use StarkBank\Transaction;
$transactions = Transaction::create([
new Transaction([
"amount" => 100, # (R$ 1.00)
"receiverId" => "1029378109327810",
"description" => "Transaction to dear provider",
"externalId" => "12345", # so we can block anything you send twice by mistake
"tags" => ["provider"]
]),
new Transaction([
"amount" => 234, # (R$ 2.34)
"receiverId" => "2093029347820947",
"description" => "Transaction to the other provider",
"externalId" => "12346", # so we can block anything you send twice by mistake
"tags" => ["provider"]
]),
]);
foreach($transactions as $transaction){
print_r($transaction);
}
Note: Instead of using Transaction objects, you can also pass each transaction element directly in array format, without using the constructor
To understand your balance changes (bank statement), you can query transactions. Note that our system creates transactions for you when you receive boleto payments, pay a bill or make transfers, for example.
use StarkBank\Transaction;
$transactions = Transaction::query([
"after" => "2020-01-01",
"before" => "2020-03-01"
]);
foreach($transactions as $transaction){
print_r($transaction);
}
You can get a specific transaction by its id:
use StarkBank\Transaction;
$transaction = Transaction::get("5155165527080960");
print_r($transaction);
To know how much money you have in your workspace, run:
use StarkBank\Balance;
$balance = Balance::get();
print_r($balance);
You can also create transfers in the SDK (TED/Pix) and configure transfer behavior according to its rules.
use StarkBank\Transfer;
$transfers = Transfer::create([
new Transfer([
"amount" => 100,
"bankCode" => "033", # TED
"branchCode" => "0001",
"accountNumber" => "10000-0",
"taxId" => "012.345.678-90",
"name" => "Tony Stark",
"tags" => ["iron", "suit"]
]),
new Transfer([
"amount" => 200,
"bankCode" => "20018183", # Pix
"branchCode" => "1234",
"accountNumber" => "123456-7",
"accountType" => "salary",
"externalId" => "my-internal-id-12345",
"taxId" => "012.345.678-90",
"name" => "Jon Snow",
"scheduled" => (new DateTime("now"))->add(new DateInterval("P1D")),
"description" => "Transaction to dear provider",
"tags" => [],
"rules" => [
new Transfer\Rule([
"key" => "resendingLimit", # Set maximum number of retries if Transfer fails due to systemic issues at the receiver bank
"value" => 5 # Our resending limit is 10 by default
])
]
])
]);
foreach($transfers as $transfer){
print_r($transfer);
}
Note: Instead of using Transfer objects, you can also pass each transfer element directly in array format, without using the constructor
You can query multiple transfers according to filters.
use StarkBank\Transfer;
$transfers = Transfer::query([
"after" => "2020-01-01",
"before" => "2020-04-01"
]);
foreach($transfers as $transfer){
print_r($transfer->name);
}
To get a single transfer by its id, run:
use StarkBank\Transfer;
$transfer = Transfer::get("5155165527080960");
print_r($transfer);
To cancel a single scheduled transfer by its id, run:
use StarkBank\Transfer;
$transfer = Transfer::delete("5155165527080960");
print_r($transfer);
A transfer PDF may also be retrieved by passing its id. This operation is only valid if the transfer status is "processing" or "success".
use StarkBank\Transfer;
$pdf = Transfer::pdf("5155165527080960");
$fp = fopen('transfer.pdf', 'w');
fwrite($fp, $pdf);
fclose($fp);
Be careful not to accidentally enforce any encoding on the raw pdf content, as it may yield abnormal results in the final file, such as missing images and strange characters.
You can query transfer logs to better understand transfer life cycles.
use StarkBank\Transfer;
$logs = Transfer\Log::query(["limit" => 50]);
foreach($logs as $log){
print_r($log->id);
}
You can also get a specific log by its id.
use StarkBank\Transfer;
$log = Transfer\Log::get("5155165527080960");
print_r($log);
You can get Pix key's parameters by its id.
use StarkBank\DictKey;
$dictKey = DictKey::get();
print_r($dictKey);
To take a look at the Pix keys linked to your workspace, just run the following:
use StarkBank\DictKey;
$dictKeys = iterator_to_array(DictKey::query(["limit" => 1, "typ
10000
e" => "evp", "status" => "registered"]));
foreach($dictKeys as $dictKey) {
print_r($dictKey);
}
You can query institutions registered by the Brazilian Central Bank for Pix and TED transactions.
use StarkBank\Institution;
$institutions = Institution::query(["search" => "stark"]);
foreach($institutions as $institution){
print_r($institution);
}
You can create dynamic QR Code invoices to charge customers or to receive money from accounts you have in other banks.
Since the banking system only understands value modifiers (discounts, fines and interest) when dealing with dates (instead of datetimes), these values will only show up in the end user banking interface if you use dates in the "due" and "discounts" fields.
If you use datetimes instead, our system will apply the value modifiers in the same manner, but the end user will only see the final value to be paid on his interface.
Also, other banks will most likely only allow payment scheduling on invoices defined with dates instead of datetimes.
use StarkBank\Invoice;
$invoices = [
new Invoice([
"amount" => 400000,
"due" => ((new DateTime("now"))->add(new DateInterval("P5D"))),
"taxId" => "012.345.678-90",
"name" => "Mr Meeseks",
"expiration" => new DateInterval("P2D"),
"fine" => 2.5,
"interest" => 1.3,
"discounts" => [
[
"percentage" => 5,
"due" => ((new DateTime("now"))->add(new DateInterval("P1D")))
],
[
"percentage" => 3,
"due" => ((new DateTime("now"))->add(new DateInterval("P2D")))
]
],
"rules" => [
new Invoice\Rule([
"key" => "allowedTaxIds", # Set TaxIds allowed to receive this Invoice
"value" => [
"012.345.678-90"
]
])
],
"tags" => [
'War supply',
'Invoice #1234'
],
"descriptions" => [
[
"key" => "product A",
"value" => "big"
],
[
"key" => "product B",
"value" => "medium"
],
[
"key" => "product C",
"value" => "small"
]
],
])
];
$invoice = Invoice::create($invoices)[0];
print_r($invoice);
Note: Instead of using Invoice objects, you can also pass each invoice element directly in array format, without using the constructor
After its creation, information on an invoice may be retrieved by its id. Its status indicates whether it's been paid.
use StarkBank\Invoice;
$invoice = Invoice::get("5656565656565656");
print_r($invoice);
After its creation, an Invoice QR Code may be retrieved by its id.
use StarkBank\Invoice;
$png = Invoice::qrcode("5881614903017472");
$fp = fopen('qrcode.png', 'w');
fwrite($fp, $png);
fclose($fp);
Be careful not to accidentally enforce any encoding on the raw png content, as it may corrupt the file.
After its creation, an invoice PDF may be retrieved by its id.
use StarkBank\Invoice;
$pdf = Invoice::pdf("5656565656565656");
$fp = fopen('invoice.pdf', 'w');
fwrite($fp, $pdf);
fclose($fp);
Be careful not to accidentally enforce any encoding on the raw pdf content, as it may yield abnormal results in the final file, such as missing images and strange characters.
You can also cancel an invoice by its id. Note that this is not possible if it has been paid already.
use StarkBank\Invoice;
$invoice = Invoice::update("5656565656565656", ["status" => "canceled"]);
print_r($invoice);
You can update an invoice's amount, due date and expiration by its id. If the invoice has already been paid, only the amount can be decreased, which will result in a payment reversal. To fully reverse the invoice, pass "amount" => 0.
use StarkBank\Invoice;
$updatedInvoice = Invoice::update(
"5656565656565656",
[
"amount" => 4321,
"due" => (new DateTime("now"))->add(new DateInterval("P5D")),
"expiration" => 123456789
]
);
print_r($updatedInvoice);
You can get a list of created invoices given some filters.
use StarkBank\Invoice;
$invoices = iterator_to_array(Invoice::query(["limit" => 10, "before" => new DateTime("now")]));
foreach($invoices as $invoice) {
print_r($invoice);
}
Whenever an Invoice is successfully reversed, a reversed log will be created. To retrieve a specific reversal receipt, you can request the corresponding log PDF:
use StarkBank\Invoice\Log;
$pdf = Log::pdf("5155165527080960");
$fp = fopen('invoice-log.pdf', 'w');
fwrite($fp, $pdf);
fclose($fp);
Be careful not to accidentally enforce any encoding on the raw pdf content, as it may yield abnormal results in the final file, such as missing images and strange characters.
Once an invoice has been paid, you can get the payment information using the Invoice.Payment sub-resource:
use StarkBank\Invoice;
$paymentInformation = Invoice::payment("5656565656565656");
print_r($paymentInformation);
Logs are pretty important to understand the life cycle of an invoice.
use StarkBank\Invoice\Log;
$invoiceLogs = iterator_to_array(Log::query(["limit" => 10, "types" => ["created"]]));
foreach($invoiceLogs as $log) {
print_r($log);
}
You can get a single log by its id.
use StarkBank\Invoice\Log;
$invoiceLog = Log::get("5656565656565656");
print_r($invoice);
You can create simplified dynamic QR Codes to receive money using Pix transactions. When a DynamicBrcode is paid, a Deposit is created with the tags parameter containing the character “dynamic-brcode/” followed by the DynamicBrcode’s uuid "dynamic-brcode/{uuid}" for conciliation.
The differences between an Invoice and the DynamicBrcode are the following:
Invoice | DynamicBrcode | |
---|---|---|
Expiration | âś“ | âś“ |
Due, fine and fee | âś“ | X |
Discount | âś“ | X |
Description | âś“ | X |
Can be updated | âś“ | X |
Note: In order to check if a BR code has expired, you must first calculate its expiration date (add the expiration to the creation date). Note: To know if the BR code has been paid, you need to query your Deposits by the tag "dynamic-brcode/{uuid}" to check if it has been paid.
use StarkBank\DynamicBrcode;
$brcodes = DynamicBrcode::create([
new DynamicBrcode([
"amount" => 23571, # R$ 235,71
"expiration" => 12345
]),
new DynamicBrcode([
"amount" => 23571, # R$ 235,71
"expiration" => 12345
])
]);
foreach($brcodes as $brcode) {
print_r($brcode);
}
Note: Instead of using DynamicBrcode objects, you can also pass each brcode element in dictionary format
After its creation, information on a DynamicBrcode may be retrieved by its uuid.
use StarkBank\DynamicBrcode;
$brcode = DynamicBrcode::get("e09e6c5293a5485d9777cc29582e3ecf");
print_r($brcode);
You can get a list of created DynamicBrcodes given some filters.
use StarkBank\DynamicBrcode;
$brcodes = iterator_to_array(DynamicBrcode::query(
[
"limit" => 10,
"after" => "2023-01-01",
"before" => "2023-01-30",
]
));
foreach($brcodes as $brcode) {
print_r($brcode);
}
You can get a list of created deposits given some filters.
use StarkBank\Deposit;
$deposits = iterator_to_array(Deposit::query(["limit" => 10, "before" => new DateTime("now")]));
foreach($deposits as $deposit) {
print_r($deposit);
}
After its creation, information on a deposit may be retrieved by its id.
use StarkBank\Deposit;
$deposit = Deposit::get("5656565656565656");
print_r($deposit);
You can update a deposit's amount by its id. To fully reverse the deposit, pass "amount" => 0.
use StarkBank\Deposit
$updatedDeposit = Deposit::update(
"5656565656565656",
[
"amount" => 4321
]
);
print_r($updatedDeposit);
Logs are pretty important to understand the life cycle of a deposit.
use StarkBank\Deposit\Log;
$depositLogs = iterator_to_array(Log::query(["limit" => 10, "types" => ["created"]]));
foreach($depositLogs as $log) {
print_r($log);
}
You can get a single log by its id.
use StarkBank\Deposit\Log;
$depositLog = Log::get("5656565656565656");
print_r($deposit);
You can create boletos to charge customers or to receive money from accounts you have in other banks.
use StarkBank\Boleto;
$boletos = Boleto::create([
new Boleto([
"amount" => 23571, # R$ 235,71
"name" => "Buzz Aldrin",
"taxId" => "012.345.678-90",
"streetLine1" => "Av. Paulista, 200",
"streetLine2" => "10 andar",
"district" => "Bela Vista",
"city" => "SĂŁo Paulo",
"stateCode" => "SP",
"zipCode" => "01310-000",
"due" => (new DateTime("now"))->add(new DateInterval("P30D")),
"fine" => 5, # 5%
"interest" => 2.5 # 2.5% per month
])
]);
foreach($boletos as $boleto){
print_r($boleto);
}
Note: Instead of using Boleto objects, you can also pass each boleto element directly in array format, without using the constructor
After its creation, information on a boleto may be retrieved by passing its id. Its status indicates whether it's been paid.
use StarkBank\Boleto;
$boleto = Boleto::get("5155165527080960");
print_r($boleto);
After its creation, a boleto PDF may be retrieved by passing its id.
use StarkBank\Boleto;
$pdf = Boleto::pdf("5155165527080960", ["layout" => "default"]);
$fp = fopen('boleto.pdf', 'w');
fwrite($fp, $pdf);
fclose($fp);
Be careful not to accidentally enforce any encoding on the raw pdf content, as it may yield abnormal results in the final file, such as missing images and strange characters.
You can also cancel a boleto by its id. Note that this is not possible if it has been processed already.
use StarkBank\Boleto;
$boleto = Boleto::delete("5155165527080960");
print_r($boleto);
You can get an array of created boletos given some filters.
use StarkBank\Boleto;
$boletos = Boleto::query([
"after" => "2020-01-01",
"before" => (new DateTime("now"))->add(new DateInterval("P1D"))
]);
foreach($boletos as $boleto){
print_r($boleto);
}
Logs are pretty important to understand the life cycle of a boleto.
use StarkBank\Boleto;
$logs = Boleto\Log::query(["limit" => 150]);
foreach($logs as $log){
print_r($log);
}
You can get a single log by its id.
use StarkBank\Boleto;
$log = Boleto\Log::get("5155165527080960");
print_r($log);