1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-29 18:52:22 +01:00

using stp_payment_intent_id instead of stp_invoice_id on new invoices

This commit is contained in:
Sylvain 2019-09-10 16:45:45 +02:00
parent 5a75db394c
commit 4d0ac9b3ca
27 changed files with 100 additions and 101 deletions

View File

@ -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.

View File

@ -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);
};
}
]

View File

@ -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);

View File

@ -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 {

View File

@ -7,7 +7,7 @@
<uib-alert ng-repeat="alert in alerts" type="{{alert.type}}" close="closeAlert($index)">{{alert.msg}}</uib-alert>
<div class="panel panel-default bg-light m-n">
<form name="stripeForm" stripe:form="payment" cart-items="cartItems" on-payment-success="onPaymentSuccess" class="form-horizontal">
<form name="stripeForm" stripe:form cart-items="cartItems" on-payment-success="onPaymentSuccess" class="form-horizontal">
<div class="panel-body">
<h3 class="m-t-xs" ng-if="walletAmount" ng-bind-html="'you_have_amount_in_wallet' | translate:{ amount: numberFilter(walletAmount, 2), currency: currencySymbol }"></h3>

View File

@ -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

View File

@ -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

View File

@ -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,

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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')

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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|

View File

@ -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

View File

@ -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
end

View File

@ -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

View File

@ -0,0 +1,5 @@
class AddStpPaymentIntentIdToInvoices < ActiveRecord::Migration
def change
add_column :invoices, :stp_payment_intent_id, :string
end
end

View File

@ -0,0 +1,5 @@
class RemoveStpInvoiceIdFromReservations < ActiveRecord::Migration
def change
remove_column :reservations, :stp_invoice_id, :string
end
end

View File

@ -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"

View File

@ -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

View File

@ -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
end

View File

@ -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? })

View File

@ -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