Skip to content
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
20 changes: 16 additions & 4 deletions lib/adyen/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ def call_adyen_api(service, action, request_data, headers, version, with_applica
raise connection_error, "Connection to #{url} failed"
end
end
if action.fetch(:method) == "delete"
begin
response = conn.delete
rescue Faraday::ConnectionFailed => connection_error
raise connection_error, "Connection to #{url} failed"
end
end
if action.fetch(:method) == "patch"
begin
response = conn.patch do |req|
Expand All @@ -169,11 +176,16 @@ def call_adyen_api(service, action, request_data, headers, version, with_applica
when 401
raise Adyen::AuthenticationError.new("Invalid API authentication; https://docs.adyen.com/user-management/how-to-get-the-api-key", request_data)
when 403
raise Adyen::PermissionError.new("Missing user permissions; https://docs.adyen.com/user-management/user-roles", request_data, response.response_body)
raise Adyen::PermissionError.new("Missing user permissions; https://docs.adyen.com/user-management/user-roles", request_data, response.body)
end

formatted_response = AdyenResult.new(response.body, response.headers, response.status)


# delete has no response.body (unless it throws an error)
if response.body == nil
formatted_response = AdyenResult.new("{}", response.headers, response.status)
else
formatted_response = AdyenResult.new(response.body, response.headers, response.status)
end

formatted_response
end

Expand Down
54 changes: 43 additions & 11 deletions lib/adyen/services/checkout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Adyen
class Checkout < Service
DEFAULT_VERSION = 68
DEFAULT_VERSION = 70

def initialize(client, version = DEFAULT_VERSION)
service = "Checkout"
Expand All @@ -13,7 +13,7 @@ def initialize(client, version = DEFAULT_VERSION)
]

with_application_info = [
:payment_session,
:payment_session
]

super(client, version, service, method_names, with_application_info)
Expand Down Expand Up @@ -42,7 +42,7 @@ def payment_links(*args)
else
action = "paymentLinks"
args[1] ||= {} # optional headers arg
@client.call_adyen_api(@service, action, args[0], args[1], @version, true)
@client.call_adyen_api(@service, action, args[0], args[1], @version)
end
end

Expand Down Expand Up @@ -75,6 +75,10 @@ def apple_pay
def modifications
@modifications ||= Adyen::Modifications.new(@client, @version)
end

def stored_payment_methods
@stored_payment_methods ||= Adyen::StoredPaymentMethods.new(@client, @version)
end
end

class CheckoutDetail < Service
Expand All @@ -93,6 +97,16 @@ def result(request, headers = {})
action = "payments/result"
@client.call_adyen_api(@service, action, request, headers, @version)
end

def donations(request, headers = {})
action = "donations"
@client.call_adyen_api(@service, action, request, headers, @version)
end

def card_details(request, headers = {})
action = "cardDetails"
@client.call_adyen_api(@service, action, request, headers, @version)
end
end

class CheckoutLink < Service
Expand All @@ -104,12 +118,12 @@ def initialize(client, version = DEFAULT_VERSION)

def get(linkId, headers = {})
action = { method: 'get', url: "paymentLinks/" + linkId }
@client.call_adyen_api(@service, action, {}, headers, @version, true)
@client.call_adyen_api(@service, action, {}, headers, @version)
end

def update(linkId, request, headers = {})
action = { method: 'patch', url: "paymentLinks/" + linkId }
@client.call_adyen_api(@service, action, request, headers, @version, false)
@client.call_adyen_api(@service, action, request, headers, @version)
end
end

Expand Down Expand Up @@ -161,12 +175,12 @@ def initialize(client, version = DEFAULT_VERSION)

def capture(linkId, request, headers = {})
action = "payments/" + linkId + "/captures"
@client.call_adyen_api(@service, action, request, headers, @version, false)
@client.call_adyen_api(@service, action, request, headers, @version)
end

def cancel(linkId, request, headers = {})
action = "payments/" + linkId + "/cancels"
@client.call_adyen_api(@service, action, request, headers, @version, false)
@client.call_adyen_api(@service, action, request, headers, @version)
end

def genericCancel(request, headers = {})
Expand All @@ -176,17 +190,35 @@ def genericCancel(request, headers = {})

def refund(linkId, request, headers = {})
action = "payments/" + linkId + "/refunds"
@client.call_adyen_api(@service, action, request, headers, @version, false)
@client.call_adyen_api(@service, action, request, headers, @version)
end

def reversal(linkId, request, headers = {})
action = "payments/" + linkId + "/reversals"
@client.call_adyen_api(@service, action, request, headers, @version, false)
@client.call_adyen_api(@service, action, request, headers, @version)
end

def amountUpdate(linkId, request, headers = {})
action = "payments/" + linkId + "/amountUpdates"
@client.call_adyen_api(@service, action, request, headers, @version, false)
@client.call_adyen_api(@service, action, request, headers, @version)
end
end

class StoredPaymentMethods < Service
def initialize(client, version = DEFAULT_VERSION)
@service = "Checkout"
@client = client
@version = version
end

def get(query_array={}, headers = {})
action = { method: 'get', url: "storedPaymentMethods" + create_query_string(query_array)}
@client.call_adyen_api(@service, action, {}, headers, @version)
end

def delete(recurringId, query_array={}, headers = {})
action = { method: 'delete', url: "storedPaymentMethods/%s" % recurringId + create_query_string(query_array)}
@client.call_adyen_api(@service, action, {}, headers, @version)
end
end
end
end
5 changes: 5 additions & 0 deletions lib/adyen/services/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,10 @@ def initialize(client, version, service, method_names, with_application_info = [
end
end
end

# create query parameter from an array
def create_query_string(arr)
"?" + URI.encode_www_form(arr)
end
end
end
50 changes: 50 additions & 0 deletions spec/checkout_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,56 @@
to eq("12345")
end

it "makes a get storedPaymentMethods call" do
response_body = json_from_file("mocks/responses/Checkout/stored_payment_methods.json")

url = @shared_values[:client].service_url(@shared_values[:service], "storedPaymentMethods?merchantAccount=TestMerchantAccount&shopperReference=test-1234", @shared_values[:client].checkout.version)
WebMock.stub_request(:get, url).
with(
headers: {
"x-api-key" => @shared_values[:client].api_key
}
).
to_return(
body: response_body
)

result = @shared_values[:client].checkout.stored_payment_methods.get({"merchantAccount" => "TestMerchantAccount", "shopperReference" => "test-1234"})
response_hash = result.response

expect(result.status).
to eq(200)
expect(response_hash).
to eq(JSON.parse(response_body))
expect(response_hash).
to be_a Adyen::HashWithAccessors
expect(response_hash).
to be_a_kind_of Hash
expect(response_hash["shopperReference"]).
to eq("test-1234")
end

it "makes a delete storedPaymentMethods call" do
response_body = json_from_file("mocks/responses/Checkout/stored_payment_methods.json")

url = @shared_values[:client].service_url(@shared_values[:service], "storedPaymentMethods/RL8FW7WZM6KXWD82?merchantAccount=TestMerchantAccount&shopperReference=test-1234", @shared_values[:client].checkout.version)
WebMock.stub_request(:delete, url).
with(
headers: {
"x-api-key" => @shared_values[:client].api_key
}
).
to_return(
body: response_body
)

result = @shared_values[:client].checkout.stored_payment_methods.delete("RL8FW7WZM6KXWD82", {"merchantAccount" => "TestMerchantAccount", "shopperReference" => "test-1234"})
response_hash = result.response

expect(result.status).
to eq(200)
end

# create client for automated tests
client = create_client(:api_key)

Expand Down
2 changes: 1 addition & 1 deletion spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
mock_response = Faraday::Response.new(status: 200)

expect(Adyen::AdyenResult).to receive(:new)
expect(Faraday).to receive(:new).with("http://localhost:3001/v68/payments/details", connection_options).and_return(mock_faraday_connection)
expect(Faraday).to receive(:new).with("http://localhost:3001/v70/payments/details", connection_options).and_return(mock_faraday_connection)
expect(mock_faraday_connection).to receive(:post).and_return(mock_response)
client.checkout.payments.details(request_body)
end
Expand Down
1 change: 1 addition & 0 deletions spec/mocks/responses/Checkout/stored_payment_methods.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"merchantAccount":"TestMerchantAccount", "shopperReference":"test-1234"}