【问题标题】:Trying to pass/fetch data for stripe api尝试为条带 api 传递/获取数据
【发布时间】:2020-07-06 22:58:45
【问题描述】:

我对 Ruby on Rails 还很陌生,我一直在关注tutorial,了解如何使用 Stripe 的 Connect API 实现在线市场。本教程将指导您如何购买单件商品,我试图超越本教程并创建一个市场,用户可以在其中购买多件商品并将它们放入购物篮并结帐,这是一个很好的挑战,因为本教程专注于一项。但是,我在结帐时卡住了_form.html.erb

之前,要获取publishable_key

<%= form_with model: current_user, local: true, url: subscription_url, method: :post, html: { id: "payment-form", class: "stripe-form" }, **data: { stripe_key: product.user.publishable_key }**  do |form| %>

但现在因为我要提交一个装满产品的购物车而不是单个项目,data: { stripe_key: } 现在功能失调。相反,我认为使用data: { stripe_key: @account.publishable_key } 从数据库中获取publishable_key 可能是个好主意,但它不起作用。

我很可能忘记了一些重要的事情。任何帮助都是合理的!

http://localhost:3000/subscription/new?account=4&amount=17

错误

NoMethodError in Subscriptions#new
Showing /Users/kaneandrewgibson/Desktop/Charlie/WOP/app/views/subscriptions/_form.html.erb where line #1 raised:

undefined method `publishable_key' for nil:NilClass

Subscriptions/_form.html.erb

<%= form_with model: current_user, local: true, url: subscription_url, method: :post, html: { id: "payment-form", class: "stripe-form" }, data: { stripe_key: @account.publishable_key }  do |form| %>
  <div>
    <label for="card-element" class="label">
      Credit or debit card
    </label>

    <div id="card-element">
      <!-- A Stripe Element will be inserted here. -->
    </div>

    <!-- Used to display Element errors. -->
    <div id="card-errors" role="alert" class="text-sm text-red-400"></div>
  </div>

  <input type="hidden" name="account" value="<%= params[:account] %>">

  <button>Back <%= number_to_currency(params[:amount]) %> /mo toward <em><%= %></em></button>
<% end %>

SubscriptionsController

class SubscriptionsController < ApplicationController
  before_action :authenticate_user!

  def new
    @account = User.find_by_id(params[:id])
  end

  # Reference:
  # https://stripe.com/docs/connect/subscriptions
  def create
    @account = User.find_by_id(params[:id])
    key = @account.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

              Stripe::PaymentIntent.create({
                customer: customer,
                amount: @cart.total_price * 100,
                confirm: true,
                currency: 'gbp',
                payment_method_types: ['card'],
                application_fee_percent: 3,
              }, {
                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.perk_subscriptions << plan_id
    current_user.update(options)

    # Update project attributes
    project_updates = {
      backings_count: @project.backings_count.next,
      current_donation_amount: @project.current_donation_amount + (plan.amount/100).to_i,
    }
    @project.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.perk_subscriptions.delete(plan_to_remove)
    current_user.save
    redirect_to root_path, notice: "Your subscription has been cancelled."
  end
end

【问题讨论】:

    标签: ruby-on-rails ruby marketplace


    【解决方案1】:

    凯恩。

    您的帐户 ID 似乎没有进入您的控制器。我希望 URL 看起来像这样(account_id 而不是 account):

    http://localhost:3000/subscription/new?account_id=4&amp;amount=17

    而且我希望SubscriptionsController#new 会查看Account 模型而不是User 模型,并使用相同的account_id 参数:

    @account = Account.find_by_id(params[:account_id])
    

    【讨论】:

    • @KaneAndrewGibson,如果我的回答有助于解决您的问题,请继续并接受答案,让其他人知道这有帮助。如果问题是其他问题,请添加您自己的答案,详细说明所做的事情,然后接受您的答案。这确实有助于社区了解是什么解决了您的问题。
    猜你喜欢
    • 2019-09-20
    • 2019-10-16
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    • 2021-05-27
    • 2020-02-18
    • 2019-08-17
    相关资源
    最近更新 更多