项目作者: princemaple

项目描述 :
Elixir Stripe API wrapper
高级语言: Elixir
项目地址: git://github.com/princemaple/extripe.git
创建时间: 2016-02-16T06:06:19Z
项目社区:https://github.com/princemaple/extripe

开源协议:MIT License

下载


Extripe

Hex.pm
Documentation

Stripe API wrapper in Elixir Read Documentation

Installation

Add extripe to your list of dependencies in mix.exs:

  1. def deps do
  2. [{:extripe, "~> 1.0"}]
  3. end

Usage

  • Use with env var stripe_secret_key

    1. stripe_secret_key=sk_test_abcdefg12345678 iex -S mix
  • Or config it

    1. config :extripe,
    2. stripe_secret_key: "sk_test_abcdefg12345678"

Features

Simple CRUD

Using Plan, Customer and Subscirption in the following examples, all entities should be supported, if you find anything that is not supported or new endpoints coming out from stripe, don’t hesitate to file an issue

  1. iex(1)> Extripe.Plan.index
  2. {:ok,
  3. %{"data" => [%{"amount" => 1500, "created" => 1455733031, "currency" => "gbp",
  4. "id" => "regular_gb", "interval" => "month", "interval_count" => 1,
  5. "livemode" => false, "metadata" => %{}, "name" => "Regular GB",
  6. "object" => "plan", "statement_descriptor" => nil,
  7. "trial_period_days" => nil},
  8. %{"amount" => 1000, "created" => 1455632453, "currency" => "aud",
  9. "id" => "regular_au", "interval" => "month", "interval_count" => 1,
  10. "livemode" => false, "metadata" => %{}, "name" => "Regular AU",
  11. "object" => "plan", "statement_descriptor" => nil,
  12. "trial_period_days" => 15},
  13. %{"amount" => 1000, "created" => 1455593353, "currency" => "usd",
  14. "id" => "regular", "interval" => "month", "interval_count" => 1,
  15. "livemode" => false, "metadata" => %{}, "name" => "REGULAR",
  16. "object" => "plan", "statement_descriptor" => nil,
  17. "trial_period_days" => 15}], "has_more" => false, "object" => "list",
  18. "url" => "/v1/plans"}}
  19. iex(2)> Extripe.Plan.show "regular"
  20. {:ok,
  21. %{"amount" => 1000, "created" => 1455593353, "currency" => "usd",
  22. "id" => "regular", "interval" => "month", "interval_count" => 1,
  23. "livemode" => false, "metadata" => %{}, "name" => "REGULAR",
  24. "object" => "plan", "statement_descriptor" => nil,
  25. "trial_period_days" => 15}}
  26. iex(3)> Extripe.Plan.create %{id: "test", name: "My Test Plan", amount: 1999, interval: "month", currency: "usd"}
  27. {:ok,
  28. %{"amount" => 1999, "created" => 1455736111,
  29. "currency" => "usd", "id" => "test", "interval" => "month",
  30. "interval_count" => 1, "livemode" => false, "metadata" => %{},
  31. "name" => "My Test Plan", "object" => "plan", "statement_descriptor" => nil,
  32. "trial_period_days" => nil}}
  33. # or Extripe.Plan.update "test_id", %{name: "MY TTTTEST PLAN"}
  34. iex(4)> Extripe.Plan.update %{id: "test_id", name: "MY TTTTEST PLAN"}
  35. {:ok,
  36. %{"amount" => 1999, "created" => 1455736111,
  37. "currency" => "usd", "id" => "test", "interval" => "month",
  38. "interval_count" => 1, "livemode" => false, "metadata" => %{},
  39. "name" => "MY TTTTEST PLAN", "object" => "plan",
  40. "statement_descriptor" => nil, "trial_period_days" => nil}}
  41. iex(5)> Extripe.Plan.delete "test"
  42. {:ok, %{"deleted" => true, "id" => "test"}}

Response

All actions return either {:ok, body :: map} or {:error, reason :: binary}

Nested resources

  1. # find a customer first
  2. iex(1)> Extripe.Customer.index
  3. {:ok,
  4. %{"data" => [%{"account_balance" => 0, "created" => 1455721994,
  5. "currency" => "usd", "default_source" => nil, "delinquent" => false,
  6. "description" => "test self", "discount" => nil,
  7. "email" => "chenpaul914@hotmail.com", "id" => "cus_7vNk0duWVulcPe",
  8. "livemode" => false, "metadata" => %{}, "object" => "customer",
  9. "shipping" => nil,
  10. "sources" => %{"data" => [], "has_more" => false, "object" => "list",
  11. "total_count" => 0,
  12. "url" => "/v1/customers/cus_7vNk0duWVulcPe/sources"},
  13. "subscriptions" => %{"data" => [], "has_more" => false,
  14. "object" => "list", "total_count" => 0,
  15. "url" => "/v1/customers/cus_7vNk0duWVulcPe/subscriptions"}}],
  16. "has_more" => false, "object" => "list", "url" => "/v1/customers"}}
  17. # find the subscriptions that belong to the customer
  18. iex(2)> Extripe.Subscription.index customer: "cus_7vNk0duWVulcPe"
  19. {:ok,
  20. %{"data" => [], "has_more" => false,
  21. "object" => "list",
  22. "url" => "/v1/customers/cus_7vNk0duWVulcPe/subscriptions"}}
  23. # manipulations...
  24. iex(3)> Extripe.Subscription.create customer: "cus_7vNk0duWVulcPe", %{plan: "regular"}
  25. {:ok,
  26. %{"application_fee_percent" => nil,
  27. "cancel_at_period_end" => false, "canceled_at" => nil,
  28. "current_period_end" => 1457032418, "current_period_start" => 1455736418,
  29. "customer" => "cus_7vNk0duWVulcPe", "discount" => nil, "ended_at" => nil,
  30. "id" => "sub_7vRdUiQQhv3M7u", "metadata" => %{}, "object" => "subscription",
  31. "plan" => %{"amount" => 1000, "created" => 1455593353, "currency" => "usd",
  32. "id" => "regular", "interval" => "month", "interval_count" => 1,
  33. "livemode" => false, "metadata" => %{}, "name" => "REGULAR",
  34. "object" => "plan", "statement_descriptor" => nil,
  35. "trial_period_days" => 15}, "quantity" => 1, "start" => 1455736418,
  36. "status" => "trialing", "tax_percent" => nil, "trial_end" => 1457032418,
  37. "trial_start" => 1455736418}}
  38. iex(4)> Extripe.Subscription.delete "sub_7vRdUiQQhv3M7u"
  39. {:ok,
  40. %{"application_fee_percent" => nil,
  41. "cancel_at_period_end" => false, "canceled_at" => 1455736452,
  42. "current_period_end" => 1457032418, "current_period_start" => 1455736418,
  43. "customer" => "cus_7vNk0duWVulcPe", "discount" => nil,
  44. "ended_at" => 1455736452, "id" => "sub_7vRdUiQQhv3M7u", "metadata" => %{},
  45. "object" => "subscription",
  46. "plan" => %{"amount" => 1000, "created" => 1455593353, "currency" => "usd",
  47. "id" => "regular", "interval" => "month", "interval_count" => 1,
  48. "livemode" => false, "metadata" => %{}, "name" => "REGULAR",
  49. "object" => "plan", "statement_descriptor" => nil,
  50. "trial_period_days" => 15}, "quantity" => 1, "start" => 1455736418,
  51. "status" => "canceled", "tax_percent" => nil, "trial_end" => 1457032418,
  52. "trial_start" => 1455736418}}

