diff --git a/app/controllers/api/payments_controller.rb b/app/controllers/api/payments_controller.rb index 9e3074ae6..2d9b70a8b 100644 --- a/app/controllers/api/payments_controller.rb +++ b/app/controllers/api/payments_controller.rb @@ -97,6 +97,7 @@ class API::PaymentsController < API::ApiController def on_reservation_success(intent, details) @reservation = Reservation.new(reservation_params) + payment_method = params[:cart_items][:reservation][:payment_method] || 'stripe' user_id = if current_user.admin? || current_user.manager? params[:cart_items][:reservation][:user_id] else @@ -107,7 +108,7 @@ class API::PaymentsController < API::ApiController payment_details: details, intent_id: intent.id, schedule: params[:cart_items][:reservation][:payment_schedule], - payment_method: params[:cart_items][:reservation][:payment_method]) + payment_method: payment_method) if intent.class == Stripe::PaymentIntent Stripe::PaymentIntent.update( intent.id, diff --git a/app/models/payment_schedule.rb b/app/models/payment_schedule.rb index 0d541a7fd..f68ed5aa6 100644 --- a/app/models/payment_schedule.rb +++ b/app/models/payment_schedule.rb @@ -55,6 +55,10 @@ class PaymentSchedule < PaymentDocument StripeService.create_stripe_subscription(self, setup_intent_id) end + def self.columns_out_of_footprint + %w[stp_subscription_id] + end + private def generate_and_send_document diff --git a/app/services/payment_document_service.rb b/app/services/payment_document_service.rb index 3906b8ef2..303286884 100644 --- a/app/services/payment_document_service.rb +++ b/app/services/payment_document_service.rb @@ -87,9 +87,10 @@ class PaymentDocumentService else return get_max_id(Invoice) + get_max_id(PaymentSchedule) end - return Invoice.count unless defined? start && defined? ending + return Invoice.count + PaymentSchedule.count unless defined? start && defined? ending - Invoice.where('created_at >= :start_date AND created_at < :end_date', start_date: start, end_date: ending).length + Invoice.where('created_at >= :start_date AND created_at < :end_date', start_date: start, end_date: ending).length + + PaymentSchedule.where('created_at >= :start_date AND created_at < :end_date', start_date: start, end_date: ending).length end ## diff --git a/app/services/stripe_service.rb b/app/services/stripe_service.rb index 0af774793..ad2a1cf3e 100644 --- a/app/services/stripe_service.rb +++ b/app/services/stripe_service.rb @@ -46,10 +46,7 @@ class StripeService def create_stripe_coupon(coupon_id) coupon = Coupon.find(coupon_id) - stp_coupon = { - id: coupon.code, - duration: coupon.validity_per_user - } + stp_coupon = { id: coupon.code } if coupon.type == 'percent_off' stp_coupon[:percent_off] = coupon.percent_off elsif coupon.type == 'amount_off' @@ -57,6 +54,7 @@ class StripeService stp_coupon[:currency] = Setting.get('stripe_currency') end + stp_coupon[:duration] = coupon.validity_per_user == 'always' ? 'forever' : 'once' stp_coupon[:redeem_by] = coupon.valid_until.to_i unless coupon.valid_until.nil? stp_coupon[:max_redemptions] = coupon.max_usages unless coupon.max_usages.nil? diff --git a/test/fixtures/addresses.yml b/test/fixtures/addresses.yml index e6cbab1a7..16302d97f 100644 --- a/test/fixtures/addresses.yml +++ b/test/fixtures/addresses.yml @@ -62,3 +62,15 @@ address_5: placeable_type: Organization created_at: 2016-08-02 11:16:24.412236000 Z updated_at: 2016-08-02 11:16:24.412236000 Z + +address_6: + id: 6 + address: 14 rue du Général Choucroute, 44000 NANTES + street_number: + route: + locality: + postal_code: + placeable_id: 8 + placeable_type: InvoicingProfile + created_at: 2016-08-02 11:16:24.412236000 Z + updated_at: 2016-08-02 11:16:24.412236000 Z diff --git a/test/fixtures/invoice_items.yml b/test/fixtures/invoice_items.yml index 9b29ab471..2fb614107 100644 --- a/test/fixtures/invoice_items.yml +++ b/test/fixtures/invoice_items.yml @@ -60,3 +60,16 @@ invoice_item_5: invoice_item_id: footprint: d4d856ee04f27b5e35f43056b46f139df73ffdfaad1716636f569164c52c5e5b +invoice_item_6: + id: 6 + invoice_id: 6 + stp_invoice_item_id: + amount: 3000 + created_at: 2016-04-05 08:36:46.856021000 Z + updated_at: 2016-04-05 08:36:46.856021000 Z + description: Mensuel - standard, association - month + subscription_id: 3 + invoice_item_id: + footprint: 79e42a65be85a40978c19c493af436ea893d15b7e054e9991bb4d3bb3d80913d + + diff --git a/test/fixtures/invoices.yml b/test/fixtures/invoices.yml index ac4e4e106..513e48eb2 100644 --- a/test/fixtures/invoices.yml +++ b/test/fixtures/invoices.yml @@ -104,3 +104,26 @@ invoice_5: footprint: 9aead7f5eb9411778d072279a058be0fe2da76eb12f6a22100cbec1f2b3f6e9d environment: test operator_profile_id: 1 + +invoice_6: + id: 6 + invoiced_id: 4 + invoiced_type: Subscription + stp_invoice_id: + total: 3000 + created_at: 2021-01-04 14:51:21.616153182 Z + updated_at: 2021-01-04 14:51:21.616153182 Z + invoicing_profile_id: 8 + statistic_profile_id: 8 + reference: '2101041' + payment_method: + avoir_date: + invoice_id: + type: + subscription_to_expire: + description: + footprint: e637ccf79157d973e87be46b09d5f90ac08f41b69bc53c3c60b6fc39d07450d9 + environment: test + operator_profile_id: 1 + + diff --git a/test/fixtures/invoicing_profiles.yml b/test/fixtures/invoicing_profiles.yml index aede0635b..3d2a4e206 100644 --- a/test/fixtures/invoicing_profiles.yml +++ b/test/fixtures/invoicing_profiles.yml @@ -46,3 +46,10 @@ lseguin: first_name: Lucile last_name: Seguin email: lucile.seguin@live.fr + +atiermoulin: + id: 8 + user_id: 8 + first_name: Amandine + last_name: Tiermoulin + email: a.tiermoulin@mail.fr diff --git a/test/fixtures/profiles.yml b/test/fixtures/profiles.yml index b63c21534..0daf95b3b 100644 --- a/test/fixtures/profiles.yml +++ b/test/fixtures/profiles.yml @@ -75,3 +75,14 @@ profile_7: software_mastered: created_at: 2012-03-12 11:00:21.411067000 Z updated_at: 2016-04-07 11:00:37.643112000 Z + +profile_8: + id: 8 + user_id: 8 + first_name: Amandine + last_name: Tiermoulin + phone: '0476456845' + interest: menuiserie + software_mastered: freecad + created_at: 2018-05-10 09:22:42.427521000 Z + updated_at: 2021-01-04 14:36:39.729052932 Z diff --git a/test/fixtures/statistic_profiles.yml b/test/fixtures/statistic_profiles.yml index f1d09da75..0acc8c1a6 100644 --- a/test/fixtures/statistic_profiles.yml +++ b/test/fixtures/statistic_profiles.yml @@ -46,3 +46,10 @@ lseguin: gender: false birthday: 1969-02-03 group_id: 1 + +atiermoulin: + id: 8 + user_id: 8 + gender: false + birthday: 1985-09-01 + group_id: 1 diff --git a/test/fixtures/subscriptions.yml b/test/fixtures/subscriptions.yml index d5057c2f4..d9fcaa62e 100644 --- a/test/fixtures/subscriptions.yml +++ b/test/fixtures/subscriptions.yml @@ -29,3 +29,14 @@ subscription_3: updated_at: 2012-03-12 11:03:31.651441000 Z expiration_date: 2012-04-12 11:03:31.648274000 Z canceled_at: + +# subscription has expired (do not change dates) +subscription_4: + id: 4 + plan_id: 1 + statistic_profile_id: 8 + stp_subscription_id: + created_at: 2018-06-11 14:27:12.427521000 Z + updated_at: 2018-06-11 14:27:12.427521000 Z + expiration_date: 2018-07-11 14:27:12.427521000 Z + canceled_at: diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index c5aacaf8b..fd1f41691 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -230,3 +230,36 @@ user_7: auth_token: merged_at: is_allow_newsletter: false + +user_8: + id: 8 + username: atiermoulin + email: a.tiermoulin@mail.fr + encrypted_password: $2a$10$5dThoeez738sWt6sILHs7eqlOMHSa0M3GXo6VL05DPu.Kdyxl.Qdm + reset_password_token: + reset_password_sent_at: + remember_created_at: + sign_in_count: 10 + current_sign_in_at: 2021-01-04 14:37:58.849941086 Z + last_sign_in_at: 2020-12-21 18:16:21.651201086 Z + current_sign_in_ip: 12.22.42.6 + last_sign_in_ip: 144.71.10.169 + confirmation_token: + confirmed_at: 2018-05-11 09:22:42.427521000 Z + confirmation_sent_at: 2018-05-10 09:22:42.427521000 Z + unconfirmed_email: + failed_attempts: 1 + unlock_token: + locked_at: + created_at: 2018-05-10 09:22:42.427521000 Z + updated_at: 2021-01-04 14:36:39.729052932 Z + is_allow_contact: true + group_id: 1 + stp_customer_id: cus_IhIynmoJbzLpwX + slug: atiermoulin + is_active: true + provider: + uid: + auth_token: + merged_at: + is_allow_newsletter: false diff --git a/test/fixtures/wallets.yml b/test/fixtures/wallets.yml index 5889fa6fa..d810a3ce3 100644 --- a/test/fixtures/wallets.yml +++ b/test/fixtures/wallets.yml @@ -25,3 +25,7 @@ wallet_1: wallet_7: invoicing_profile_id: 7 amount: 25500 + +wallet_8: + invoicing_profile_id: 8 + amount: 0 diff --git a/test/integration/exports/accounting_export_test.rb b/test/integration/exports/accounting_export_test.rb index 29a3c6632..0f2cdf91b 100644 --- a/test/integration/exports/accounting_export_test.rb +++ b/test/integration/exports/accounting_export_test.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true +require 'test_helper' module Exports; end class Exports::AccountingExportTest < ActionDispatch::IntegrationTest @@ -113,11 +114,11 @@ class Exports::AccountingExportTest < ActionDispatch::IntegrationTest assert_equal 0, data[1][I18n.t('accounting_export.debit_euro')].to_f, 'Debit euro amount does not match' # test with another invoice - last_invoice = Invoice.last - client_row = data[data.length - 2] - item_row = data[data.length - 1] + machine_invoice = Invoice.find(5) + client_row = data[data.length - 4] + item_row = data[data.length - 3] - if last_invoice.invoiced_type == 'Reservation' && last_invoice.invoiced.reservable_type == 'Machine' + if machine_invoice.invoiced_type == 'Reservation' && machine_invoice.invoiced.reservable_type == 'Machine' assert_match I18n.t('accounting_export.Machine_reservation'), client_row[I18n.t('accounting_export.line_label')], 'Line label does not contains the reference to the invoiced item' @@ -129,7 +130,7 @@ class Exports::AccountingExportTest < ActionDispatch::IntegrationTest assert_equal machine_label, item_row[I18n.t('accounting_export.account_label')], 'Account label for machine reservation is wrong' else - STDERR.puts "WARNING: unable to test accurately accounting export: invoice #{last_invoice.id} is not a Machine reservation" + STDERR.puts "WARNING: unable to test accurately accounting export: invoice #{machine_invoice.id} is not a Machine reservation" end # Clean CSV file diff --git a/test/integration/invoices/as_admin_test.rb b/test/integration/invoices/as_admin_test.rb index 78342865b..b21f5887d 100644 --- a/test/integration/invoices/as_admin_test.rb +++ b/test/integration/invoices/as_admin_test.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require 'test_helper' + class InvoicesTest < ActionDispatch::IntegrationTest # Called before every test method runs. Can be used # to set up fixture information. @@ -27,8 +29,10 @@ class InvoicesTest < ActionDispatch::IntegrationTest assert_equal Invoice.count, invoices.size, 'some invoices are missing' # Check that invoices are ordered by reference - assert_equal '1604002', invoices.first[:reference] - assert_equal '1203001', invoices.last[:reference] + first_invoice = Invoice.order(:reference).limit(1).first + last_invoice = Invoice.order(reference: :desc).limit(1).first + assert_equal last_invoice.reference, invoices.first[:reference] + assert_equal first_invoice.reference, invoices.last[:reference] end test 'admin generates a refund' do diff --git a/test/integration/reservations/create_as_admin_test.rb b/test/integration/reservations/create_as_admin_test.rb index 813e8c0b1..9ef816b2d 100644 --- a/test/integration/reservations/create_as_admin_test.rb +++ b/test/integration/reservations/create_as_admin_test.rb @@ -465,7 +465,7 @@ class Reservations::CreateAsAdminTest < ActionDispatch::IntegrationTest VCR.use_cassette('reservations_admin_training_subscription_with_payment_schedule') do post reservations_path, params: { reservation: { user_id: @user_without_subscription.id, - payment_method: '', # pay by check + payment_method: 'check', reservable_id: training.id, reservable_type: training.class.name, slots_attributes: [ @@ -480,6 +480,10 @@ class Reservations::CreateAsAdminTest < ActionDispatch::IntegrationTest } }.to_json, headers: default_headers end + # get the objects + reservation = Reservation.last + payment_schedule = PaymentSchedule.last + # Check response format & status assert_equal 201, response.status, response.body assert_equal Mime[:json], response.content_type @@ -498,7 +502,25 @@ class Reservations::CreateAsAdminTest < ActionDispatch::IntegrationTest assert_equal plan.id, @user_without_subscription.subscribed_plan.id, "user's plan does not match" # Check the answer - reservation = json_response(response.body) - assert_equal plan.id, reservation[:user][:subscribed_plan][:id], 'subscribed plan does not match' + reservation_res = json_response(response.body) + assert_equal plan.id, reservation_res[:user][:subscribed_plan][:id], 'subscribed plan does not match' + + # reservation assertions + assert_equal reservation_res[:id], reservation.id + assert reservation.payment_schedule + assert_equal payment_schedule.scheduled, reservation + + # payment schedule assertions + assert_not_nil payment_schedule.reference + assert_equal 'check', payment_schedule.payment_method + assert_nil payment_schedule.stp_subscription_id + assert_nil payment_schedule.stp_setup_intent_id + assert_nil payment_schedule.wallet_transaction + assert_nil payment_schedule.wallet_amount + assert_nil payment_schedule.coupon_id + assert_equal 'test', payment_schedule.environment + assert payment_schedule.check_footprint + assert_equal @user_without_subscription.invoicing_profile.id, payment_schedule.invoicing_profile_id + assert_equal @admin.invoicing_profile.id, payment_schedule.operator_profile_id end end diff --git a/test/integration/subscriptions/renew_as_user_test.rb b/test/integration/subscriptions/renew_as_user_test.rb index 8f694626a..3d142ddc1 100644 --- a/test/integration/subscriptions/renew_as_user_test.rb +++ b/test/integration/subscriptions/renew_as_user_test.rb @@ -1,8 +1,10 @@ # frozen_string_literal: true +require 'test_helper' + class Subscriptions::RenewAsUserTest < ActionDispatch::IntegrationTest setup do - @user = User.find_by(username: 'lseguin') + @user = User.find_by(username: 'atiermoulin') login_as(@user, scope: :user) end