Ruby on rails 4 使用Stripe在支付操作之前登录用户
我试图强迫用户在使用Stripe支付操作之前登录。当我将pay添加到before_操作过滤器时,用户仍然会被提示输入其信用卡详细信息,并且只有在提交表单时才会被要求登录。当他们实际单击“付款”按钮时,如何在付款单出现之前强制登录?谢谢你的帮助Ruby on rails 4 使用Stripe在支付操作之前登录用户,ruby-on-rails-4,stripe-payments,Ruby On Rails 4,Stripe Payments,我试图强迫用户在使用Stripe支付操作之前登录。当我将pay添加到before_操作过滤器时,用户仍然会被提示输入其信用卡详细信息,并且只有在提交表单时才会被要求登录。当他们实际单击“付款”按钮时,如何在付款单出现之前强制登录?谢谢你的帮助 class ItemsController < ApplicationController before_action :logged_in_user, only: [:edit, :update, :new, :destroy, :pay
class ItemsController < ApplicationController
before_action :logged_in_user, only: [:edit, :update, :new, :destroy, :pay]
def pay
item = Item.find( params[:id] )
amount = (item.price * 100).round
name = item.name
fee = 100
email = current_user.email
begin
charge = Stripe::Charge.create(
{
amount: amount,
currency: item.user.currency,
card: params[:token],
description: name,
application_fee: fee,
receipt_email: email,
},
item.user.secret_key
)
flash[:success] = "Charged successfully! You will receive an email receipt from Stripe shortly. In the meantime feel free to browse other items. Thank you for shopping with a Sewingly seller."
rescue Stripe::CardError => e
error = e.json_body[:error][:message]
flash[:error] = "Charge failed! #{error}"
end
if charge["paid"] == true
ItemMailer.item_sold(item).deliver_now
item.update(sold: "true")
redirect_to item.user
end
end
end
雇员再培训局:
现在付款
window.stripePublishableKey='';
window.currentUserEmail='';
window.payPath='';
window.total='';
window.avatar='';
window.item=''
window.stripePublishableKey='';
window.payPath='';
window.total='';
window.avatar='';
window.item=''
在他们输入信用卡详细信息的视图中使用的控制器/操作是什么?显然,pay
方法假定已经输入了信用卡详细信息。
$(document).ready ->
return unless StripeCheckout?
# Keeps track of whether we're in the middle of processing
# a payment or not. This way we can tell if the 'closed'
# event was due to a successful token generation, or the user
# closing it by hand.
submitting = false
payButton = $('.pay-button')
form = payButton.closest('form')
indicator = form.find('.indicator').height( form.outerHeight() )
handler = StripeCheckout.configure
# The publishable key of the **connected account**.
key: window.stripePublishableKey
#key: 'pk_test_MEryapIaDEU6ckqhwuvb7NzU'
# The email of the logged in user.
email: window.currentUserEmail
allowRememberMe: false
closed: ->
form.removeClass('processing') unless submitting
token: ( token ) ->
submitting = true
form.find('input[name=token]').val( token.id )
form.get(0).submit()
payButton.click ( e ) ->
e.preventDefault()
form.addClass( 'processing' )
handler.open
name: window.item
description: window.description
amount: window.total
address: 'true'
image: window.avatar
<%= form_tag pay_item_path do %>
<%= hidden_field_tag :token %>
<button class="btn btn-primary pay-button">Pay now</button>
<% end %>
<%= javascript_include_tag 'https://checkout.stripe.com/checkout.js' %>
<% if logged_in? %>
<script>
window.stripePublishableKey = '<%= @item.user.publishable_key %>';
window.currentUserEmail = '<%= current_user.email %>';
window.payPath = '<%= pay_item_path( @item.user ) %>';
window.total = '<%= item_total %>';
window.avatar = '<%= @item.user.avatar_url %>';
window.item = '<%= @item.name %>'
</script>
<% else %>
<script>
window.stripePublishableKey = '<%= @item.user.publishable_key %>';
window.payPath = '<%= pay_item_path( @item.user ) %>';
window.total = '<%= item_total %>';
window.avatar = '<%= @item.user.avatar_url %>';
window.item = '<%= @item.name %>'
</script>