1
我發現如果用戶第二次購買產品,則會在我的條紋儀表板中創建新客戶。我希望一旦客戶首次購買,以下購物將向該現有客戶及其保存的卡收取費用。 我的應用程序正在運行的Rails 4條紋爲現有用戶的購買創建新客戶
charges_controller.rb
def create
@user = current_user
@gift = Gift.friendly.find(params[:gift_id])
@charge = Charge.new(charge_params)
@codes = Code.all
@code = @gift.codes.where(:used => false).order("created_at ASC").first
token = params[:stripeToken]
if @user.customer_id.nil?
# Create customer
customer = Stripe::Customer.create(
:card => token,
:email => @user.email
)
# Charge the customer instead of the card
Stripe::Charge.create(
:amount => (@gift.price * 100).to_i,
:currency => "cad",
:customer => customer.id
)
# Customer_id is set to the Stripe customer.id
customer_id = customer.id
# If the transaction is approved
if @charge.save
# And user selects save_cc
if @charge.save_cc == true
# Update User's cusomter_id field to the var
@user.update_attributes(:customer_id => customer_id)
send_code(@user, @gift, @charge, @code)
else
send_code(@user, @gift, @charge, @code)
end
else
render 'charges/new'
end
else
# If user already has customer_id
Stripe::Charge.create(
:amount => (@gift.price * 100).to_i,
:currency => "cad",
:customer => @user.customer_id
)
if @charge.save
send_code(@user, @gift, @charge, @code)
else
render 'charges/new'
end
end
end
請允許我積極評論。這段代碼很醜陋,你重複了很多代碼,對於控制器操作來說太多了。試着先讓它幹,我相信一些錯誤將被修復。 –