Skip to content

Commit

Permalink
Fix URI::InvalidURIError in client.customers.checkout_url (#168)
Browse files Browse the repository at this point in the history
* fix checkout url bug

* add test

* modify return object
  • Loading branch information
tnlong311 authored Dec 28, 2023
1 parent 48248a6 commit f1c35ad
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/lago/api/resources/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ def checkout_url(external_customer_id)
"#{client.base_api_url}#{api_resource}/#{external_customer_id}/checkout_url",
)

response = connection.post(uri, identifier: nil)[root_name]
response = connection.post(uri)[root_name]

JSON.parse(response.to_json, object_class: OpenStruct).checkout_url
JSON.parse(response.to_json, object_class: OpenStruct)
end

def whitelist_params(params)
Expand Down
52 changes: 52 additions & 0 deletions spec/lago/api/resources/customer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,56 @@
end
end
end

describe '#checkout_url' do
let(:response_body) do
{
"customer": {
"lago_customer_id": customer_external_id,
"external_customer_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
"payment_provider": "stripe",
"checkout_url": "https://checkout.stripe.com/c/pay/foobar"
}
}.to_json
end

context 'when the customer exists' do
before do
# NOTE: Api makes POST to /customers endpoint first
# stub_request(:post, "https://api.getlago.com/api/v1/customers/#{customer_external_id}/checkout_url")
# .to_return(body: response_body, status: 200)

stub_request(:post, 'https://api.getlago.com/api/v1/customers')
.to_return(body: response_body, status: 200)
end

it 'returns the checkout URL' do
response = resource.checkout_url(customer_external_id)

expect(response.checkout_url).to eq('https://checkout.stripe.com/c/pay/foobar')
expect(response.lago_customer_id).to eq(customer_external_id)
expect(response.external_customer_id).to eq("1a901a90-1a90-1a90-1a90-1a901a901a90")
expect(response.payment_provider).to eq("stripe")
end
end

context 'when the customer does not exists' do
let(:customer_external_id) { 'DOESNOTEXIST' }

before do
# NOTE: Api makes POST to /customers endpoint first
# stub_request(:post, "https://api.getlago.com/api/v1/customers/#{customer_external_id}/checkout_url")
# .to_return(body: JSON.generate(status: 404, error: 'Not Found'), status: 404)

stub_request(:post, 'https://api.getlago.com/api/v1/customers')
.to_return(body: JSON.generate(status: 404, error: 'Not Found'), status: 404)
end

it 'raises an error' do
expect do
resource.checkout_url(customer_external_id)
end.to raise_error(Lago::Api::HttpError)
end
end
end
end

0 comments on commit f1c35ad

Please sign in to comment.