diff --git a/CHANGELOG.md b/CHANGELOG.md index 9308be149..242adc004 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Changelog Fab Manager - Fix a bug: Users with role 'member' cannot download their invoices +- [TODO DEPLOY] `rake db:migrate` ## v4.0.4 2019 August 14 - Fix a bug: #140 VAT rate is erronous in invoices. diff --git a/app/assets/javascripts/controllers/events.js.erb b/app/assets/javascripts/controllers/events.js.erb index dc43d3e2a..70b8cfdd2 100644 --- a/app/assets/javascripts/controllers/events.js.erb +++ b/app/assets/javascripts/controllers/events.js.erb @@ -617,16 +617,22 @@ Application.Controllers.controller('ShowEventController', ['$scope', '$state', ' }, coupon () { return $scope.coupon.applied; + }, + cartItems () { + return mkRequestParams(reservation, $scope.coupon.applied); } }, - controller: ['$scope', '$uibModalInstance', '$state', 'reservation', 'price', 'cgv', 'Auth', 'Reservation', 'growl', 'wallet', 'helpers', '$filter', 'coupon', - function ($scope, $uibModalInstance, $state, reservation, price, cgv, Auth, Reservation, growl, wallet, helpers, $filter, coupon) { + controller: ['$scope', '$uibModalInstance', '$state', 'reservation', 'price', 'cgv', 'Auth', 'Reservation', 'growl', 'wallet', 'helpers', '$filter', 'coupon', 'cartItems', + function ($scope, $uibModalInstance, $state, reservation, price, cgv, Auth, Reservation, growl, wallet, helpers, $filter, coupon, cartItems) { // User's wallet amount $scope.walletAmount = wallet.amount; // Price $scope.amount = helpers.getAmountToPay(price.price, wallet.amount); + // Cart items + $scope.cartItems = cartItems; + // CGV $scope.cgv = cgv.custom_asset; @@ -636,23 +642,9 @@ Application.Controllers.controller('ShowEventController', ['$scope', '$state', ' // Used in wallet info template to interpolate some translations $scope.numberFilter = $filter('number'); - // Callback for the stripe payment authorization - return $scope.payment = function (status, response) { - if (response.error) { - return growl.error(response.error.message); - } else { - $scope.attempting = true; - $scope.reservation.card_token = response.id; - Reservation.save(mkRequestParams($scope.reservation, coupon), function (reservation) { $uibModalInstance.close(reservation); } - , function (response) { - $scope.alerts = []; - $scope.alerts.push({ - msg: response.data.card[0], - type: 'danger' - }); - return $scope.attempting = false; - }); - } + // Callback to handle the post-payment and reservation + return $scope.onPaymentSuccess = function (reservation) { + $uibModalInstance.close(reservation); }; } ] diff --git a/app/assets/javascripts/directives/cart.js.erb b/app/assets/javascripts/directives/cart.js.erb index 69089cb7f..f34e3a289 100644 --- a/app/assets/javascripts/directives/cart.js.erb +++ b/app/assets/javascripts/directives/cart.js.erb @@ -487,7 +487,7 @@ Application.Directives.directive('cart', [ '$rootScope', '$uibModal', 'dialogs', // Price $scope.amount = helpers.getAmountToPay(price.price, wallet.amount); - // cartItems + // Cart items $scope.cartItems = cartItems; // CGV @@ -500,7 +500,7 @@ Application.Directives.directive('cart', [ '$rootScope', '$uibModal', 'dialogs', $scope.numberFilter = $filter('number'); /** - * Callback to process the payment with Stripe, triggered on button click + * Callback to handle the post-payment and reservation */ $scope.onPaymentSuccess = function (response) { $uibModalInstance.close(response); diff --git a/app/assets/javascripts/directives/stripe-form.js.erb b/app/assets/javascripts/directives/stripe-form.js.erb index 187c73318..e61416f2b 100644 --- a/app/assets/javascripts/directives/stripe-form.js.erb +++ b/app/assets/javascripts/directives/stripe-form.js.erb @@ -1,5 +1,11 @@ /* global Stripe */ +/** + * This directive allows to extend a form with the Stripe payment input and error handling area. + * Strong-customer authentication is supported. + * -- + * https://stripe.com/docs/payments/payment-intents/web-manual + */ Application.Directives.directive('stripeForm', ['Payment', 'growl', '_t', function (Payment, growl, _t) { return ({ @@ -48,7 +54,6 @@ Application.Directives.directive('stripeForm', ['Payment', 'growl', '_t', const button = form.find('button'); button.prop('disabled', true); - // TODO https://stripe.com/docs/payments/payment-intents/web-manual stripe.createPaymentMethod('card', card).then(function({ paymentMethod, error }) { if (error) { growl.error(error.message); @@ -58,14 +63,18 @@ Application.Directives.directive('stripeForm', ['Payment', 'growl', '_t', Payment.confirm({ payment_method_id: paymentMethod.id, cart_items: $scope.cartItems }, function (response) { // Handle server response (see Step 3) handleServerResponse(response, button); - }); + }, function(error) { handleServerResponse({ error }) }); } }); }); function handleServerResponse(response, confirmButton) { if (response.error) { - growl.error(`${_t('payment_card_error')} ${response.error}`); + if (response.error.statusText) { + growl.error(response.error.statusText); + } else { + growl.error(`${_t('payment_card_error')} ${response.error}`); + } confirmButton.prop('disabled', false); } else if (response.requires_action) { // Use Stripe.js to handle required card action @@ -80,7 +89,7 @@ Application.Directives.directive('stripeForm', ['Payment', 'growl', '_t', // The PaymentIntent can be confirmed again on the server Payment.confirm({ payment_intent_id: result.paymentIntent.id, cart_items: $scope.cartItems }, function(confirmResult) { handleServerResponse(confirmResult, confirmButton); - }); + }, function(error) { handleServerResponse({ error }) }); } }); } else { diff --git a/app/assets/templates/stripe/payment_modal.html.erb b/app/assets/templates/stripe/payment_modal.html.erb index 748279373..955750e79 100644 --- a/app/assets/templates/stripe/payment_modal.html.erb +++ b/app/assets/templates/stripe/payment_modal.html.erb @@ -7,7 +7,7 @@ {{alert.msg}}
-
+

