diff --git a/app/controllers/alaveteli_pro/stripe_webhooks_controller.rb b/app/controllers/alaveteli_pro/stripe_webhooks_controller.rb index 383a5acb15..924e1b1347 100644 --- a/app/controllers/alaveteli_pro/stripe_webhooks_controller.rb +++ b/app/controllers/alaveteli_pro/stripe_webhooks_controller.rb @@ -57,8 +57,10 @@ def invoice_payment_succeeded charge = Stripe::Charge.retrieve(charge_id) subscription_id = @stripe_event.data.object.subscription - subscription = Stripe::Subscription.retrieve(subscription_id) - plan_name = subscription.plan.name + subscription = Stripe::Subscription.retrieve( + id: subscription_id, expand: ['plan.product'] + ) + plan_name = subscription.plan.product.name Stripe::Charge.update( charge.id, description: "#{pro_site_name}: #{plan_name}" diff --git a/spec/controllers/alaveteli_pro/stripe_webhooks_controller_spec.rb b/spec/controllers/alaveteli_pro/stripe_webhooks_controller_spec.rb index 3c3078b801..41a38e282d 100644 --- a/spec/controllers/alaveteli_pro/stripe_webhooks_controller_spec.rb +++ b/spec/controllers/alaveteli_pro/stripe_webhooks_controller_spec.rb @@ -7,7 +7,7 @@ let(:signing_secret) { config_secret } let(:stripe_helper) { StripeMock.create_test_helper } - let(:product) { stripe_helper.create_product } + let(:product) { stripe_helper.create_product(name: 'Test') } let(:stripe_customer) do Stripe::Customer.create(source: stripe_helper.generate_card_token, @@ -16,7 +16,7 @@ let(:stripe_plan) do stripe_helper.create_plan( - id: 'test', name: 'Test', product: product.id, + id: 'test', product: product.id, amount: 10, currency: 'gbp' ) end @@ -37,7 +37,7 @@ currency: stripe_plan.currency, type: 'subscription' }, - plan: { id: stripe_plan.id, name: stripe_plan.name } + plan: { id: stripe_plan.id } } ], subscription: stripe_subscription.id @@ -203,7 +203,7 @@ def send_request context 'the webhook is for a matching namespaced plan' do let(:stripe_plan) do stripe_helper.create_plan( - id: 'WDTK-test', name: 'Test', product: product.id, + id: 'WDTK-test', product: product.id, amount: 10, currency: 'gbp' ) end @@ -219,7 +219,16 @@ def send_request end it 'returns a 200 OK response' do + allow(Stripe::Subscription).to receive(:retrieve).with( + id: stripe_subscription.id, expand: ['plan.product'] + ).and_return(stripe_subscription) + + allow(stripe_subscription.plan).to receive(:product).and_return( + product + ) + send_request + expect(response.status).to eq(200) expect(response.body).to match('OK') end @@ -330,6 +339,14 @@ def send_request describe 'updating the Stripe charge description when a payment succeeds' do before do + allow(Stripe::Subscription).to receive(:retrieve).with( + id: stripe_subscription.id, expand: ['plan.product'] + ).and_return(stripe_subscription) + + allow(stripe_subscription.plan).to receive(:product).and_return( + product + ) + send_request end