From b864cbef210231eebf5930aca69c32195109f2d3 Mon Sep 17 00:00:00 2001 From: James Ferguson Date: Thu, 26 Nov 2026 13:10:17 +1100 Subject: [PATCH 1/6] fix: send one string-keyed payload from Save#save - An object's attributes are string-keyed (they come from the API's JSON) but callers pass symbols, so web_hook.update(name: 'x') built a payload with both "name" and :name. json 2.x sent both keys; json 3 raises JSON::GeneratorError, so update failed outright for merchants on json 3. - Normalising both sides to strings makes the caller's value replace the old one, and lets the existing delete('id') catch a symbol :id too. Co-Authored-By: Claude Opus 5.5 --- lib/fat_zebra/api_operation/save.rb | 2 +- spec/lib/fat_zebra/web_hook_spec.rb | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/fat_zebra/api_operation/save.rb b/lib/fat_zebra/api_operation/save.rb index cd3dc5f..950193e 100644 --- a/lib/fat_zebra/api_operation/save.rb +++ b/lib/fat_zebra/api_operation/save.rb @@ -50,7 +50,7 @@ def save(params = {}, options = {}) path = singleton_methods.include?(:id) ? "#{resource_path}/#{id}" : resource_path method = singleton_methods.include?(:id) ? :put : :post - params_for_save = to_hash.merge(params) + params_for_save = to_hash.transform_keys(&:to_s).merge(params.transform_keys(&:to_s)) # Remove the id from the params to save, it should not be updated. params_for_save.delete('id') diff --git a/spec/lib/fat_zebra/web_hook_spec.rb b/spec/lib/fat_zebra/web_hook_spec.rb index 7560262..7ba91a6 100644 --- a/spec/lib/fat_zebra/web_hook_spec.rb +++ b/spec/lib/fat_zebra/web_hook_spec.rb @@ -47,4 +47,22 @@ it { is_expected.to be_accepted } end + describe '#update payload' do + subject(:web_hook) { FatZebra::WebHook.initialize_from(api_response('test hook')) } + + def api_response(name) + { 'successful' => true, 'response' => { 'id' => '071-WH-1', 'name' => name, 'mode' => 'Test' } } + end + + before do + allow(FatZebra::WebHook).to receive(:request).and_return(api_response('New Test hook')) + web_hook.update(name: 'New Test hook', id: '071-WH-2') + end + + it 'sends each attribute once with string keys, the new value and no id' do + expect(FatZebra::WebHook).to have_received(:request) + .with(:put, anything, { 'name' => 'New Test hook', 'mode' => 'Test' }, {}) + end + end + end From 1f215c1a074e5d9aa83bcb60f538f874d05c1735 Mon Sep 17 00:00:00 2001 From: James Ferguson Date: Thu, 26 Nov 2026 13:10:39 +1100 Subject: [PATCH 2/6] fix: anchor both words in the boolean validator - \A and \z bound only one alternative each, so /\Atrue|false\z/ accepted anything starting with "true" or ending in "false". - Only Batch.create uses it, and it always sets multipart to true, so no merchant request changes. Co-Authored-By: Claude Opus 5.5 --- lib/fat_zebra/validation.rb | 2 +- spec/lib/fat_zebra/validation_spec.rb | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/fat_zebra/validation.rb b/lib/fat_zebra/validation.rb index 2dc9853..21fb9f9 100644 --- a/lib/fat_zebra/validation.rb +++ b/lib/fat_zebra/validation.rb @@ -56,7 +56,7 @@ def validate_type(field, options, params) when :file_type /\A\# Date: Thu, 26 Nov 2026 13:10:51 +1100 Subject: [PATCH 3/6] build: require Ruby 3.3 or later - CI stopped testing Ruby 3.2 in February and 3.2 went end of life in March 2026, but the gemspec still allowed it, so Bundler would install releases on 3.2 that nobody had run there. - Merchants still on 3.2 keep resolving to the last release that allowed it. - rubocop's target follows the floor. Co-Authored-By: Claude Opus 5.5 --- .rubocop.yml | 2 +- fat_zebra.gemspec | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index b75ffb3..3e9e13f 100755 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,7 +1,7 @@ AllCops: NewCops: enable SuggestExtensions: false - TargetRubyVersion: 3.2 + TargetRubyVersion: 3.3 Exclude: - vendor/**/* - spec/**/* diff --git a/fat_zebra.gemspec b/fat_zebra.gemspec index 492e70e..80b24d4 100644 --- a/fat_zebra.gemspec +++ b/fat_zebra.gemspec @@ -21,7 +21,7 @@ Gem::Specification.new do |s| s.files = Dir.glob('lib/**/*') + %w[README.md fat_zebra.gemspec] s.require_paths = ['lib'] - s.required_ruby_version = '>= 3.2' + s.required_ruby_version = '>= 3.3' s.add_development_dependency 'byebug' s.add_development_dependency 'rake' From b592035146e270e770032349c407d9fc45939d0e Mon Sep 17 00:00:00 2001 From: James Ferguson Date: Thu, 26 Nov 2026 13:10:57 +1100 Subject: [PATCH 4/6] ci: drop an unused env var from the release job - The base-ref check set JSON_ENV_VARS from inputs.credentials_json, but the workflow has no inputs (it runs on push) and the step never reads it. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/ci.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2bbb2fe..cf7afe0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -54,8 +54,6 @@ jobs: - name: Validate Base Ref id: validate_base_ref - env: - JSON_ENV_VARS: ${{ inputs.credentials_json }} shell: bash run: | if [ "${{ github.event.base_ref }}" != "refs/heads/main" ]; then From ab20286a815868cf3a9089f452ed270e3ae14dbe Mon Sep 17 00:00:00 2001 From: James Ferguson Date: Tue, 1 Dec 2026 11:38:14 +1100 Subject: [PATCH 5/6] fix: keep the caller's keys as given in Save#save Stringifying every key fixed the "name"/:name duplicate, but made new ones: in test mode, save(test: true) sent both "test" and the :test that APIResource.request adds, which json 3 refuses to generate, and a multipart save lost the symbol :multipart, :file and :filename keys Request#post and the multipart encoder read. - Save#save now drops the old value of any attribute the caller sets, whatever its key type, and sends the caller's keys unchanged. - The id is still never sent, as "id" or :id. Co-Authored-By: Claude Opus 5.5 --- lib/fat_zebra/api_operation/save.rb | 9 +++++---- spec/lib/fat_zebra/web_hook_spec.rb | 26 ++++++++++++++++++++------ 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/lib/fat_zebra/api_operation/save.rb b/lib/fat_zebra/api_operation/save.rb index 950193e..41c5462 100644 --- a/lib/fat_zebra/api_operation/save.rb +++ b/lib/fat_zebra/api_operation/save.rb @@ -50,10 +50,11 @@ def save(params = {}, options = {}) path = singleton_methods.include?(:id) ? "#{resource_path}/#{id}" : resource_path method = singleton_methods.include?(:id) ? :put : :post - params_for_save = to_hash.transform_keys(&:to_s).merge(params.transform_keys(&:to_s)) - - # Remove the id from the params to save, it should not be updated. - params_for_save.delete('id') + # Drop the old value of anything the caller sets, whatever the key's type, so each + # attribute is sent once. The id is never sent: it can't be updated. + replaced = params.keys.map(&:to_s) << 'id' + params_for_save = to_hash.reject { |key, _| replaced.include?(key.to_s) } + .merge(params.reject { |key, _| key.to_s == 'id' }) response = request(method, path, params_for_save, options) update_from(response) diff --git a/spec/lib/fat_zebra/web_hook_spec.rb b/spec/lib/fat_zebra/web_hook_spec.rb index 7ba91a6..bee3ced 100644 --- a/spec/lib/fat_zebra/web_hook_spec.rb +++ b/spec/lib/fat_zebra/web_hook_spec.rb @@ -54,14 +54,28 @@ def api_response(name) { 'successful' => true, 'response' => { 'id' => '071-WH-1', 'name' => name, 'mode' => 'Test' } } end - before do - allow(FatZebra::WebHook).to receive(:request).and_return(api_response('New Test hook')) - web_hook.update(name: 'New Test hook', id: '071-WH-2') + context 'with a new value for an existing attribute and an id' do + before do + allow(FatZebra::WebHook).to receive(:request).and_return(api_response('New Test hook')) + web_hook.update(name: 'New Test hook', id: '071-WH-2') + end + + it 'sends each attribute once, with the new value and no id' do + expect(FatZebra::WebHook).to have_received(:request) + .with(:put, anything, { name: 'New Test hook', 'mode' => 'Test' }, {}) + end end - it 'sends each attribute once with string keys, the new value and no id' do - expect(FatZebra::WebHook).to have_received(:request) - .with(:put, anything, { 'name' => 'New Test hook', 'mode' => 'Test' }, {}) + context 'with test passed in test mode' do + before do + allow(FatZebra::Request).to receive(:execute).and_return(double(body: api_response('New Test hook'))) + web_hook.update(name: 'New Test hook', test: true) + end + + it 'sends test once' do + expect(FatZebra::Request).to have_received(:execute) + .with(hash_including(payload: { name: 'New Test hook', 'mode' => 'Test', test: true })) + end end end From 633deae93ba3e2e513fdf0e098561f3a54072fc0 Mon Sep 17 00:00:00 2001 From: James Ferguson Date: Tue, 1 Dec 2026 12:52:29 +1100 Subject: [PATCH 6/6] fix: send each attribute once when the caller repeats a key json 3 still raised when one name arrived as both a string and a symbol inside the caller's own params, not just across stored and new values. PaymentPlan#suspend! and #active! merge a symbol default into the caller's hash, so suspend!('new_status' => 'Cancelled') sent both, and in test mode a caller's string 'test' sat beside the :test that APIResource.request adds. - Save#save keeps one value per name across the stored attributes and the caller's params, the last one given, under its own key type, so multipart's symbol keys still reach Request#post. - Test mode replaces a string 'test' rather than adding beside it. Co-Authored-By: Claude Opus 5.5 --- lib/fat_zebra/api_operation/save.rb | 11 ++++++----- lib/fat_zebra/api_resource.rb | 5 ++++- spec/lib/fat_zebra/payment_plan_spec.rb | 16 ++++++++++++++++ spec/lib/fat_zebra/web_hook_spec.rb | 24 ++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 6 deletions(-) diff --git a/lib/fat_zebra/api_operation/save.rb b/lib/fat_zebra/api_operation/save.rb index 41c5462..c072510 100644 --- a/lib/fat_zebra/api_operation/save.rb +++ b/lib/fat_zebra/api_operation/save.rb @@ -50,11 +50,12 @@ def save(params = {}, options = {}) path = singleton_methods.include?(:id) ? "#{resource_path}/#{id}" : resource_path method = singleton_methods.include?(:id) ? :put : :post - # Drop the old value of anything the caller sets, whatever the key's type, so each - # attribute is sent once. The id is never sent: it can't be updated. - replaced = params.keys.map(&:to_s) << 'id' - params_for_save = to_hash.reject { |key, _| replaced.include?(key.to_s) } - .merge(params.reject { |key, _| key.to_s == 'id' }) + # Send each attribute once, whether it's keyed by a string or a symbol: the caller's + # value over the stored one, and the last of any the caller repeats, under that key. + # The id is never sent: it can't be updated. + params_for_save = [*to_hash, *params].reject { |key, _| key.to_s == 'id' } + .group_by { |key, _| key.to_s } + .to_h { |_, pairs| pairs.last } response = request(method, path, params_for_save, options) update_from(response) diff --git a/lib/fat_zebra/api_resource.rb b/lib/fat_zebra/api_resource.rb index e50b7bf..1bf7539 100644 --- a/lib/fat_zebra/api_resource.rb +++ b/lib/fat_zebra/api_resource.rb @@ -26,7 +26,10 @@ def base_path # @raise [FatZebra::RequestError] if the request is invalid # rubocop:disable-next Metrics/AbcSize def request(method, path, payload = {}, options = {}) - payload[:test] = true if configurations.test_mode + if configurations.test_mode + payload.delete('test') + payload[:test] = true + end payload = Util.format_dates_in_hash(payload) url_params = Util.encode_parameters(payload) if method == :get uri = build_endpoint_url(configurations.gateway, path, url_params, http_secure: configurations.http_secure) diff --git a/spec/lib/fat_zebra/payment_plan_spec.rb b/spec/lib/fat_zebra/payment_plan_spec.rb index a706a54..7498ee9 100644 --- a/spec/lib/fat_zebra/payment_plan_spec.rb +++ b/spec/lib/fat_zebra/payment_plan_spec.rb @@ -114,4 +114,20 @@ it { expect(payment_plan).to be_a(FatZebra::PaymentPlan) } end + describe '#suspend! payload' do + subject(:payment_plan) do + FatZebra::PaymentPlan.initialize_from('successful' => true, 'response' => { 'id' => '071-PP-1', 'status' => 'Active' }) + end + + before do + allow(FatZebra::PaymentPlan).to receive(:request).and_return('successful' => true, 'response' => {}) + payment_plan.suspend!('new_status' => 'Cancelled') + end + + it "sends the caller's new_status in place of the default" do + expect(FatZebra::PaymentPlan).to have_received(:request) + .with(:put, anything, { 'status' => 'Active', 'new_status' => 'Cancelled' }, {}) + end + end + end diff --git a/spec/lib/fat_zebra/web_hook_spec.rb b/spec/lib/fat_zebra/web_hook_spec.rb index bee3ced..40e0903 100644 --- a/spec/lib/fat_zebra/web_hook_spec.rb +++ b/spec/lib/fat_zebra/web_hook_spec.rb @@ -77,6 +77,30 @@ def api_response(name) .with(hash_including(payload: { name: 'New Test hook', 'mode' => 'Test', test: true })) end end + + context 'with test passed as a string in test mode' do + before do + allow(FatZebra::Request).to receive(:execute).and_return(double(body: api_response('New Test hook'))) + web_hook.update(name: 'New Test hook', 'test' => true) + end + + it 'sends test once' do + expect(FatZebra::Request).to have_received(:execute) + .with(hash_including(payload: { name: 'New Test hook', 'mode' => 'Test', test: true })) + end + end + + context 'with one attribute passed as both a symbol and a string' do + before do + allow(FatZebra::WebHook).to receive(:request).and_return(api_response('Second name')) + web_hook.update(:name => 'First name', 'name' => 'Second name') + end + + it 'sends it once, with the last value' do + expect(FatZebra::WebHook).to have_received(:request) + .with(:put, anything, { 'name' => 'Second name', 'mode' => 'Test' }, {}) + end + end end end