2017-10-06 66 views
0

我有條紋集成Stripe Gem,我可以成功創建費用和條紋用戶;我無法讓這兩人建立聯繫。如果我的:source => params[:stripeToken]相關聯的充電經歷,但如果我把它與我的用戶模型:source => current_user.stripe_customer聯繫起來,我與我的用戶模型相聯繫,當得到一個No such token: cus_BX2RXGoOVkpvhL error.條紋元素費用無關Rails5設計current_user - 在CheckoutsController條紋:: InvalidRequestError#創建

這裏是我的請求參數。

Request 

Parameters: 

{"utf8"=>"✓", 
"authenticity_token"=>"/KI7rzkJsM2uXIynoZkWKa1L+kNgyVcw+UsqfPNOnLYO3t4vfL+R7Yb4XhonyhwTbqlyWTtgYjey71ZroSmoRA==", 
"charge"=>"2200", 
"stripeToken"=>"tok_1B9yneHbiQgBeZWGq78mXM2l", 
"card_brand"=>"Visa", 
"card_exp_month"=>"4", 
"card_exp_year"=>"2024", 
"card_last4"=>"4242"} 

的意見/捐贈/ show.html.erb

... 
<%= link_to 'Confirm Donation', new_checkout_path(charge: @donation.amount), class: 'btn btn-primary' %> 
... 

的意見/籤/ new.html.erb

<section> 
    <h1>Secure Payment</h1> 
    <div class="pages-content"> 
    <% if current_user.card_last4? %> 
     <%= form_tag checkout_path, id: 'existing-card' do %> 
      <p>Pay with existing card:</p> 
      <div><strong>Card on file:</strong> 
       <%= current_user.card_brand %> (**** **** **** <%= current_user.card_last4 %>) 
      </div> 
      <div><strong>Expiration:</strong> <%= current_user.card_exp_month %>/<%= current_user.card_exp_year %></div> 
      <p>or <%= link_to 'add a new card', '#', class: 'show-card-form' %></p> 
      <%= hidden_field_tag :charge, params[:charge] %> 
      <input type="submit" class="btn btn-default" value="Submit Payment"> 
     <% end %> 
    <% end %> 
    <%= form_tag checkout_path, id: 'payment-form', style: (current_user.card_last4? ? 'display:none' : nil) do %> 
     <div class="stripe-wrapper"> 
      <div class="form-row"> 
      <label for="card-element"> 
       Enter Your Credit or Debit Card 
      </label> 
      <div id="card-element"> 
       <!-- a Stripe Element will be inserted here. --> 
      </div> 
      <%= hidden_field_tag :charge, params[:charge] %> 
      <!-- Used to display Element errors --> 
      <div id="card-errors" role="alert"></div> 
      </div> 

      <button>Submit</button> 
     </div> 
    <% end %> 
    </div> 
</section> 

型號/ user.rb

class User < ApplicationRecord 
    ... 

    def stripe_customer 
    if stripe_id? 
     Stripe::Customer.retrieve(stripe_id) 
    else 
     stripe_customer = Stripe::Customer.create(email: email) 
     update(stripe_id: stripe_customer.id) 
     stripe_customer 
    end 
    end 

回答

1

你需要這樣做,而不是,如果你通過ID充電的客戶:

:customer => current_user.stripe_customer 
0

我曾經有過類似的問題,我記得那是因爲我的搭檔在測試模式中使用了不同的[Publishable key],所以我給了他我的工作,我希望你確保用戶條紋上已經存在。

我希望這對你有所幫助。