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

Add Adyen HPP endpoint to Spree::API #94

Open
wants to merge 1 commit 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
28 changes: 28 additions & 0 deletions app/controllers/spree/api/adyen_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Spree
module Api
class AdyenController < Spree::Api::BaseController
before_action :find_order
around_action :lock_order
before_action :find_payment_method

def hpp
@brands = Spree::Adyen::HPP.payment_methods_from_directory(
@order,
@payment_method)

render json: @brands
end

private

def find_order
@order = Spree::Order.find_by(number: order_id)
authorize! :read, @order, order_token
end

def find_payment_method
@payment_method = Spree::PaymentMethod.find_by(id: params[:payment_method_id])
end
end
end
end
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
end
end

namespace :api, defaults: { format: 'json' } do
get "/orders/:order_id/payment_methods/:payment_method_id/adyen", to: "adyen#hpp"
end

get "checkout/payment/adyen", to: "adyen_redirect#confirm", as: :adyen_confirmation
post "adyen/notify", to: "adyen_notifications#notify"
post "adyen/authorise3d", to: "adyen_redirect#authorise3d", as: :adyen_authorise3d
Expand Down
43 changes: 43 additions & 0 deletions spec/controllers/spree/api/adyen_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require "spec_helper"

RSpec.describe Spree::Api::AdyenController, type: :controller do
render_views

let(:order) { create :order }
let(:payment_method) { create :hpp_gateway }
let(:parsed_directory_response) { [{
name: "American Express",
brandCode: "amex",
payment_url: "www.test-payment-url.com/amex"}] }

let(:params) do
{ order_id: order.to_param,
payment_method_id: payment_method.id }
end

before do
allow_any_instance_of(Spree::Ability).to receive(:can?).and_return(true)

stub_authentication!

allow(Spree::Order).to receive(:find_by!).
with(number: order.number).
and_return(order)

allow(Spree::Adyen::HPP).to receive(:payment_methods_from_directory).
with(order, payment_method).
and_return(parsed_directory_response)
end

subject { get action, params }

context "hpp" do
let(:action) { "hpp" }

before { subject }

it { expect(response.status).to eq(200) }

it { expect(response.body).to include("www.test-payment-url.com/amex") }
end
end
5 changes: 5 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
require "spree/testing_support/controller_requests"
require "spree/testing_support/url_helpers"
require "spree/testing_support/authorization_helpers"
require "spree/api/testing_support/helpers"
require "spree/api/testing_support/setup"


# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Expand All @@ -54,6 +57,8 @@
config.include FactoryGirl::Syntax::Methods
config.include Spree::TestingSupport::ControllerRequests, type: :controller
config.include Spree::TestingSupport::UrlHelpers
config.include Spree::Api::TestingSupport::Helpers, type: :controller
config.include Spree::Api::TestingSupport::Setup, type: :controller

config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
Expand Down