Skip to content

Commit

Permalink
Capture completed
Browse files Browse the repository at this point in the history
  • Loading branch information
subpal authored Apr 24, 2018
1 parent 6a7bd2e commit b8b1124
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions lib/gringotts/gateways/mercadopago.ex
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ defmodule Gringotts.Gateways.Mercadopago do
"""

# The Base module has the (abstract) public API, and some utility
# implementations.
# implementations.
@base_url "https://api.mercadopago.com"
use Gringotts.Gateways.Base
alias Gringotts.CreditCard
Expand Down Expand Up @@ -139,8 +139,15 @@ defmodule Gringotts.Gateways.Mercadopago do
{_, value, _, _} = Money.to_integer_exp(amount)
url_params = [access_token: opts[:config][:access_token]]

params = [
authorize_params(value, opts, card_token, false, card),
customer_params(card, customer_id, opts)
]

body =
authorize_params(value, card, opts, card_token, customer_id, false) |> Poison.encode!()
params
|> Enum.reduce(&Map.merge/2)
|> Poison.encode!()

commit(:post, "/v1/payments", body, opts, params: url_params)
end
Expand Down Expand Up @@ -190,7 +197,8 @@ defmodule Gringotts.Gateways.Mercadopago do
@spec commit(atom, String.t(), String.t(), keyword, keyword) :: {:ok | :error, Response.t()}
defp commit(method, path, body, opts, url_params) do
headers = [{"content-type", "application/json"}, {"accept", "application/json"}]
HTTPoison.request(method, "#{@base_url}#{path}", body, headers, url_params) |> respond(opts)
res = HTTPoison.request(method, "#{@base_url}#{path}", body, headers, url_params)
respond(res, opts)
end

# Parses mercadopago's response and returns a `Gringotts.Response` struct
Expand Down Expand Up @@ -224,7 +232,8 @@ defmodule Gringotts.Gateways.Mercadopago do

defp create_token(%CreditCard{} = card, opts) do
url_params = [public_key: opts[:config][:public_key]]
body = token_params(card) |> Poison.encode!()
body = token_params(card)
body = body |> Poison.encode!()

{state, res} =
commit(:post, "/v1/card_tokens/#{opts[:customer_id]}", body, opts, params: url_params)
Expand All @@ -235,7 +244,17 @@ defmodule Gringotts.Gateways.Mercadopago do
end
end

defp authorize_params(value, %CreditCard{} = card, opts, token_id, customer_id, capture) do
defp authorize_params(value, opts, token_id, capture, %CreditCard{} = card) do
%{
installments: opts[:installments] || 1,
transaction_amount: value,
payment_method_id: String.downcase(card.brand),
token: token_id,
capture: capture
}
end

defp customer_params(%CreditCard{} = card, customer_id, opts) do
%{
payer: %{
type: "customer",
Expand All @@ -244,14 +263,9 @@ defmodule Gringotts.Gateways.Mercadopago do
last_name: card.last_name
},
order: %{
type: "mercadopago",
type: opts[:order_type],
id: opts[:order_id]
},
installments: opts[:installments] || 1,
transaction_amount: value,
payment_method_id: String.downcase(card.brand),
token: token_id,
capture: capture
}
}
end

Expand Down

0 comments on commit b8b1124

Please sign in to comment.