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

[SecurionPay] Implemented purchase/3 #143

Open
wants to merge 1 commit into
base: securion-pay
Choose a base branch
from
Open
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
36 changes: 36 additions & 0 deletions lib/gringotts/gateways/securion_pay.ex
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,42 @@ defmodule Gringotts.Gateways.SecurionPay do
commit([], "charges/#{payment_id}/capture", opts)
end

@doc """
Transfers `amount` from the customer to the merchant.

SecurionPay attempts to process a purchase on behalf of the customer, by debiting
`amount` from the customer's account by charging the customer's `card`.

The second argument can be a `CreditCard` or a `card_id`. The `customer_id` of the customer who owns the card must be
given in optional field if `card_id` is used.

## Example
### With a `CreditCard` struct
iex> amount = Money.new(20, :USD)
iex> card = %Gringotts.CreditCard{first_name: "Harry", last_name: "Potter", number: "4200000000000000", year: 2099, month: 12, verification_code: "123", brand: "VISA"}
iex> result = Gringotts.Gateways.SecurionPay.purchase(amount, card, [])

## Example
### With a `card_token` and `customer_token`
iex> amount = Money.new(20, :USD)
iex> opts = [customer_id: "cust_9999999999999999999999999"]
iex> card = "card_999999999999999"
iex> result = Gringotts.Gateways.SecurionPay.purchase(amount, card, opts)
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't call your module directly in doc examples.


"""

@spec purchase(Money.t(), CreditCard.t() | String.t(), keyword) :: {:ok | :error, Response.t()}

def purchase(amount, %CreditCard{} = card, opts) do
params = common_params(amount, true) ++ card_params(card)
commit(params, "charges", opts)
end

def purchase(amount, card_id, opts) when is_binary(card_id) do
params = common_params(amount, true) ++ card_params(card_id, opts)
commit(params, "charges", opts)
end

##########################################################################
# PRIVATE METHODS #
##########################################################################
Expand Down
37 changes: 37 additions & 0 deletions test/integration/gateways/securion_pay_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,41 @@ defmodule Gringotts.Integration.Gateways.SecurionPayTest do
end
end
end

describe "[purchase]" do
test "with CreditCard" do
use_cassette "securion_pay/purchase_with_credit_card" do
assert {:ok, response} = Gateway.purchase(@amount, @good_card, @opts)
assert response.success == true
assert response.status_code == 200
end
end

test "with card_id and customer_id" do
use_cassette "securion_pay/purchase_with_card_id" do
assert {:ok, response} = Gateway.purchase(@amount, @card_id, @opts)
assert response.success == true
assert response.status_code == 200
end
end

test "with expired CreditCard" do
use_cassette "securion_pay/purchase_with_expired_card" do
assert {:error, response} = Gateway.purchase(@amount, @bad_card, @opts)
assert response.success == false
refute response.status_code == 200
assert response.message == "card_error"
assert response.reason == "The card has expired."
end
end

test "with card_id but no customer_id" do
Copy link
Contributor

Choose a reason for hiding this comment

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

Could this check perhaps happen on our side, without making a network request?

use_cassette "securion_pay/purchase_without_customer_id" do
assert {:error, response} = Gateway.purchase(@amount, @card_id, @bad_opts)
assert response.success == false
refute response.status_code == 200
assert response.message == "invalid_request"
end
end
end
end