Skip to content

Instantly share code, notes, and snippets.

@rocodev-tech
Created June 22, 2014 19:51
Show Gist options
  • Select an option

  • Save rocodev-tech/c0780bf77ab3a535913d to your computer and use it in GitHub Desktop.

Select an option

Save rocodev-tech/c0780bf77ab3a535913d to your computer and use it in GitHub Desktop.
card_charges_controller.rb
class CardChargesController < ApplicationController
before_action :authenticate_user!
def create
@order = current_user.orders.find_by_token(params[:order_id])
@amount = @order.total * 100 # in cents
Stripe.api_key = 'sk_test_iszGpQ7DUU1QsF6oBGnH8bf2'
customer = Stripe::Customer.create(
:email => current_user.email,
:card => params[:stripeToken]
)
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => @amount,
:description => @order.token ,
:currency => 'usd'
)
@order.set_payment_with!("credit_card")
@order.make_payment!
redirect_to order_path(@order.token), :notice => "成功完成付款"
rescue Stripe::CardError => e
flash[:error] = e.message
render "orders/pay_with_credit_card"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment