Skip to content

Commit

Permalink
Connect enabled Charges module. Refactored, beefed up,cleaned up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nicrioux committed Dec 19, 2015
1 parent d70a116 commit f6c13c0
Show file tree
Hide file tree
Showing 3 changed files with 221 additions and 21 deletions.
27 changes: 27 additions & 0 deletions lib/stripe.ex
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,24 @@ defmodule Stripe do
make_request_with_key( method, endpoint, config_or_env_key, body, headers, options )
end

def make_oauth_token_callback_request(body) do
IO.puts "=====================oauthB"
rb = Stripe.URI.encode_query(body)
rh = req_headers( "sk_test_ZQ1ofROPQQjS23vI8qQhKwi0" )
|> Dict.to_list
options = []
HTTPoison.request(:post, "https://connect.stripe.com/oauth/token", rb, rh, options)
end

def make_oauth_authorize_request(body) do
IO.puts "=====================authorizeA"
rb = Stripe.URI.encode_query(body)
rh = req_headers( Stripe.config_or_env_key )
|> Dict.to_list
options = []
HTTPoison.request(:post, "https://connect.stripe.com/oauth/authorize", rb, rh, options)
end

@doc """
Grabs STRIPE_SECRET_KEY from system ENV
Returns binary
Expand All @@ -94,4 +112,13 @@ defmodule Stripe do
Application.get_env(:stripity_stripe, :secret_key) ||
System.get_env "STRIPE_SECRET_KEY"
end

@doc """
Grabs STRIPE_PLATFORM_CLIENT_ID from system ENV
Returns binary
"""
def config_or_env_platform_client_id do
Application.get_env(:stripity_stripe, :platform_client_id) ||
System.get_env "STRIPE_PLATFORM_CLIENT_ID"
end
end
157 changes: 143 additions & 14 deletions lib/stripe/charges.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
defmodule Stripe.Charges do
@moduledoc """
Handles charges to the Stripe API.
(API ref: https://stripe.com/docs/api#charges)
Operations:
- create
- update
- get one
- list
- count
- refund
- refund partial
"""

@endpoint "charges"
Expand Down Expand Up @@ -29,13 +39,26 @@ defmodule Stripe.Charges do
```
"""
def create(amount, params) do
create amount, params, Stripe.config_or_env_key
end

@doc """
Creates a charge for a customer or card. You must pass in the amount, and also a source for the charge
that can be a token or customer. See the Stripe docs for proper source specs.
Using a given stripe key to apply against the account associated.
## Examples
{:ok, result} = Stripe.Charges.create 1000,params, key
"""
def create(amount, params, key) do
#default currency
params = Keyword.put_new params, :currency, "USD"
#drop in the amount
params = Keyword.put_new params, :amount, amount

Stripe.make_request(:post, @endpoint, params)
|> Stripe.Util.handle_stripe_response
Stripe.make_request_with_key(:post, @endpoint, key, params)
|> Stripe.Util.handle_stripe_response
end


Expand All @@ -48,8 +71,21 @@ defmodule Stripe.Charges do
```
"""
def list(limit \\ 10) do
Stripe.make_request(:get, "#{@endpoint}?limit=#{limit}")
|> Stripe.Util.handle_stripe_response
list Stripe.config_or_env_key, limit
end

@doc """
Lists out charges from your account with a default limit of 10. You can override this by passing in a limit.
Using a given stripe key to apply against the account associated.
## Examples
```
{:ok, charges} = Stripe.Charges.list(key, 100)
```
"""
def list(key, limit) do
Stripe.make_request_with_key(:get, "#{@endpoint}?limit=#{limit}", key)
|> Stripe.Util.handle_stripe_response
end


Expand All @@ -63,8 +99,22 @@ defmodule Stripe.Charges do
```
"""
def change(id, params) do
Stripe.make_request(:post, "#{@endpoint}/#{id}",params)
|> Stripe.Util.handle_stripe_response
change id, params, Stripe.config_or_env_key
end

@doc """
Updates a charge with changeable information (see the Stripe docs on what you can change)
Using a given stripe key to apply against the account associated.
## Examples
```
params = [description: "Changed charge"]
{:ok, charge} = Stripe.Charges.change("charge_id", params, key)
```
"""
def change(id, params, key) do
Stripe.make_request_with_key(:post, "#{@endpoint}/#{id}", key, params)
|> Stripe.Util.handle_stripe_response
end

@doc """
Expand All @@ -77,8 +127,22 @@ defmodule Stripe.Charges do
```
"""
def capture(id) do
Stripe.make_request(:post, "#{@endpoint}/#{id}/capture")
|> Stripe.Util.handle_stripe_response
capture id, Stripe.config_or_env_key
end

@doc """
Captures a charge that is currently pending. Note: you can default a charge to be automatically captured by setting `capture: true` in the charge create params.
Using a given stripe key to apply against the account associated.
## Example
```
{:ok, charge} = Stripe.Charges.capture("charge_id", key)
```
"""
def capture(id,key) do
Stripe.make_request_with_key(:post, "#{@endpoint}/#{id}/capture", key)
|> Stripe.Util.handle_stripe_response
end


Expand All @@ -92,8 +156,22 @@ defmodule Stripe.Charges do
```
"""
def get(id) do
Stripe.make_request(:get, "#{@endpoint}/#{id}")
|> Stripe.Util.handle_stripe_response
get id, Stripe.config_or_env_key
end

@doc """
Retrieves a given charge.
Using a given stripe key to apply against the account associated.
## Example
```
{:ok, charge} = Stripe.Charges.get("charge_id", key)
```
"""
def get(id, key) do
Stripe.make_request_with_key(:get, "#{@endpoint}/#{id}", key)
|> Stripe.Util.handle_stripe_response
end

@doc """
Expand All @@ -106,8 +184,21 @@ defmodule Stripe.Charges do
```
"""
def refund(id) do
Stripe.make_request(:post, "#{@endpoint}/#{id}/refunds")
|> Stripe.Util.handle_stripe_response
refund id, Stripe.config_or_env_key
end

@doc """
Refunds a charge completely. Use `refund_partial` if you just want to... you know... partially refund
Using a given stripe key to apply against the account associated.
## Example
```
{:ok, charge} = Stripe.Charges.refund("charge_id", key)
"""
def refund(id, key) do
Stripe.make_request_with_key(:post, "#{@endpoint}/#{id}/refunds", key)
|> Stripe.Util.handle_stripe_response
end

@doc """
Expand All @@ -120,8 +211,46 @@ defmodule Stripe.Charges do
```
"""
def refund_partial(id, amount) do
refund_partial id, amount, Stripe.config_or_env_key
end

@doc """
Refunds a charge partially; the amount is required.
Using a given stripe key to apply against the account associated.
## Example
```
{:ok, charge} = Stripe.Charges.refund_partial("charge_id",500, key)
```
"""
def refund_partial(id, amount, key) do
params = [amount: amount]
Stripe.make_request(:post, "#{@endpoint}/#{id}/refunds", params)
|> Stripe.Util.handle_stripe_response
Stripe.make_request_with_key(:post, "#{@endpoint}/#{id}/refunds", key, params)
|> Stripe.Util.handle_stripe_response
end

@doc """
Count number of charges.
## Example
```
{:ok, cnt} = Stripe.Charges.count
```
"""
def count do
count Stripe.config_or_env_key
end

@doc """
Count number of charges.
Using a given stripe key to apply against the account associated.
## Example
```
{:ok, cnt} = Stripe.Charges.count key
```
"""
def count(key) do
Stripe.Util.count "#{@endpoint}", key
end
end
58 changes: 51 additions & 7 deletions test/stripe/charge_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,49 @@ defmodule Stripe.ChargeTest do
{:ok, [params: params]}
end

test "A valid charge is successful with card as source", %{params: params} do

test "Create with card works", %{params: params} do
case Stripe.Charges.create(1000,params) do
{:ok, res} -> assert res.id
{:error, err} -> flunk err
end
end

test "Listing returns charges" do
test "Create with card, w/key works", %{params: params} do
case Stripe.Charges.create(1000,params, Stripe.config_or_env_key) do
{:ok, res} -> assert res.id
{:error, err} -> flunk err
end
end
test "List works" do
case Stripe.Charges.list() do
{:ok, charges} -> assert length(charges) > 0
{:error, err} -> flunk err
end
end

test "Getting a charge" do
test "List w/key works" do
case Stripe.Charges.list Stripe.config_or_env_key, 1 do
{:ok, charges} -> assert length(charges) > 0
{:error, err} -> flunk err
end
end
test "Get works" do
{:ok,[first | _]} = Stripe.Charges.list()
case Stripe.Charges.get(first.id) do
{:ok, charge} -> assert charge.id == first.id
{:error, err} -> flunk err
end
end

test "Capturing a charge", %{params: params} do
test "Get w/key works" do
{:ok,[first | _]} = Stripe.Charges.list Stripe.config_or_env_key, 1
case Stripe.Charges.get(first.id, Stripe.config_or_env_key) do
{:ok, charge} -> assert charge.id == first.id
{:error, err} -> flunk err
end
end

test "Capture works", %{params: params} do
params = Keyword.put_new params, :capture, false
{:ok, charge} = Stripe.Charges.create(1000,params)
case Stripe.Charges.capture(charge.id) do
Expand All @@ -49,20 +68,45 @@ defmodule Stripe.ChargeTest do
end
end

test "Changing a charge", %{params: params} do
test "Capture w/key works", %{params: params} do
params = Keyword.put_new params, :capture, false
{:ok, charge} = Stripe.Charges.create(1000,params, Stripe.config_or_env_key)
case Stripe.Charges.capture(charge.id, Stripe.config_or_env_key) do
{:ok, captured} -> assert captured.id == charge.id
{:error, err} -> flunk err
end
end
test "Change(Update) works", %{params: params} do
{:ok, charge} = Stripe.Charges.create(1000,params)
params = [description: "Changed charge"]
case Stripe.Charges.change(charge.id, params) do
{:ok, changed} -> assert changed.description == "Changed charge"
{:error, err} -> flunk err
end
end

test "Change(Update) w/key works", %{params: params} do
{:ok, charge} = Stripe.Charges.create(1000,params, Stripe.config_or_env_key)
params = [description: "Changed charge"]
case Stripe.Charges.change(charge.id, params, Stripe.config_or_env_key) do
{:ok, changed} -> assert changed.description == "Changed charge"
{:error, err} -> flunk err
end
end

test "Refunding a charge", %{params: params} do
test "Refund works", %{params: params} do
{:ok, charge} = Stripe.Charges.create(1000,params)
case Stripe.Charges.refund_partial(charge.id,500) do
{:ok, refunded} -> assert refunded.amount == 500
{:error, err} -> flunk err
end
end

test "Refund w/key works", %{params: params} do
{:ok, charge} = Stripe.Charges.create(1000,params, Stripe.config_or_env_key)
case Stripe.Charges.refund_partial(charge.id,500, Stripe.config_or_env_key) do
{:ok, refunded} -> assert refunded.amount == 500
{:error, err} -> flunk err
end
end
end

0 comments on commit f6c13c0

Please sign in to comment.