Skip to content

Commit

Permalink
Modification in authorize/3, and implemented capture
Browse files Browse the repository at this point in the history
1. Used commit for HTTPoison requests.
2. Handled network and arguement errors.
3. Updated docs
  • Loading branch information
subpal committed Mar 26, 2018
1 parent 4a2f032 commit 61f09ba
Showing 1 changed file with 0 additions and 94 deletions.
94 changes: 0 additions & 94 deletions lib/gringotts/gateways/mercadopago.ex
Original file line number Diff line number Diff line change
Expand Up @@ -177,101 +177,7 @@ defmodule Gringotts.Gateways.Mercadopago do
commit(:put, url, body, headers) |> respond(opts)
end

@doc """
Transfers `amount` from the customer to the merchant.
mercadopago attempts to process a purchase on behalf of the customer, by
debiting `amount` from the customer's account by charging the customer's
`card`.
## Example
The following example shows how one would process a payment worth 42 BRL in
one-shot, without (pre) authorization.
iex> amount = Money.new(42, :BRL)
iex> card = %Gringotts.CreditCard{first_name: "Harry", last_name: "Potter", number: "4200000000000000", year: 2099, month: 12, verification_code: "123", brand: "VISA"}
iex> {:ok, purchase_result} = Gringotts.purchase(Gringotts.Gateways.Mercadopago, amount, card, opts)
iex> purchase_result.token # This is the customer ID/token
"""
@spec purchase(Money.t, CreditCard.t(), keyword) :: {:ok | :error, Response}
def purchase(amount, %CreditCard{} = card, opts) do
if Keyword.has_key?(opts, :customer_id) do
auth_token(amount, card, opts, opts[:customer_id], true)
else
{state, res} = get_customer_id(opts)
if state == :error do
{state, res}
else
auth_token(amount, card, opts, res, true)
end
end
end

@doc """
Voids the referenced payment.
This method attempts a reversal of a previous transaction referenced by
`payment_id`.
> As a consequence, the customer will never see any booking on his statement.
## Note
> Only pending or in_process payments can be cancelled.
> Cancelled coupon payments, deposits and/or transfers will be deposited in the buyer’s Mercadopago account.
## Example
The following example shows how one would void a previous (pre)
authorization. Remember that our `capture/3` example only did a partial
capture.
iex> {:ok, void_result} = Gringotts.void(Gringotts.Gateways.Mercadopago, auth_result.id, opts)

"""
@spec void(String.t(), keyword) :: {:ok | :error, Response}
def void(payment_id, opts) do
url = "#{@base_url}/v1/payments/#{payment_id}?access_token=#{opts[:config][:access_token]}"
headers = [{"content-type", "application/json"}]
body = %{"status": "cancelled"} |> Poison.encode!
commit(:put, url, body, headers) |> respond(opts)
end

@doc """
Refunds the `amount` to the customer's account with reference to a prior transfer.
mercadopago processes a full or partial refund worth `amount`, referencing a
previous `purchase/3` or `capture/3`.
## Note
> 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.
> You can refund a payment within 90 days after it was accredited.
> You can only refund approved payments.
> You can perform up to 20 partial refunds in one payment.
## Example
The following example shows how one would (completely) refund a previous
purchase (and similarily for captures).
iex> amount = Money.new(35, :BRL)
iex> {:ok, refund_result} = Gringotts.refund(Gringotts.Gateways.Mercadopago, purchase_result.id, amount)
"""
@spec refund(Money.t, String.t(), keyword) :: {:ok | :error, Response}
def refund(payment_id, amount, opts) do
{_, value, _, _} = Money.to_integer_exp(amount)
url = "#{@base_url}/v1/payments/#{payment_id}/refunds?access_token=#{opts[:config][:access_token]}"
body = %{"amount": value} |> Poison.encode!
headers = [{"content-type", "application/json"}]
commit(:post, url, body, headers) |> respond(opts)
end


###############################################################################
Expand Down

0 comments on commit 61f09ba

Please sign in to comment.