Skip to content

Commit

Permalink
Refactored the authorised purchase method for transaction id
Browse files Browse the repository at this point in the history
  • Loading branch information
gopalshimpi committed Dec 20, 2017
1 parent 035915b commit 2447223
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
51 changes: 27 additions & 24 deletions lib/gringotts/gateways/cams.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ defmodule Gringotts.Gateways.Cams do
Here `money` is required field which contains amount to be deducted.
Required fields in credit card are `Credit Card Number` & `Expiration Date`.
Whereas `options` contains other information like billing address,order information etc.
After successful transaction it returns response containing **authcode** & **transactionid**.
After successful transaction it returns response containing **transactionid**.
## Examples
payment = %{
Expand All @@ -58,16 +58,19 @@ defmodule Gringotts.Gateways.Cams do
|> add_invoice(money, options)
|> add_payment(payment)
|> add_address(payment, options)
commit("sale", post, options)

response = commit("sale", post, options)
{:ok, response} = response
extract_auth([], response)
end

@doc """
Use this method for authorizing the credit card for particular transaction.
`authorize/3` method only authorize the transaction,it does not transfer the funds.
After authorized a transaction, we need to call `capture/3` method to complete the transaction.
After successful authorization it returns response containing **authcode** and **transactionid**.
We required **authcode**,**transactionid** and **money** for capturing transaction later on.
After successful authorization it returns response containing **transactionid**.
We required **transactionid** and **money** for capturing transaction later on.
It perform operation by taking `money`, `payment` (credit card details) & `options` as parameters.
Here `money` is required field which contains amount to be deducted.
Required fields in credit card are `Credit Card Number` & `Expiration Date`.
Expand All @@ -91,31 +94,33 @@ defmodule Gringotts.Gateways.Cams do
|> add_invoice(money, options)
|> add_payment(payment)
|> add_address(payment, options)
commit("auth", post, options)

response = commit("auth", post, options)
{:ok, response} = response
extract_auth([], response)
end

@doc """
Use this method for capture the amount of the authorized transaction which is previously authorized by `authorize/3` method.
It takes `money`, `authorization` and `options` as parameters.
Where `money` is a amount to be captured and `authorization` is a response returned by `authorize/3` method.
From response it takes `authcode` and `transactionid` for further processing.
Both *money* and *authorization* are required fields, whereas `options` are as same as `authorize/3` and `purchase/3` methods.
From response it takes `transactionid` for further processing.
Both `money` and `authorization` are required fields, whereas `options` are as same as `authorize/3` and `purchase/3` methods.
## Examples
authorization = "response=1&responsetext=SUCCESS&authcode=123456&transactionid=3904093075&avsresponse=N&cvvresponse=N&orderid=&type=sale&response_code=100"
authorization = "3904093075"
options = [currency: "USD"]
money = 100
iex> Gringotts.capture(:payment_worker, Gringotts.Gateways.Cams, money, authorization, options)
"""
@spec capture(number, String.t, Keyword) :: Response
def capture(money, authorization, options) do
post = []
|> extract_auth(authorization)
|> add_invoice(money,options)
post = [transactionid: authorization]
add_invoice(post, money,options)
commit("capture", post, options)
end

Expand All @@ -127,22 +132,21 @@ defmodule Gringotts.Gateways.Cams do
Only purchased(sale) transactions can be refund based on thier `transactionid`.
It takes `money`, `authorization` and `options` as parameters.
Where `money` is a amount to be refund and `authorization` is a response returned by `purchase/3` method.
From response it takes `authcode` and `transactionid` for further processing.
Both *money* and *authorization* are required fields, whereas `options` are as same as `authorize/3`, `purchase/3` and `capture/3` methods.
From response it takes `transactionid` for further processing.
Both `money` and `authorization` are required fields, whereas `options` are as same as `authorize/3`, `purchase/3` and `capture/3` methods.
## Examples
authorization = "response=1&responsetext=SUCCESS&authcode=123456&transactionid=3904093075&avsresponse=N&cvvresponse=N&orderid=&type=sale&response_code=100"
authorization = "3904093078"
options = [currency: "USD"]
money = 100
iex> Gringotts.refund(:payment_worker, Gringotts.Gateways.Cams, money, authorization, options)
"""
@spec refund(number, String.t, Keyword) :: Response
def refund(money, authorization, options) do
post = []
|> extract_auth(authorization)
|> add_invoice(money, options)
post = [transactionid: authorization]
add_invoice(post, money,options)
commit("refund", post, options)
end

Expand All @@ -151,17 +155,17 @@ defmodule Gringotts.Gateways.Cams do
It is use to cancel the purchase(sale) transaction before settlement.
Authorised transaction can be canceled, but once it captured, it can not be canceled.
It requires *transactionid* to cancle transaction.Amount is returned to the authorized payment source.
It requires `transactionid` to cancle transaction.Amount is returned to the authorized payment source.
## Examples
authorization = "response=1&responsetext=SUCCESS&authcode=123456&transactionid=3904093075&avsresponse=N&cvvresponse=N&orderid=&type=sale&response_code=100"
authorization = "3904093075"
options = []
iex> Gringotts.void(:payment_worker, Gringotts.Gateways.Cams, authorization, options)
"""
@spec void(String.t, Keyword) :: Response
def void(authorization , options) do
post = extract_auth([], authorization)
post = [transactionid: authorization]
commit("void", post, options)
end

Expand Down Expand Up @@ -228,9 +232,8 @@ defmodule Gringotts.Gateways.Cams do
{ :error, "Some error has been occurred" }
end

defp extract_auth(post,authorization) do
response_body = URI.decode_query(authorization)
Keyword.put([],:transactionid,String.to_integer(response_body["transactionid"]))
|> Keyword.put(:authcode, String.to_integer(response_body["authcode"]))
defp extract_auth(post, response) do
response_body = URI.decode_query(response)
response_body["transactionid"]
end
end
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"hackney": {:hex, :hackney, "1.10.1", "c38d0ca52ea80254936a32c45bb7eb414e7a96a521b4ce76d00a69753b157f21", [:rebar3], [{:certifi, "2.0.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "5.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm"},
"httpoison": {:hex, :httpoison, "0.13.0", "bfaf44d9f133a6599886720f3937a7699466d23bb0cd7a88b6ba011f53c6f562", [:mix], [{:hackney, "~> 1.8", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm"},
"idna": {:hex, :idna, "5.1.0", "d72b4effeb324ad5da3cab1767cb16b17939004e789d8c0ad5b70f3cea20c89a", [:rebar3], [{:unicode_util_compat, "0.3.1", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm"},
"inch_ex": {:hex, :inch_ex, "0.5.6", "418357418a553baa6d04eccd1b44171936817db61f4c0840112b420b8e378e67", [], [{:poison, "~> 1.5 or ~> 2.0 or ~> 3.0", [hex: :poison, repo: "hexpm", optional: false]}], "hexpm"},
"jsx": {:hex, :jsx, "2.8.3", "a05252d381885240744d955fbe3cf810504eb2567164824e19303ea59eef62cf", [], [], "hexpm"},
"meck": {:hex, :meck, "0.8.9", "64c5c0bd8bcca3a180b44196265c8ed7594e16bcc845d0698ec6b4e577f48188", [:rebar3], [], "hexpm"},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], []},
Expand Down

0 comments on commit 2447223

Please sign in to comment.