Skip to content

Commit

Permalink
Use the voucher adjustment amount for redeeming
Browse files Browse the repository at this point in the history
  • Loading branch information
rioug authored and mkllnk committed Nov 26, 2024
1 parent cbb11de commit 5852708
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
13 changes: 7 additions & 6 deletions app/services/vine/voucher_redeemer_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
5 changes: 3 additions & 2 deletions spec/services/vine/voucher_redeemer_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5852708

Please sign in to comment.