Giter Club home page Giter Club logo

stripe-elixir's Introduction

Stripe Build Status

Stripe API client for Elixir. Documentation

  • Everything except for Relay features are complete and tested.
  • Looking for more contributors/maintainers for this project, currently need help with documentation.

Installation

  1. Add stripe to your list of dependencies in mix.exs:
def deps do
  [{:stripe, "~> 0.8.0", hex: :stripe_elixir}]
end
  1. (Pre-Elixir 1.4) Ensure stripe is started before your application:
def application do
  [applications: [:stripe]]
end
  1. Make sure your stripe secret_key is added to your config file:
config :stripe, :secret_key, <YOUR_SECRET_KEY>
  1. Alternatively, you can also set the secret key as an environment variable:
export STRIPE_SECRET_KEY=<YOUR_SECRET_KEY>

Basic Usage

This lib closely follows the official Ruby Client API.

  • Stripe.{RESOURCE}.create
  • Stripe.{RESOURCE}.retrieve
  • Stripe.{RESOURCE}.update
  • Stripe.{RESOURCE}.list

Returns {:ok, RESPONSE_BODY} when the request is successful.

{:error, %ERROR_STRUCT{}} tuples are returned when there is a request/api error.
See all error types at https://stripe.com/docs/api/ruby#errors

Some Basic Examples

Create a customer:

{:ok, %{"id" => "cus_asdfghjkl"} =
    Stripe.Customer.create(email: "[email protected]")

Note that either KeywordLists or Maps with either String or Atom keys are acceptable for arguments and options. So all of the following would also work:

Stripe.Customer.create(%{email: "[email protected]"})
Stripe.Customer.create(%{"email" => "[email protected]"})
Stripe.Customer.create([{"email", "[email protected]"}])

Retrieve that customer:

{:ok, customer} = Stripe.Customer.retrieve("cus_asdfghjkl")

Update the customer:

{:ok, %{"metadata" => %{"somedata" => "somevalue"}}} =
  Stripe.Customer.update("cus_asdfghjkl", metadata: [somedata: "somevalue"])

Delete the customer:

{:ok, %{"deleted" => true}} = Stripe.Customer.delete("cus_asdfghjkl")

Stripe Connect

To perform a Direct Charge on a connected stripe account, simply pass :stripe_account as an option

Stripe.Charge.create([customer: "cus_asdfghjkl", amount: 400], stripe_account: "acct_sOMeAcCountId")

Generate a Connect authorization url via Stripe.Connect.authorize_url/1.

Stripe.Connect.authorize_url([
  redirect_uri: <OPTIONAL CALLBACK URL>,
  state: <OPTIONAL CSRF TOKEN>,
  client_id: <OPTIONAL STRIPE PLATFORM CLIENT ID>
])

Options:

  • redirect_uri: An optional callback url after authorization succeeds.
  • state: You can protect your request from CSRF attacks by passing a csrf token.
  • client_id: You can pass in an optional client_id to be used for this url. Defaults to STRIPE_CLIENT_ID environment variable or config :stripe, :client_id config value.

Handling Webhooks

Stripe uses webhooks to notify your web app with events. Stripe.Webhook provides construct_event/3 to authenticate the requests, which can be useful in plugs.

payload = # HTTP content body (e.g. from Plug.Conn.read_body/3)
signature = # 'Stripe-Signature' HTTP header (e.g. from Plug.Conn.get_req_header/2)
secret = # Provided by Stripe

case Stripe.Webhook.construct_event(payload, signature, secret) do
  {:ok, event} ->
    # Return 2XX
  {:error, %Stripe.SignatureVerificationError{}} ->
    # Return non-2XX and handle error
end

The default tolerance is 5 minutes (300 seconds as per official libraries). If your app is rejecting requests because the tolerance is too low, consider passing a higher number to construct_event/4.

Stripe.Webhook.construct_event(payload, signature, secret, 600)

stripe-elixir's People

Contributors

bnns avatar drapergeek avatar jayjun avatar jrichocean avatar lowks avatar sikanhe avatar stevedomin avatar sublimecoder avatar tngan 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

stripe-elixir's Issues

Webhook does not accept body as Map

When passing conn.body_params into Stripe.Webhook.construct_event(payload, signature, secret) it fails to parse. When passing in Poison.encode!(conn.body_params) instead, it fails to validate against the webhook key, presumable because of all the escaping slashes

Stubbing in tests

I am trying to write tests against my controllers that have stripe calls, but I am getting

Invalid API Key provided: ******.****_******_KEY

Is there a common way to stub out the calls and return sample responses

Documentation

In terms of documentation what do you think about following the Official Guides where possible, I've largely removed sections I feel are not relevant:

  • Payments
    • Quickstart
    • Checkout
    • Charges
      • Creating Charges
      • Declines & Failed Payments
      • Disputes
      • Refunds
      • Receiving Payouts
      • Supported Currencies
    • Products & Orders
      • Orders
      • Tax Integrations
      • Shipping Integrations
  • Subscriptions
    • Quickstart
    • Creating Subscriptions
  • Connect
    • Overview
      • On Demand Apps
      • Store Builders
    • Connecting to Accounts
    • Standalone Accounts
    • Managed Accounts
    • Creating Charges
    • Debiting Managed Accounts
    • Using Subscriptions
    • Multiple Currencies
    • Account Balances
    • Testing
  • Handling Webhooks

I'm happy to get a PR done stubbing these out and starting on them. Thanks for this library, it looks amazing and seems very complete. Just be good to have examples for the above I think, with links to the original docs.

Request to take over maintainership

Hi @sikanhe.

We (Narrative) have been maintaining our own fork of this stripe library for some time in which we have been making improvements as we've needed them. I notice that there have been no updates to it since December 2017 - so I am wondering if you'd be amenable to transferring maintainership over to us? Personally, I'm keen for the improvements we've made to get back to the community.

Thanks for all the code. We really do appreciate it.

❤️

usage issue

Hey It might sound a bit dumb, but I am trying to learn elixir and migrating an app to phoenix background and I need stripe, and I can't seem to find examples really that where would the payment happen, in the controller?
I get the data for the payment in my params and I think I need to cast them, and I have my schema but I how do i get back the casted value to make the call to create a customer?

Many thanks if you read this :D

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.