Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Adds list_receivers and list_supported_receivers to the gem #105

Open
wants to merge 16 commits into
base: master
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

All notable changes to this project will be documented in this file.

## [2.0.25] - 2020-10-08
### Added
- @hoenth - Add list_receivers
- @hoenth - Add list_supported_receivers

## [2.0.24] - 2021-07-12
### Added
- @almalee24 - Add MIT Framework support flag, supports_populate_mit_fields
Expand Down
1 change: 1 addition & 0 deletions lib/spreedly.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
require 'spreedly/transactions/store'
require 'spreedly/gateway'
require 'spreedly/receiver'
require 'spreedly/supported_receiver'
require 'spreedly/gateway_class'
require 'spreedly/receiver_class'
require 'spreedly/error'
Expand Down
10 changes: 10 additions & 0 deletions lib/spreedly/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,16 @@ def add_receiver(receiver_type, host_names = nil, credentials = [])
Receiver.new(xml_doc)
end

def list_receivers(since_token = nil)
xml_doc = ssl_get(list_receivers_url(since_token), headers)
Receiver.new_list_from(xml_doc)
end

def list_supported_receivers
xml_doc = ssl_get(list_supported_receivers_url, headers)
SupportedReceiver.new_list_from(xml_doc)
end

def add_credit_card(options)
api_post(add_payment_method_url, add_credit_card_body(options), false)
end
Expand Down
7 changes: 7 additions & 0 deletions lib/spreedly/receiver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ def initialize(xml_doc)
init_credentials(xml_doc)
end

def self.new_list_from(xml_doc)
receivers = xml_doc.xpath('.//receivers/receiver')
receivers.map do |each|
self.new(each)
end
end

private
def init_credentials(xml_doc)
@credentials = {}
Expand Down
20 changes: 20 additions & 0 deletions lib/spreedly/supported_receiver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module Spreedly
class SupportedReceiver
include Fields

field :receiver_type, :hostnames, :name, :company_name

def initialize(xml_doc)
initialize_fields(xml_doc)
end

def self.new_list_from(xml_doc)
receivers = xml_doc.xpath('.//receivers/receiver')
receivers.map do |each|
self.new(each)
end
end
end
end
9 changes: 9 additions & 0 deletions lib/spreedly/urls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ def list_gateways_url(since_token)
"#{base_url}/v1/gateways.xml#{since_param}"
end

def list_receivers_url(since_token)
since_param = "?since_token=#{since_token}" if since_token
"#{base_url}/v1/receivers.xml#{since_param}"
end

def list_supported_receivers_url
"#{base_url}/v1/receivers_options.xml"
end

def gateway_options_url
"#{base_url}/v1/gateways.xml"
end
Expand Down
12 changes: 7 additions & 5 deletions test/remote/remote_add_credit_card_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ def test_failed_with_validation_errors
assert_equal "First name can't be blank\nLast name can't be blank", error.message
end

def test_payment_required
assert_raise_with_message(Spreedly::PaymentRequiredError, "Your environment (R7lHscqcYkZeDGGbthKp6GKMu15) has not been activated for real transactions with real payment methods. If you're using a Test Gateway you can *ONLY* use Test payment methods - ( https://docs.spreedly.com/test-data). All other credit card numbers are considered real credit cards; real credit cards are not allowed when using a Test Gateway.") do
@environment.add_credit_card(card_deets(number: '343'))
end
end
# 2022-09-08 There appears to no longer be a concept of a non-active environment. All environments are treated as production environments. There are only Test gateways that you can run test transactions against.
# def test_payment_required
# assert_raise_with_message(Spreedly::PaymentRequiredError, "Your environment (" + remote_test_environment_key + ") has not been activated for real transactions with real payment methods. If you're using a Test Gateway you can *ONLY* use Test payment methods - ( https://docs.spreedly.com/test-data). All other credit card numbers are considered real credit cards; real credit cards are not allowed when using a Test Gateway.") do
# @environment.add_credit_card(card_deets(number: '5474152271778594'))
# end
# end

def test_successful_add_card
t = @environment.add_credit_card(card_deets)
Expand Down Expand Up @@ -63,6 +64,7 @@ def test_successfull_add_using_full_name
def card_deets(options = {})
{
email: '[email protected]', number: '5555555555554444', month: 1, year: 2023,
verification_value: '123',
last_name: 'Aybara', first_name: 'Perrin', data: "occupation: Blacksmith"
}.merge(options)
end
Expand Down
11 changes: 6 additions & 5 deletions test/remote/remote_add_gateway_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ def test_add_test_gateway
assert_equal "Spreedly Test", gateway.name
end

def test_need_active_account
assert_raise_with_message(Spreedly::PaymentRequiredError, "Your environment (R7lHscqcYkZeDGGbthKp6GKMu15) has not been activated for real transactions with real payment methods. If you're using a Test Gateway you can *ONLY* use Test payment methods - ( https://docs.spreedly.com/test-data). All other credit card numbers are considered real credit cards; real credit cards are not allowed when using a Test Gateway.") do
@environment.add_gateway(:wirecard)
end
end
# 2022-09-08 There appears to no longer be a concept of a non-active environment. All environments are treated as production environments. There are only Test gateways that you can run test transactions against.
# def test_need_active_account
# assert_raise_with_message(Spreedly::PaymentRequiredError, "Your environment (" + remote_test_environment_key + ") has not been activated for real transactions with real payment methods. If you're using a Test Gateway you can *ONLY* use Test payment methods - ( https://docs.spreedly.com/test-data). All other credit card numbers are considered real credit cards; real credit cards are not allowed when using a Test Gateway.") do
# @environment.add_gateway(:wirecard)
# end
# end

end
22 changes: 12 additions & 10 deletions test/remote/remote_add_receiver_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,24 @@ def test_non_existent_receiver_type
end
end

def test_add_test_receiver_sans_hostname
assert_raise_with_message(Spreedly::TransactionCreationError, "Hostnames can't be blank") do
@environment.add_receiver(:test, nil)
end
end
# 2022-09-08 host names are no longer required for test receivers, and are ignored for production receivers
# def test_add_test_receiver_sans_hostname
# assert_raise_with_message(Spreedly::TransactionCreationError, "Hostnames can't be blank") do
# @environment.add_receiver(:test, nil)
# end
# end

def test_add_test_receiver
receiver = @environment.add_receiver(:test, 'http://spreedly-echo.herokuapp.com')
assert_equal "test", receiver.receiver_type
assert_equal 'http://spreedly-echo.herokuapp.com', receiver.hostnames
end

def test_need_active_account
assert_raise_with_message(Spreedly::PaymentRequiredError, "Your environment (R7lHscqcYkZeDGGbthKp6GKMu15) has not been activated for real transactions with real payment methods. If you're using a Test Gateway you can *ONLY* use Test payment methods - ( https://docs.spreedly.com/test-data). All other credit card numbers are considered real credit cards; real credit cards are not allowed when using a Test Gateway.") do
@environment.add_receiver(:braintree)
end
end
# 2022-09-08 There appears to no longer be a concept of a non-active environment. All environments are treated as production environments. There are only Test gateways that you can run test transactions against.
# def test_need_active_account
# assert_raise_with_message(Spreedly::PaymentRequiredError, "Your environment (R7lHscqcYkZeDGGbthKp6GKMu15) has not been activated for real transactions with real payment methods. If you're using a Test Gateway you can *ONLY* use Test payment methods - ( https://docs.spreedly.com/test-data). All other credit card numbers are considered real credit cards; real credit cards are not allowed when using a Test Gateway.") do
# @environment.add_receiver(:braintree)
# end
# end

end
7 changes: 5 additions & 2 deletions test/remote/remote_authorize_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ def test_invalid_login
end

def test_payment_method_not_found
assert_raise_with_message(Spreedly::TransactionCreationError, "There is no payment method corresponding to the specified payment method token.") do
@environment.authorize_on_gateway('gateway_token', 'unknown_payment_method', 100)
gateway_token = @environment.add_gateway(:test).token

assert_raise_with_message(Spreedly::NotFoundError, "There is no payment method corresponding to the specified payment method token.") do
@environment.authorize_on_gateway(gateway_token, 'unknown_payment_method', 100)
end
end

def test_gateway_not_found
card_token = create_card_on(@environment).token

assert_raise_with_message(Spreedly::NotFoundError, "Unable to find the specified gateway.") do
@environment.authorize_on_gateway('unknown_gateway', card_token, 100)
end
Expand Down
5 changes: 3 additions & 2 deletions test/remote/remote_deliver_payment_method_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ def test_successfully_deliver_receiver
assert_equal(true, transaction.succeeded?)
assert_equal(card_token, transaction.payment_method.token)
assert_equal(receiver_token, transaction.receiver.token)
assert_match(/HOST: spreedly-echo.herokuapp.com\nCONNECTION: close/m, transaction.response.body)
assert_equal('200', transaction.response.status)
assert_match(/Server: thin/m, transaction.response.headers)
assert_match(/Server: Spreedly Echo Server/, transaction.response.headers)
assert_match(/CONNECTION: close/, transaction.response.headers)

end

private
Expand Down
2 changes: 1 addition & 1 deletion test/remote/remote_gateway_options_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def test_successfully_get_options
gateway_classes = @environment.gateway_options
braintree = gateway_classes.select { |each| each.name == "Braintree" }.first
assert_equal "http://www.braintreepaymentsolutions.com/", braintree.homepage
assert_equal %w(credit_card third_party_token apple_pay android_pay google_pay), braintree.payment_methods
assert_equal %w(credit_card third_party_token apple_pay android_pay google_pay bank_account paypal venmo), braintree.payment_methods
assert_equal %w(asia_pacific europe north_america), braintree.regions
assert_equal %w(orange blue), braintree.auth_modes.map { |e| e.auth_mode_type }
assert_equal %w(login password), braintree.auth_modes.first.credentials.map { |e| e.name }
Expand Down
6 changes: 3 additions & 3 deletions test/remote/remote_list_gateways_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ def test_successfully_list_gateways
first_twenty = @environment.list_gateways
assert_equal 20, first_twenty.size
assert_kind_of Spreedly::Model, first_twenty.first

gateways = @environment.list_gateways(g1)
assert_equal 2, gateways.size
gateways = @environment.list_gateways(g2)
assert_equal 1, gateways.size
assert_kind_of(Spreedly::Gateway, gateways.first)
assert_kind_of(Spreedly::Gateway, gateways.last)

Expand Down
8 changes: 4 additions & 4 deletions test/remote/remote_list_payment_methods_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ def test_successfully_list_payment_methods
assert_equal 20, first_twenty.size
assert_kind_of Spreedly::Model, first_twenty.first

payment_methods = @environment.list_payment_methods(c1)
assert_equal 2, payment_methods.size
payment_methods = @environment.list_payment_methods(c2)
assert_equal 1, payment_methods.size
assert_kind_of(Spreedly::CreditCard, payment_methods.first)
assert_kind_of(Spreedly::CreditCard, payment_methods.last)
# assert_kind_of(Spreedly::CreditCard, payment_methods.last)

assert_equal c2, payment_methods.first.token
assert_equal c3, payment_methods.last.token
# assert_equal c3, payment_methods.last.token
end

end
Expand Down
5 changes: 3 additions & 2 deletions test/remote/remote_list_transactions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ def test_successfully_list_transactions
assert_equal 20, first_twenty.size
assert_kind_of Spreedly::Model, first_twenty.first

transactions = @environment.list_transactions(t1.token)
assert_equal 2, transactions.size
transactions = @environment.list_transactions(t2.token)
p transactions
assert_equal 1, transactions.size
assert_kind_of(Spreedly::Authorization, transactions.first)
assert_kind_of(Spreedly::Capture, transactions.last)

Expand Down
6 changes: 4 additions & 2 deletions test/remote/remote_purchase_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ def test_invalid_login
end

def test_payment_method_not_found
assert_raise_with_message(Spreedly::TransactionCreationError, "There is no payment method corresponding to the specified payment method token.") do
@environment.purchase_on_gateway('gateway_token', 'unknown_payment_method', 100)
gateway_token = @environment.add_gateway(:test).token

assert_raise_with_message(Spreedly::NotFoundError, "There is no payment method corresponding to the specified payment method token.") do
@environment.purchase_on_gateway(gateway_token, 'unknown_payment_method', 100)
end
end

Expand Down
3 changes: 1 addition & 2 deletions test/remote/remote_receiver_options_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ def setup
def test_successfully_get_options
receiver_classes = @environment.receiver_options
ace = receiver_classes.select { |each| each.name == "Acerentacar Receiver" }.first
assert_equal "https://ota.acerentacar.com", ace.hostnames
assert_match /https:\/\/ota.acerentacar\.com/, ace.hostnames
assert_equal "Ace Rent a Car", ace.company_name
assert_equal "ace_rent_a_car", ace.receiver_type
end

end

6 changes: 4 additions & 2 deletions test/remote/remote_store_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ def test_invalid_login
end

def test_payment_method_not_found
assert_raise_with_message(Spreedly::TransactionCreationError, "There is no payment method corresponding to the specified payment method token.") do
@environment.store_on_gateway('gateway_token', 'unknown_payment_method')
gateway_token = @environment.add_gateway(:test).token

assert_raise_with_message(Spreedly::NotFoundError, "There is no payment method corresponding to the specified payment method token.") do
@environment.store_on_gateway(gateway_token, 'unknown_payment_method')
end
end

Expand Down
6 changes: 4 additions & 2 deletions test/remote/remote_verify_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ def test_invalid_login
end

def test_payment_method_not_found
assert_raise_with_message(Spreedly::TransactionCreationError, "There is no payment method corresponding to the specified payment method token.") do
@environment.verify_on_gateway('gateway_token', 'unknown_payment_method')
gateway_token = @environment.add_gateway(:test).token

assert_raise_with_message(Spreedly::NotFoundError, "There is no payment method corresponding to the specified payment method token.") do
@environment.verify_on_gateway(gateway_token, 'unknown_payment_method')
end
end

Expand Down
46 changes: 46 additions & 0 deletions test/unit/list_receivers_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'test_helper'
require 'unit/response_stubs/list_receivers_stubs'

class ListReceiversTest < Test::Unit::TestCase

include ListReceiversStubs

def setup
@environment = Spreedly::Environment.new("key", "secret")
end

def test_successful_list_receivers
list = list_using(successful_list_receivers_response)

assert_kind_of(Array, list)
assert_equal 2, list.size

assert_equal 'OJUFe5ZR6pFfL4i4ZGVmvGWkZUY', list.first.token
assert_equal '52wqOssuKZSXEYde30AGTG6xl8v', list.last.token
assert_kind_of Spreedly::Receiver, list.first
assert_kind_of Spreedly::Receiver, list.last
end

def test_request_url
assert_request_url 'https://core.spreedly.com/v1/receivers.xml' do
@environment.list_receivers
end

assert_request_url 'https://core.spreedly.com/v1/receivers.xml?since_token=SomeToken' do
@environment.list_receivers("SomeToken")
end
end

private
def list_using(response)
@environment.stubs(:raw_ssl_request).returns(response)
@environment.list_receivers
end

def assert_request_url(expected_url)
actual_url = get_request_url(successful_list_receivers_response) do
yield
end
assert_equal expected_url, actual_url
end
end
42 changes: 42 additions & 0 deletions test/unit/list_supported_receivers_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'test_helper'
require 'unit/response_stubs/list_supported_receivers_stubs'

class ListSupportedReceiversTest < Test::Unit::TestCase

include ListSupportedReceiversStubs

def setup
@environment = Spreedly::Environment.new("key", "secret")
end

def test_successful_list_supported_receivers
list = list_using(successful_list_supported_receivers_response)

assert_kind_of(Array, list)
assert_equal 2, list.size

assert_equal 'Example Production', list.first.name
assert_equal 'Example Sandbox', list.last.name
assert_kind_of Spreedly::SupportedReceiver, list.first
assert_kind_of Spreedly::SupportedReceiver, list.last
end

def test_request_url
assert_request_url 'https://core.spreedly.com/v1/receivers_options.xml' do
@environment.list_supported_receivers
end
end

private
def list_using(response)
@environment.stubs(:raw_ssl_request).returns(response)
@environment.list_supported_receivers
end

def assert_request_url(expected_url)
actual_url = get_request_url(successful_list_supported_receivers_response) do
yield
end
assert_equal expected_url, actual_url
end
end
Loading