2012-08-19 3 views
1

저는 Rails 앱을 만들고 신용 카드를 사용하고 있는데 Stripe을 사용하려고합니다. 청구하기 위해 내 앱에서 스트라이프로 데이터를 전달하는 데 문제가 있습니다. 이것이 내가이 주제에 대한 도움을 얻기를 희망하는 이유입니다.Rails 앱에서 Stripe으로 카드를 충전 할 때 문제가 발생했습니다.

먼저 표준 형식 (테스트 목적으로 빠른 제출을위한 자리 표시 자 대신 값 사용)이 있습니다. 양식이 성공적으로 DB에 이름과 이메일을 입력하고 고객의 "계획은"당분간 컨트롤러에 하드 코딩 : 위의 코드는 signups_view, 나는이 JS 한 내에서 또한

<%= form_for @customer do |f| %> 
     <div class="payment-errors"></div> 
     <div class="name field"> 
     <%= f.label :name %> 
     <%= f.text_field :name, :value => "Your name" %> 
     </div> 
     <div class="email field"> 
     <%= f.label :email %> 
     <%= f.text_field :email, :value => "[email protected]" %> 
     </div> 
     <div class="cc_number field"> 
     <%= label_tag 'cc_number' %> 
     <%= text_field_tag 'cc_number', nil, :value => "4242424242424242" %> 
     </div> 
     <div class="ccv field"> 
     <%= label_tag 'ccv' %> 
     <%= text_field_tag 'ccv', nil, :value => "123" %> 
     </div> 
     <div class="cc_expiration field"> 
     <%= label_tag 'cc_month', "Expiration date" %> 
     <%= text_field_tag 'cc_month', nil, :value => "12" %> 
     <%= text_field_tag 'cc_year', nil, :value => "2012" %> 
     </div> 
     <div class="actions"> 
     <%= f.submit "Continue", :class => 'btn' %> 
     </div> 
    <% end %> 

, mostly provided by Stripe : 그것은 customer[stripe_token]에 오면

<script type="text/javascript"> 
    // this identifies your website in the createToken call below 
    Stripe.setPublishableKey('<%= STRIPE['public'] %>'); 

    function stripeResponseHandler(status, response) { 
     if (response.error) { 
      // show the errors on the form 
      $(".payment-errors").text(response.error.message); 
      $("input[type=submit]").removeAttr("disabled"); 
     } else { 
      var form$ = $("form"); 
      // token contains id, last4, and card type 
      var token = response['id']; 
      // insert the token into the form so it gets submitted to the server 
      form$.append("<input type='hidden' name='customer[stripe_token]' id='stripeToken' value='" + token + "'/>"); 
      // and submit 
      $('.cc_number.field, .ccv.field, .cc_expiration.field').remove(); 
      form$.get(0).submit(); 
     } 
    } 

    $(document).ready(function() { 
    $("form").submit(function(event) { 
     // disable the submit button to prevent repeated clicks 
     $('input[type=submit]').attr("disabled", "disabled"); 

     Stripe.createToken({ 
      number: $('#cc_number').val(), 
      cvc: $('#ccv').val(), 
      exp_month: $('#cc_month').val(), 
      exp_year: $('#cc_year').val() 
     }, stripeResponseHandler); 

     // prevent the form from submitting with the default action 
     return false; 
    }); 
    }); 

</script> 

내 루비 애플 리케이션 나누기로, 라인 form$.append("<input type='hidden' name='customer[stripe_token]' id='stripeToken' value='" + token + "'/>");에 문제가 발생한 것 같습니다. 내가 양식을 제출 때마다

Finally, in my `customers_controller`, I have: 

    def create 
    @customer = Customer.new(params[:customer]) 
    @customer.product = 

    if @customer.save 
     save_order 
     redirect_to @customer 
    else 
     render action: 'new' 
    end 

    def save_order 
    Stripe.api_key = STRIPE['secret'] 
    charge = Stripe::Charge.create(
     :amount => 20, 
     :currency => "usd", 
     :card => @customer.stripe_token, 
     :description => "Product 1" 
    ) 
    end 

, 그것은 컨트롤러마다 else 절을 명중 및 디버깅 많은 주위 인터넷 검색과에서이를 제거하고 처음부터 다시 작성 후, 난 여전히 난처한 상황에 빠진거야.

도움이 될 매우 대단히 감사하겠습니다.

편집은 다음 customer model

attr_accessible :name, :email, :stripe_token, :product 

    email_regex = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 

    validates :email, :presence => true, 
        :format => { :with => email_regex }, 
        :length => { :minimum => 6, :maximum => 60 }, 
        :uniqueness => { :case_sensitive => false } 

    validates :name, :length => {:minimum => 2, :maximum => 80 } 

답변

0

그것은 무슨 일이 일어나고 있는지에 대한 아이디어를 얻을 수 있도록 고객 모델을보고 도움이 추가되었습니다. @ customer.save가 false를 반환하면 유효성 검사기가 실패 할 가능성이 높음을 의미합니다.

또한 모델에서 액세스 가능한 속성으로 stripe_token을 사용하고 있습니까? 그렇지 않으면 자신이하는 것처럼 양식에서 양식을 지정할 수 없습니다. 토큰은 이 아니며은 데이터베이스에 저장해야하며 한 번만 사용할 수 있기 때문에 유의하십시오.

class Customer 
    attr_accessor :stripe_token # do you have this? 
end 

더 많은 메모 : 고객 지불 정보를 검색하고 나중에 계정을 취소 할 수 있도록 Stripe ID 필드를 저장하는 것이 좋습니다.

+0

나는 액세스 할 수있는 속성으로 stripe_token을 가지고 있습니다. 원래 게시물에 고객 모델을 추가하면 –

+0

attr_accessible은 데이터베이스에 저장되어 있음을 나타냅니다. 실제로는 안됩니다. attr_accessor는 인스턴스에 임시로 저장할 수 있도록 getter/setter를 생성합니다. 여기서는 의도 된 동작입니다. 여하튼 저장이 실패한 후에 @ customer.errors.full_messages.inspect의 결과를 인쇄 할 수 있습니까? – ajselvig

관련 문제