diff --git a/app/controllers/api/payments_controller.rb b/app/controllers/api/payments_controller.rb index a2263b0a3..436090e7a 100644 --- a/app/controllers/api/payments_controller.rb +++ b/app/controllers/api/payments_controller.rb @@ -39,7 +39,8 @@ class API::PaymentsController < API::ApiController amount: total - wallet_debit, currency: Rails.application.secrets.stripe_currency, confirmation_method: 'manual', - confirm: true + confirm: true, + customer: current_user.stp_customer_id, ) elsif params[:payment_intent_id].present? intent = Stripe::PaymentIntent.confirm(params[:payment_intent_id]) @@ -49,19 +50,23 @@ class API::PaymentsController < API::ApiController render(status: 200, json: { error: e.message }) and return end - render(on_payment_success) and return if intent.status == 'succeeded' + render(on_payment_success(intent)) and return if intent.status == 'succeeded' render generate_payment_response(intent) end private - def on_payment_success + def on_payment_success(intent) # TODO create subscription is needed user_id = params[:cart_items][:reservation][:user_id] @reservation = Reservation.new(reservation_params) is_reserve = Reservations::Reserve.new(user_id, current_user.invoicing_profile.id) - .pay_and_save(@reservation, coupon_params[:coupon_code]) + .pay_and_save(@reservation, coupon: coupon_params[:coupon_code], payment_intent_id: intent.id) + Stripe::PaymentIntent.update( + intent.id, + description: "Invoice reference: #{@reservation.invoice.reference}" + ) if is_reserve SubscriptionExtensionAfterReservation.new(@reservation).extend_subscription_if_eligible diff --git a/app/controllers/api/reservations_controller.rb b/app/controllers/api/reservations_controller.rb index 9518883b3..9384586c7 100644 --- a/app/controllers/api/reservations_controller.rb +++ b/app/controllers/api/reservations_controller.rb @@ -28,7 +28,7 @@ class API::ReservationsController < API::ApiController @reservation = Reservation.new(reservation_params) is_reserve = Reservations::Reserve.new(user_id, current_user.invoicing_profile.id) - .pay_and_save(@reservation, coupon_params[:coupon_code]) + .pay_and_save(@reservation, coupon: coupon_params[:coupon_code]) if is_reserve SubscriptionExtensionAfterReservation.new(@reservation).extend_subscription_if_eligible diff --git a/app/doc/open_api/v1/invoices_doc.rb b/app/doc/open_api/v1/invoices_doc.rb index d6289a599..d9c966cfc 100644 --- a/app/doc/open_api/v1/invoices_doc.rb +++ b/app/doc/open_api/v1/invoices_doc.rb @@ -26,6 +26,7 @@ class OpenAPI::V1::InvoicesDoc < OpenAPI::V1::BaseDoc "user_id": 211, "invoiced_type": "Reservation", "stp_invoice_id": "in_187DLE4zBvgjueAZ6L7SyQlU", + "stp_payment_intent_id": "pi_1Dat4P2eZvKYlo2C3MxszwQp", "reference": "1605017/VL", "total": 1000, "type": null, @@ -41,6 +42,7 @@ class OpenAPI::V1::InvoicesDoc < OpenAPI::V1::BaseDoc "user_id": 211, "invoiced_type": "Reservation", "stp_invoice_id": "in_185Hmt4zBvgjueAZl5lio1pK", + "stp_payment_intent_id": "pi_2Dat4P2eYbKYlo2C3MxszwQp", "reference": "1604176/VL", "total": 2000, "type": null, @@ -56,6 +58,7 @@ class OpenAPI::V1::InvoicesDoc < OpenAPI::V1::BaseDoc "user_id": 211, "invoiced_type": "Reservation", "stp_invoice_id": "in_184oNK4zBvgjueAZJdOxHJjT", + "stp_payment_intent_id": "pi_1Pub4P2eZvKYlo2C3MxszwQm", "reference": "1604166/VL", "total": 2000, "type": null, diff --git a/app/models/invoice.rb b/app/models/invoice.rb index b052253ad..72e30084a 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -80,7 +80,7 @@ class Invoice < ActiveRecord::Base reference.gsub!(/DD(?![^\[]*\])/, Time.now.strftime('%-d')) # information about online selling (X[text]) - if stp_invoice_id + if paid_with_stripe? reference.gsub!(/X\[([^\]]+)\]/, '\1') else reference.gsub!(/X\[([^\]]+)\]/, ''.to_s) @@ -251,6 +251,10 @@ class Invoice < ActiveRecord::Base end end + def paid_with_stripe? + stp_payment_intent_id? || stp_invoice_id? + end + private def generate_and_send_invoice diff --git a/app/models/price.rb b/app/models/price.rb index 2cdc0e55c..6b131d1e0 100644 --- a/app/models/price.rb +++ b/app/models/price.rb @@ -134,7 +134,7 @@ class Price < ActiveRecord::Base total_amount = CouponService.new.apply(total_amount, coupon_code) # return result - { elements: all_elements, total: total_amount, before_coupon: _amount_no_coupon } + { elements: all_elements, total: total_amount.to_i, before_coupon: _amount_no_coupon.to_i } end diff --git a/app/models/reservation.rb b/app/models/reservation.rb index af0ba60ae..7cb3414b4 100644 --- a/app/models/reservation.rb +++ b/app/models/reservation.rb @@ -201,11 +201,12 @@ class Reservation < ActiveRecord::Base pending_invoice_items.each(&:delete) end - def save_with_payment(operator_profile_id, coupon_code = nil) + def save_with_payment(operator_profile_id, coupon_code = nil, payment_intent_id = nil) build_invoice( invoicing_profile: user.invoicing_profile, statistic_profile: user.statistic_profile, - operator_profile_id: operator_profile_id + operator_profile_id: operator_profile_id, + stp_payment_intent_id: payment_intent_id ) generate_invoice_items(true, coupon_code) diff --git a/app/pdfs/pdf/invoice.rb b/app/pdfs/pdf/invoice.rb index f15c152ae..0efde3216 100644 --- a/app/pdfs/pdf/invoice.rb +++ b/app/pdfs/pdf/invoice.rb @@ -306,7 +306,7 @@ class PDF::Invoice < Prawn::Document end # payment method - payment_verbose = if invoice.stp_invoice_id + payment_verbose = if invoice.paid_with_stripe? I18n.t('invoices.settlement_by_debit_card') else I18n.t('invoices.settlement_done_at_the_reception') diff --git a/app/services/reservations/reserve.rb b/app/services/reservations/reserve.rb index e1f3144eb..7c9cd9e98 100644 --- a/app/services/reservations/reserve.rb +++ b/app/services/reservations/reserve.rb @@ -9,8 +9,8 @@ class Reservations::Reserve @operator_profile_id = operator_profile_id end - def pay_and_save(reservation, coupon) + def pay_and_save(reservation, coupon: nil, payment_intent_id: nil) reservation.statistic_profile_id = StatisticProfile.find_by(user_id: user_id).id - reservation.save_with_payment(operator_profile_id, coupon) + reservation.save_with_payment(operator_profile_id, coupon, payment_intent_id) end end diff --git a/app/services/users_export_service.rb b/app/services/users_export_service.rb index 4273888e6..6225b9b7d 100644 --- a/app/services/users_export_service.rb +++ b/app/services/users_export_service.rb @@ -31,7 +31,7 @@ class UsersExportService # export reservations def export_reservations(export) - @reservations = Reservation.all.includes(:slots, :reservable, statistic_profile: [user: [:profile]]) + @reservations = Reservation.all.includes(:slots, :reservable, :invoice, statistic_profile: [user: [:profile]]) ActionController::Base.prepend_view_path './app/views/' # place data in view_assigns diff --git a/app/views/api/invoices/index.json.jbuilder b/app/views/api/invoices/index.json.jbuilder index eaa3db529..ffda598af 100644 --- a/app/views/api/invoices/index.json.jbuilder +++ b/app/views/api/invoices/index.json.jbuilder @@ -6,7 +6,7 @@ json.array!(@invoices) do |invoice| json.has_avoir invoice.refunded? json.is_avoir invoice.is_a?(Avoir) json.is_subscription_invoice invoice.subscription_invoice? - json.stripe invoice.stp_invoice_id? + json.stripe invoice.paid_with_stripe? json.date invoice.is_a?(Avoir) ? invoice.avoir_date : invoice.created_at json.prevent_refund invoice.prevent_refund? end diff --git a/app/views/api/invoices/list.json.jbuilder b/app/views/api/invoices/list.json.jbuilder index 97336dab1..60c9af411 100644 --- a/app/views/api/invoices/list.json.jbuilder +++ b/app/views/api/invoices/list.json.jbuilder @@ -12,7 +12,7 @@ json.array!(@invoices) do |invoice| json.has_avoir invoice.refunded? json.is_avoir invoice.is_a?(Avoir) json.is_subscription_invoice invoice.subscription_invoice? - json.stripe invoice.stp_invoice_id? + json.stripe invoice.paid_with_stripe? json.date invoice.is_a?(Avoir) ? invoice.avoir_date : invoice.created_at json.prevent_refund invoice.prevent_refund? json.chained_footprint invoice.check_footprint diff --git a/app/views/api/invoices/show.json.jbuilder b/app/views/api/invoices/show.json.jbuilder index 21b6bb972..a15a04046 100644 --- a/app/views/api/invoices/show.json.jbuilder +++ b/app/views/api/invoices/show.json.jbuilder @@ -7,7 +7,7 @@ json.name @invoice.user.profile.full_name json.has_avoir @invoice.refunded? json.is_avoir @invoice.is_a?(Avoir) json.is_subscription_invoice @invoice.subscription_invoice? -json.stripe @invoice.stp_invoice_id? +json.stripe @invoice.paid_with_stripe? json.date @invoice.is_a?(Avoir) ? @invoice.avoir_date : @invoice.created_at json.chained_footprint @invoice.check_footprint json.items @invoice.invoice_items do |item| diff --git a/app/views/archive/_accounting.json.jbuilder b/app/views/archive/_accounting.json.jbuilder index f0315858c..de1178eb7 100644 --- a/app/views/archive/_accounting.json.jbuilder +++ b/app/views/archive/_accounting.json.jbuilder @@ -2,7 +2,7 @@ json.invoices do json.array!(invoices) do |invoice| - json.extract! invoice[:invoice], :id, :stp_invoice_id, :created_at, :reference, :footprint + json.extract! invoice[:invoice], :id, :stp_invoice_id, :stp_payment_intent_id, :created_at, :reference, :footprint json.total number_to_currency(invoice[:invoice].total / 100.0) json.invoiced do json.type invoice[:invoice].invoiced_type diff --git a/app/views/exports/users_reservations.xlsx.axlsx b/app/views/exports/users_reservations.xlsx.axlsx index 98a56b8c4..2c0f8cadd 100644 --- a/app/views/exports/users_reservations.xlsx.axlsx +++ b/app/views/exports/users_reservations.xlsx.axlsx @@ -1,6 +1,6 @@ wb = xlsx_package.workbook -header = wb.styles.add_style :b => true, :bg_color => Stylesheet.primary.upcase.gsub('#', 'FF'), :fg_color => 'FFFFFFFF' +header = wb.styles.add_style b: true, :bg_color => Stylesheet.primary.upcase.gsub('#', 'FF'), :fg_color => 'FFFFFFFF' date = wb.styles.add_style :format_code => Rails.application.secrets.excel_date_format wb.add_worksheet(name: t('export_reservations.reservations')) do |sheet| @@ -10,7 +10,7 @@ wb.add_worksheet(name: t('export_reservations.reservations')) do |sheet| columns = [t('export_reservations.customer_id'), t('export_reservations.customer'), t('export_reservations.email'), t('export_reservations.reservation_date'), t('export_reservations.reservation_type'), t('export_reservations.reservation_object'), t('export_reservations.slots_number_hours_tickets'), t('export_reservations.payment_method')] - sheet.add_row columns, :style => header + sheet.add_row columns, style: header # data rows @reservations.each do |resrv| @@ -22,11 +22,11 @@ wb.add_worksheet(name: t('export_reservations.reservations')) do |sheet| resrv.reservable_type, (resrv.reservable.nil? ? '' : resrv.reservable.name), (resrv.reservable_type == 'Event') ? resrv.total_booked_seats: resrv.slots.count, - (resrv.stp_invoice_id.nil?)? t('export_reservations.local_payment') : t('export_reservations.online_payment') + (resrv.invoice.paid_with_stripe?) ? t('export_reservations.local_payment') : t('export_reservations.online_payment') ] styles = [nil, nil, nil, date, nil, nil, nil, nil] types = [:integer, :string, :string, :date, :string, :string, :integer, :string] - sheet.add_row data, :style => styles, :types => types + sheet.add_row data, style: styles, types: types end -end \ No newline at end of file +end diff --git a/app/views/open_api/v1/invoices/index.json.jbuilder b/app/views/open_api/v1/invoices/index.json.jbuilder index a3a4a508b..520db23ae 100644 --- a/app/views/open_api/v1/invoices/index.json.jbuilder +++ b/app/views/open_api/v1/invoices/index.json.jbuilder @@ -1,5 +1,5 @@ json.invoices @invoices do |invoice| - json.extract! invoice, :id, :invoiced_id, :user_id, :invoiced_type, :stp_invoice_id, :reference, :total, :type, :description + json.extract! invoice, :id, :invoiced_id, :user_id, :invoiced_type, :stp_invoice_id, :stp_payment_intent_id, :reference, :total, :type, :description json.invoice_url download_open_api_v1_invoice_path(invoice) json.invoiced do diff --git a/db/migrate/20190910131825_add_stp_payment_intent_id_to_invoices.rb b/db/migrate/20190910131825_add_stp_payment_intent_id_to_invoices.rb new file mode 100644 index 000000000..c86115848 --- /dev/null +++ b/db/migrate/20190910131825_add_stp_payment_intent_id_to_invoices.rb @@ -0,0 +1,5 @@ +class AddStpPaymentIntentIdToInvoices < ActiveRecord::Migration + def change + add_column :invoices, :stp_payment_intent_id, :string + end +end diff --git a/db/migrate/20190910141336_remove_stp_invoice_id_from_reservations.rb b/db/migrate/20190910141336_remove_stp_invoice_id_from_reservations.rb new file mode 100644 index 000000000..28bb5deb0 --- /dev/null +++ b/db/migrate/20190910141336_remove_stp_invoice_id_from_reservations.rb @@ -0,0 +1,5 @@ +class RemoveStpInvoiceIdFromReservations < ActiveRecord::Migration + def change + remove_column :reservations, :stp_invoice_id, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index f1baff8ba..17cc741a6 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,12 +11,12 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20190606074801) do +ActiveRecord::Schema.define(version: 20190910141336) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" - enable_extension "unaccent" enable_extension "pg_trgm" + enable_extension "unaccent" create_table "abuses", force: :cascade do |t| t.integer "signaled_id" @@ -202,10 +202,11 @@ ActiveRecord::Schema.define(version: 20190606074801) do t.string "category" t.string "export_type" t.string "query" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false t.integer "user_id" t.string "key" + t.string "extension", default: "xlsx" end add_index "exports", ["user_id"], name: "index_exports_on_user_id", using: :btree @@ -281,6 +282,7 @@ ActiveRecord::Schema.define(version: 20190606074801) do t.integer "invoicing_profile_id" t.integer "operator_profile_id" t.integer "statistic_profile_id" + t.string "stp_payment_intent_id" end add_index "invoices", ["coupon_id"], name: "index_invoices_on_coupon_id", using: :btree @@ -550,14 +552,12 @@ ActiveRecord::Schema.define(version: 20190606074801) do t.datetime "updated_at" t.integer "reservable_id" t.string "reservable_type" - t.string "stp_invoice_id" t.integer "nb_reserve_places" t.integer "statistic_profile_id" end add_index "reservations", ["reservable_type", "reservable_id"], name: "index_reservations_on_reservable_type_and_reservable_id", using: :btree add_index "reservations", ["statistic_profile_id"], name: "index_reservations_on_statistic_profile_id", using: :btree - add_index "reservations", ["stp_invoice_id"], name: "index_reservations_on_stp_invoice_id", using: :btree create_table "roles", force: :cascade do |t| t.string "name" diff --git a/lib/tasks/fablab/stripe.rake b/lib/tasks/fablab/stripe.rake index 85e7e3e21..4584cdca1 100644 --- a/lib/tasks/fablab/stripe.rake +++ b/lib/tasks/fablab/stripe.rake @@ -15,6 +15,7 @@ namespace :fablab do desc 'find any invoices with incoherent total between stripe and DB' task :find_incoherent_invoices, [:start_date] => :environment do |_task, args| + puts 'DEPRECATION WARNING: Will not work for invoices created from version 4.1.0 and above' date = Date.parse('2017-05-01') if args.start_date begin diff --git a/test/integration/events/as_user_test.rb b/test/integration/events/as_user_test.rb index cf1239cba..de2248713 100644 --- a/test/integration/events/as_user_test.rb +++ b/test/integration/events/as_user_test.rb @@ -68,13 +68,12 @@ module Events reservation = Reservation.last assert reservation.invoice - refute reservation.stp_invoice_id.blank? assert_equal 1, reservation.invoice.invoice_items.count # invoice assertions invoice = reservation.invoice - refute invoice.stp_invoice_id.blank? + refute invoice.stp_payment_intent_id.blank? refute invoice.total.blank? assert_equal 43350, invoice.total # total minus coupon @@ -91,8 +90,8 @@ module Events assert_invoice_pdf invoice VCR.use_cassette('reserve_event_with_many_prices_and_payment_means_retrieve_invoice_from_stripe') do - stp_invoice = Stripe::Invoice.retrieve(invoice.stp_invoice_id) - assert_equal stp_invoice.total, (invoice.total - invoice.wallet_amount) # total minus coupon minus wallet = amount really payed by the user + stp_intent = Stripe::PaymentIntent.retrieve(invoice.stp_payment_intent_id) + assert_equal stp_intent.amount, (invoice.total - invoice.wallet_amount) # total minus coupon minus wallet = amount really payed by the user end # wallet assertions @@ -109,4 +108,4 @@ module Events end end -end \ No newline at end of file +end diff --git a/test/integration/reservations/create_as_admin_test.rb b/test/integration/reservations/create_as_admin_test.rb index 55b6fb66b..6eacf2251 100644 --- a/test/integration/reservations/create_as_admin_test.rb +++ b/test/integration/reservations/create_as_admin_test.rb @@ -44,19 +44,17 @@ module Reservations reservation = Reservation.last assert reservation.invoice - assert reservation.stp_invoice_id.blank? assert_equal 1, reservation.invoice.invoice_items.count # invoice assertions invoice = reservation.invoice - assert invoice.stp_invoice_id.blank? + assert invoice.stp_payment_intent_id.blank? refute invoice.total.blank? # invoice_items assertions invoice_item = InvoiceItem.last - refute invoice_item.stp_invoice_item_id assert_equal invoice_item.amount, machine.prices.find_by(group_id: @user_without_subscription.group_id, plan_id: nil).amount # invoice assertions @@ -102,18 +100,16 @@ module Reservations reservation = Reservation.last assert reservation.invoice - assert reservation.stp_invoice_id.blank? assert_equal 1, reservation.invoice.invoice_items.count # invoice assertions invoice = reservation.invoice - assert invoice.stp_invoice_id.blank? + assert invoice.stp_payment_intent_id.blank? refute invoice.total.blank? # invoice_items invoice_item = InvoiceItem.last - refute invoice_item.stp_invoice_item_id assert_equal invoice_item.amount, training.amount_by_group(@user_without_subscription.group_id).amount # invoice assertions @@ -168,13 +164,12 @@ module Reservations reservation = Reservation.last assert reservation.invoice - assert reservation.stp_invoice_id.blank? assert_equal 2, reservation.invoice.invoice_items.count # invoice assertions invoice = reservation.invoice - assert invoice.stp_invoice_id.blank? + assert invoice.stp_payment_intent_id.blank? refute invoice.total.blank? # invoice_items assertions @@ -183,7 +178,6 @@ module Reservations assert(invoice_items.any? { |ii| ii.amount.zero? }) assert(invoice_items.any? { |ii| ii.amount == machine_price }) - assert(invoice_items.all? { |ii| ii.stp_invoice_item_id.blank? }) # users_credits assertions users_credit = UsersCredit.last @@ -237,19 +231,17 @@ module Reservations reservation = Reservation.last assert reservation.invoice - assert reservation.stp_invoice_id.blank? assert_equal 1, reservation.invoice.invoice_items.count # invoice assertions invoice = reservation.invoice - assert invoice.stp_invoice_id.blank? + assert invoice.stp_payment_intent_id.blank? refute invoice.total.blank? # invoice_items assertions invoice_item = InvoiceItem.last - refute invoice_item.stp_invoice_item_id assert_equal invoice_item.amount, machine.prices.find_by(group_id: @vlonchamp.group_id, plan_id: nil).amount # invoice assertions @@ -312,13 +304,12 @@ module Reservations reservation = Reservation.last assert reservation.invoice - assert reservation.stp_invoice_id.blank? assert_equal 2, reservation.invoice.invoice_items.count # invoice assertions invoice = reservation.invoice - assert invoice.stp_invoice_id.blank? + assert invoice.stp_payment_intent_id.blank? refute invoice.total.blank? assert_equal invoice.total, 2000 @@ -377,7 +368,6 @@ module Reservations reservation = Reservation.last assert_not_nil reservation.invoice - assert reservation.stp_invoice_id.blank? # notification assert_not_empty Notification.where(attached_object: reservation) @@ -428,7 +418,6 @@ module Reservations reservation = Reservation.find(result[:id]) assert reservation.invoice - assert reservation.stp_invoice_id.blank? assert_equal 2, reservation.invoice.invoice_items.count # credits assertions @@ -439,14 +428,13 @@ module Reservations # invoice assertions invoice = reservation.invoice - assert invoice.stp_invoice_id.blank? + assert invoice.stp_payment_intent_id.blank? refute invoice.total.blank? assert_equal plan.amount, invoice.total # invoice_items invoice_items = InvoiceItem.last(2) - assert(invoice_items.all? { |ii| ii.stp_invoice_item_id.blank? }) assert(invoice_items.any? { |ii| ii.amount == plan.amount && !ii.subscription_id.nil? }) assert(invoice_items.any? { |ii| ii.amount.zero? }) diff --git a/test/integration/reservations/create_test.rb b/test/integration/reservations/create_test.rb index a07fb71cc..a0d9ec77a 100644 --- a/test/integration/reservations/create_test.rb +++ b/test/integration/reservations/create_test.rb @@ -49,20 +49,18 @@ module Reservations reservation = Reservation.last assert reservation.invoice - refute reservation.stp_invoice_id.blank? assert_equal 1, reservation.invoice.invoice_items.count # invoice assertions invoice = reservation.invoice - refute invoice.stp_invoice_id.blank? + refute invoice.stp_payment_intent_id.blank? refute invoice.total.blank? assert invoice.check_footprint # invoice_items assertions invoice_item = InvoiceItem.last - assert invoice_item.stp_invoice_item_id assert_equal invoice_item.amount, machine.prices.find_by(group_id: @user_without_subscription.group_id, plan_id: nil).amount assert invoice_item.check_footprint @@ -153,20 +151,18 @@ module Reservations reservation = Reservation.last assert reservation.invoice - refute reservation.stp_invoice_id.blank? assert_equal 1, reservation.invoice.invoice_items.count # invoice assertions invoice = reservation.invoice - refute invoice.stp_invoice_id.blank? + refute invoice.stp_payment_intent_id.blank? refute invoice.total.blank? assert invoice.check_footprint # invoice_items invoice_item = InvoiceItem.last - assert invoice_item.stp_invoice_item_id assert_equal invoice_item.amount, training.amount_by_group(@user_without_subscription.group_id).amount assert invoice_item.check_footprint @@ -227,13 +223,12 @@ module Reservations reservation = Reservation.last assert reservation.invoice - refute reservation.stp_invoice_id.blank? assert_equal 2, reservation.invoice.invoice_items.count # invoice assertions invoice = reservation.invoice - refute invoice.stp_invoice_id.blank? + refute invoice.stp_payment_intent_id.blank? refute invoice.total.blank? assert invoice.check_footprint @@ -243,7 +238,6 @@ module Reservations assert(invoice_items.any? { |inv| inv.amount.zero? }) assert(invoice_items.any? { |inv| inv.amount == machine_price }) - assert(invoice_items.all?(&:stp_invoice_item_id)) assert(invoice_items.all?(&:check_footprint)) # users_credits assertions @@ -303,20 +297,18 @@ module Reservations reservation = Reservation.last assert reservation.invoice - refute reservation.stp_invoice_id.blank? assert_equal 1, reservation.invoice.invoice_items.count # invoice assertions invoice = reservation.invoice - refute invoice.stp_invoice_id.blank? + refute invoice.stp_payment_intent_id.blank? refute invoice.total.blank? assert invoice.check_footprint # invoice_items invoice_item = InvoiceItem.last - assert invoice_item.stp_invoice_item_id assert_equal 0, invoice_item.amount # amount is 0 because this training is a credited training with that plan assert invoice_item.check_footprint @@ -376,20 +368,18 @@ module Reservations reservation = Reservation.last assert reservation.invoice - refute reservation.stp_invoice_id.blank? assert_equal 1, reservation.invoice.invoice_items.count # invoice assertions invoice = reservation.invoice - refute invoice.stp_invoice_id.blank? + refute invoice.stp_payment_intent_id.blank? refute invoice.total.blank? assert invoice.check_footprint # invoice_items assertions invoice_item = InvoiceItem.last - assert invoice_item.stp_invoice_item_id assert_equal invoice_item.amount, machine.prices.find_by(group_id: @vlonchamp.group_id, plan_id: nil).amount assert invoice_item.check_footprint @@ -455,13 +445,12 @@ module Reservations reservation = Reservation.last assert reservation.invoice - refute reservation.stp_invoice_id.blank? assert_equal 2, reservation.invoice.invoice_items.count # invoice assertions invoice = reservation.invoice - refute invoice.stp_invoice_id.blank? + refute invoice.stp_payment_intent_id.blank? refute invoice.total.blank? assert_equal invoice.total, 2000 assert invoice.check_footprint @@ -532,13 +521,12 @@ module Reservations reservation = Reservation.last assert reservation.invoice - refute reservation.stp_invoice_id.blank? assert_equal 2, reservation.invoice.invoice_items.count # invoice assertions invoice = reservation.invoice - refute invoice.stp_invoice_id.blank? + refute invoice.stp_payment_intent_id.blank? refute invoice.total.blank? assert invoice.check_footprint @@ -547,7 +535,6 @@ module Reservations reservation_item = invoice.invoice_items.where(subscription_id: nil).first assert_not_nil reservation_item - assert reservation_item.stp_invoice_item_id assert_equal reservation_item.amount, machine.prices.find_by(group_id: @user_without_subscription.group_id, plan_id: plan.id).amount assert reservation_item.check_footprint ## subscription @@ -557,7 +544,6 @@ module Reservations subscription = Subscription.find(subscription_item.subscription_id) - assert subscription_item.stp_invoice_item_id assert_equal subscription_item.amount, plan.amount assert_equal subscription.plan_id, plan.id assert subscription_item.check_footprint @@ -567,8 +553,8 @@ module Reservations assert_invoice_pdf invoice VCR.use_cassette('reservations_machine_and_plan_using_coupon_retrieve_invoice_from_stripe') do - stp_invoice = Stripe::Invoice.retrieve(invoice.stp_invoice_id) - assert_equal stp_invoice.total, invoice.total + stp_intent = Stripe::PaymentIntent.retrieve(invoice.stp_payment_intent_id) + assert_equal stp_intent.amount, invoice.total end # notifications