2016-03-23 18:39:41 +01:00
|
|
|
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
|
|
|
|
)
|
|
|
|
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 = {
|
2016-08-08 14:42:17 +02: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
|
|
|
|
stp_coupon[:currency] = Rails.application.secrets.stripe_currency
|
|
|
|
end
|
|
|
|
|
2016-08-10 17:48:34 +02:00
|
|
|
unless coupon.valid_until.nil?
|
|
|
|
stp_coupon[:redeem_by] = coupon.valid_until.to_i
|
|
|
|
end
|
|
|
|
stp_coupon
|
|
|
|
unless coupon.max_usages.nil?
|
2016-08-16 18:12:13 +02:00
|
|
|
stp_coupon[:max_redemptions] = coupon.max_usages
|
2016-08-10 17:48:34 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
Stripe::Coupon.create(stp_coupon)
|
2016-08-08 14:42:17 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def delete_stripe_coupon(coupon_code)
|
|
|
|
cpn = Stripe::Coupon.retrieve(coupon_code)
|
|
|
|
cpn.delete
|
|
|
|
end
|
2016-03-23 18:39:41 +01:00
|
|
|
end
|