@@ -134,13 +134,15 @@ defmodule Gringotts.Gateways.Mercadopago do
134
134
135
135
@ spec authorize ( Money . t ( ) , CreditCard . t ( ) , keyword ) :: { :ok | :error , Response }
136
136
def authorize ( amount , % CreditCard { } = card , opts ) do
137
- if Keyword . has_key? ( opts , :customer_id ) do
138
- auth_token ( amount , card , opts , opts [ :customer_id ] , false )
139
- else
140
- case create_customer ( opts ) do
141
- { :error , res } -> { :error , res }
142
- { :ok , customer_id } -> auth_token ( amount , card , opts , customer_id , false )
143
- end
137
+ with { :ok , customer_id } <- create_customer ( opts ) ,
138
+ { :ok , card_token } <- create_token ( card , opts ) do
139
+ { _ , value , _ , _ } = Money . to_integer_exp ( amount )
140
+ url_params = [ access_token: opts [ :config ] [ :access_token ] ]
141
+
142
+ body =
143
+ authorize_params ( value , card , opts , card_token , customer_id , false ) |> Poison . encode! ( )
144
+
145
+ commit ( :post , "/v1/payments" , body , opts , params: url_params )
144
146
end
145
147
end
146
148
@@ -161,39 +163,19 @@ defmodule Gringotts.Gateways.Mercadopago do
161
163
# Parses mercadopago's response and returns a `Gringotts.Response` struct
162
164
# in a `:ok`, `:error` tuple.
163
165
164
- defp auth_token ( amount , % CreditCard { } = card , opts , customer_id , capture ) do
165
- opts = opts ++ [ customer_id: customer_id ]
166
- { state , res } = create_token ( card , opts )
167
-
168
- if state == :error do
169
- { state , res }
170
- else
171
- auth ( amount , card , opts , res , capture )
172
- end
173
- end
174
-
175
- defp auth ( amount , % CreditCard { } = card , opts , token_id , capture ) do
176
- { _ , value , _ , _ } = Money . to_integer_exp ( amount )
177
- opts = opts ++ [ token_id: token_id ]
178
- url_params = [ access_token: opts [ :config ] [ :access_token ] ]
179
-
180
- body =
181
- authorize_params ( value , card , opts , opts [ :token_id ] , opts [ :customer_id ] , capture )
182
- |> Poison . encode! ( )
183
-
184
- commit ( :post , "/v1/payments" , body , opts , params: url_params )
185
- end
186
-
187
166
defp create_customer ( opts ) do
188
- url_params = [ access_token: opts [ :config ] [ :access_token ] ]
189
- body = % { email: opts [ :email ] } |> Poison . encode! ( )
190
-
191
- { state , res } = commit ( :post , "/v1/customers" , body , opts , params: url_params )
192
-
193
- if state == :error do
194
- { state , res }
167
+ if Keyword . has_key? ( opts , :customer_id ) do
168
+ { :ok , opts [ :customer_id ] }
195
169
else
196
- { state , res . id }
170
+ url_params = [ access_token: opts [ :config ] [ :access_token ] ]
171
+ body = % { email: opts [ :email ] } |> Poison . encode! ( )
172
+ { state , res } = commit ( :post , "/v1/customers" , body , opts , params: url_params )
173
+
174
+ if state == :error do
175
+ { state , res }
176
+ else
177
+ { state , res . id }
178
+ end
197
179
end
198
180
end
199
181
@@ -229,7 +211,7 @@ defmodule Gringotts.Gateways.Mercadopago do
229
211
last_name: card . last_name
230
212
} ,
231
213
order: % {
232
- type: "mercadopago" ,
214
+ type: opts [ :order_type ] ,
233
215
id: opts [ :order_id ]
234
216
} ,
235
217
installments: opts [ :installments ] || 1 ,
0 commit comments