2017-12-31 113 views
0

스트라이프를 사용하여 레일스로 간단한 체크 아웃을했습니다. 선택한 옵션/버튼을 기반으로 사용자에게 다른 금액이 청구되고 설명 및 목록 ID가 변경됩니다 (예 :레일 사용자 정의 금액으로 스트라이프 체크 아웃

<%= link_to "Pay To Activate", 
new_charge_path(:id => listing.id, :amount => 123, :desc => "Option A"), 
class: "btn btn-primary btn-sm", :method=> :get %> 

내가 매개 변수에서 양, 설명 및 ID를 받고있어 ChargesController이를 보낼 때 :

http://localhost:3000/charges/new?amount=123&desc=Option+A&id=45 

을 사용자가 URL에 양을 변경할 수 있기 때문에 분명히 이것은 안전하지 않습니다.

행동은 아래와 같습니다 작성 :

def create 
    customer = Stripe::Customer.create(
    :email => params[:stripeEmail], 
    :source => params[:stripeToken] 
) 

    charge = Stripe::Charge.create(
    :customer => customer.id, 
    :amount  => @amount, 
    :description => @description, 
    :currency => 'eur' 
) 

    redirect_to thankyou_path 

    rescue Stripe::CardError => e 
    flash[:error] = e.message 
    redirect_to new_charge_path 
end 

는 어떻게하면보다 안전하게 사용하려면 코드를 변경해야합니까?

+0

나는 문제의 일부가 대신 컨트롤러의 관점에서 양을 설정하는 것이있을 것 같아요. [Stripe docs] (https://stripe.com/docs/checkout/rails)는 생성 작업에 저장되는 금액을 표시합니다. –

답변

0

전문가의 의견에 따라 결제 프로세스 기능을 모델링해야합니다. 실제로

#=> model.rb 

def process_payment 
    customer = Stripe::Customer.create email: email, card: token 
    Stripe::Charge.create customer: customer.id, amount: 1000, description: 'Premium', currency: 'usd' #=> 1000 means 10 USD 
end 

컨트롤러와 같은 방법에 의해 지불을 위해 아무것도를 사용하여 어떤 모델을 내가

@payment = Payment.new({ email: params[:stripeEmail], 
         token: params[:stripeToken] 
        }) 

begin 
    @payment.process_payment 
    @payment.save 
    redirect_to thankyou_path 
rescue Exception => e 
    flash[:error] = e.message 
    @payment.destroy 
    redirect_to new_charge_path 
end 

내가 때 문제를 만들 생각 모르는 payment 모델에

이 공식을 구현하여 최상의 상식을 수립하고 안전한 지불을하는 것이 좋습니다. 귀하의 프로젝트 구조가 안전한 지불을위한 수식 일 뿐이므로 귀하의 사용이 정확하지 않다고 말씀 드려야합니다. 희망이 도움이

감사