1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-01 12:24:28 +01:00
fab-manager/app/workers/stripe_worker.rb
Sylvain feeed2f450 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'
2020-12-28 18:30:27 +01:00

71 lines
2.0 KiB
Ruby

# frozen_string_literal: true
# This worker perform various requests to the Stripe API (payment service)
class StripeWorker
include Sidekiq::Worker
sidekiq_options queue: :stripe
def perform(action, *params)
send(action, *params)
end
def create_stripe_customer(user_id)
user = User.find(user_id)
customer = Stripe::Customer.create(
{
description: user.profile.full_name,
email: user.email
},
{ api_key: Setting.get('stripe_secret_key') }
)
user.update_columns(stp_customer_id: customer.id)
end
def create_stripe_coupon(coupon_id)
coupon = Coupon.find(coupon_id)
stp_coupon = {
id: coupon.code,
duration: coupon.validity_per_user
}
if coupon.type == 'percent_off'
stp_coupon[:percent_off] = coupon.percent_off
elsif coupon.type == 'amount_off'
stp_coupon[:amount_off] = coupon.amount_off
stp_coupon[:currency] = Setting.get('stripe_currency')
end
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?
Stripe::Coupon.create(stp_coupon, api_key: Setting.get('stripe_secret_key'))
end
def delete_stripe_coupon(coupon_code)
cpn = Stripe::Coupon.retrieve(coupon_code, api_key: Setting.get('stripe_secret_key'))
cpn.delete
end
def create_or_update_stp_product(class_name, id)
object = class_name.constantize.find(id)
if !object.stp_product_id.nil?
Stripe::Product.update(
object.stp_product_id,
{ name: object.name },
{ api_key: Setting.get('stripe_secret_key') }
)
else
product = Stripe::Product.create(
{
name: object.name,
metadata: {
id: object.id,
type: class_name
}
}, { 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