mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2024-12-01 12:24:28 +01:00
fix take a reservation + subscription
FIXME: ActionView::Template::Error (undefined method `iso8601' for nil:NilClass) Callstack: app/views/api/reservations/show.json.jbuilder:40:in `_app_views_api_reservations_show_json_jbuilder' app/controllers/api/payments_controller.rb:91:in `confirm_payment_schedule'
This commit is contained in:
parent
3e4892d3a5
commit
feeed2f450
@ -187,6 +187,11 @@ class API::PaymentsController < API::ApiController
|
||||
slots = cart_items_params[:slots_attributes] || []
|
||||
nb_places = cart_items_params[:nb_reserve_places]
|
||||
tickets = cart_items_params[:tickets_attributes]
|
||||
user_id = if current_user.admin? || current_user.manager?
|
||||
params[:cart_items][:reservation][:user_id]
|
||||
else
|
||||
current_user.id
|
||||
end
|
||||
else
|
||||
raise NotImplementedError unless params[:cart_items][:subscription]
|
||||
|
||||
@ -195,10 +200,15 @@ class API::PaymentsController < API::ApiController
|
||||
slots = []
|
||||
nb_places = nil
|
||||
tickets = nil
|
||||
user_id = if current_user.admin? || current_user.manager?
|
||||
params[:cart_items][:subscription][:user_id]
|
||||
else
|
||||
current_user.id
|
||||
end
|
||||
end
|
||||
|
||||
price_details = Price.compute(false,
|
||||
current_user,
|
||||
User.find(user_id),
|
||||
reservable,
|
||||
slots,
|
||||
plan_id: plan_id,
|
||||
|
@ -24,7 +24,11 @@ function extractHumanReadableMessage(error: any): string {
|
||||
// parse ruby error pages
|
||||
const parser = new DOMParser();
|
||||
const htmlDoc = parser.parseFromString(error, 'text/html');
|
||||
return htmlDoc.querySelector('h2').textContent;
|
||||
if (htmlDoc.querySelectorAll('h2').length > 2) {
|
||||
return htmlDoc.querySelector('h2').textContent;
|
||||
} else {
|
||||
return htmlDoc.querySelector('h1').textContent;
|
||||
}
|
||||
}
|
||||
return error;
|
||||
}
|
||||
|
@ -55,9 +55,9 @@ class PaymentSchedule < PaymentDocument
|
||||
return unless Setting.get('invoicing_module')
|
||||
|
||||
unless Rails.env.test?
|
||||
puts "Creating an InvoiceWorker job to generate the following invoice: id(#{id}), invoiced_id(#{invoiced_id}), " \
|
||||
"invoiced_type(#{invoiced_type}), user_id(#{invoicing_profile.user_id})"
|
||||
puts "Creating an PaymentScheduleWorker job to generate the following payment schedule: id(#{id}), scheduled_id(#{scheduled_id}), " \
|
||||
"scheduled_type(#{scheduled_type}), user_id(#{invoicing_profile.user_id})"
|
||||
end
|
||||
InvoiceWorker.perform_async(id, user&.subscription&.expired_at)
|
||||
PaymentScheduleWorker.perform_async(id)
|
||||
end
|
||||
end
|
||||
|
@ -64,7 +64,7 @@ class PaymentScheduleService
|
||||
item.save!
|
||||
end
|
||||
|
||||
StripeService.create_stripe_subscription(ps.id, reservation&.reservable&.stp_product_id, setup_intent_id) if payment_method == 'stripe'
|
||||
StripeService.create_stripe_subscription(ps.id, subscription, reservation&.reservable&.stp_product_id, setup_intent_id) if payment_method == 'stripe'
|
||||
ps
|
||||
end
|
||||
end
|
||||
|
@ -5,7 +5,7 @@ class StripeService
|
||||
class << self
|
||||
|
||||
# Create the provided PaymentSchedule on Stripe, using the Subscription API
|
||||
def create_stripe_subscription(payment_schedule_id, reservable_stp_id, setup_intent_id)
|
||||
def create_stripe_subscription(payment_schedule_id, subscription, reservable_stp_id, setup_intent_id)
|
||||
stripe_key = Setting.get('stripe_secret_key')
|
||||
payment_schedule = PaymentSchedule.find(payment_schedule_id)
|
||||
first_item = payment_schedule.ordered_items.first
|
||||
@ -14,14 +14,14 @@ class StripeService
|
||||
intent = Stripe::SetupIntent.retrieve(setup_intent_id, api_key: stripe_key)
|
||||
# subscription (recurring price)
|
||||
price = create_price(first_item.details['recurring'],
|
||||
payment_schedule.scheduled.plan.stp_product_id,
|
||||
subscription.plan.stp_product_id,
|
||||
nil, monthly: true)
|
||||
# other items (not recurring)
|
||||
items = subscription_invoice_items(payment_schedule, first_item, reservable_stp_id)
|
||||
items = subscription_invoice_items(payment_schedule, subscription, first_item, reservable_stp_id)
|
||||
|
||||
stp_subscription = Stripe::Subscription.create({
|
||||
customer: payment_schedule.invoicing_profile.user.stp_customer_id,
|
||||
cancel_at: payment_schedule.scheduled.expiration_date.to_i,
|
||||
cancel_at: subscription.expiration_date.to_i,
|
||||
promotion_code: payment_schedule.coupon&.code,
|
||||
add_invoice_items: items,
|
||||
items: [
|
||||
@ -34,7 +34,7 @@ class StripeService
|
||||
|
||||
private
|
||||
|
||||
def subscription_invoice_items(payment_schedule, first_item, reservable_stp_id)
|
||||
def subscription_invoice_items(payment_schedule, subscription, first_item, reservable_stp_id)
|
||||
second_item = payment_schedule.ordered_items[1]
|
||||
|
||||
items = []
|
||||
@ -43,7 +43,7 @@ class StripeService
|
||||
# adjustment: when dividing the price of the plan / months, sometimes it forces us to round the amount per month.
|
||||
# The difference is invoiced here
|
||||
p1 = create_price(first_item.details['adjustment'],
|
||||
payment_schedule.scheduled.plan.stp_product_id,
|
||||
subscription.plan.stp_product_id,
|
||||
"Price adjustment for payment schedule #{payment_schedule.id}")
|
||||
items.push(price: p1[:id])
|
||||
end
|
||||
|
@ -64,6 +64,7 @@ class StripeWorker
|
||||
}, { api_key: Setting.get('stripe_secret_key') }
|
||||
)
|
||||
object.update_attributes(stp_product_id: product.id)
|
||||
puts "Stripe product was created for the #{class_name} \##{id}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user