Created
June 22, 2014 19:51
-
-
Save rocodev-tech/c0780bf77ab3a535913d to your computer and use it in GitHub Desktop.
card_charges_controller.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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