2019-12-03 16:32:59 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# This worker perform various requests to the Stripe API (payment service)
|
2016-03-23 18:39:41 +01:00
|
|
|
class StripeWorker
|
2021-04-14 12:18:37 +02:00
|
|
|
require 'stripe/helper'
|
2016-03-23 18:39:41 +01:00
|
|
|
include Sidekiq::Worker
|
2019-12-03 16:32:59 +01:00
|
|
|
sidekiq_options queue: :stripe
|
2016-03-23 18:39:41 +01:00
|
|
|
|
|
|
|
def perform(action, *params)
|
2021-04-14 12:18:37 +02:00
|
|
|
return false unless Stripe::Helper.enabled?
|
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
send(action, *params)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_stripe_customer(user_id)
|
|
|
|
user = User.find(user_id)
|
|
|
|
customer = Stripe::Customer.create(
|
2020-06-10 11:33:03 +02:00
|
|
|
{
|
|
|
|
description: user.profile.full_name,
|
|
|
|
email: user.email
|
|
|
|
},
|
|
|
|
{ api_key: Setting.get('stripe_secret_key') }
|
2016-03-23 18:39:41 +01:00
|
|
|
)
|
2021-06-04 18:26:20 +02:00
|
|
|
pgo = PaymentGatewayObject.find_or_initialize_by(item: user)
|
|
|
|
pgo.gateway_object = customer
|
|
|
|
pgo.save!
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|
2016-08-08 14:42:17 +02:00
|
|
|
|
|
|
|
def delete_stripe_coupon(coupon_code)
|
2020-06-10 11:33:03 +02:00
|
|
|
cpn = Stripe::Coupon.retrieve(coupon_code, api_key: Setting.get('stripe_secret_key'))
|
2016-08-08 14:42:17 +02:00
|
|
|
cpn.delete
|
2020-12-30 10:16:39 +01:00
|
|
|
rescue Stripe::InvalidRequestError => e
|
|
|
|
STDERR.puts "WARNING: Unable to delete the coupon on Stripe: #{e}"
|
2016-08-08 14:42:17 +02:00
|
|
|
end
|
2020-10-27 16:46:38 +01:00
|
|
|
|
2020-11-12 12:14:51 +01:00
|
|
|
def create_or_update_stp_product(class_name, id)
|
|
|
|
object = class_name.constantize.find(id)
|
2021-04-23 17:54:59 +02:00
|
|
|
if !object.payment_gateway_object.nil?
|
2020-11-12 12:14:51 +01:00
|
|
|
Stripe::Product.update(
|
2021-04-23 17:54:59 +02:00
|
|
|
object.payment_gateway_object.gateway_object_id,
|
2020-11-12 12:14:51 +01:00
|
|
|
{ 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') }
|
|
|
|
)
|
2021-04-23 17:54:59 +02:00
|
|
|
pgo = PaymentGatewayObject.new(item: object)
|
|
|
|
pgo.gateway_object = product
|
|
|
|
pgo.save!
|
2020-12-28 18:30:03 +01:00
|
|
|
puts "Stripe product was created for the #{class_name} \##{id}"
|
2020-11-12 12:14:51 +01:00
|
|
|
end
|
2020-12-30 18:39:46 +01:00
|
|
|
|
|
|
|
rescue Stripe::InvalidRequestError
|
2021-04-23 17:54:59 +02:00
|
|
|
obj_id = object.payment_gateway_object.gateway_object_id
|
|
|
|
STDERR.puts "WARNING: saved payment_gateway_object#id (#{obj_id}) does not match on Stripe, recreating..."
|
2020-12-30 18:39:46 +01:00
|
|
|
product = Stripe::Product.create(
|
|
|
|
{
|
|
|
|
name: object.name,
|
|
|
|
metadata: {
|
|
|
|
id: object.id,
|
|
|
|
type: class_name
|
|
|
|
}
|
|
|
|
}, { api_key: Setting.get('stripe_secret_key') }
|
|
|
|
)
|
2021-04-23 17:54:59 +02:00
|
|
|
pgo = PaymentGatewayObject.new(item: object)
|
|
|
|
pgo.gateway_object = product
|
|
|
|
pgo.save!
|
2020-12-30 18:39:46 +01:00
|
|
|
puts "Stripe product was created for the #{class_name} \##{id}"
|
2020-10-27 16:46:38 +01:00
|
|
|
end
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|