Skip to content

Commit

Permalink
handle false success condition
Browse files Browse the repository at this point in the history
  • Loading branch information
zigahertz committed Mar 26, 2022
1 parent 04e7dd8 commit 5825e64
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/gringotts/gateways/authorize_net.ex
Expand Up @@ -858,6 +858,10 @@ defmodule Gringotts.Gateways.AuthorizeNet do
|> Enum.find(:undefined_response, & &1)
end

defp build_response(%{"messages" => %{"resultCode" => "Ok"}, "transactionResponse" => %{"errors" => _}} = result, base_response) do
{:error, ResponseHandler.parse_gateway_error(result, base_response)}
end

defp build_response(%{"messages" => %{"resultCode" => "Ok"}} = result, base_response) do
{:ok, ResponseHandler.parse_gateway_success(result, base_response)}
end
Expand Down
16 changes: 16 additions & 0 deletions test/gateways/authorize_net_test.exs
Expand Up @@ -21,6 +21,13 @@ defmodule Gringotts.Gateways.AuthorizeNetTest do
verification_code: 123
}

@expired_card %CreditCard{
number: "5424000000000015",
month: 12,
year: 2000,
verification_code: 999
}

@amount FakeMoney.new("2.99", :USD)

@opts [
Expand Down Expand Up @@ -157,6 +164,15 @@ defmodule Gringotts.Gateways.AuthorizeNetTest do
assert {:error, _response} = ANet.purchase(@amount, @bad_card, @opts)
end
end

test "with expired card" do
with_mock HTTPoison,
post: fn _url, _body, _headers ->
MockResponse.falsely_successful_expired_card_response()
end do
assert {:error, _response} = ANet.purchase(@amount, @expired_card, @opts)
end
end
end

describe "authorize" do
Expand Down
24 changes: 24 additions & 0 deletions test/support/mocks/authorize_net_mock.ex
Expand Up @@ -73,6 +73,30 @@ defmodule Gringotts.Gateways.AuthorizeNetMock do
}}
end

def falsely_successful_expired_card_response do
{:ok,
%HTTPoison.Response{
body:
~s(<?xml version="1.0" encoding="utf-8"?><createTransactionResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd"><refId /><messages><resultCode>Ok</resultCode><message><code>I00001</code><text>Successful.</text></message></messages><transactionResponse><responseCode>2</responseCode><authCode>000000</authCode><avsResultCode>P</avsResultCode><cvvResultCode>P</cvvResultCode><cavvResultCode /><transId>43289066237</transId><refTransID /><transHash /><testRequest>0</testRequest><accountNumber>XXXX6008</accountNumber><accountType>Visa</accountType><errors><error><errorCode>2</errorCode><errorText>This transaction has been declined.</errorText></error></errors><transHashSha2 /><profile><customerProfileId>553747225</customerProfileId><customerPaymentProfileId>552573068</customerPaymentProfileId></profile><networkTransId>382083585479253 N 01</networkTransId></transactionResponse></createTransactionResponse>),
headers: [
{"Cache-Control", "private"},
{"Content-Type", "application/xml; charset=utf-8"},
{"X-OPNET-Transaction-Trace", "a2_b6b84b43-d399-4dde-bc12-fb1f8ccf4b27-51156-17537805"},
{"Access-Control-Allow-Origin", "*"},
{"Access-Control-Allow-Methods", "PUT,OPTIONS,POST,GET"},
{"Access-Control-Allow-Headers",
"x-requested-with,cache-control,content-type,origin,method,SOAPAction"},
{"Access-Control-Allow-Credentials", "true"},
{"X-Cnection", "close"},
{"Content-Length", "828"},
{"Date", "Thu, 28 Dec 2017 13:54:20 GMT"},
{"Connection", "keep-alive"}
],
request_url: "https://apitest.authorize.net/xml/v1/request.api",
status_code: 200
}}
end

def bad_amount_purchase_response do
{:ok,
%HTTPoison.Response{
Expand Down

0 comments on commit 5825e64

Please sign in to comment.