Pagination

  1. iex(1)> Extripe.Plan.index starting_after: "regular_au"
  2. {:ok,
  3. %{"data" => [%{"amount" => 1000, "created" => 1455593353, "currency" => "usd",
  4. "id" => "regular", "interval" => "month", "interval_count" => 1,
  5. "livemode" => false, "metadata" => %{}, "name" => "REGULAR",
  6. "object" => "plan", "statement_descriptor" => nil,
  7. "trial_period_days" => 15}], "has_more" => false, "object" => "list",
  8. "url" => "/v1/plans"}}
  9. iex(2)> Extripe.Plan.index ending_before: "regular_au"
  10. {:ok,
  11. %{"data" => [%{"amount" => 1500,
  12. "created" => 1455733031, "currency" => "gbp", "id" => "regular_gb",
  13. "interval" => "month", "interval_count" => 1, "livemode" => false,
  14. "metadata" => %{}, "name" => "Regular GB", "object" => "plan",
  15. "statement_descriptor" => nil, "trial_period_days" => nil}],
  16. "has_more" => false, "object" => "list", "url" => "/v1/plans"}}
  17. iex(3)> Extripe.Plan.index limit: 1
  18. {:ok,
  19. %{"data" => [%{"amount" => 1500,
  20. "created" => 1455733031, "currency" => "gbp", "id" => "regular_gb",
  21. "interval" => "month", "interval_count" => 1, "livemode" => false,
  22. "metadata" => %{}, "name" => "Regular GB", "object" => "plan",
  23. "statement_descriptor" => nil, "trial_period_days" => nil}],
  24. "has_more" => true, "object" => "list", "url" => "/v1/plans"}}
  25. iex(4)> {:ok, events} = Extripe.Event.index created: [lt: 1455733031]
  26. {:ok,
  27. %{"data" => [%{"api_version" => "2016-02-03", "created" => 1455732840,
  28. "data" => %{"object" => %{"amount" => 1000, "created" => 1455593353,
  29. "currency" => "usd", "id" => "regular", "interval" => "month",
  30. "interval_count" => 1, "livemode" => false, "metadata" => %{},
  31. "name" => "REGULAR", "object" => "plan",
  32. "statement_descriptor" => nil, "trial_period_days" => 15},
  33. "previous_attributes" => %{"name" => "Regular"}},
  34. "id" => "evt_17fZoKEhB5xMvgQ7Fdl7krpl", "livemode" => false,
  35. "object" => "event", "pending_webhooks" => 0,
  36. "request" => "req_7vQfTWcjfnH7JW", "type" => "plan.updated"},
  37. %{"api_version" => "2016-02-03", "created" => 1455732634,
  38. "data" => %{"object" => %{"amount" => 1500, "created" => 1455730831,
  39. "currency" => "gbp", "id" => "regular_gb", "interval" => "month",
  40. "interval_count" => 1, "livemode" => false, "metadata" => %{},
  41. "name" => "REGULAR GB", "object" => "plan",
  42. "statement_descriptor" => nil, "trial_period_days" => nil}},
  43. "id" => "evt_17fZl0EhB5xMvgQ7Ekqu42UQ", "livemode" => false,
  44. "object" => "event", "pending_webhooks" => 0,
  45. "request" => "req_7vQc2AYrZYzkv3", "type" => "plan.deleted"},
  46. %{"api_version" => "2016-02-03", "created" => 1455732348,
  47. "data" => %{"object" => %{"application_fee_percent" => nil,
  48. "cancel_at_period_end" => false, "canceled_at" => 1455732348,
  49. "current_period_end" => 1457027674,
  50. "current_period_start" => 1455731674,
  51. "customer" => "cus_7vNk0duWVulcPe", "discount" => nil,
  52. "ended_at" => 1455732348, "id" => "sub_7vQMcLg9Qvbuzn",
  53. "metadata" => %{}, "object" => "subscription",
  54. "plan" => %{"amount" => 1000, "created" => 1455593353,
  55. "currency" => "usd", "id" => "regular", "interval" => "month",
  56. "interval_count" => 1, "livemode" => false, "metadata" => %{},
  57. "name" => "Regular", "object" => "plan",
  58. "statement_descriptor" => nil, "trial_period_days" => 15},
  59. "quantity" => 1, "start" => 1455731674, "status" => "canceled",
  60. "tax_percent" => nil, "trial_end" => 1457027674,
  61. "trial_start" => 1455731674}}, "id" => "evt_17fZgOEhB5xMvgQ750S7kXaH",
  62. "livemode" => false, "object" => "event", "pending_webhooks" => 0,
  63. "request" => "req_7vQXJ6d8hVgjA5",
  64. "type" => "customer.subscription.deleted"},
  65. ...]}}
  66. iex(5)> {:ok, events} = Extripe.Event.index created: [gt: 1455733031]
  67. # similar to the previous one
  68. # and you also have gte, lte
  69. # or you could just specify an integer unix timestamp for :created instead of a map or a keyword list

Contributing

  1. # Adding a new resource
  2. defmodule Extripe.NewResource do
  3. # normal resource
  4. # note: new_resource is probably plural
  5. use Extripe.Actions.CRUD, resource: "new_resource"
  6. # not fully CRUDable resource
  7. use Extripe.Actions.CRUD, only: [:index, :show], resource: "new_resource"
  8. # or
  9. use Extripe.Actions.CRUD, except: [:delete], resource: "new_resource"
  10. # nested resource
  11. # when the scoping resource is plural, e.g. /v1/customers/customer_id/subscriptions
  12. use Extripe.Actions.CRUD, scope: "customers", resource: "subscriptions"
  13. # when the scoping resource is singular, e.g. /v1/balance/history
  14. use Extripe.Actions.CRUD, scope: {"balance", :singular}, resource: "history"
  15. # singluar resource
  16. # currently only Balance is of this kind, /v1/balance
  17. use Extripe.Actions.CRUD, only: [:show], resource: "balance", singular: true
  18. # special case implementations
  19. def pay(id), do: # pay bill
  20. # special CRUD implementations
  21. def list do
  22. # special implementation of list
  23. end
  24. end