Popularity
4.7
Stable
Activity
1.6
-
36
0
7

Description

Stripy is a micro wrapper intended to be used for sending requests to Stripe's REST API. It is made for developers who prefer to work directly with the official API and provide their own abstractions on top if such are needed.

Stripy takes care of setting headers, encoding the data, configuration settings, etc (the usual boring boilerplate); it also provides a parse/1 helper function for decoding.

Monthly Downloads: 1,545
Programming language: Elixir
License: MIT License
Tags: Third Party APIs     Stripe     Payments    
Latest version: v1.2.1

Stripy alternatives and similar packages

Based on the "Third Party APIs" category

Do you think we are missing an alternative of Stripy or a related project?

Add another 'Third Party APIs' Package

README

Stripy hex.pm hexdocs.pm

Stripy is a micro wrapper intended to be used for sending requests to Stripe's REST API. It is made for developers who prefer to work directly with the official API and provide their own abstractions on top if such are needed.

Stripy takes care of setting headers, encoding the data, configuration settings, etc (the usual boring boilerplate); it also makes testing easy by letting you plug your own mock server (see Testing section below).

Some basic examples:

iex> Stripy.req(:get, "subscriptions")
{:ok, %HTTPoison.Response{...}}

iex> Stripy.req(:post, "customers", %{"email" => "a@b.c", "metadata[user_id]" => 1})
{:ok, %HTTPoison.Response{...}}

Where subscriptions and customers are REST API resources.

If you prefer to work with a higher-level library, check out "stripity_stripe" or "stripe_elixir" on Hex.

Installation

Add to your mix.exs as usual:

def deps do
  [{:stripy, "~> 1.0"}]
end

If you're not using application inference, then add :stripy to your applications list.

Then configure the stripy app per environment like so:

config :stripy,
  secret_key: "sk_test_xxxxxxxxxxxxx", # required
  endpoint: "https://api.stripe.com/v1/", # optional
  version: "2017-06-05", # optional
  httpoison: [recv_timeout: 5000, timeout: 8000] # optional

Testing

You can disable actual calls to the Stripe API like so:

# Usually in your test.exs.
config :stripy,
  testing: true

All functions that use Stripy would receive response {:ok, %{status_code: 200, body: "{}"}}.

To provide your own responses, you need to configure a mock server:

config :stripy,
  testing: true,
  mock_server: MyApp.StripeMockServer

Here's an example mock server that mocks the /customer endpoint and returns a basic object for a customer with id cus_test

defmodule MyApp.StripeMockServer do
  @behaviour Stripy.MockServer

  @ok_res %{status_code: 200}

  @impl Stripy.MockServer
  def request(:get, "customers/cus_test", %{}) do
    body = Poison.encode!(%{"email" => "email@email.com"})
    {:ok, Map.put(@ok_res, :body, body)}
  end
end

Now let's quickly write a naive function that gets user's billing email:

def stripe_email(user) do
  {:ok, res} = Stripy.req(:get, "customers/#{user.stripe_id}")
  res["email"]
end

We can test it like so:

fake_user = %{stripe_id: "cus_test"}
assert stripe_email(fake_user) == "email@email.com"

About

This project is sponsored by Heresy. We're always looking for great engineers to join our team, so if you love Elixir, open source and enjoy some challenge, drop us a line and say hello!

License

  • Stripy: See LICENSE file.
  • "Heresy" name and logo: Copyright © 2019 Heresy Software Ltd


*Note that all licence references and agreements mentioned in the Stripy README section above are relevant to that project's source code only.