正在尝试 pass/fetch 条带 api 的数据
Trying to pass/fetch data for stripe api
我在 Rails 上对 Ruby 还很陌生,我一直在关注 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
凯恩。
您的帐户 ID 似乎没有进入您的控制器。我希望 URL 看起来像这样(account_id
而不是 account
):
http://localhost:3000/subscription/new?account_id=4&amount=17
而且我希望 SubscriptionsController#new
查找 Account
模型而不是 User
模型,并使用相同的 account_id
参数:
@account = Account.find_by_id(params[:account_id])
我在 Rails 上对 Ruby 还很陌生,我一直在关注 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
凯恩。
您的帐户 ID 似乎没有进入您的控制器。我希望 URL 看起来像这样(account_id
而不是 account
):
http://localhost:3000/subscription/new?account_id=4&amount=17
而且我希望 SubscriptionsController#new
查找 Account
模型而不是 User
模型,并使用相同的 account_id
参数:
@account = Account.find_by_id(params[:account_id])