1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-10 21:24:20 +01:00
fab-manager/app/workers/stripe_worker.rb
Sylvain 728ae4310c improved testing of payment schedules + some fixes
TODO: fix the test
rails test test/integration/reservations/create_test.rb
Reservations::CreateTest#test_user_reserves_a_machine_and_renew_a_subscription_with_payment_schedule_and_coupon_and_wallet [test/integration/reservations/create_test.rb:841]
Minitest::Assertion: Expected: "stripe"
  Actual: nil
2020-12-30 18:43:33 +01:00

68 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 delete_stripe_coupon(coupon_code)
cpn = Stripe::Coupon.retrieve(coupon_code, api_key: Setting.get('stripe_secret_key'))
cpn.delete
rescue Stripe::InvalidRequestError => e
STDERR.puts "WARNING: Unable to delete the coupon on Stripe: #{e}"
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
rescue Stripe::InvalidRequestError
STDERR.puts "WARNING: saved stp_product_id (#{object.stp_product_id}) does not match on Stripe, recreating..."
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