Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Address and Credit Card struct #74

Merged
merged 3 commits into from
Jan 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 92 additions & 50 deletions lib/gringotts/gateways/trexle.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ defmodule Gringotts.Gateways.Trexle do
use Gringotts.Gateways.Base
use Gringotts.Adapter, required_config: [:api_key, :default_currency]
import Poison, only: [decode: 1]
alias Gringotts.{Response}
alias Gringotts.{Response, CreditCard, Address}

@doc """
Performs the authorization of the card to be used for payment.
Expand All @@ -50,26 +50,33 @@ defmodule Gringotts.Gateways.Trexle do
```
iex> amount = 100

iex> card = %{
name: "John Doe",
iex> card = %CreditCard{
number: "5200828282828210",
expiry_month: 1,
expiry_year: 2018,
cvc: "123",
address_line1: "456 My Street",
address_city: "Ottawa",
address_postcode: "K1C2N6",
address_state: "ON",
address_country: "CA"
month: 12,
year: 2018,
first_name: "John",
last_name: "Doe",
verification_code: "123",
brand: "visa"
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jyotigautam you are assigning a value for card whereas you are declaring module attribute for address, since these examples are going to be executed in iex, module attribute will not work. change it to below throughout the examples.

address = %Address{
 +    street1: "123 Main",
 +    street2: "Suite 100",
 +    city: "New York",
 +    region: "NY",
 +    country: "US",
 +    postal_code: "11111",
 +    phone: "(555)555-5555"
 +  }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have assigned this address struct to a variable instead of using it as a module attribute as you said.

iex> options = [email: "john@trexle.com", ip_address: "66.249.79.118" , description: "Store Purchase 1437598192"]
iex> address = %Address{
street1: "123 Main",
street2: "Suite 100",
city: "New York",
region: "NY",
country: "US",
postal_code: "11111",
phone: "(555)555-5555"
}

iex> options = [email: "john@trexle.com", ip_address: "66.249.79.118", billing_address: address, description: "Store Purchase 1437598192"]

iex> Gringotts.authorize(:payment_worker, Gringotts.Gateways.Trexle, amount, card, options)
```
"""

@spec authorize(float, map, list) :: map
@spec authorize(float, CreditCard.t, list) :: map
def authorize(amount, payment, opts \\ []) do
params = create_params_for_auth_or_purchase(amount, payment, opts, false)
commit(:post, "charges", params, opts)
Expand All @@ -82,28 +89,47 @@ defmodule Gringotts.Gateways.Trexle do

## Example
```
iex> card = %{
name: "John Doe",
iex> card = %CreditCard{
number: "5200828282828210",
expiry_month: 1,
expiry_year: 2018,
cvc: "123",
address_line1: "456 My Street",
address_city: "Ottawa",
address_postcode: "K1C2N6",
address_state: "ON",
address_country: "CA"
month: 12,
year: 2018,
first_name: "John",
last_name: "Doe",
verification_code: "123",
brand: "visa"
}

iex> address = %Address{
street1: "123 Main",
street2: "Suite 100",
city: "New York",
region: "NY",
country: "US",
postal_code: "11111",
phone: "(555)555-5555"
}

iex> options = [email: "john@trexle.com", ip_address: "66.249.79.118" ,billing_address: address, description: "Store Purchase 1437598192"]

iex> @address %Address{
street1: "123 Main",
street2: "Suite 100",
city: "New York",
region: "NY",
country: "US",
postal_code: "11111",
phone: "(555)555-5555"
}

iex> options = [email: "john@trexle.com", ip_address: "66.249.79.118" ,description: "Store Purchase 1437598192"]
iex> options = [email: "john@trexle.com", ip_address: "66.249.79.118" ,billing_address: @address, description: "Store Purchase 1437598192"]

iex> amount = 50

iex> Gringotts.purchase(:payment_worker, Gringotts.Gateways.Trexle, amount, card, options)
```
"""

@spec purchase(float, map, list) :: map
@spec purchase(float, CreditCard.t, list) :: map
def purchase(amount, payment, opts \\ []) do
params = create_params_for_auth_or_purchase(amount, payment, opts)
commit(:post, "charges", params, opts)
Expand Down Expand Up @@ -165,28 +191,37 @@ defmodule Gringotts.Gateways.Trexle do
## Example
The following session shows how one would store a card (a payment-source) for future use.
```
iex> card = %{
name: "John Doe",
iex> card = %CreditCard{
number: "5200828282828210",
expiry_month: 1,
expiry_year: 2018,
cvc: "123",
address_line1: "456 My Street",
address_city: "Ottawa",
address_postcode: "K1C2N6",
address_state: "ON",
address_country: "CA"
month: 12,
year: 2018,
first_name: "John",
last_name: "Doe",
verification_code: "123",
brand: "visa"
}

iex> options = [email: "john@trexle.com", ip_address: "66.249.79.118", description: "Store Purchase 1437598192"]
iex> address = %Address{
street1: "123 Main",
street2: "Suite 100",
city: "New York",
region: "NY",
country: "US",
postal_code: "11111",
phone: "(555)555-5555"
}

iex> options = [email: "john@trexle.com", ip_address: "66.249.79.118", billing_address: address, description: "Store Purchase 1437598192"]

iex> Gringotts.store(:payment_worker, Gringotts.Gateways.Trexle, card, options)
```
"""

