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
21 changes: 21 additions & 0 deletions app/controllers/api/v1/customers/payment_methods_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,32 @@ def index
payment_method_index(external_customer_id: customer.external_id)
end

def set_as_default
payment_method = customer.payment_methods.find_by(id: params[:id])
return not_found_error(resource: "payment_method") unless payment_method

result = PaymentMethods::SetAsDefaultService.call(payment_method:)
if result.success?
render_payment_method(result.payment_method)
else
render_error_response(result)
end
end

private

def resource_name
"payment_method"
end

def render_payment_method(payment_method)
render(
json: ::V1::PaymentMethodSerializer.new(
payment_method,
root_name: "payment_method"
)
)
end
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@
resources :credit_notes, only: %i[index]
resources :invoices, only: %i[index]
resources :payments, only: %i[index]
resources :payment_methods, only: %i[index]
resources :payment_requests, only: %i[index]
resources :subscriptions, only: %i[index]
resources :wallets, only: %i[index]
resources :payment_methods, only: %i[index] do
put :set_as_default, on: :member
end
end
end

Expand Down
83 changes: 79 additions & 4 deletions spec/requests/api/v1/customers/payment_methods_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
require "rails_helper"

RSpec.describe Api::V1::Customers::PaymentMethodsController, type: :request do
let(:customer) { create(:customer, organization:) }
let(:organization) { create(:organization) }
let(:external_id) { customer.external_id }
let(:payment_method) { create(:payment_method, customer:, organization:) }

describe "GET /api/v1/customers/:external_id/payment_methods" do
subject { get_with_token(organization, "/api/v1/customers/#{external_id}/payment_methods", {}) }

let(:customer) { create(:customer, organization:) }
let(:organization) { create(:organization) }
let(:external_id) { customer.external_id }
let(:payment_method) { create(:payment_method, customer:, organization:) }
let(:second_payment_method) { create(:payment_method, organization:, customer:, is_default: false) }

include_examples "requires API permission", "payment_method", "read"
Expand Down Expand Up @@ -54,4 +55,78 @@
end
end
end

describe "PUT /api/v1/customers/:external_id/payment_methods/:id/set_as_default" do
subject { put_with_token(organization, "/api/v1/customers/#{external_id}/payment_methods/#{payment_method.id}/set_as_default") }

include_examples "requires API permission", "payment_method", "write"

context "with unknown customer" do
let(:external_id) { SecureRandom.uuid }

it "returns a not found error" do
subject

expect(response).to have_http_status(:not_found)
expect(json[:code]).to eq("customer_not_found")
end
end

context "with unknown payment method" do
subject { put_with_token(organization, "/api/v1/customers/#{external_id}/payment_methods/invalid/set_as_default") }

it "returns a not found error" do
subject

expect(response).to have_http_status(:not_found)
expect(json[:code]).to eq("payment_method_not_found")
end
end

context "when payment method is already default" do
let(:payment_method) { create(:payment_method, customer:, organization:, is_default: true) }
let(:payment_method2) { create(:payment_method, customer:, organization:, is_default: false) }
let(:payment_method3) { create(:payment_method, customer:, organization:, is_default: false) }

before do
payment_method
payment_method2
payment_method3
end

it "returns valid payment method" do
subject

expect(response).to have_http_status(:success)
expect(json[:payment_method][:lago_id]).to eq(payment_method.id)
expect(json[:payment_method][:is_default]).to eq(true)
expect(payment_method.reload.is_default).to eq(true)
expect(payment_method2.reload.is_default).to eq(false)
expect(payment_method2.reload.is_default).to eq(false)
end
end

context "when payment method is not default" do
let(:payment_method) { create(:payment_method, customer:, organization:, is_default: false) }
let(:payment_method2) { create(:payment_method, customer:, organization:, is_default: true) }
let(:payment_method3) { create(:payment_method, customer:, organization:, is_default: false) }

before do
payment_method
payment_method2
payment_method3
end

it "sets payment method to default and returns valid payment method" do
subject

expect(response).to have_http_status(:success)
expect(json[:payment_method][:lago_id]).to eq(payment_method.id)
expect(json[:payment_method][:is_default]).to eq(true)
expect(payment_method.reload.is_default).to eq(true)
expect(payment_method2.reload.is_default).to eq(false)
expect(payment_method2.reload.is_default).to eq(false)
end
end
end
end
Loading