Skip to content

Commit

Permalink
Replace deprecated Stripe save calls
Browse files Browse the repository at this point in the history
These have been replaced with calls to `create` or `update`.
  • Loading branch information
gbp committed Oct 10, 2024
1 parent 6eda678 commit dc49b45
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 39 deletions.
7 changes: 3 additions & 4 deletions app/controllers/alaveteli_pro/stripe_webhooks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,9 @@ def invoice_payment_succeeded
subscription = Stripe::Subscription.retrieve(subscription_id)
plan_name = subscription.plan.name

charge.description =
"#{ pro_site_name }: #{ plan_name }"

charge.save
Stripe::Charge.update(
charge.id, description: "#{pro_site_name}: #{plan_name}"
)
end
end

Expand Down
10 changes: 4 additions & 6 deletions app/controllers/alaveteli_pro/subscriptions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,14 @@ def create
@pro_account.token = @token
@pro_account.update_stripe_customer

@subscription = @pro_account.subscriptions.build
@subscription.update_attributes(
attributes = {
plan: params.require(:plan_id),
tax_percent: tax_percent,
payment_behavior: 'allow_incomplete'
)

@subscription.coupon = coupon_code if coupon_code?
}
attributes[:coupon] = coupon_code if coupon_code?

@subscription.save
@subscription = @pro_account.subscriptions.create(attributes)

rescue ProAccount::CardError,
Stripe::CardError => e
Expand Down
7 changes: 2 additions & 5 deletions app/models/alaveteli_pro/subscription_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@ def initialize(customer)
@customer = customer
end

def build
def create(attributes = {})
AlaveteliPro::Subscription.new(
Stripe::Subscription.new.tap do |subscription|
params = { customer: @customer }
subscription.update_attributes(params)
end
Stripe::Subscription.create(attributes.merge(customer: @customer.id))
)
end

Expand Down
31 changes: 14 additions & 17 deletions app/models/pro_account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,29 +49,26 @@ def update_stripe_customer
return unless feature_enabled?(:pro_pricing)

@subscriptions = nil unless stripe_customer
@stripe_customer = stripe_customer || Stripe::Customer.new

update_email
update_source
attributes = {}
attributes[:email] = user.email if stripe_customer.try(:email) != user.email
attributes[:source] = @token.id if @token

@stripe_customer = (
if attributes.empty?
stripe_customer
elsif stripe_customer
Stripe::Customer.update(stripe_customer.id, attributes)
else
Stripe::Customer.create(attributes)
end
)

stripe_customer.save
update(stripe_customer_id: stripe_customer.id)
update(stripe_customer_id: @stripe_customer.id)
end

private

def update_email
return unless stripe_customer.try(:email) != user.email

stripe_customer.email = user.email
end

def update_source
return unless @token

stripe_customer.source = @token.id
end

def stripe_customer!
return unless stripe_customer_id

Expand Down
30 changes: 25 additions & 5 deletions spec/models/alaveteli_pro/subscription_collection_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'spec_helper'
require 'stripe_mock'

RSpec.describe AlaveteliPro::SubscriptionCollection do
let(:collection) { described_class.new(customer) }
Expand Down Expand Up @@ -26,20 +27,39 @@
end
end

describe '#build' do
describe '#create' do
before { StripeMock.start }
after { StripeMock.stop }

let(:stripe_helper) { StripeMock.create_test_helper }

let(:product) { stripe_helper.create_product }

let(:plan) do
stripe_helper.create_plan(
id: 'pro', product: product.id, amount: 1000
)
end

let(:customer) do
Stripe::Customer.create(
email: '[email protected]', source: stripe_helper.generate_card_token
)
end

let(:collection) { described_class.new(customer) }
let(:subscription) { collection.build }
let(:subscription) { collection.create(plan: plan.id) }

it 'should build new subscription' do
it 'should create new subscription' do
expect(subscription).to be_a AlaveteliPro::Subscription
end

it 'should delegated to a Stripe subscription' do
expect(subscription.__getobj__).to be_a Stripe::Subscription
end

it 'should set customer object' do
expect(subscription.customer).to eq customer
it 'should set customer ID' do
expect(subscription.customer).to eq customer.id
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/models/pro_account_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@
end

it 'creates Stripe customer' do
allow(customer).to receive(:save)
allow(Stripe::Customer).to receive(:create).and_call_original
pro_account.update_stripe_customer
expect(customer).to have_received(:save)
expect(Stripe::Customer).to have_received(:create)
end

it 'sets Stripe customer email' do
Expand Down

0 comments on commit dc49b45

Please sign in to comment.