Current section

Files

Jump to
beanstream lib beanstream profile.ex
Raw

lib/beanstream/profile.ex

defmodule Beanstream.Profile do
@moduledoc """
Manage customer profiles.
"""
alias Beanstream.{Gateway,Profile}
defstruct [
card: nil,
# token: nil, # is this a deprecated legato token?
billing: nil,
custom: nil,
language: nil,
comment: nil,
]
@type t :: %Beanstream.Profile{
card: Beanstream.Card,
billing: Beanstream.Address,
custom: map,
language: String.t, # 2 characters
comment: String.t,
}
@doc """
make a customer profile
"""
@spec make(Gateway.t, Profile.t) :: tuple
def make(gw, req) do
result = Beanstream.post("profiles", req, auth_header(gw))
|> Beanstream.process_response
case result do
{:ok, %{"customer_code" => customer_code}} -> {:ok, customer_code}
_ -> result
end
end
@spec get(Gateway.t, String.t) :: tuple
def get(gw, customer_code) do
Beanstream.get("profiles/#{customer_code}", auth_header(gw))
|> Beanstream.process_response
end
@doc """
Update a customer profile
NOTE: only the name on the card can be updated this way (based on the docs)
"""
@spec update(Gateway.t, String.t, Profile.t) :: tuple
def update(gw, customer_code, req) do
result = Beanstream.put("profiles/#{customer_code}", req, auth_header(gw))
|> Beanstream.process_response
case result do
{:ok, %{"customer_code" => customer_code}} -> {:ok, customer_code}
_ -> result
end
end
@spec delete(Gateway.t, String.t) :: any
def delete(gw, customer_code) do
result = Beanstream.delete("profiles/#{customer_code}", auth_header(gw))
|> Beanstream.process_response
case result do
{:ok, _} -> :ok
_ -> result
end
end
defp auth_header(gw) do
Gateway.auth_header(gw, "profiles")
end
end