I am currently creating an integration with a payment gateway via JSON API.
But I don’t know how I can make the client fill in the forms, and insert it into my local database and simultaneously send it to the API.
Has anyone done this?
Can you help me?
I hope it was not confusing, below I will send what has already been done.
Note: I managed to get the API to work manually, but I don’t know how to make it work with the information passed in the template.
Controller:
def create(conn, %{"client" => client_params}) do
case Structure.create_client(client_params) do
{:ok, client} ->
conn
|> put_flash(:info, "Client created successfully.")
|> redirect(to: Routes.client_path(conn, :show, client))
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
Structure.api_create_client()
def api_create_client() do
url = "https://test.test.com.br/v2/customers/"
headers = %{"Content-Type" => "application/json", "Authorization" => "Basic AUTHAPIBASE64"}
hackney = [basic_auth: {"AUTHAPI", "AUTHAPI"}]
body = Poison.encode!(
%{
ownId: "3",
fullname: "Test Teste",
email: "test@test.com",
birthDate: "1980-5-10",
taxDocument: %{
type: "CPF",
number: "00013390000"
},
phone: %{
countryCode: "55",
areaCode: "11",
number: "22226842"
},
shippingAddress: %{
city: "Rio de Janeiro",
district: "Ipanema",
street: "Avenida Atlântica",
streetNumber: "60",
zipCode: "02446000",
state: "RJ",
country: "BRA"
}
}
)
HTTPoison.post!(url, body, headers, [hackney: hackney])
end