我正在尝试强制用户在使用Stripe进行付费操作之前登录。当我向before_action过滤器添加付款时,仍然会提示用户输入他们的信用卡详细信息,并且只有当他们提交表单时才会要求他们登录。在付款表单出现之前,当他们实际点击付款按钮时,您如何强制登录?谢谢你的帮助!
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
pay.coffee:
$(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
ERB:
<%= 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>