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
19 changes: 19 additions & 0 deletions .github/workflows/rubygems_release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Publish Gem

on:
release:
types: [published]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1

- name: Release Gem on RubyGems
if: contains(github.ref, 'refs/tags/v')
uses: cadwallion/publish-rubygems-action@master
env:
GITHUB_TOKEN: ${{secrets.TOKEN_RUBYGEMS_RELEASES_WITH_EXPIRATION}}
RUBYGEMS_API_KEY: ${{secrets.RUBYGEMS_API_KEY}}
7 changes: 4 additions & 3 deletions lib/adyen/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
module Adyen
class Client
attr_accessor :ws_user, :ws_password, :api_key, :client, :adapter, :live_url_prefix
attr_reader :env
attr_reader :env, :connection_options

def initialize(ws_user: nil, ws_password: nil, api_key: nil, env: :live, adapter: nil, mock_port: 3001, live_url_prefix: nil, mock_service_url_base: nil)
def initialize(ws_user: nil, ws_password: nil, api_key: nil, env: :live, adapter: nil, mock_port: 3001, live_url_prefix: nil, mock_service_url_base: nil, connection_options: nil)
@ws_user = ws_user
@ws_password = ws_password
@api_key = api_key
@env = env
@adapter = adapter || Faraday.default_adapter
@mock_service_url_base = mock_service_url_base || "http://localhost:#{mock_port}"
@live_url_prefix = live_url_prefix
@connection_options = connection_options || Faraday::ConnectionOptions.new
end

# make sure that env can only be :live, :test, or :mock
Expand Down Expand Up @@ -96,7 +97,7 @@ def call_adyen_api(service, action, request_data, headers, version, with_applica
end

# initialize Faraday connection object
conn = Faraday.new(url: url) do |faraday|
conn = Faraday.new(url, @connection_options) do |faraday|
faraday.adapter @adapter
faraday.headers["Content-Type"] = "application/json"
faraday.headers["User-Agent"] = Adyen::NAME + "/" + Adyen::VERSION
Expand Down
42 changes: 42 additions & 0 deletions lib/adyen/services/checkout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ def orders(*args)
def apple_pay
@apple_pay ||= Adyen::CheckoutApplePay.new(@client, @version)
end

def modifications
@modifications ||= Adyen::Modifications.new(@client, @version)
end
end

class CheckoutDetail < Service
Expand Down Expand Up @@ -147,4 +151,42 @@ def sessions(request, headers = {})
@client.call_adyen_api(@service, action, request, headers, @version)
end
end

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

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

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

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

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

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

def amountUpdate(linkId, request, headers = {})
action = "payments/" + linkId + "/amountUpdates"
@client.call_adyen_api(@service, action, request, headers, @version, false)
end
end
end
2 changes: 1 addition & 1 deletion lib/adyen/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Adyen
NAME = "adyen-ruby-api-library"
VERSION = "6.1.0".freeze
VERSION = "6.2.0".freeze
end
192 changes: 192 additions & 0 deletions spec/checkout_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,198 @@
to be_a_kind_of Hash
end

it "makes a capture call" do
request_body = JSON.parse(json_from_file("mocks/requests/Checkout/capture.json"))

response_body = json_from_file("mocks/responses/Checkout/capture.json")

url = @shared_values[:client].service_url(@shared_values[:service], "payments/12345/captures", @shared_values[:client].checkout.version)
WebMock.stub_request(:post, url).
with(
body: request_body,
headers: {
"x-api-key" => @shared_values[:client].api_key
}
)
.to_return(body: response_body, status: 201)

result = @shared_values[:client].checkout.modifications.capture("12345", request_body)
response_hash = result.response

expect(result.status).
to eq(201)
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.reference).
to eq("123456789")
expect(response_hash.pspReference).
to eq("12345")
end

it "makes a psp specific cancel call" do
request_body = JSON.parse(json_from_file("mocks/requests/Checkout/psp_cancel.json"))

response_body = json_from_file("mocks/responses/Checkout/psp_cancel.json")

url = @shared_values[:client].service_url(@shared_values[:service], "payments/12345/cancels", @shared_values[:client].checkout.version)
WebMock.stub_request(:post, url).
with(
body: request_body,
headers: {
"x-api-key" => @shared_values[:client].api_key
}
)
.to_return(body: response_body, status: 201)

result = @shared_values[:client].checkout.modifications.cancel("12345", request_body)
response_hash = result.response

expect(result.status).
to eq(201)
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.reference).
to eq("123456789")
expect(response_hash.pspReference).
to eq("12345")
end

it "makes a psp specific refunds call" do
request_body = JSON.parse(json_from_file("mocks/requests/Checkout/refund.json"))

response_body = json_from_file("mocks/responses/Checkout/refund.json")

url = @shared_values[:client].service_url(@shared_values[:service], "payments/12345/refunds", @shared_values[:client].checkout.version)
WebMock.stub_request(:post, url).
with(
body: request_body,
headers: {
"x-api-key" => @shared_values[:client].api_key
}
)
.to_return(body: response_body, status: 201)

