2016-08-31 12 views
0

ストライプとレールの統合中にこのエラーが発生しましたが、その理由を理解できません。ストライプ "公開可能なAPIキーでこのAPI呼び出しを行うことはできません。"

マイ料コントローラ:

class ChargesController < ApplicationController 

before_action :check_if_user_already_subscribed, only: [:new] 

    def new 

    end 


    def create 
    # Amount in cents 
    @amount = 100 



    # Create the charge on Stripe's servers - this will charge the user's card 
    begin 
     # Get the credit card details submitted by the form 
    customer = Stripe::Customer.create(
     :email => params[:email], 
     :source => params[:stripeToken] 
    ) 

     Stripe::Charge.create(
      :amount => @amount, 
      :currency => 'usd', 
      :customer => customer.id, 
      :description => 'Example charge custom form' 
    ) 

     current_user.subscribed = true 
     current_user.stripe_id = customer.id 
     current_user.expiry_date = Date.today + 90.days 
     current_user.save 


     flash[:success] = "Thank you for subscribing. Your account has been unlocked." 
     redirect_to root_path 

     rescue Stripe::CardError => e 
     flash[:danger] = e.message 
     redirect_to root_path 
    end 


end 

    private 

    def check_if_user_already_subscribed 

     if current_user.subscribed 
     flash[:danger] = "You have already subscribed. Please wait until your subscription runs out to resubscribe." 
     redirect_to root_path 
     end 


    end 

end 

私の初期化子stripe.rb:

Rails.configuration.stripe = { 
    :publishable_key => ENV['PUBLISHABLE_KEY'], 
    :secret_key  => ENV['SECRET_KEY'] 
} 

Stripe.api_key = Rails.configuration.stripe[:secret_key] 

私は(私は実際のキーの値を置き換えてきた)、フィガロのでconfiguration.yml使用しています:

PUBLISHABLE_KEY: "sk_test_key" 
SECRET_KEY: "pk_test_key" 

マイビュー:

<script type="text/javascript" src="https://js.stripe.com/v2/"></script> 

<script type="text/javascript"> 
    Stripe.setPublishableKey("pk_test_N9K3OPekS1Wi5zyyWtgcVLEe"); 
</script> 

<div id= 'charges-new-promo'> 
<div class = 'container-fluid'> 

    <div class = 'index-header'> 
    <h2 class ='text-center charge-title'>Upgrade Now.</h2> 
    <p class = ' text-center charge-sub-title'>Instantly unlock the entire site for 90 days.</p> 
    <p class = ' text center charge-stripe-title'><%= image_tag("stripe.png", :class => "stripe-img") %></p> 
    </div> 


</div> 
</div> 

<div class = 'container'> 
<div class = 'row'> 
<div class = 'col-md-5 col-sm-12'> 
<%= form_tag charges_path, id: 'payment-form' do %> 


<div class = 'charge-form'> 
    <span class="payment-errors"></span> 

    <div class="form-group"> 
    <label> 
     <input value="<%= current_user.email if current_user %>" type="hidden" data-stripe="email" > 
    </label> 
    </div> 

    <div class="form-group"> 
    <label> 
     <span>Full name</span><span class = 'small-text'> (e.g. John Smith)</span> 
     <input type="text" size="5" data-stripe="name" class = 'form-field'> 
    </label> 
    </div> 

    <div class="form-group"> 
    <label> 
     <span>Country</span><span class = 'small-text'> (e.g. United States)</span> 
     <input type="text" size="10" data-stripe="address_country" class = 'form-field'> 
    </label> 
    </div> 

    <div class="form-group"> 
    <label> 
     <span>Address</span> 
     <input type="text" size="10" data-stripe="address_line1" class = 'form-field'> 
    </label> 
    </div> 

    <div class="form-group"> 
    <label> 
     <span>Postal Code/Zip Code</span> 
     <input type="text" size="4" data-stripe="address_zip" class = 'form-field'> 
    </label> 
    </div> 

    <div class="form-group"> 
    <label> 
     <span>Card Number</span> 
     <input type="text" size="20" data-stripe="number" class = 'form-field'> 
    </label> 
    </div> 

    <div class="form-group"> 
    <label> 
     <span>Expiration (MM/YY)</span> 
     <input type="text" size="2" data-stripe="exp_month" class ='form-field-expiry'> 
     <span>/</span> 
    <input type="text" size="2" data-stripe="exp_year" class ='form-field-expiry'> 
    </label> 

    </div> 

    <div class="form-group"> 
    <label> 
     <span>CVC</span> 
     <input type="text" size="3" data-stripe="cvc" class = 'form-field'> 
    </label> 
    </div> 



    <input type="submit" class="c-btn-submit-charge" value="Submit Payment"> 

    </div> 
