From be034c1c067946975f116e44074b7f81a697d658 Mon Sep 17 00:00:00 2001 From: Du Peng Date: Thu, 28 Dec 2023 18:32:16 +0100 Subject: [PATCH] (bug) unable to create Stripe coupon with duration = forever --- CHANGELOG.md | 1 + lib/stripe/service.rb | 2 +- lib/tasks/fablab/fix.rake | 14 ++++++++++++++ test/fixtures/coupons.yml | 2 +- test/models/coupon_test.rb | 2 +- 5 files changed, 18 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e2bd4923..b066a8ee5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ - Fix a bug: unable to show wallet payment mean for invoice - Fix a bug: PayZen amount of subscription compute error for Coupon validity per user = forever +- Fix a bug: unable to create Stripe coupon with duration = forever - improvement: remove show_username_in_admin_list setting - improvement: show invoice payment method in accounting line - [TODO DEPLOY] `rails fablab:setup:build_accounting_lines` diff --git a/lib/stripe/service.rb b/lib/stripe/service.rb index 49b13b62a..f9ee3cbce 100644 --- a/lib/stripe/service.rb +++ b/lib/stripe/service.rb @@ -66,7 +66,7 @@ class Stripe::Service < Payment::Service stp_coupon[:currency] = Setting.get('stripe_currency') end - stp_coupon[:duration] = coupon.validity_per_user == 'always' ? 'forever' : 'once' + stp_coupon[:duration] = coupon.validity_per_user 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? diff --git a/lib/tasks/fablab/fix.rake b/lib/tasks/fablab/fix.rake index 507f4ab56..bf3b1b629 100644 --- a/lib/tasks/fablab/fix.rake +++ b/lib/tasks/fablab/fix.rake @@ -1,4 +1,5 @@ # frozen_string_literal: true +require 'stripe/service' # Correctives for bugs or upgrades migrations tasks namespace :fablab do @@ -348,5 +349,18 @@ namespace :fablab do end end end + + desc '[release 6.3.6] fix stripe coupon duration' + task stripe_coupon_duration: :environment do |_task, _args| + if Setting.get('payment_gateway') == 'stripe' + Coupon.where(validity_per_user: 'forever').each do |c| + cpn = Stripe::Coupon.retrieve(c.code, api_key: Setting.get('stripe_secret_key')) + cpn.delete + Stripe::Service.new.create_coupon(c.id) + rescue Stripe::InvalidRequestError => e + puts "Unable to create coupon #{c.code} on stripe: #{e}" + end + end + end end end diff --git a/test/fixtures/coupons.yml b/test/fixtures/coupons.yml index 11673d6c7..f19fecb77 100644 --- a/test/fixtures/coupons.yml +++ b/test/fixtures/coupons.yml @@ -16,7 +16,7 @@ two: valid_until: <%= 1.month.from_now.utc.strftime('%Y-%m-%d %H:%M:%S.%9N %Z') %> max_usages: 10 active: true - validity_per_user: always + validity_per_user: forever cash: name: Cash Code diff --git a/test/models/coupon_test.rb b/test/models/coupon_test.rb index 701afdc25..bef300f4e 100644 --- a/test/models/coupon_test.rb +++ b/test/models/coupon_test.rb @@ -22,7 +22,7 @@ class CouponTest < ActiveSupport::TestCase end test 'two coupons cannot have the same code' do - c = Coupon.new({ name: 'Summer deals', code: 'SUNNYFABLAB', percent_off: 15, validity_per_user: 'always' }) + c = Coupon.new({ name: 'Summer deals', code: 'SUNNYFABLAB', percent_off: 15, validity_per_user: 'forever' }) assert c.invalid? end