1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-17 06:52:27 +01:00

rake task to sync users with stripe

This commit is contained in:
Sylvain 2019-12-03 16:32:59 +01:00
parent 60755a2c2a
commit f50e45d2de
4 changed files with 23 additions and 11 deletions

View File

@ -14,6 +14,7 @@
- Handle Ctrl^C in upgrade scripts
- Updated moment-timezone
- Added freeCAD files as default allowed extensions
- Rake task to sync local users with Stripe
- Fix a bug: unable to remove the picture from a training
- Fix a bug: no alerts on errors during admin creation
- Fix a bug: replaces all Time.now by DateTime.current to prevent time zones issues [Taiga#134]

View File

@ -1,6 +1,9 @@
# frozen_string_literal: true
# This worker perform various requests to the Stripe API (payment service)
class StripeWorker
include Sidekiq::Worker
sidekiq_options :queue => :stripe
sidekiq_options queue: :stripe
def perform(action, *params)
send(action, *params)
@ -18,8 +21,8 @@ class StripeWorker
def create_stripe_coupon(coupon_id)
coupon = Coupon.find(coupon_id)
stp_coupon = {
id: coupon.code,
duration: coupon.validity_per_user,
id: coupon.code,
duration: coupon.validity_per_user
}
if coupon.type == 'percent_off'
stp_coupon[:percent_off] = coupon.percent_off
@ -28,13 +31,8 @@ class StripeWorker
stp_coupon[:currency] = Rails.application.secrets.stripe_currency
end
unless coupon.valid_until.nil?
stp_coupon[:redeem_by] = coupon.valid_until.to_i
end
stp_coupon
unless coupon.max_usages.nil?
stp_coupon[:max_redemptions] = coupon.max_usages
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)
end

View File

@ -61,6 +61,7 @@ Retrieve them from https://dashboard.stripe.com/account/apikeys.
**MANDATORY**: Even if you don't want to charge your customers, you must fill this settings.
For this purpose, you can use a stripe account in test mode, which will provide you test keys.
If you change these keys during the application lifecycle, you must run `rake fablab:stripe:sync_members`, otherwise your users won't be able to do card payments.
<a name="STRIPE_CURRENCY"></a>
STRIPE_CURRENCY
@ -229,7 +230,7 @@ The check will run every weeks and if the threshold is exceeded, an alert will b
ADMIN_EMAIL, ADMIN_PASSWORD
Credentials for the first admin user created when seeding the project.
By default, theses variables are not present in application.yml because they are only used once, when running the database seed with the command `rake db:seed.
By default, theses variables are not present in application.yml because they are only used once, when running the database seed with the command `rake db:seed`.
<a name="SUPERADMIN_EMAIL"></a>
SUPERADMIN_EMAIL

View File

@ -45,5 +45,17 @@ namespace :fablab do
File.write(cassette_file, cassette)
end
end
desc 'sync users to the stripe database'
task sync_members: :environment do
User.with_role(:member).each do |member|
begin
stp_customer = Stripe::Customer.retrieve member.stp_customer_id
StripeWorker.perform_async(:create_stripe_customer, member.id) if stp_customer.nil? || stp_customer[:deleted]
rescue Stripe::InvalidRequestError
StripeWorker.perform_async(:create_stripe_customer, member.id)
end
end
end
end
end