From 700c5e772743dc8c1f648ad8b6bf42018ef938cb Mon Sep 17 00:00:00 2001 From: Blake Erickson Date: Wed, 28 Aug 2024 15:03:22 -0600 Subject: [PATCH] FIX: Payments not showing up for users (#233) Pricing table payments for one off purchases need to be attached to the customer or they don't show up on the user's billing payments page. --- .../discourse_subscriptions/hooks_controller.rb | 6 +++++- .../discourse_subscriptions/user/payments_controller.rb | 8 ++++++++ spec/requests/hooks_controller_spec.rb | 6 ++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/app/controllers/discourse_subscriptions/hooks_controller.rb b/app/controllers/discourse_subscriptions/hooks_controller.rb index cc5e143..c7210fa 100644 --- a/app/controllers/discourse_subscriptions/hooks_controller.rb +++ b/app/controllers/discourse_subscriptions/hooks_controller.rb @@ -57,9 +57,13 @@ module DiscourseSubscriptions discourse_customer = Customer.create(user_id: user.id, customer_id: customer_id) subscription = checkout_session[:subscription] + payment_intent_id = checkout_session[:payment_intent] - if !subscription.nil? + if subscription.present? Subscription.create(customer_id: discourse_customer.id, external_id: subscription) + elsif payment_intent_id + # Attach the payment intent to the customer for one-off purchases + ::Stripe::PaymentIntent.update(payment_intent_id, { customer: customer_id }) end line_items = diff --git a/app/controllers/discourse_subscriptions/user/payments_controller.rb b/app/controllers/discourse_subscriptions/user/payments_controller.rb index b1acff7..70973fa 100644 --- a/app/controllers/discourse_subscriptions/user/payments_controller.rb +++ b/app/controllers/discourse_subscriptions/user/payments_controller.rb @@ -28,10 +28,18 @@ module DiscourseSubscriptions payments_from_invoices = payments[:data].select { |payment| invoice_ids.include?(payment[:invoice]) } + if SiteSetting.discourse_subscriptions_enable_verbose_logging + Rails.logger.warn("Payments from invoices: #{payments_from_invoices}") + end + # Pricing table one-off purchases do not have invoices payments_without_invoices = payments[:data].select { |payment| payment[:invoice].nil? } + if SiteSetting.discourse_subscriptions_enable_verbose_logging + Rails.logger.warn("Payments without invoices: #{payments_without_invoices}") + end + data = data | payments_from_invoices | payments_without_invoices end end diff --git a/spec/requests/hooks_controller_spec.rb b/spec/requests/hooks_controller_spec.rb index 9641036..6afc958 100644 --- a/spec/requests/hooks_controller_spec.rb +++ b/spec/requests/hooks_controller_spec.rb @@ -94,6 +94,7 @@ RSpec.describe DiscourseSubscriptions::HooksController do metadata: { }, mode: "subscription", + payment_intent: "pi_3PsohkGHcn", payment_status: "paid", status: "complete", submit_type: nil, @@ -219,6 +220,11 @@ RSpec.describe DiscourseSubscriptions::HooksController do ::Stripe::Webhook.stubs(:construct_event).returns(event) ::Stripe::Customer.stubs(:create).returns(id: "cus_1234") + + ::Stripe::PaymentIntent + .expects(:update) + .with("pi_3PsohkGHcn", { customer: "cus_1234" }) + .returns({ id: "pi_3PsohkGHcn" }) end it "is returns 200" do