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
|
|
|
|
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)
|
|
|
|
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
|
|
|
)
|
|
|
|
user.update_columns(stp_customer_id: customer.id)
|
|
|
|
end
|
2016-08-08 14:42:17 +02:00
|
|
|
|
|
|
|
def create_stripe_coupon(coupon_id)
|
|
|
|
coupon = Coupon.find(coupon_id)
|
2016-08-10 17:48:34 +02:00
|
|
|
stp_coupon = {
|
2019-12-03 16:32:59 +01:00
|
|
|
id: coupon.code,
|
|
|
|
duration: coupon.validity_per_user
|
2016-08-10 17:48:34 +02:00
|
|
|
}
|
2016-11-23 15:44:59 +01:00
|
|
|
if coupon.type == 'percent_off'
|
|
|
|
stp_coupon[:percent_off] = coupon.percent_off
|
|
|
|
elsif coupon.type == 'amount_off'
|
|
|
|
stp_coupon[:amount_off] = coupon.amount_off
|
2020-11-16 14:24:36 +01:00
|
|
|
stp_coupon[:currency] = Setting.get('stripe_currency')
|
2016-11-23 15:44:59 +01:00
|
|
|
end
|
|
|
|
|
2019-12-03 16:32:59 +01:00
|
|
|
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?
|
2016-08-10 17:48:34 +02:00
|
|
|
|
2020-06-10 11:33:03 +02:00
|
|
|
Stripe::Coupon.create(stp_coupon, api_key: Setting.get('stripe_secret_key'))
|
2016-08-08 14:42:17 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
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)
|
|
|
|
if !object.stp_product_id.nil?
|
|
|
|
Stripe::Product.update(
|
|
|
|
object.stp_product_id,
|
|
|
|
{ name: object.name },
|
|
|
|
{ api_key: Setting.get('stripe_secret_key') }
|
|
|
|
)
|
|
|
|
p.product
|
|
|
|
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)
|
|
|
|
end
|
2020-10-27 16:46:38 +01:00
|
|
|
end
|
2020-11-10 17:02:21 +01:00
|
|
|
|
2020-11-12 16:44:55 +01:00
|
|
|
def create_stripe_subscription(payment_schedule_id, reservable_stp_id)
|
2020-11-10 17:02:21 +01:00
|
|
|
payment_schedule = PaymentSchedule.find(payment_schedule_id)
|
|
|
|
|
2020-11-12 16:44:55 +01:00
|
|
|
first_item = payment_schedule.ordered_items.first
|
|
|
|
second_item = payment_schedule.ordered_items[1]
|
|
|
|
|
2020-11-10 17:02:21 +01:00
|
|
|
items = []
|
2020-11-12 16:44:55 +01:00
|
|
|
if first_item.amount != second_item.amount
|
2020-11-16 16:37:40 +01:00
|
|
|
unless first_item.details['adjustment']&.zero?
|
2020-11-12 16:44:55 +01:00
|
|
|
# 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 = Stripe::Price.create({
|
2020-11-16 16:37:40 +01:00
|
|
|
unit_amount: first_item.details['adjustment'],
|
2020-11-12 16:44:55 +01:00
|
|
|
currency: Setting.get('stripe_currency'),
|
|
|
|
product: payment_schedule.scheduled.plan.stp_product_id,
|
2020-11-16 16:37:40 +01:00
|
|
|
nickname: "Price adjustment for payment schedule #{payment_schedule_id}"
|
2020-11-12 16:44:55 +01:00
|
|
|
}, { api_key: Setting.get('stripe_secret_key') })
|
|
|
|
items.push(price: p1[:id])
|
|
|
|
end
|
2020-11-16 16:37:40 +01:00
|
|
|
unless first_item.details['other_items']&.zero?
|
2020-11-12 16:44:55 +01:00
|
|
|
# when taking a subscription at the same time of a reservation (space, machine or training), the amount of the
|
|
|
|
# reservation is invoiced here.
|
|
|
|
p2 = Stripe::Price.create({
|
2020-11-16 16:37:40 +01:00
|
|
|
unit_amount: first_item.details['other_items'],
|
2020-11-12 16:44:55 +01:00
|
|
|
currency: Setting.get('stripe_currency'),
|
|
|
|
product: reservable_stp_id,
|
|
|
|
nickname: "Reservations for payment schedule #{payment_schedule_id}"
|
|
|
|
}, { api_key: Setting.get('stripe_secret_key') })
|
|
|
|
items.push(price: p2[:id])
|
|
|
|
end
|
2020-11-10 17:02:21 +01:00
|
|
|
end
|
2020-11-12 16:44:55 +01:00
|
|
|
|
|
|
|
# subscription (recurring price)
|
|
|
|
price = Stripe::Price.create({
|
2020-11-16 16:37:40 +01:00
|
|
|
unit_amount: first_item.details['recurring'],
|
2020-11-12 16:44:55 +01:00
|
|
|
currency: Setting.get('stripe_currency'),
|
|
|
|
recurring: { interval: 'month', interval_count: 1 },
|
|
|
|
product: payment_schedule.scheduled.plan.stp_product_id
|
|
|
|
},
|
|
|
|
{ api_key: Setting.get('stripe_secret_key') })
|
|
|
|
|
2020-11-12 12:14:51 +01:00
|
|
|
stp_subscription = Stripe::Subscription.create({
|
|
|
|
customer: payment_schedule.invoicing_profile.user.stp_customer_id,
|
2020-11-17 16:23:55 +01:00
|
|
|
cancel_at: payment_schedule.scheduled.expiration_date.to_i,
|
2020-11-12 12:14:51 +01:00
|
|
|
promotion_code: payment_schedule.coupon&.code,
|
|
|
|
add_invoice_items: items,
|
|
|
|
items: [
|
2020-11-12 16:44:55 +01:00
|
|
|
{ price: price[:id] }
|
2020-11-12 12:14:51 +01:00
|
|
|
]
|
|
|
|
}, { api_key: Setting.get('stripe_secret_key') })
|
|
|
|
payment_schedule.update_attributes(stp_subscription_id: stp_subscription.id)
|
2020-11-10 17:02:21 +01:00
|
|
|
end
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|