result = @shared_values[:client].checkout.modifications.refund("12345", request_body)
response_hash = result.response

expect(result.status).
to eq(201)
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.reference).
to eq("123456789")
expect(response_hash.pspReference).
to eq("12345")
end

it "makes a psp specific reversals call" do
request_body = JSON.parse(json_from_file("mocks/requests/Checkout/psp_cancel.json"))

response_body = json_from_file("mocks/responses/Checkout/psp_cancel.json")

url = @shared_values[:client].service_url(@shared_values[:service], "payments/12345/reversals", @shared_values[:client].checkout.version)
WebMock.stub_request(:post, url).
with(
body: request_body,
headers: {
"x-api-key" => @shared_values[:client].api_key
}
)
.to_return(body: response_body, status: 201)

result = @shared_values[:client].checkout.modifications.reversal("12345", request_body)
response_hash = result.response

expect(result.status).
to eq(201)
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.reference).
to eq("123456789")
expect(response_hash.pspReference).
to eq("12345")
end

it "makes a psp specific amountUpdates call" do
request_body = JSON.parse(json_from_file("mocks/requests/Checkout/amount_updates.json"))

response_body = json_from_file("mocks/responses/Checkout/amount_updates.json")

url = @shared_values[:client].service_url(@shared_values[:service], "payments/12345/amountUpdates", @shared_values[:client].checkout.version)
WebMock.stub_request(:post, url).
with(
body: request_body,
headers: {
"x-api-key" => @shared_values[:client].api_key
}
)
.to_return(body: response_body, status: 201)

result = @shared_values[:client].checkout.modifications.amountUpdate("12345", request_body)
response_hash = result.response

expect(result.status).
to eq(201)
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.reference).
to eq("123456789")
expect(response_hash.pspReference).
to eq("12345")
end

it "makes a generic cancel call" do
request_body = JSON.parse(json_from_file("mocks/requests/Checkout/generic_cancel.json"))

response_body = json_from_file("mocks/responses/Checkout/generic_cancel.json")

url = @shared_values[:client].service_url(@shared_values[:service], "cancels", @shared_values[:client].checkout.version)
WebMock.stub_request(:post, url).
with(
body: request_body,
headers: {
"x-api-key" => @shared_values[:client].api_key
}
)
.to_return(body: response_body, status: 201)

result = @shared_values[:client].checkout.modifications.genericCancel(request_body)
response_hash = result.response

expect(result.status).
to eq(201)
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.reference).
to eq("123456789")
expect(response_hash.pspReference).
to eq("12345")
end

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

Expand Down
27 changes: 27 additions & 0 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,31 @@
expect(client.service_url_base("Payment")).
to eq("https://abcdef1234567890-TestCompany-pal-live.adyenpayments.com/pal/servlet")
end

it "generates a new set of ConnectionOptions when none are provided" do
expect(Faraday::ConnectionOptions).to receive(:new).and_call_original
client = Adyen::Client.new(env: :test)
end

it "uses the ConnectionOptions provided" do
connection_options = Faraday::ConnectionOptions.new
expect(Faraday::ConnectionOptions).not_to receive(:new)
client = Adyen::Client.new(env: :test, connection_options: connection_options)
end

it "initiates a Faraday connection with the provided options" do
connection_options = Faraday::ConnectionOptions.new
expect(Faraday::ConnectionOptions).not_to receive(:new)
client = Adyen::Client.new(api_key: "api_key", env: :mock, connection_options: connection_options)

mock_faraday_connection = double(Faraday::Connection)
url = client.service_url(@shared_values[:service], "payments/details", client.checkout.version)
request_body = JSON.parse(json_from_file("mocks/requests/Checkout/payment-details.json"))
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(mock_faraday_connection).to receive(:post).and_return(mock_response)
client.checkout.payments.details(request_body)
end
end
22 changes: 22 additions & 0 deletions spec/mocks/requests/Checkout/amount_updates.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"amount": {
"currency": "str",
"value": 0
},
"merchantAccount": "TestMerchant",
"reason": "delayedCharge",
"reference": "123456789",
"splits": [
{
"account": "string",
"amount": {
"currency": "str",
"value": 0
},
"description": "string",
"reference": "string",
"type": "BalanceAccount"
}
]
}

34 changes: 34 additions & 0 deletions spec/mocks/requests/Checkout/capture.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"amount": {
"currency": "str",
"value": 0
},
"lineItems": [
{
"amountExcludingTax": 0,
"amountIncludingTax": 0,
"description": "string",
"id": "string",
"imageUrl": "string",
"itemCategory": "string",
"productUrl": "string",
"quantity": 0,
"taxAmount": 0,
"taxPercentage": 0
}
],
"merchantAccount": "string",
"reference": "string",
"splits": [
{
"account": "string",
"amount": {
"currency": "str",
"value": 0
},
"description": "string",
"reference": "string",
"type": "BalanceAccount"
}
]
}
Loading