Packages

Elixir library to deal with PIX payments

Current section

Files

Jump to
ex_pix lib ex_pix.ex
Raw

lib/ex_pix.ex

defmodule ExPIX do
@moduledoc """
Module with basic operations.
All functions in this library use the parameters in the `config.exs` file. By default, the configuration for building static QR-Codes is:
```elixir
config :ex_pix, :static_code,
payload_indicator: "01",
start_method: "11",
gui: "br.gov.bcb.pix",
currency_code: "986",
country_code: "BR",
default_value: "0.00",
category_code: "0000"
```
If you need to change any of these parameters, just define it in your own configuration file.
"""
alias ExPIX.Data.{
Additional,
Static,
StaticAccount,
}
alias ExPIX.Code.Builder
@typedoc """
Basic information for building static code. It must be a map with the following parameters:
- `:merchant_name` - Required field that represents the name of the user to be charged.
- `:merchant_city` - Required field that represents the city of the user to be charged.
- `:postal_code` - Non-Required field that represents the user's postal code.
- `:amount` - The amount of the operation. If not provided, the code will use the default value defined in the configuration. It must be a string in the format 0.00.
- `:payload_indicator` - The payload indicator of the operation. If not provided, the code will use the default value defined in the configuration.
- `:initiation_method` - The initiation method of the operation. If not provided, the code will use the default value defined in the configuration.
- `:category_code` - The category code of the operation. If not provided, the code will use the default value defined in the configuration.
- `:currency_code` - The currency code of the operation. If not provided, the code will use the default value defined in the configuration.
- `:country_code` - The country code of the operation. If not provided, the code will use the default value defined in the configuration.
"""
@type static_params :: %{
required(:merchant_name) => String.t,
required(:merchant_city) => String.t,
optional(:amount) => String.t,
optional(:payload_indicator) => String.t,
optional(:initiation_method) => String.t,
optional(:category_code) => String.t,
optional(:currency_code) => String.t,
optional(:country_code) => String.t,
optional(:postal_code) => String.t,
}
@typedoc """
Optional parameters to build the notes of the operation. The available parameters are:
- `:info` - A string with the operation notes.
**Important:**
- The count of characters of the `info` + count of characters of the `gui` + count of characters of the `key` must not exceed the limit of 99 characters. If this limit is exceeded, an error will be returned.
"""
@type account_params :: %{optional(:info) => String.t}
@typedoc """
Additional parameters to build the code. The available parameters are:
- `:txid` - An ID to represent the operation. If not provided, the `txid` will be `***`, that represents a random string to be generated by Banco Central.
**Important:**
- Banco Central does not check if the provided ID is unique, so the recomendation here is use an UUID (ideally the v4).
- The count of characters must not exceed the limit of 99 characters. If this limit is exceeded, an error will be returned.
"""
@type additional_params :: %{optional(:txid) => String.t}
@doc """
Generates a static raw code.
This function uses the default parameters defined in the `static_code` configuration, but if you need a special parameter, this function can receive such parameters as well.
* `key` - The 'key' to be charged. It can be a CNPJ, CPF, phone number or a random binary string. It is not necessary to send the key type, but this function expects that the key is already in the format for your type (see the PIX docs for some examples).
* `static_params` - Basic information to build a static code.
* `account_params` - Optional parameters to build the notes of the operation.
* `additional_params` - Additional parameters to build the code.
### Example:
```elixir
iex> ExPIX.generate_static_code("email@email.com", %{merchant_name: "Leonardo Leite", merchant_city: "Florianopolis"})
{:ok, "00020101021126370014br.gov.bcb.pix0115email@email.com52040000530398654040.005802BR5914Leonardo Leite6013Florianopolis62070503***6304891C"}
```
"""
@spec generate_static_code(String.t(), static_params, account_params, additional_params) :: {:ok, String.t()} | {:error, any}
def generate_static_code(key, static_params, account_params \\ %{}, additional_params \\ %{}) do
account_info = Map.merge(%StaticAccount{}, Map.merge(%{key: key}, account_params))
additional_info = Map.merge(%Additional{}, additional_params)
%Static{}
|> Map.merge(
static_params
|> Map.put(:account_information, account_info)
|> Map.put(:additional, additional_info)
)
|> Builder.build()
end
end