Skip to content

Commit 61f09ba

Browse files
committed
Modification in authorize/3, and implemented capture
1. Used commit for HTTPoison requests. 2. Handled network and arguement errors. 3. Updated docs
1 parent 4a2f032 commit 61f09ba

File tree

1 file changed

+0
-94
lines changed

1 file changed

+0
-94
lines changed

lib/gringotts/gateways/mercadopago.ex

-94
Original file line numberDiff line numberDiff line change
@@ -177,101 +177,7 @@ defmodule Gringotts.Gateways.Mercadopago do
177177
commit(:put, url, body, headers) |> respond(opts)
178178
end
179179

180-
@doc """
181-
Transfers `amount` from the customer to the merchant.
182-
183-
mercadopago attempts to process a purchase on behalf of the customer, by
184-
debiting `amount` from the customer's account by charging the customer's
185-
`card`.
186-
187-
## Example
188-
189-
The following example shows how one would process a payment worth 42 BRL in
190-
one-shot, without (pre) authorization.
191-
192-
iex> amount = Money.new(42, :BRL)
193-
iex> card = %Gringotts.CreditCard{first_name: "Harry", last_name: "Potter", number: "4200000000000000", year: 2099, month: 12, verification_code: "123", brand: "VISA"}
194-
iex> {:ok, purchase_result} = Gringotts.purchase(Gringotts.Gateways.Mercadopago, amount, card, opts)
195-
iex> purchase_result.token # This is the customer ID/token
196-
197-
"""
198-
@spec purchase(Money.t, CreditCard.t(), keyword) :: {:ok | :error, Response}
199-
def purchase(amount, %CreditCard{} = card, opts) do
200-
if Keyword.has_key?(opts, :customer_id) do
201-
auth_token(amount, card, opts, opts[:customer_id], true)
202-
else
203-
{state, res} = get_customer_id(opts)
204-
if state == :error do
205-
{state, res}
206-
else
207-
auth_token(amount, card, opts, res, true)
208-
end
209-
end
210-
end
211-
212-
@doc """
213-
Voids the referenced payment.
214-
215-
This method attempts a reversal of a previous transaction referenced by
216-
`payment_id`.
217-
218-
> As a consequence, the customer will never see any booking on his statement.
219-
220-
## Note
221-
222-
> Only pending or in_process payments can be cancelled.
223-
224-
> Cancelled coupon payments, deposits and/or transfers will be deposited in the buyer’s Mercadopago account.
225-
226-
## Example
227-
228-
The following example shows how one would void a previous (pre)
229-
authorization. Remember that our `capture/3` example only did a partial
230-
capture.
231-
232-
iex> {:ok, void_result} = Gringotts.void(Gringotts.Gateways.Mercadopago, auth_result.id, opts)
233180

234-
"""
235-
@spec void(String.t(), keyword) :: {:ok | :error, Response}
236-
def void(payment_id, opts) do
237-
url = "#{@base_url}/v1/payments/#{payment_id}?access_token=#{opts[:config][:access_token]}"
238-
headers = [{"content-type", "application/json"}]
239-
body = %{"status": "cancelled"} |> Poison.encode!
240-
commit(:put, url, body, headers) |> respond(opts)
241-
end
242-
243-
@doc """
244-
Refunds the `amount` to the customer's account with reference to a prior transfer.
245-
246-
mercadopago processes a full or partial refund worth `amount`, referencing a
247-
previous `purchase/3` or `capture/3`.
248-
249-
## Note
250-
251-
> You must have enough available money in your account so you can refund the payment amount successfully. Otherwise, you'll get a 400 Bad Request error.
252-
253-
> You can refund a payment within 90 days after it was accredited.
254-
255-
> You can only refund approved payments.
256-
257-
> You can perform up to 20 partial refunds in one payment.
258-
259-
## Example
260-
261-
The following example shows how one would (completely) refund a previous
262-
purchase (and similarily for captures).
263-
264-
iex> amount = Money.new(35, :BRL)
265-
iex> {:ok, refund_result} = Gringotts.refund(Gringotts.Gateways.Mercadopago, purchase_result.id, amount)
266-
"""
267-
@spec refund(Money.t, String.t(), keyword) :: {:ok | :error, Response}
268-
def refund(payment_id, amount, opts) do
269-
{_, value, _, _} = Money.to_integer_exp(amount)
270-
url = "#{@base_url}/v1/payments/#{payment_id}/refunds?access_token=#{opts[:config][:access_token]}"
271-
body = %{"amount": value} |> Poison.encode!
272-
headers = [{"content-type", "application/json"}]
273-
commit(:post, url, body, headers) |> respond(opts)
274-
end
275181

276182

277183
###############################################################################

0 commit comments

Comments
 (0)