【问题标题】:Stripe Webhook firing but Stripe event method not runningStripe Webhook 触发但 Stripe 事件方法未运行
【发布时间】:2020-01-21 03:53:57
【问题描述】:

使用条带事件 gem,我正在尝试在事件取消订阅时更新条带订阅。即使他们正在接收 webhook,我的事件也没有触发。

我在 ngrok 上运行我的端点并使用 stripe-events gem。我已经安装了条带 cli,所以我可以看到 webhook 事件正在触发。

我的想法是我尝试使用的代码有问题

@user = User.find_by_stripe_id(event.data.object.customer) 但是我从 GoRails 上的一个线程中获得了这段代码。 Chris Oliver 说它看起来是正确的,我已经将它重新用于我的应用程序,所以这让我觉得它可能是别的东西。

# config/initializers/stripe_events.rb
Stripe.api_key = Rails.application.credentials.stripe_publishable_key
Stripe.api_key = Rails.application.credentials.stripe_signing_secret

StripeEvent.configure do |events|
  events.subscribe "customer.subscription.updated" do |event|
      @user = User.find_by_stripe_id(event.data.object.customer)  
      logger.info "Processing the request..."
      if DateTime.now >= @user.subscription_end_date
        logger.info "if statement is running..."
      @user.update_attribute(subscribed: false)
      @user.save!
      end
  end
end

我不确定这是否需要,但这里是触发 webhook 的方法。我添加了 DateTime.now-1 用于测试 webhook 是否可以模拟过期订阅。

# app/controllers/subscriptions_controller.rb
  def destroy
        if Rails.env.production? 
            Stripe.api_key = Rails.application.credentials.stripe_live_api
        else
            Stripe.api_key = Rails.application.credentials.stripe_api_key
        end
        customer = Stripe::Customer.retrieve(current_user.stripe_id)
        customer.subscriptions.retrieve(current_user.stripe_subscription_id)
        Stripe::Subscription.update(current_user.stripe_subscription_id,:cancel_at_period_end => true)
        ##current_user.update(subscription_end_date: Time.at(customer.subscriptions[:data].first[:current_period_end]))
        current_user.update(subscription_end_date: DateTime.now-1)
        redirect_to root_path, notice: "Your subscription has been cancelled"
    end

我测试了条带 webhook 的响应

{
  "object": {
    "id": "sub_FqLLZZ4pKhlM0y",
    "object": "subscription",
    "application_fee_percent": null,
    "billing": "charge_automatically",
    "billing_cycle_anchor": 1568957293,
    "billing_thresholds": null,
    "cancel_at": 1571549293,
    "cancel_at_period_end": true,
    "canceled_at": 1568957302,
    "collection_method": "charge_automatically",
    "created": 1568957293,
    "current_period_end": 1571549293,
    "current_period_start": 1568957293,
    "customer": "cus_FqLLXYHyVl7mJq",
    "days_until_due": null,
    "default_payment_method": null,
    "default_source": null,
    "default_tax_rates": [
    ],
    "discount": null,
    "ended_at": null,
    "items": {
      "object": "list",
      "data": [
        {
          "id": "si_FqLLngQNhxl5p4",
          "object": "subscription_item",
          "billing_thresholds": null,
          "created": 1568957293,
          "metadata": {
          },
          "plan": {
            "id": "plan_FYHrCVDopLPS37",
            "object": "plan",
            "active": true,
            "aggregate_usage": null,
            "amount": 2900,
            "amount_decimal": "2900",
            "billing_scheme": "per_unit",
            "created": 1564792787,
            "currency": "usd",
            "interval": "month",
            "interval_count": 1,
            "livemode": false,
            "metadata": {
            },
            "nickname": "Uproar Basic",
            "product": "prod_FYHpmwiftJrqpC",
            "tiers": null,
            "tiers_mode": null,
            "transform_usage": null,
            "trial_period_days": null,
            "usage_type": "licensed"
          },
          "quantity": 1,
          "subscription": "sub_FqLLZZ4pKhlM0y",
          "tax_rates": [
          ]
        }
      ],
      "has_more": false,
      "total_count": 1,
      "url": "/v1/subscription_items?subscription=sub_FqLLZZ4pKhlM0y"
    },
    "latest_invoice": "in_1FKefBJCkfo4YGOMvdEjZ3ny",
    "livemode": false,
    "metadata": {
    },
    "pending_setup_intent": null,
    "plan": {
      "id": "plan_FYHrCVDopLPS37",
      "object": "plan",
      "active": true,
      "aggregate_usage": null,
      "amount": 2900,
      "amount_decimal": "2900",
      "billing_scheme": "per_unit",
      "created": 1564792787,
      "currency": "usd",
      "interval": "month",
      "interval_count": 1,
      "livemode": false,
      "metadata": {
      },
      "nickname": "Uproar Basic",
      "product": "prod_FYHpmwiftJrqpC",
      "tiers": null,
      "tiers_mode": null,
      "transform_usage": null,
      "trial_period_days": null,
      "usage_type": "licensed"
    },
    "quantity": 1,
    "schedule": null,
    "start": 1568957302,
    "start_date": 1568957293,
    "status": "active",
    "tax_percent": null,
    "trial_end": null,
    "trial_start": null
  },
  "previous_attributes": {
    "cancel_at": null,
    "cancel_at_period_end": false,
    "canceled_at": null,
    "start": 1568957293
  }
}

【问题讨论】:

    标签: ruby-on-rails stripe-payments


    【解决方案1】:

    我不确定 config/initializers/stripe_events.rb 中的代码是否可以在您的应用启动后访问应用/控制器。

    你应该调用一个服务:

    config/initializers/stripe_events.rb

    StripeEvent.configure do |events|
      events.subscribe "customer.subscription.updated" do |event|
        StripeUdpateSubscription.new
      end
    end
    

    app/services/stripe_update_subscription.rb

    class StripeUpdateSubscription
      def call(event)
        ..// you code here
      end
    end
    

    【讨论】:

      猜你喜欢
      • 2020-06-10
      • 2016-04-14
      • 2015-10-24
      • 2019-04-14
      • 2020-12-01
      • 2023-03-11
      • 2018-10-20
      • 2014-05-01
      • 2020-06-21
      相关资源
      最近更新 更多