From 26dfbef5e14a7c782a1dd746b4294e2d6c844fb8 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Wed, 21 Apr 2021 17:38:06 +0200 Subject: [PATCH] WIP: refactor models to use payment_gateway_objects --- CHANGELOG.md | 1 + .../api/payment_schedules_controller.rb | 2 +- app/doc/open_api/v1/invoices_doc.rb | 18 ++++++---- .../javascript/controllers/admin/invoices.js | 4 +-- app/models/invoice_item.rb | 2 +- app/models/payment_gateway_object.rb | 4 +++ app/models/payment_schedule.rb | 8 +++-- app/models/payment_schedule_item.rb | 2 +- app/models/subscription.rb | 2 +- app/services/payment_schedule_service.rb | 8 +++-- app/services/subscriptions/subscribe.rb | 9 +++-- app/views/api/invoices/avoir.json.jbuilder | 1 - app/views/api/invoices/list.json.jbuilder | 2 +- app/views/api/invoices/show.json.jbuilder | 1 - app/views/api/members/_member.json.jbuilder | 1 - app/views/api/members/index.json.jbuilder | 1 - .../subscriptions/_subscription.json.jbuilder | 3 +- app/views/archive/_accounting.json.jbuilder | 16 +++++++-- app/views/archive/_subscription.json.jbuilder | 8 ++++- .../exports/users_subscriptions.xlsx.axlsx | 33 ++++++++++--------- .../open_api/v1/invoices/index.json.jbuilder | 10 +++++- app/workers/archive_worker.rb | 10 +++--- app/workers/payment_schedule_item_worker.rb | 6 ++-- config/locales/de.yml | 1 - config/locales/en.yml | 1 - config/locales/es.yml | 1 - config/locales/fr.yml | 1 - config/locales/pt.yml | 1 - config/locales/zu.yml | 1 - ...e_stripe_ids_to_payment_gateway_objects.rb | 8 +++-- test/fixtures/invoice_items.yml | 6 ---- test/fixtures/payment_gateway_objects.yml | 11 +++++++ test/fixtures/subscriptions.yml | 4 --- .../reservations/create_as_admin_test.rb | 2 +- test/integration/reservations/create_test.rb | 2 +- 35 files changed, 120 insertions(+), 71 deletions(-) create mode 100644 test/fixtures/payment_gateway_objects.yml diff --git a/CHANGELOG.md b/CHANGELOG.md index 340b1e7e6..6e460660a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - Fix a bug: unable to set date formats during installation - Fix a bug: unable to cancel the upgrade before it begins - `SUPERADMIN_EMAIL` renamed to `ADMINSYS_EMAIL` +- [BREAKING CHANGE] GET `open_api/v1/invoices` won't return `stp_invoice_id` OR `stp_payment_intent_id` anymore. The new field `payment_gateway_object` will contain some similar data if the invoice was paid online by card. - [TODO DEPLOY] `rails fablab:stripe:set_gateway` - [TODO DEPLOY] `rails fablab:maintenance:rebuild_stylesheet` - [TODO DEPLOY] `\curl -sSL https://raw.githubusercontent.com/sleede/fab-manager/master/scripts/rename-adminsys.sh | bash` diff --git a/app/controllers/api/payment_schedules_controller.rb b/app/controllers/api/payment_schedules_controller.rb index 8192818da..e943ffb81 100644 --- a/app/controllers/api/payment_schedules_controller.rb +++ b/app/controllers/api/payment_schedules_controller.rb @@ -37,7 +37,7 @@ class API::PaymentSchedulesController < API::ApiController def cash_check authorize @payment_schedule_item.payment_schedule - PaymentScheduleService.new.generate_invoice(@payment_schedule_item) + PaymentScheduleService.new.generate_invoice(@payment_schedule_item, payment_method: 'check') attrs = { state: 'paid', payment_method: 'check' } @payment_schedule_item.update_attributes(attrs) diff --git a/app/doc/open_api/v1/invoices_doc.rb b/app/doc/open_api/v1/invoices_doc.rb index e364c6982..c365d114c 100644 --- a/app/doc/open_api/v1/invoices_doc.rb +++ b/app/doc/open_api/v1/invoices_doc.rb @@ -26,8 +26,10 @@ class OpenAPI::V1::InvoicesDoc < OpenAPI::V1::BaseDoc "invoiced_id": 3257, "user_id": 211, "invoiced_type": "Reservation", - "stp_invoice_id": "in_187DLE4zBvgjueAZ6L7SyQlU", - "stp_payment_intent_id": "pi_1Dat4P2eZvKYlo2C3MxszwQp", + "payment_gateway_object": { + id: "in_187DLE4zBvgjueAZ6L7SyQlU", + type: "Stripe::Invoice" + }, "reference": "1605017/VL", "total": 1000, "type": null, @@ -42,8 +44,10 @@ class OpenAPI::V1::InvoicesDoc < OpenAPI::V1::BaseDoc "invoiced_id": 3229, "user_id": 211, "invoiced_type": "Reservation", - "stp_invoice_id": "in_185Hmt4zBvgjueAZl5lio1pK", - "stp_payment_intent_id": "pi_2Dat4P2eYbKYlo2C3MxszwQp", + "payment_gateway_object": { + id: "pi_2Dat4P2eYbKYlo2C3MxszwQp", + type: "Stripe::PaymentIntent" + }, "reference": "1604176/VL", "total": 2000, "type": null, @@ -58,8 +62,10 @@ class OpenAPI::V1::InvoicesDoc < OpenAPI::V1::BaseDoc "invoiced_id": 3218, "user_id": 211, "invoiced_type": "Reservation", - "stp_invoice_id": "in_184oNK4zBvgjueAZJdOxHJjT", - "stp_payment_intent_id": "pi_1Pub4P2eZvKYlo2C3MxszwQm", + "payment_gateway_object": { + id: "ba15dc9d8f3e0fa17bf527466", + type: "PayZen::Order" + }, "reference": "1604166/VL", "total": 2000, "type": null, diff --git a/app/frontend/src/javascript/controllers/admin/invoices.js b/app/frontend/src/javascript/controllers/admin/invoices.js index 7f483b255..11754db58 100644 --- a/app/frontend/src/javascript/controllers/admin/invoices.js +++ b/app/frontend/src/javascript/controllers/admin/invoices.js @@ -1126,8 +1126,8 @@ Application.Controllers.controller('AvoirModalController', ['$scope', '$uibModal }); }); - if (invoice.stripe) { - return $scope.avoirModes.push({ name: _t('app.admin.invoices.online_payment'), value: 'stripe' }); + if (invoice.online_payment) { + return $scope.avoirModes.push({ name: _t('app.admin.invoices.online_payment'), value: 'card' }); } }; diff --git a/app/models/invoice_item.rb b/app/models/invoice_item.rb index 90a696cfb..40e03d88f 100644 --- a/app/models/invoice_item.rb +++ b/app/models/invoice_item.rb @@ -6,7 +6,7 @@ class InvoiceItem < Footprintable belongs_to :subscription has_one :invoice_item # associates invoice_items of an invoice to invoice_items of an Avoir - has_one :payment_gateway_object + has_one :payment_gateway_object, as: :item after_create :chain_record after_update :log_changes diff --git a/app/models/payment_gateway_object.rb b/app/models/payment_gateway_object.rb index eda062524..69c74dae3 100644 --- a/app/models/payment_gateway_object.rb +++ b/app/models/payment_gateway_object.rb @@ -6,6 +6,10 @@ require 'payment/item_builder' class PaymentGatewayObject < ApplicationRecord belongs_to :item, polymorphic: true belongs_to :invoice, foreign_type: 'Invoice', foreign_key: 'item_id' + belongs_to :invoice_item, foreign_type: 'InvoiceItem', foreign_key: 'item_id' + belongs_to :subscription, foreign_type: 'Subscription', foreign_key: 'item_id' + belongs_to :payment_schedule, foreign_type: 'PaymentSchedule', foreign_key: 'item_id' + belongs_to :payment_schedule_item, foreign_type: 'PaymentScheduleItem', foreign_key: 'item_id' def gateway_object item = Payment::ItemBuilder.build(gateway_object_type) diff --git a/app/models/payment_schedule.rb b/app/models/payment_schedule.rb index b7c282c4c..04948ade9 100644 --- a/app/models/payment_schedule.rb +++ b/app/models/payment_schedule.rb @@ -16,7 +16,7 @@ class PaymentSchedule < PaymentDocument belongs_to :reservation, foreign_type: 'Reservation', foreign_key: 'scheduled_id' has_many :payment_schedule_items - has_many :payment_gateway_object + has_many :payment_gateway_objects, as: :item before_create :add_environment after_create :update_reference, :chain_record @@ -48,6 +48,10 @@ class PaymentSchedule < PaymentDocument payment_schedule_items.order(due_date: :asc) end + def gateway_object(klass) + payment_gateway_objects.find_by(gateway_object_type: klass) + end + def user invoicing_profile.user end @@ -63,7 +67,7 @@ class PaymentSchedule < PaymentDocument end def post_save(setup_intent_id) - return unless payment_method == 'stripe' + return unless payment_method == 'card' StripeService.create_stripe_subscription(self, setup_intent_id) end diff --git a/app/models/payment_schedule_item.rb b/app/models/payment_schedule_item.rb index 02300b488..4683091dd 100644 --- a/app/models/payment_schedule_item.rb +++ b/app/models/payment_schedule_item.rb @@ -4,7 +4,7 @@ class PaymentScheduleItem < Footprintable belongs_to :payment_schedule belongs_to :invoice - has_one :payment_gateway_object + has_one :payment_gateway_object, as: :item after_create :chain_record diff --git a/app/models/subscription.rb b/app/models/subscription.rb index 2b5c07f0c..b18b6ca58 100644 --- a/app/models/subscription.rb +++ b/app/models/subscription.rb @@ -8,7 +8,7 @@ class Subscription < ApplicationRecord belongs_to :statistic_profile has_one :payment_schedule, as: :scheduled, dependent: :destroy - has_one :payment_gateway_object + has_one :payment_gateway_object, as: :item has_many :invoices, as: :invoiced, dependent: :destroy has_many :offer_days, dependent: :destroy diff --git a/app/services/payment_schedule_service.rb b/app/services/payment_schedule_service.rb index b332e5404..a31ce7d9a 100644 --- a/app/services/payment_schedule_service.rb +++ b/app/services/payment_schedule_service.rb @@ -78,15 +78,19 @@ class PaymentScheduleService # Generate the invoice associated with the given PaymentScheduleItem, with the children elements (InvoiceItems). # @param payment_method {String} the payment method or gateway in use # @param payment_id {String} the identifier of the payment as provided by the payment gateway, in case of card payment + # @param payment_type {String} the object type of payment_id ## - def generate_invoice(payment_schedule_item, payment_method: nil, payment_id: nil) + def generate_invoice(payment_schedule_item, payment_method: nil, payment_id: nil, payment_type: nil) # build the base invoice invoice = Invoice.new( invoiced: payment_schedule_item.payment_schedule.scheduled, invoicing_profile: payment_schedule_item.payment_schedule.invoicing_profile, statistic_profile: payment_schedule_item.payment_schedule.statistic_profile, operator_profile_id: payment_schedule_item.payment_schedule.operator_profile_id, - stp_payment_intent_id: payment_id, + payment_gateway_object_attributes: { + gateway_object_id: payment_id, + gateway_object_type: payment_type + }, payment_method: payment_method ) # complete the invoice with some InvoiceItem diff --git a/app/services/subscriptions/subscribe.rb b/app/services/subscriptions/subscribe.rb index 7743ac2a0..3ddc1eab8 100644 --- a/app/services/subscriptions/subscribe.rb +++ b/app/services/subscriptions/subscribe.rb @@ -68,9 +68,14 @@ class Subscriptions::Subscribe operator_profile_id: operator_profile_id, user: new_sub.user, payment_method: schedule.payment_method, - payment_id: schedule.stp_setup_intent_id) + payment_id: schedule.gateway_object('Stripe::SetupIntent').id) else - generate_invoice(subscription, operator_profile_id, details) + generate_invoice(subscription, + operator_profile_id, + details, + payment_id: schedule.gateway_object('Stripe::SetupIntent').id, + payment_type: 'Stripe::SetupIntent', + payment_method: schedule.payment_method) end payment.save payment.post_save(schedule&.stp_setup_intent_id) diff --git a/app/views/api/invoices/avoir.json.jbuilder b/app/views/api/invoices/avoir.json.jbuilder index 5ff6ea8fd..cefd3de2d 100644 --- a/app/views/api/invoices/avoir.json.jbuilder +++ b/app/views/api/invoices/avoir.json.jbuilder @@ -8,7 +8,6 @@ json.date @avoir.avoir_date json.chained_footprint @avoir.check_footprint json.items @avoir.invoice_items do |item| json.id item.id - json.stp_invoice_item_id item.stp_invoice_item_id json.amount item.amount / 100.0 json.description item.description json.invoice_item_id item.invoice_item_id diff --git a/app/views/api/invoices/list.json.jbuilder b/app/views/api/invoices/list.json.jbuilder index 9e4eb5641..e24baeb11 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.paid_by_card? + json.online_payment invoice.paid_by_card? 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 8d3a92283..77c06551e 100644 --- a/app/views/api/invoices/show.json.jbuilder +++ b/app/views/api/invoices/show.json.jbuilder @@ -12,7 +12,6 @@ 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| json.id item.id - json.stp_invoice_item_id item.stp_invoice_item_id json.amount item.amount / 100.0 json.description item.description json.avoir_item_id item.invoice_item.id if item.invoice_item diff --git a/app/views/api/members/_member.json.jbuilder b/app/views/api/members/_member.json.jbuilder index 9ecb76b62..ce47ba100 100644 --- a/app/views/api/members/_member.json.jbuilder +++ b/app/views/api/members/_member.json.jbuilder @@ -65,7 +65,6 @@ if member.subscription json.id member.subscription.id json.expired_at member.subscription.expired_at.iso8601 json.canceled_at member.subscription.canceled_at.iso8601 if member.subscription.canceled_at - json.stripe member.subscription.stp_subscription_id.present? json.plan do # TODO, refactor: duplicates subscribed_plan json.id member.subscription.plan.id json.base_name member.subscription.plan.base_name diff --git a/app/views/api/members/index.json.jbuilder b/app/views/api/members/index.json.jbuilder index d6ea24324..eefe34b94 100644 --- a/app/views/api/members/index.json.jbuilder +++ b/app/views/api/members/index.json.jbuilder @@ -54,7 +54,6 @@ json.array!(@members) do |member| json.id member.subscription.id json.expired_at member.subscription.expired_at.iso8601 json.canceled_at member.subscription&.canceled_at&.iso8601 - json.stripe member.subscription.stp_subscription_id.present? json.plan do json.id member.subscription.plan.id json.name member.subscription.plan.name diff --git a/app/views/api/subscriptions/_subscription.json.jbuilder b/app/views/api/subscriptions/_subscription.json.jbuilder index f2311a818..887f3a093 100644 --- a/app/views/api/subscriptions/_subscription.json.jbuilder +++ b/app/views/api/subscriptions/_subscription.json.jbuilder @@ -1,7 +1,8 @@ +# frozen_string_literal: true + json.extract! subscription, :id, :plan_id json.expired_at subscription.expired_at.iso8601 json.canceled_at subscription.canceled_at.iso8601 if subscription.canceled_at -json.stripe subscription.stp_subscription_id.present? json.plan do json.partial! 'api/shared/plan', plan: subscription.plan end diff --git a/app/views/archive/_accounting.json.jbuilder b/app/views/archive/_accounting.json.jbuilder index e4c115a0e..064c9b4ab 100644 --- a/app/views/archive/_accounting.json.jbuilder +++ b/app/views/archive/_accounting.json.jbuilder @@ -2,7 +2,13 @@ json.invoices do json.array!(invoices) do |invoice| - json.extract! invoice[:invoice], :id, :stp_invoice_id, :stp_payment_intent_id, :created_at, :reference, :footprint + json.extract! invoice[:invoice], :id, :payment_method, :created_at, :reference, :footprint + if invoice[:invoice].payment_gateway_object + json.payment_gateway_object do + json.id invoice[:invoice].payment_gateway_object.gateway_object_id + json.type invoice[:invoice].payment_gateway_object.gateway_object_type + end + end json.total number_to_currency(invoice[:invoice].total / 100.0) json.invoiced do json.type invoice[:invoice].invoiced_type @@ -25,7 +31,13 @@ json.invoices do end end json.invoice_items invoice[:invoice].invoice_items do |item| - json.extract! item, :id, :stp_invoice_item_id, :created_at, :description, :footprint + json.extract! item, :id, :created_at, :description, :footprint + if item.payment_gateway_object + json.payment_gateway_object do + json.id item.payment_gateway_object.gateway_object_id + json.type item.payment_gateway_object.gateway_object_type + end + end json.partial! 'archive/vat', price: item.amount, vat_rate: invoice[:vat_rate] end end diff --git a/app/views/archive/_subscription.json.jbuilder b/app/views/archive/_subscription.json.jbuilder index 886adaf3d..cb4db840d 100644 --- a/app/views/archive/_subscription.json.jbuilder +++ b/app/views/archive/_subscription.json.jbuilder @@ -1,6 +1,12 @@ # frozen_string_literal: true -json.extract! invoiced, :stp_subscription_id, :created_at, :expiration_date, :canceled_at +json.extract! invoiced, :created_at, :expiration_date, :canceled_at +if invoiced.payment_gateway_object + json.payment_gateway_object do + json.id invoiced.payment_gateway_object.gateway_object_id + json.type invoiced.payment_gateway_object.gateway_object_type + end +end json.plan do json.extract! invoiced.plan, :id, :base_name, :interval, :interval_count, :stp_plan_id, :is_rolling json.group do diff --git a/app/views/exports/users_subscriptions.xlsx.axlsx b/app/views/exports/users_subscriptions.xlsx.axlsx index bdcf01590..05af09724 100644 --- a/app/views/exports/users_subscriptions.xlsx.axlsx +++ b/app/views/exports/users_subscriptions.xlsx.axlsx @@ -1,7 +1,9 @@ +# frozen_string_literal: true + wb = xlsx_package.workbook -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 +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_subscriptions.subscriptions')) do |sheet| @@ -9,25 +11,24 @@ wb.add_worksheet(name: t('export_subscriptions.subscriptions')) do |sheet| # heading labels columns = [t('export_subscriptions.id'), t('export_subscriptions.customer'), t('export_subscriptions.email'), t('export_subscriptions.subscription'), t('export_subscriptions.period'), t('export_subscriptions.start_date'), - t('export_subscriptions.expiration_date'), t('export_subscriptions.amount'), t('export_subscriptions.payment_method')] - sheet.add_row columns, :style => header + t('export_subscriptions.expiration_date'), t('export_subscriptions.amount')] + sheet.add_row columns, style: header # data rows @subscriptions.each do |sub| data = [ - sub.user&.id, - sub.user&.profile&.full_name || t('export_subscriptions.deleted_user'), - sub.user&.email, - sub.plan.human_readable_name(group: true), - t("duration.#{sub.plan.interval}", count: sub.plan.interval_count), - sub.created_at.to_date, - sub.expired_at.to_date, - number_to_currency(sub.plan.amount / 100.00), - (sub.stp_subscription_id.nil?)? t('export_subscriptions.local_payment') : t('export_subscriptions.online_payment') + sub.user&.id, + sub.user&.profile&.full_name || t('export_subscriptions.deleted_user'), + sub.user&.email, + sub.plan.human_readable_name(group: true), + t("duration.#{sub.plan.interval}", count: sub.plan.interval_count), + sub.created_at.to_date, + sub.expired_at.to_date, + number_to_currency(sub.plan.amount / 100.00) ] styles = [nil, nil, nil, nil, nil, date, date, nil, nil] - types = [:integer, :string, :string, :string, :string, :date, :date, :string, :string] + types = %i[integer string string string string date date string 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 520db23ae..39ca11021 100644 --- a/app/views/open_api/v1/invoices/index.json.jbuilder +++ b/app/views/open_api/v1/invoices/index.json.jbuilder @@ -1,5 +1,13 @@ +# frozen_string_literal: true + json.invoices @invoices do |invoice| - json.extract! invoice, :id, :invoiced_id, :user_id, :invoiced_type, :stp_invoice_id, :stp_payment_intent_id, :reference, :total, :type, :description + json.extract! invoice, :id, :invoiced_id, :user_id, :invoiced_type, :reference, :total, :type, :description + if invoice.payment_gateway_object + json.payment_gateway_object do + json.id invoice.payment_gateway_object.gateway_object_id + json.type invoice.payment_gateway_object.gateway_object_type + end + end json.invoice_url download_open_api_v1_invoice_path(invoice) json.invoiced do diff --git a/app/workers/archive_worker.rb b/app/workers/archive_worker.rb index ec8530f72..7b53b7843 100644 --- a/app/workers/archive_worker.rb +++ b/app/workers/archive_worker.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require 'integrity/checksum' + # Will generate a ZIP archive file containing all invoicing data for the given period. # This file will be asynchronously generated by sidekiq and a notification will be sent to the requesting user when it's done. class ArchiveWorker @@ -10,11 +12,11 @@ class ArchiveWorker data = period.invoices.includes(:invoice_items).order(id: :asc) previous_file = period.previous_period&.archive_file - last_archive_checksum = previous_file ? Checksum.file(previous_file) : nil + last_archive_checksum = previous_file ? Integrity::Checksum.file(previous_file) : nil json_data = to_json_archive(period, data, previous_file, last_archive_checksum) - current_archive_checksum = Checksum.text(json_data) + current_archive_checksum = Integrity::Checksum.text(json_data) date = DateTime.iso8601 - chained = Checksum.text("#{current_archive_checksum}#{last_archive_checksum}#{date}") + chained = Integrity::Checksum.text("#{current_archive_checksum}#{last_archive_checksum}#{date}") Zip::OutputStream.open(period.archive_file) do |io| io.put_next_entry(period.archive_json_file) @@ -33,7 +35,7 @@ class ArchiveWorker private def to_json_archive(period, invoices, previous_file, last_checksum) - code_checksum = Checksum.code + code_checksum = Integrity::Checksum.code ApplicationController.new.view_context.render( partial: 'archive/accounting', locals: { diff --git a/app/workers/payment_schedule_item_worker.rb b/app/workers/payment_schedule_item_worker.rb index f20582987..bbbb07538 100644 --- a/app/workers/payment_schedule_item_worker.rb +++ b/app/workers/payment_schedule_item_worker.rb @@ -18,15 +18,15 @@ class PaymentScheduleItemWorker def check_item(psi) # the following depends on the payment method (stripe/check) - if psi.payment_schedule.payment_method == 'stripe' + if psi.payment_schedule.payment_method == 'card' ### Stripe stripe_key = Setting.get('stripe_secret_key') stp_subscription = Stripe::Subscription.retrieve(psi.payment_schedule.stp_subscription_id, api_key: stripe_key) stp_invoice = Stripe::Invoice.retrieve(stp_subscription.latest_invoice, api_key: stripe_key) if stp_invoice.status == 'paid' ##### Stripe / Successfully paid - PaymentScheduleService.new.generate_invoice(psi, payment_method: 'stripe', payment_id: stp_invoice.payment_intent) - psi.update_attributes(state: 'paid', payment_method: 'stripe', stp_invoice_id: stp_invoice.id) + PaymentScheduleService.new.generate_invoice(psi, payment_method: 'card', payment_id: stp_invoice.payment_intent, payment_type: 'Stripe::PaymentIntent') # FIXME + psi.update_attributes(state: 'paid', payment_method: 'card', stp_invoice_id: stp_invoice.id) elsif stp_subscription.status == 'past_due' || stp_invoice.status == 'open' ##### Stripe / Payment error if psi.state == 'new' diff --git a/config/locales/de.yml b/config/locales/de.yml index e987adaff..e237de579 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -213,7 +213,6 @@ de: start_date: "Startdatum" expiration_date: "Ablaufdatum" amount: "Betrag" - payment_method: "Zahlungsmethode" local_payment: "Zahlung an der Rezeption" online_payment: "Online-Zahlung" deleted_user: "Gelöschter Nutzer" diff --git a/config/locales/en.yml b/config/locales/en.yml index 70bbd862d..62706de23 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -213,7 +213,6 @@ en: start_date: "Start date" expiration_date: "Expiration date" amount: "Amount" - payment_method: "Payment method" local_payment: "Payment at the reception" online_payment: "Online payment" deleted_user: "Deleted user" diff --git a/config/locales/es.yml b/config/locales/es.yml index 9ed33e4a2..9ec699883 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -213,7 +213,6 @@ es: start_date: "Fecha de comienzo" expiration_date: "Fecha de fin" amount: "Cantidad" - payment_method: "Método de pago" local_payment: "Pago en recepción" online_payment: "Pago en línea" deleted_user: "Usario eliminado" diff --git a/config/locales/fr.yml b/config/locales/fr.yml index faff52be8..6155ddaed 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -213,7 +213,6 @@ fr: start_date: "Date de début" expiration_date: "Date d'expiration" amount: "Montant" - payment_method: "Méthode de paiement" local_payment: "Paiement à l'accueil" online_payment: "Paiement en ligne" deleted_user: "Utilisateur supprimé" diff --git a/config/locales/pt.yml b/config/locales/pt.yml index 95bcd4bc3..f06d05d1f 100755 --- a/config/locales/pt.yml +++ b/config/locales/pt.yml @@ -213,7 +213,6 @@ pt: start_date: "Data de início" expiration_date: "Data de expiração" amount: "Montante" - payment_method: "Método de pagamento" local_payment: "Pagamento na recepção" online_payment: "Pagamento online" deleted_user: "Usuário deletado" diff --git a/config/locales/zu.yml b/config/locales/zu.yml index 6a93d7130..debbc8c7e 100644 --- a/config/locales/zu.yml +++ b/config/locales/zu.yml @@ -213,7 +213,6 @@ zu: start_date: "crwdns3519:0crwdne3519:0" expiration_date: "crwdns3521:0crwdne3521:0" amount: "crwdns3523:0crwdne3523:0" - payment_method: "crwdns3525:0crwdne3525:0" local_payment: "crwdns3527:0crwdne3527:0" online_payment: "crwdns3529:0crwdne3529:0" deleted_user: "crwdns20888:0crwdne20888:0" diff --git a/db/migrate/20210416083610_migrate_stripe_ids_to_payment_gateway_objects.rb b/db/migrate/20210416083610_migrate_stripe_ids_to_payment_gateway_objects.rb index 9c1156181..27b3ccd38 100644 --- a/db/migrate/20210416083610_migrate_stripe_ids_to_payment_gateway_objects.rb +++ b/db/migrate/20210416083610_migrate_stripe_ids_to_payment_gateway_objects.rb @@ -22,12 +22,14 @@ class MigrateStripeIdsToPaymentGatewayObjects < ActiveRecord::Migration[5.2] gateway_object_id: i.stp_invoice_id, gateway_object_type: 'Stripe::Invoice' ) + i.update(payment_method: 'card') elsif i.stp_payment_intent_id PaymentGatewayObject.create!( item: i, gateway_object_id: i.stp_payment_intent_id, gateway_object_type: 'Stripe::PaymentIntent' ) + i.update(payment_method: 'card') end end remove_column :invoices, :stp_invoice_id @@ -38,10 +40,11 @@ class MigrateStripeIdsToPaymentGatewayObjects < ActiveRecord::Migration[5.2] InvoiceItem.order(:id).all.each do |ii| next unless ii.stp_invoice_item_id + is_subscription = /^sub_/.match? ii.stp_invoice_item_id PaymentGatewayObject.create!( item: ii, gateway_object_id: ii.stp_invoice_item_id, - gateway_object_type: 'Stripe::InvoiceItem' + gateway_object_type: is_subscription ? 'Stripe::Subscription' : 'Stripe::InvoiceItem' ) end remove_column :invoice_items, :stp_invoice_item_id @@ -82,6 +85,7 @@ class MigrateStripeIdsToPaymentGatewayObjects < ActiveRecord::Migration[5.2] gateway_object_id: ps.stp_setup_intent_id, gateway_object_type: 'Stripe::SetupIntent' ) + ps.update(payment_method: 'card') end remove_column :payment_schedules, :stp_subscription_id remove_column :payment_schedules, :stp_setup_intent_id @@ -165,7 +169,7 @@ class MigrateStripeIdsToPaymentGatewayObjects < ActiveRecord::Migration[5.2] when 'Stripe::SetupIntent' 'stp_setup_intent_id' when 'Stripe::Subscription' - 'stp_subscription_id' + pgo.item_type == InvoiceItem.name ? 'stp_invoice_item_id' : 'stp_subscription_id' when 'Stripe::InvoiceItem' 'stp_invoice_item_id' when 'Stripe::PaymentIntent' diff --git a/test/fixtures/invoice_items.yml b/test/fixtures/invoice_items.yml index 2fb614107..3c93cf167 100644 --- a/test/fixtures/invoice_items.yml +++ b/test/fixtures/invoice_items.yml @@ -2,7 +2,6 @@ invoice_item_1: id: 1 invoice_id: 1 - stp_invoice_item_id: sub_8DGB4ErIc2asOv amount: 10000 created_at: 2012-03-12 11:03:31.651441000 Z updated_at: 2012-03-12 11:03:31.651441000 Z @@ -14,7 +13,6 @@ invoice_item_1: invoice_item_2: id: 2 invoice_id: 2 - stp_invoice_item_id: amount: 2000 created_at: 2012-03-12 13:40:22.342717000 Z updated_at: 2012-03-12 13:40:22.342717000 Z @@ -27,7 +25,6 @@ invoice_item_2: invoice_item_3: id: 3 invoice_id: 3 - stp_invoice_item_id: amount: 3000 created_at: 2015-06-10 11:20:01.341130000 Z updated_at: 2015-06-10 11:20:01.341130000 Z @@ -39,7 +36,6 @@ invoice_item_3: invoice_item_4: id: 4 invoice_id: 4 - stp_invoice_item_id: amount: 0 created_at: 2016-04-05 08:35:52.934725000 Z updated_at: 2016-04-05 08:35:52.934725000 Z @@ -51,7 +47,6 @@ invoice_item_4: invoice_item_5: id: 5 invoice_id: 5 - stp_invoice_item_id: amount: 1500 created_at: 2016-04-05 08:36:46.856021000 Z updated_at: 2016-04-05 08:36:46.856021000 Z @@ -63,7 +58,6 @@ invoice_item_5: 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 diff --git a/test/fixtures/payment_gateway_objects.yml b/test/fixtures/payment_gateway_objects.yml new file mode 100644 index 000000000..810867bcb --- /dev/null +++ b/test/fixtures/payment_gateway_objects.yml @@ -0,0 +1,11 @@ +pgo_1: + item_type: 'InvoiceItem' + item_id: 1 + gateway_object_type: 'Stripe::Subscription' + gateway_object_id: 'sub_8DGB4ErIc2asOv' + +pgo2: + item_type: 'Subscription' + item_id: 1 + gateway_object_type: 'Stripe::Subscription' + gateway_object_id: 'sub_8DGB4ErIc2asOv' diff --git a/test/fixtures/subscriptions.yml b/test/fixtures/subscriptions.yml index d9fcaa62e..49cac741d 100644 --- a/test/fixtures/subscriptions.yml +++ b/test/fixtures/subscriptions.yml @@ -3,7 +3,6 @@ subscription_1: id: 1 plan_id: 2 statistic_profile_id: 3 - stp_subscription_id: sub_8DGB4ErIc2asOv created_at: <%= 10.days.ago.utc.strftime('%Y-%m-%d %H:%M:%S.%9N Z') %> updated_at: <%= 10.days.ago.utc.strftime('%Y-%m-%d %H:%M:%S.%9N Z') %> expiration_date: <%= (1.month.from_now - 11.days).utc.strftime('%Y-%m-%d %H:%M:%S.%9N Z') %> @@ -13,7 +12,6 @@ subscription_2: id: 2 plan_id: 3 statistic_profile_id: 4 - stp_subscription_id: created_at: <%= 10.days.ago.utc.strftime('%Y-%m-%d %H:%M:%S.%9N Z') %> updated_at: <%= 10.days.ago.utc.strftime('%Y-%m-%d %H:%M:%S.%9N Z') %> expiration_date: <%= (1.month.from_now - 11.days).utc.strftime('%Y-%m-%d %H:%M:%S.%9N Z') %> @@ -24,7 +22,6 @@ subscription_3: id: 3 plan_id: 1 statistic_profile_id: 7 - stp_subscription_id: created_at: 2012-03-12 11:03:31.651441000 Z updated_at: 2012-03-12 11:03:31.651441000 Z expiration_date: 2012-04-12 11:03:31.648274000 Z @@ -35,7 +32,6 @@ 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 diff --git a/test/integration/reservations/create_as_admin_test.rb b/test/integration/reservations/create_as_admin_test.rb index 9ef816b2d..26b06a1f1 100644 --- a/test/integration/reservations/create_as_admin_test.rb +++ b/test/integration/reservations/create_as_admin_test.rb @@ -514,7 +514,7 @@ class Reservations::CreateAsAdminTest < ActionDispatch::IntegrationTest 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.stp_setup_intent_id # FIXME assert_nil payment_schedule.wallet_transaction assert_nil payment_schedule.wallet_amount assert_nil payment_schedule.coupon_id diff --git a/test/integration/reservations/create_test.rb b/test/integration/reservations/create_test.rb index e761276d4..b2e356dd8 100644 --- a/test/integration/reservations/create_test.rb +++ b/test/integration/reservations/create_test.rb @@ -840,7 +840,7 @@ class Reservations::CreateTest < ActionDispatch::IntegrationTest assert_not_nil payment_schedule.reference assert_equal 'stripe', payment_schedule.payment_method assert_not_nil payment_schedule.stp_subscription_id - assert_not_nil payment_schedule.stp_setup_intent_id + assert_not_nil payment_schedule.stp_setup_intent_id # FIXME assert_not_nil payment_schedule.wallet_transaction assert_equal payment_schedule.ordered_items.first.amount, payment_schedule.wallet_amount assert_equal Coupon.find_by(code: 'GIME3EUR').id, payment_schedule.coupon_id