Junex (Junex v0.2.0) View Source
Junex is a library for help you to interact to the Juno API in a easier way!
WARNINGS
- Although you can build the maps mannualy, like
charge_info
andpayment_billing_info
, Junex provide a bunch of helper functions to build the exactly structure that the Juno API requests, so consider using them! - All main function receive as last param an atom that could be
:prod
or:sandbox
- The
create_client
andget_access_token
functions can also been called with config fromconfig.exs
Config
You can provide config information for Junex in three ways:
- On
config.exs
config file - Calling from code, with
Junex.configure/1 or /2
- Providing manually all configs
The available configs are:
client_id
client_secret
resource_token
mode
Example config on config.exs
:
config :junex, :tokens,
client_id: System.get_env("CLIENT_ID"),
client_secret: System.get_env("CLIENT_SECRET"),
resource_token: System.get_env("RESOURCE_TOKEN"),
mode: :prod
Example of use
As an example, see how you could create a charge and a payment:
First, you need an access_token
, to get one, you need to have a client_id
and client_secret
pair.
You can generate one for production or sandbox on the Juno's Integration screen.
After that:
defmodule MyApp.Payment do
def jwt_token(client_id, client_secret) do
token_params = [
client_id: System.get_env("CLIENT_ID"),
client_secret: System.get_env("CLIENT_SECRET"),
mode: :sandbox
]
case Junex.get_access_token(token_params) do
{:ok, token} ->
token
{:error, error} ->
{:error, error}
end
end
end
Now you have an access_token
you can make another requests! Let create a charge now:
For this, you need first to create a client, providing the access_token
and also the resource_token
, that
is the Private Token
that you also can generate on the Integration screen.
defmodule MyApp.Payment do
def charges do
with {:ok, client} <- Junex.create_client/2 or /1,
{:ok, charge_info} <- Junex.get_charge_info(params),
{:ok, charge_billing_info} <- Junex.get_charge_billing_info(params),
{:ok, charges} <-
Junex.create_charges(client, charge_info: charge_info, charge_billing_info: charge_billing_info) do
charges
else
{:error, error} ->
{:error, error}
end
end
end
Ok, charges created and returned as a list, so, if the payment_type
was :credit_card
, you can
generate the payment in sequence
defmodule MyApp.Payment do
def payment do
with {:ok, card_info} <- Junex.get_card_info(params),
{:ok, payment_billing_info} <- Junex.get_payment_billing_info(params),
{:ok, params} <-
Junex.get_payment_info(card_info: card_info, payment_billing_info: payment_billing_info),
payment_results <- charges |> Task.async_stream(&do_payment(&1, params)) do
payment_results
else
error ->
error
end
end
def do_payment(charge) do
case Junex.create_payment(client, payment_info: payment_info, mode: :sandbox) do
{:ok, payment} ->
payment
{:error, error} ->
{:error, error}
end
end
end
Link to this section Summary
Functions
Returns the latest charge status
Returns current Junex configuration settings for accessing Juno server.
Provides configuration settings for accessing Juno server.
Provides configuration settings for accessing Juno server.
Creates and return a new charge
Same as Junex.create_client/2
however uses config from config.exs
Returns a new client to perform other requests!
Creates and returns a new Payment
Same as Junex.get_access_token/1, however, uses config from config.exs
Return a access_token to be used on other Junex requests
Return you current balance!
Return a card_info map to use on Junex.get_payment_info/1
Return a new charge_billing_info map to be used on Junex.create_charges/2
Returns a charge_info map to be used on Junex.create_charges/2
Returns a payment_billing_info map to use on Junex.get_payment_info/1
Returns a payment_info map to be used on Junex.create_payment/2
List all possible banks for Juno transfers
Link to this section Functions
Returns the latest charge status
Parameters
- client: Got from Junex.create_client/1
- charge_id: One of results do Junex.create_charges/2
- mode: :prod | :sandbox
Example
Junex.check_charge_status(
Junex.create_client(params),
client_id: "client_id",
mode: :sandbox
)
Returns current Junex configuration settings for accessing Juno server.
Provides configuration settings for accessing Juno server.
The specified configuration applies globally. Use Junex.configure/2
for setting different configurations on each processes.
Example
Junex.configure(
client_id: System.get_env("CLIENT_ID"),
client_secret: System.get_env("CLIENT_SECRET"),
mode: System.get_env("JUNO_MODE")
)
Provides configuration settings for accessing Juno server.
Options
The scope
can have one of the following values.
:global
- configuration is shared for all processes.:process
- configuration is isolated for each process.
Example
Junex.configure(
:global,
client_id: System.get_env("CLIENT_ID"),
client_secret: System.get_env("CLIENT_SECRET"),
mode: System.get_env("JUNO_MODE")
)
Creates and return a new charge
Parameters
- client: Got from Junex.create_client/1
- charge_info: Build mannualy or generated with Junex.get_charge_info/1
- billing: Build mannualy or generated with Junex.get_charge_billing_info/1
- mode: :prod | :sandbox
Example
Junex.create_charges(
Junex.create_client(params),
Map.merge(Junex.get_charge_info(), Junex.get_charge_billing_info())
)
Same as Junex.create_client/2
however uses config from config.exs
Params
- access_token: Got from Junex.get_access_token/1 or /0
Examples
Junex.create_client(access_token)
Returns a new client to perform other requests!
Params
- access_token: Got from Junex.Auth.get_access_token
- resource_token: You can generate one on your Juno's account, is the "Private Token"
Examples
Junex.Client.create(
System.get_env("ACCESS_TOKEN"),
System.get_env("RESOURCE_TOKEN")
)
Creates and returns a new Payment
Parameters
- client: Got from Junex.create_client/1
- payment_info: Build mannualy or got from Junex.get_payment_info/1
- mode: :prod | :sandbox
Example
Junex.create_payment(
Junex.create_client(params),
payment_info: Junex.get_payment_info(params),
mode: :sandbox
)
Same as Junex.get_access_token/1, however, uses config from config.exs
Return a access_token to be used on other Junex requests
You can get the client_id and client_secret on the Integration section on your Juno account and generate the pair!
Parameters
- client_id: string
- client_secret: string
- mode: :prod | :sandbox
Examples
Junex.Auth.get_access_token(client_id: "client_id", client_secret: "client_secret", mode: :mode)
Return you current balance!
Parameters
- client: Get from Junex.create_client/1
- mode: :prod | :sandbox
Examples
Junex.get_balance(Junex.create_client(), :sandbox)
Return a card_info map to use on Junex.get_payment_info/1
Return a new charge_billing_info map to be used on Junex.create_charges/2
Example
Junex.get_charge_billing_info(
name: "name",
document: "document",
email: "email",
phone: "phone"
)
Returns a charge_info map to be used on Junex.create_charges/2
Example
Junex.get_charge_info(
description: "description",
amount: 123,
installments: 2,
payment_type: :boleto
)
Returns a payment_billing_info map to use on Junex.get_payment_info/1
Examples
Junex.get_payment_billing_info(
email: "email",
street: "street",
st_number: 12,
city: "city",
state: "state",
complement: "complement",
post_code: "post_code"
)
Returns a payment_info map to be used on Junex.create_payment/2
Parameters
- charge_id: Result of one entries of Junex.create_charges/2
- card_info: Build mannualy or got from Junex.get_card_info/1
- payment_billing_info: Build mannually or got from Junex.get_payment_billing_info/1
Example
Junex.get_payment_info(
charge_id: "charge_id",
card_info: Junex.get_card_info(params),
payment_billing_info: Junex.get_payment_billing_info(params)
)
List all possible banks for Juno transfers
Parameters
- client: from Junex.create_client/1
- mode: :prod | :sandbox
Examples
Junex.list_banks(Junex.create_client(), :sandbox)