From 5852708f794df6d3b4436b976def07c24c3757cf Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Mon, 18 Nov 2024 15:09:52 +1100 Subject: [PATCH] Use the voucher adjustment amount for redeeming --- app/services/vine/voucher_redeemer_service.rb | 13 +++++++------ spec/services/vine/voucher_redeemer_service_spec.rb | 5 +++-- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/app/services/vine/voucher_redeemer_service.rb b/app/services/vine/voucher_redeemer_service.rb index c9e0fa27ce7..45aaa71244e 100644 --- a/app/services/vine/voucher_redeemer_service.rb +++ b/app/services/vine/voucher_redeemer_service.rb @@ -11,16 +11,16 @@ def initialize(order: ) def redeem # Do nothing if we don't have a vine voucher added to the order - voucher_adjustment = order.voucher_adjustments.first - @voucher = voucher_adjustment&.originator + @voucher_adjustment = order.voucher_adjustments.first + @voucher = @voucher_adjustment&.originator - return true if voucher_adjustment.nil? || !@voucher.is_a?(Vouchers::Vine) + return true if @voucher_adjustment.nil? || !@voucher.is_a?(Vouchers::Vine) return false if vine_settings.nil? call_vine_api - voucher_adjustment.close + @voucher_adjustment.close true rescue Faraday::ClientError => e @@ -44,9 +44,10 @@ def call_vine_api jwt_service = Vine::JwtService.new(secret: vine_settings["secret"]) vine_api = Vine::ApiService.new(api_key: vine_settings["api_key"], jwt_generator: jwt_service) - # Voucher amount is stored in dollars, VINE expect cents + # Voucher adjustment amount is stored in dollars and negative, VINE expect cents + amount = -1 * @voucher_adjustment.amount * 100 vine_api.voucher_redemptions( - @voucher.external_voucher_id, @voucher.external_voucher_set_id, (@voucher.amount * 100) + @voucher.external_voucher_id, @voucher.external_voucher_set_id, amount ) end diff --git a/spec/services/vine/voucher_redeemer_service_spec.rb b/spec/services/vine/voucher_redeemer_service_spec.rb index 4152e3a4b6c..3b12ad8d590 100644 --- a/spec/services/vine/voucher_redeemer_service_spec.rb +++ b/spec/services/vine/voucher_redeemer_service_spec.rb @@ -12,7 +12,7 @@ let(:vine_voucher) { create(:vine_voucher, code: 'some_code', enterprise: distributor, - amount: 6, external_voucher_id: voucher_id, + amount: 50, external_voucher_id: voucher_id, external_voucher_set_id: voucher_set_id ) } let(:voucher_id) { "9d316d27-0dad-411a-8953-316a1aaf7742" } @@ -56,8 +56,9 @@ before { add_voucher(vine_voucher) } it "redeems the voucher with VINE" do + # Order pre discount total is $10, so we expect to redeen 1000 cents expect(vine_api_service).to receive(:voucher_redemptions) - .with(voucher_id, voucher_set_id, 600) + .with(voucher_id, voucher_set_id, 1000) .and_return(mock_api_response(data:)) voucher_redeemer_service.redeem