<% end %> 

</div> 

<div class ='col-md-4 col-sm-12'> 
    <div class = 'payment-box'> 
    <p> Information about your order:</p> 
    <ul> 
    <li>You are subscribing to the full membership.</li> 
    <li>This subscription lasts for 3 months (90 days).</li> 
    <li>Do not subscribe again if you are already subscribed and your expiry date has not been passed.</li> 
    <li>Subscribing grants you access to every test and practice material on this website.</li> 
    <li>At the moment, this consists of 19 online DAT reading comprehension tests along with answer explanations.</li> 
    <li>Any additional premium material added (such as more tests or more DAT sections) after you subscribe will be automatically available to you with 
    no extra charge.</li> 


    </ul> 

    <div class = 'inner-box'> 
    <p class = 'text-center'> Total cost: $20 </p> 


    </div> 

    <div class ='inner-box-payment'> 


    </div> 



</div> 



</div> 

</div> 
</div> 

<script> 
    $(function() { 
    var $form = $('#payment-form'); 
    $form.submit(function(event) { 
     // Disable the submit button to prevent repeated clicks: 
     $form.find('.submit').prop('disabled', true); 
     // Request a token from Stripe: 
     Stripe.card.createToken($form, stripeResponseHandler); 
     // Prevent the form from being submitted: 
     return false; 
    }); 
    }); 
    function stripeResponseHandler(status, response) { 
    // Grab the form: 
    var $form = $('#payment-form'); 
    if (response.error) { // Problem! 
     // Show the errors on the form: 
     $form.find('.payment-errors').text(response.error.message); 
     $form.find('.submit').prop('disabled', false); // Re-enable submission 
    } else { // Token was created! 
     // Get the token ID: 
     var token = response.id; 
     // Insert the token ID into the form so it gets submitted to the server: 
     $form.append($('<input type="hidden" name="stripeToken">').val(token)); 
     // Submit the form: 
     $form.get(0).submit(); 
    } 
    }; 
</script> 

これだけです。私は今完全に失われている。何が悪かったのか?

+1

あなたはそこにAPIキーを混同しました。公開鍵(クライアント側で使用)はpk_で始まり、秘密鍵(使用済みサーバ側)はsk_で始まります。あなたは反対をした – koopajah

+0

私の悪い私はその10分を固定した私は質問を投稿した。問題はまだそこにある。 – nachime

+0

正確なエラーはあなたのAPIキーに関するものなので、私は完全にまだ修正していないことを100%確信しています。設定しているキーを出力するためのログを追加するだけで、間違っていることがわかります – koopajah

答えて

0

私は(私は実際のキー の値を置き換えてきた)、フィガロのでconfiguration.yml使用しています:

PUBLISHABLE_KEY: "sk_test_key"

はSECRET_KEY: "pk_test_key"

キー値が正しく設定されていません。

だった:

PUBLISHABLE_KEY: "sk_test_key" 

SECRET_KEY: "pk_test_key" 

する必要があります。

PUBLISHABLE_KEY: "pk_test_key" 

SECRET_KEY: "sk_test_key" 

P.s "sk_test_key" - SK - > secret_keyに

関連する問題