Giter Club home page Giter Club logo

stripy's Introduction

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" => "[email protected]", "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, "~> 2.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

You may also use environment variables:

config :stripy,
  secret_key: {:system, "STRIPE_SECRET_KEY"},
  endpoint: {:system, "STRIPE_ENDPOINT"},
  version: {:system, "STRIPE_VERSION"}

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 protected]"})
    {: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 protected]"

Custom headers

You can add custom headers to the request by supplying a fourth parameter:

Stripy.req(:post, "charges", %{amount: 1000}, %{"Idempotency-Key" => "123456"})

License

  • Stripy: See LICENSE file.

stripy's People

Contributors

danschultzer avatar svileng avatar tomconroy avatar vic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

stripy's Issues

Change the way how request body is sent out

At this moment post body is encoded and concatenated in url
see: https://github.com/heresydev/stripy/blob/1.2.1/lib/stripy.ex#L36

If the request contains a large body(>5000 characters), it will be rejected by Stripe with

{
  "body": {
    "error": {
      "message": "Invalid string: Lorem Ipsu...rem Ipsum.; must be at most 5000 characters",
      "param": "line_items[0][description]",
      "type": "invalid_request_error"
    }
  }
}

I belive it is better for https://github.com/heresydev/stripy/blob/1.2.1/lib/stripy.ex#L65 to send out the actual body, rather than just throw everything to the url.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.