@@ -25,10 +25,11 @@ defmodule Gringotts.Gateways.Mercadopago do
25
25
26
26
| Key | Remark |
27
27
| ---- | --- |
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 |
32
33
33
34
## Registering your mercadopago account at `Gringotts`
34
35
@@ -81,7 +82,7 @@ defmodule Gringotts.Gateways.Mercadopago do
81
82
"""
82
83
83
84
# The Base module has the (abstract) public API, and some utility
84
- # implementations.
85
+ # implementations.
85
86
@ base_url "https://api.mercadopago.com"
86
87
use Gringotts.Gateways.Base
87
88
alias Gringotts.CreditCard
@@ -90,8 +91,6 @@ defmodule Gringotts.Gateways.Mercadopago do
90
91
# `required_config` list
91
92
use Gringotts.Adapter , required_config: [ :public_key , :access_token ]
92
93
93
- import Poison , only: [ decode: 1 ]
94
-
95
94
alias Gringotts . { CreditCard , Response }
96
95
97
96
@ doc """
@@ -100,7 +99,7 @@ defmodule Gringotts.Gateways.Mercadopago do
100
99
The authorization validates the `card` details with the banking network,
101
100
places a hold on the transaction `amount` in the customer’s issuing bank.
102
101
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) :
104
103
105
104
* `capture/3` _an_ amount.
106
105
* `void/2` a pre-authorization.
@@ -125,7 +124,7 @@ defmodule Gringotts.Gateways.Mercadopago do
125
124
Mention `customer_id`.
126
125
iex> amount = Money.new(42, :BRL)
127
126
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"]
129
128
iex> {:ok, auth_result} = Gringotts.authorize(Gringotts.Gateways.Mercadopago, amount, card, opts)
130
129
iex> auth_result.id # This is the authorization ID
131
130
iex> auth_result.token # This is the customer ID/token
@@ -139,8 +138,15 @@ defmodule Gringotts.Gateways.Mercadopago do
139
138
{ _ , value , _ , _ } = Money . to_integer_exp ( amount )
140
139
url_params = [ access_token: opts [ :config ] [ :access_token ] ]
141
140
141
+ params = [
142
+ authorize_params ( value , opts , card_token , false , card ) ,
143
+ customer_params ( card , customer_id , opts )
144
+ ]
145
+
142
146
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! ( )
144
150
145
151
commit ( :post , "/v1/payments" , body , opts , params: url_params )
146
152
end
@@ -157,7 +163,8 @@ defmodule Gringotts.Gateways.Mercadopago do
157
163
@ spec commit ( atom , String . t ( ) , String . t ( ) , keyword , keyword ) :: { :ok | :error , Response . t ( ) }
158
164
defp commit ( method , path , body , opts , url_params ) do
159
165
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 )
161
168
end
162
169
163
170
# Parses mercadopago's response and returns a `Gringotts.Response` struct
@@ -191,7 +198,7 @@ defmodule Gringotts.Gateways.Mercadopago do
191
198
192
199
defp create_token ( % CreditCard { } = card , opts ) do
193
200
url_params = [ public_key: opts [ :config ] [ :public_key ] ]
194
- body = token_params ( card ) |> Poison . encode! ( )
201
+ body = Poison . encode! ( token_params ( card ) )
195
202
196
203
{ state , res } =
197
204
commit ( :post , "/v1/card_tokens/#{ opts [ :customer_id ] } " , body , opts , params: url_params )
@@ -202,7 +209,17 @@ defmodule Gringotts.Gateways.Mercadopago do
202
209
end
203
210
end
204
211
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
206
223
% {
207
224
payer: % {
208
225
type: "customer" ,
@@ -213,12 +230,7 @@ defmodule Gringotts.Gateways.Mercadopago do
213
230
order: % {
214
231
type: opts [ :order_type ] ,
215
232
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
+ }
222
234
}
223
235
end
224
236
0 commit comments