@spec store(map, list) :: map
@spec store(CreditCard.t, list) :: map
def store(payment, opts \\ []) do
params = [email: opts[:email]]++card_params(payment)
params = [email: opts[:email]]
++ card_params(payment)
++ address_params(opts[:billing_address])
commit(:post, "customers", params, opts)
end

Expand All @@ -199,24 +234,31 @@ defmodule Gringotts.Gateways.Trexle do
ip_address: opts[:ip_address],
description: opts[:description]
] ++ card_params(payment)
++ address_params(opts[:billing_address])
end

defp card_params(%CreditCard{} = card) do
[
"card[name]": CreditCard.full_name(card),
"card[number]": card.number,
"card[expiry_year]": card.year,
"card[expiry_month]": card.month,
"card[cvc]": card.verification_code
]
end

defp card_params(%{} = card) do
defp address_params(%Address{} = address) do
[
"card[name]": card[:name],
"card[number]": card[:number],
"card[expiry_year]": card[:expiry_year],
"card[expiry_month]": card[:expiry_month],
"card[cvc]": card[:cvc],
"card[address_line1]": card[:address_line1],
"card[address_city]": card[:address_city],
"card[address_postcode]": card[:address_postcode],
"card[address_state]": card[:address_state],
"card[address_country]": card[:address_country]
"card[address_line1]": address.street1,
"card[address_line2]": address.street2,
"card[address_city]": address.city,
"card[address_postcode]": address.postal_code,
"card[address_state]": address.region,
"card[address_country]": address.country
]
end

defp commit(method, path, params \\ [], opts \\ []) do
defp commit(method, path, params, opts) do
auth_token = "Basic #{Base.encode64(opts[:config][:api_key])}"
headers = [{"Content-Type", "application/x-www-form-urlencoded"}, {"Authorization", auth_token}]
data = params_to_string(params)
Expand Down
51 changes: 31 additions & 20 deletions test/gateways/trexle_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,41 @@ defmodule Gringotts.Gateways.TrexleTest do
use ExUnit.Case, async: false
alias Gringotts.Gateways.TrexleMock, as: MockResponse
alias Gringotts.Gateways.Trexle
alias Gringotts.{
CreditCard,
Address
}

import Mock

@valid_card %{
name: "John Doe",
@valid_card %CreditCard{
number: "5200828282828210",
expiry_month: 1,
expiry_year: 2018,
cvc: "123",
address_line1: "456 My Street",
address_city: "Ottawa",
address_postcode: "K1C2N6",
address_state: "ON",
address_country: "CA"
month: 12,
year: 2018,
first_name: "John",
last_name: "Doe",
verification_code: "123",
brand: "visa"
}

@invalid_card %{
name: "John Doe",
@invalid_card %CreditCard{
number: "5200828282828210",
expiry_month: 1,
expiry_year: 2010,
cvc: "123",
address_line1: "456 My Street",
address_city: "Ottawa",
address_postcode: "K1C2N6",
address_state: "ON",
address_country: "CA"
month: 12,
year: 2010,
first_name: "John",
last_name: "Doe",
verification_code: "123",
brand: "visa"
}

@address %Address{
street1: "123 Main",
street2: "Suite 100",
city: "New York",
region: "NY",
country: "US",
postal_code: "11111",
phone: "(555)555-5555"
}

@amount 100
Expand All @@ -42,19 +50,22 @@ defmodule Gringotts.Gateways.TrexleTest do
@opts [
config: %{api_key: "J5RGMpDlFlTfv9mEFvNWYoqHufyukPP4", default_currency: "USD"},
email: "john@trexle.com",
billing_address: @address,
ip_address: "66.249.79.118",
description: "Store Purchase 1437598192"
]

@missingip_opts [
config: %{api_key: "J5RGMpDlFlTfv9mEFvNWYoqHufyukPP4", default_currency: "USD"},
email: "john@trexle.com",
billing_address: @address,
description: "Store Purchase 1437598192"
]

@invalid_opts [
config: %{api_key: "J5RGMpDlFlTfv9mEFvNWYoqHufyukPP4"},
email: "john@trexle.com",
billing_address: @address,
ip_address: "66.249.79.118",
description: "Store Purchase 1437598192"
]
Expand Down