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

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(...):

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:

How It Works

The library uses compile-time metaprogramming to generate all API functions:

  1. Stancer.OpenAPILoader fetches the OpenAPI spec from https://docs.stancer.com/api/openapi.json
  2. On failure, falls back to a cached version in priv/openapi.json
  3. The spec is parsed to extract all operations/endpoints
  4. An Elixir for loop with unquote generates each function at compile time
  5. 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.