ruby-on-rails 条带化不会创建订阅

atmip9wb  于 2023-10-21  发布在  Ruby
关注(0)|答案(2)|浏览(118)

我正在创建一个定期订阅服务。每个人都可以提供服务并订阅它。但未创建订阅

errStripe::PermissionError (The provided key 'sk_test_***********************************************************************************************5ERf' does not have access to account 'sk_test_51IFXucFi40vayVzCHn4wI2NLmnRO2f7dq1Bs8YQ5OEq0UfqtfXnBE5Ki4sCqASQjQopnRIUUIcELMkQHGnJlQ51N00BEpN5ERf' (or that account does not exist). Application access may have been revoked.):
app/controllers/subscriptions_controller.rb:26:in `create'

我希望创建订阅,但没有创建。如果我使用数据传输参数,而不是“account”,我在订阅控制器的第35行写了smth,比如“acccccccccount”,从而没有传递用户的密钥,那么订阅就创建了,一切都很好,但是不可能查看用户的订阅,另一个错误就会发生。
则创建订阅,但无法读取用户的订阅

app/views/devise/registrations/edit.html.erb where line #77 raised:

undefined method `subscriptions' for #<Stripe::Customer:0x55dc id=cus_OZ8JiUXM7tbdhR> JSON: {
  "id": "cus_OZ8JiUXM7tbdhR",
  "object": "customer",
  "address": null,
  "balance": 0,
  "created": 1693685042,
  "currency": "eur",
  "default_source": "card_1Nm032JD03t24L8TuSZbeBce",
  "delinquent": false,
  "description": null,
  "discount": null,
  "email": "[email protected]",
  "invoice_prefix": "830E3B39",
  "invoice_settings": {"custom_fields":null,"default_payment_method":null,"footer":null,"rendering_options":null},
  "livemode": false,
  "metadata": {},
  "name": null,
  "phone": null,
  "preferred_locales": [

  ],
  "shipping": null,
  "tax_exempt": "none",
  "test_clock": null
}
Did you mean?  description

subscription_controller:

class SubscriptionsController < ApplicationController
  before_action :authenticate_user!

  def new
    @service = Service.find(params[:service])
  end

  # Reference:
  # https://stripe.com/docs/connect/subscriptions
  def create
    @service = Service.find(params[:service])
    key = @service.user.access_code
    Stripe.api_key = key

    plan_id = params[:plan]
    plan = Stripe::Plan.retrieve(plan_id)
    token = params[:stripeToken]

    customer = if current_user.stripe_id?
                 Stripe::Customer.retrieve(current_user.stripe_id)
               else
                 Stripe::Customer.create(email: current_user.email, source: token)
               end

    subscription = Stripe::Subscription.create({
                                                 customer: customer,
                                                 items: [
                                                   {
                                                     plan: plan
                                                   }
                                                 ],
                                                 expand: ["latest_invoice.payment_intent"],
                                                 application_fee_percent: 10,
                                               }, stripe_account: key)

    options = {
      stripe_id: customer.id,
      subscribed: true,
    }

    options.merge!(
      card_last4: params[:user][:card_last4],
      card_exp_month: params[:user][:card_exp_month],
      card_exp_year: params[:user][:card_exp_year],
      card_type: params[:user][:card_brand]
    )

    current_user.feature_subscriptions << plan_id
    current_user.update(options)

    # Update project attributes
    project_updates = {
      backings_count: @service.backings_count.next,
      current_amount: @service.current_amount + (plan.amount/100).to_i,
    }
    @service.update(project_updates)

    redirect_to root_path, notice: "Your subscription was setup successfully!"
  end

  def destroy
    subscription_to_remove = params[:id]
    plan_to_remove = params[:plan_id]
    customer = Stripe::Customer.retrieve(current_user.stripe_id)
    customer.subscriptions.retrieve(subscription_to_remove).delete
    current_user.subscribed = false
    current_user.feature_subscriptions.delete(plan_to_remove)
    current_user.save
    redirect_to root_path, notice: "Your subscription has been cancelled."
  end
end

使用用户订阅设计视图:

<% if resource.subscribed? %>
    <% customer = Stripe::Customer.retrieve(current_user.stripe_id) %>
    <% if customer.subscriptions.any? %>
      <div class="p-6 mt-10 text-gray-900 bg-white rounded shadow-lg">
        <h4 class="font-bold text-gray-900">Active subscriptions</h4>
        <ul>
          <% customer.subscriptions.list.data.each do |sub| %>
            <li class="flex items-center justify-between py-4 border-b">
              <div><%= sub.plan.nickname %></div>
              <%= link_to "Cancel Subscription", subscription_path(id: sub.id, plan_id: sub.plan.id), method: :delete, data: { confirm: "Are you sure?" } %>
            </li>
          <% end %>
        </ul>
      </div>
    <% end %>
  <% end %>
</div>

我想创建订阅并监视用户订阅

vktxenjb

vktxenjb1#

看起来你完全错误地使用了Stripe API的多个部分。
第一个错误,测试密钥-为了安全起见,你应该把它给每个人-看起来像你正在使用添加密钥而不是连接的帐户ID(看起来像'acct_xxx')。
您在查找客户订阅列表时遇到的问题可能是API版本问题-要么太旧而无法拥有该属性,要么更新/等于2020-08-27,此时Stripe将其内容删除到默认调用,使您需要扩展它们。
看起来你还在编造一些不是Stripe的有效API路径的方法(比如你的customer.subscriptions.retrieve().delete()),但这可能是因为我缺乏Ruby知识。
此外,除了查看Customer的subscriptions属性之外,您还有其他选项,您还可以按Customer列出Subscriptions(使用customer参数)。
我认为很多问题都可以通过查找一些Stripe官方文档来解决。

zte4gxcn

zte4gxcn2#

Stripe的文档在项目处于停滞状态时发生了变化。我们需要expand for subscriptiondelete来更改为cancel

def destroy
    subscription_to_remove = params[:id]
    plan_to_remove = params[:plan_id]
    customer = Stripe::Customer.retrieve(id: current_user.stripe_id, expand: ['subscriptions'])
    customer.subscriptions.retrieve(subscription_to_remove).cancel
    current_user.subscribed = false
    current_user.feature_subscriptions.delete(plan_to_remove)
    current_user.save
    redirect_to root_path, notice: "Your subscription has been cancelled."
  end

相关问题