diff --git a/CHANGELOG.md b/CHANGELOG.md index 1dfc55238..cfdbb36e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Keep usage history of prepaid packs - OpenAPI reservation endpoint can be filtered by date - OpenAPI users endpoint now returns the ID of the InvoicingProfile +- Fix a bug: privileged users cannot order free carts for themselves in the store - Fix a bug: wrong counting of minutes used when using a prepaid pack - Fix a bug: empty advanced accounting code is not defaulted to the general setting - Fix a bug: invalid style in accounting codes settings diff --git a/app/services/checkout/payment_service.rb b/app/services/checkout/payment_service.rb index 5f8c12615..96a5df497 100644 --- a/app/services/checkout/payment_service.rb +++ b/app/services/checkout/payment_service.rb @@ -17,7 +17,7 @@ class Checkout::PaymentService CouponService.new.validate(coupon_code, order.statistic_profile.user.id) - amount = debit_amount(order) + amount = debit_amount(order, coupon_code) if (operator.privileged? && operator != order.statistic_profile.user) || amount.zero? Payments::LocalService.new.payment(order, coupon_code) elsif Stripe::Helper.enabled? && payment_id.present? diff --git a/test/fixtures/coupons.yml b/test/fixtures/coupons.yml index a483c2fb5..b1f1409d2 100644 --- a/test/fixtures/coupons.yml +++ b/test/fixtures/coupons.yml @@ -47,3 +47,11 @@ twentyp: updated_at: '2021-06-18 14:53:54.770895' validity_per_user: forever amount_off: +internal: + name: Internal use + code: INTERNCOUP100 + percent_off: 100 + valid_until: + max_usages: + active: true + validity_per_user: forever diff --git a/test/integration/store/admin_order_for_himself_test.rb b/test/integration/store/admin_order_for_himself_test.rb index fdb9223de..81e02908a 100644 --- a/test/integration/store/admin_order_for_himself_test.rb +++ b/test/integration/store/admin_order_for_himself_test.rb @@ -206,4 +206,51 @@ class Store::AdminOrderForHimselfTest < ActionDispatch::IntegrationTest assert_equal 403, response.status end + + test 'admin pay a free order with success' do + login_as(@admin, scope: :user) + + invoice_count = Invoice.count + invoice_items_count = InvoiceItem.count + + post '/api/checkout/payment', + params: { + coupon_code: 'INTERNCOUP100', + order_token: @cart1.token, + customer_id: @admin.id + }.to_json, headers: default_headers + + @cart1.reload + + # general assertions + assert_equal 200, response.status + assert_equal invoice_count + 1, Invoice.count + assert_equal invoice_items_count + 2, InvoiceItem.count + + # invoice_items assertions + invoice_item = InvoiceItem.last + + assert invoice_item.check_footprint + + # invoice assertions + invoice = Invoice.last + assert_invoice_pdf invoice + assert_not_nil invoice.debug_footprint + + assert @cart1.payment_gateway_object.blank? + assert invoice.payment_gateway_object.blank? + assert invoice.total.zero? + assert invoice.check_footprint + + # notification + assert_not_empty Notification.where(attached_object: invoice) + + assert_equal 'paid', @cart1.state + assert_equal 'local', @cart1.payment_method + assert_equal 0, @cart1.paid_total + + activity = @cart1.order_activities.last + assert_equal 'paid', activity.activity_type + assert_equal @admin.invoicing_profile.id, activity.operator_profile_id + end end