Skip to content

Commit 9f9d3d7

Browse files
committed
1. Added docs for order_type and installment field of opts.
2. Added docs for Response.token. 3. Removed unused import. 4. Masked customer id in example. 5. Reduced the arity of authorize_params
1 parent 1fa2019 commit 9f9d3d7

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

lib/gringotts/gateways/mercadopago.ex

+31-19
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ defmodule Gringotts.Gateways.Mercadopago do
2525
2626
| Key | Remark |
2727
| ---- | --- |
28-
| `email` | Email of the customer. Type - string . |
29-
| `order_id` | Order id issued by the merchant. Type- integer. |
30-
| `payment_method_id` | Payment network operators, eg. `visa`, `mastercard`. Type- string. |
31-
| `customer_id` | Unique customer id issued by the gateway. For new customer it must be nil. Type- string |
28+
| `email` | Email of the customer. Type - string |
29+
| `order_id` | Order id issued by the merchant. Type- integer |
30+
| `customer_id` | Unique customer id issued by the gateway. For new customer it must skipped. Type- string|
31+
| `order_type` | `"mercadopago"` or `"mercadolibre"` as per the order. Type- string |
32+
| `installments` | No of installments for payment. Type- integer |
3233
3334
## Registering your mercadopago account at `Gringotts`
3435
@@ -81,7 +82,7 @@ defmodule Gringotts.Gateways.Mercadopago do
8182
"""
8283

8384
# The Base module has the (abstract) public API, and some utility
84-
# implementations.
85+
# implementations.
8586
@base_url "https://api.mercadopago.com"
8687
use Gringotts.Gateways.Base
8788
alias Gringotts.CreditCard
@@ -90,8 +91,6 @@ defmodule Gringotts.Gateways.Mercadopago do
9091
# `required_config` list
9192
use Gringotts.Adapter, required_config: [:public_key, :access_token]
9293

93-
import Poison, only: [decode: 1]
94-
9594
alias Gringotts.{CreditCard, Response}
9695

9796
@doc """
@@ -100,7 +99,7 @@ defmodule Gringotts.Gateways.Mercadopago do
10099
The authorization validates the `card` details with the banking network,
101100
places a hold on the transaction `amount` in the customer’s issuing bank.
102101
103-
mercadoapgo's `authorize` returns authorization ID(available in the `Response.id` field) :
102+
mercadoapgo's `authorize` returns customer id(available in `Response.token`) and authorization id(available in the `Response.id` field) :
104103
105104
* `capture/3` _an_ amount.
106105
* `void/2` a pre-authorization.
@@ -125,7 +124,7 @@ defmodule Gringotts.Gateways.Mercadopago do
125124
Mention `customer_id`.
126125
iex> amount = Money.new(42, :BRL)
127126
iex> card = %Gringotts.CreditCard{first_name: "Hermione", last_name: "Granger", number: "4509953566233704", year: 2099, month: 12, verification_code: "123", brand: "VISA"}
128-
iex> opts = [email: "[email protected]", order_id: 123125, customer_id: "308537342-HStv9cJCgK0dWU", payment_method_id: "visa"]
127+
iex> opts = [email: "[email protected]", order_id: 123125, customer_id: "hermoine's customer id", payment_method_id: "visa"]
129128
iex> {:ok, auth_result} = Gringotts.authorize(Gringotts.Gateways.Mercadopago, amount, card, opts)
130129
iex> auth_result.id # This is the authorization ID
131130
iex> auth_result.token # This is the customer ID/token
@@ -139,8 +138,15 @@ defmodule Gringotts.Gateways.Mercadopago do
139138
{_, value, _, _} = Money.to_integer_exp(amount)
140139
url_params = [access_token: opts[:config][:access_token]]
141140

141+
params = [
142+
authorize_params(value, opts, card_token, false, card),
143+
customer_params(card, customer_id, opts)
144+
]
145+
142146
body =
143-
authorize_params(value, card, opts, card_token, customer_id, false) |> Poison.encode!()
147+
params
148+
|> Enum.reduce(&Map.merge/2)
149+
|> Poison.encode!()
144150

145151
commit(:post, "/v1/payments", body, opts, params: url_params)
146152
end
@@ -157,7 +163,8 @@ defmodule Gringotts.Gateways.Mercadopago do
157163
@spec commit(atom, String.t(), String.t(), keyword, keyword) :: {:ok | :error, Response.t()}
158164
defp commit(method, path, body, opts, url_params) do
159165
headers = [{"content-type", "application/json"}, {"accept", "application/json"}]
160-
HTTPoison.request(method, "#{@base_url}#{path}", body, headers, url_params) |> respond(opts)
166+
res = HTTPoison.request(method, "#{@base_url}#{path}", body, headers, url_params)
167+
respond(res, opts)
161168
end
162169

163170
# Parses mercadopago's response and returns a `Gringotts.Response` struct
@@ -191,7 +198,7 @@ defmodule Gringotts.Gateways.Mercadopago do
191198

192199
defp create_token(%CreditCard{} = card, opts) do
193200
url_params = [public_key: opts[:config][:public_key]]
194-
body = token_params(card) |> Poison.encode!()
201+
body = Poison.encode!(token_params(card))
195202

196203
{state, res} =
197204
commit(:post, "/v1/card_tokens/#{opts[:customer_id]}", body, opts, params: url_params)
@@ -202,7 +209,17 @@ defmodule Gringotts.Gateways.Mercadopago do
202209
end
203210
end
204211

205-
defp authorize_params(value, %CreditCard{} = card, opts, token_id, customer_id, capture) do
212+
defp authorize_params(value, opts, token_id, capture, %CreditCard{} = card) do
213+
%{
214+
installments: opts[:installments] || 1,
215+
transaction_amount: value,
216+
payment_method_id: String.downcase(card.brand),
217+
token: token_id,
218+
capture: capture
219+
}
220+
end
221+
222+
defp customer_params(%CreditCard{} = card, customer_id, opts) do
206223
%{
207224
payer: %{
208225
type: "customer",
@@ -213,12 +230,7 @@ defmodule Gringotts.Gateways.Mercadopago do
213230
order: %{
214231
type: opts[:order_type],
215232
id: opts[:order_id]
216-
},
217-
installments: opts[:installments] || 1,
218-
transaction_amount: value,
219-
payment_method_id: String.downcase(card.brand),
220-
token: token_id,
221-
capture: capture
233+
}
222234
}
223235
end
224236

0 commit comments

Comments
 (0)