Stancer
Elixir client library for Stancer, a French payment processor.
Built with compile-time metaprogramming to automatically generate 51+ API functions from the OpenAPI specification. Provides a simple, type-safe interface to tokenize cards, create payments, manage refunds, and more.
Features
- ๐ง 51+ API functions automatically generated from OpenAPI spec at compile time
- ๐ Secure by default: Never logs sensitive data, supports 3D Secure (SCA)
- โก Minimal dependencies: Uses
Reqfor HTTP andJasonfor JSON - ๐ฆ Zero config secrets: API key via standard Elixir config
- ๐ Complete API coverage: All Stancer endpoints (customers, payments, cards, etc.)
- ๐งช Fully tested: Comprehensive test suite with mocking
Installation
Add ex_stancer to your mix.exs dependencies:
def deps do
[
{:ex_stancer, "~> 0.1"}
]
end
Then run:
mix deps.get
Configuration
Add your Stancer API key to your Elixir config:
# config/config.exs
config :stancer,
api_key: System.get_env("STANCER_API_KEY"),
api_url: "https://api.stancer.com",
api_version: "v1"
Quick Start
Tokenize a Card
{:ok, card} = Stancer.API.tokens_create(%{
"card" => %{
"number" => "4111111111111111",
"exp_month" => 12,
"exp_year" => 2026,
"cvc" => "123"
}
})
Create a Payment
{:ok, payment} = Stancer.API.payments_create(%{
"amount" => 5000, # in cents (50โฌ)
"currency" => "EUR",
"card" => card["id"],
"capture" => false # auth only, capture later
})
Capture a Payment
{:ok, result} = Stancer.API.payments_capture(payment["id"], 5000)
Refund a Payment
{:ok, result} = Stancer.API.payments_refund(payment["id"])
API Reference
All endpoints follow the pattern Stancer.API.resource_action(...):
Stancer.API.customers_list()โGET /customersStancer.API.customers_create(data)โPOST /customersStancer.API.customers_get(id)โGET /customers/{id}Stancer.API.customers_update(id, data)โPUT /customers/{id}Stancer.API.payments_create(data)โPOST /paymentsStancer.API.payments_capture(id, amount)โPOST /payments/{id}/capture- ... and 45+ more
See HexDocs for the complete API reference.
Error Handling
All functions return {:ok, result} | {:error, reason}:
case Stancer.API.payments_create(payment_data) do
{:ok, payment} ->
IO.inspect(payment)
{:error, reason} ->
IO.warn("Payment failed: #{reason}")
end
Common errors:
"Access Denied"โ Invalid or missing API key"Invalid card"โ Card data incomplete or malformed"Amount too low"โ Minimum is โฌ0.50 (50 cents)
How It Works
The library uses compile-time metaprogramming to generate all API functions:
Stancer.OpenAPILoaderfetches the OpenAPI spec fromhttps://docs.stancer.com/api/openapi.json- On failure, falls back to a cached version in
priv/openapi.json - The spec is parsed to extract all operations/endpoints
- An Elixir
forloop withunquotegenerates each function at compile time - Each function has a unique name, correct arity, and type guards
No runtime reflection. No magic. Just pure Elixir metaprogramming.
Documentation
Documentation can be generated locally with:
mix docs
open doc/index.html
Or view the latest version on HexDocs.