2017-02-20 42 views
0

使用Stripe Connect Standalone來處理我的應用程序。到目前爲止,用戶可以成功地連接他們的條紋賬戶,並且支付將進入我的條紋賬戶。問題是我需要用戶能夠互相支付,我保留申請費。未初始化的常量ChargesController :: CONNECTED_STRIPE_ACCOUNT_ID

目前,所有付款都只是進入我的(平臺)帳戶,不需要申請費用,最終用戶也不會收到任何款項。在我搜索解決方案後,我爲我的收費控制器添加了一個條形標題,並使用了條紋文檔中顯示的收費方法。有一次,我提交了信用卡的形式,我收到此錯誤:

uninitialized constant ChargesController::CONNECTED_STRIPE_ACCOUNT_ID

這是爲了應對這一行我的費用控制:

Stripe::Account.retrieve(CONNECTED_STRIPE_ACCOUNT_ID) 

這裏是我的charges_controller.rb:

class ChargesController < ApplicationController 

    def new 
    end 

def create 
Stripe.api_key = "sk_test_1q2w3e4r5t6y...." 

Stripe::Account.retrieve(CONNECTED_STRIPE_ACCOUNT_ID) 

token = params[:stripeToken] 

# Create a customer 
Stripe::Customer.create(
    {:description => "[email protected]"}, 
    {:stripe_account => CONNECTED_STRIPE_ACCOUNT_ID} 
) 
# Create a Charge: 
charge = Stripe::Charge.create({ 
    :amount => 1000, 
    :currency => "cad", 
    :source => token, 
    :application_fee => 200 # amount in cents 
}, :stripe_account => "{CONNECTED_STRIPE_ACCOUNT_ID}") 

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

初始化/ stripe.rb

Rails.configuration.stripe = { 
    :publishable_key => ENV['PUBLISHABLE_KEY'], 
    :secret_key  => ENV['STRIPE_SECRET_KEY'], 
    :stripe_account => ENV['CONNECTED_STRIPE_ACCOUNT_ID'] 
} 
Stripe.api_key = Rails.configuration.stripe[:secret_key] 

application.yml

development: 

    PUBLISHABLE_KEY: pk_test_########### 
    STRIPE_SECRET_KEY: sk_test_########## 
    STRIPE_CLIENT_ID: ca_########### 

費用/ new.html.erb

<script type="text/javascript" src="https://js.stripe.com/v2/"></script> 
<script type="text/javascript"> 
Stripe.setPublishableKey('pk_test_###############'); 

$(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> 


<%= form_tag charges_path, :id => 'payment-form' do %> 
    <article> 
    <% if flash[:error].present? %> 
     <div id="error_explanation"> 
     <p><%= flash[:error] %></p> 
     </div> 
    <% end %> 
    <label class="amount"> 
     <span>Amount: $10.00</span> 
    </label> 
    </article> 

    <script src="https://checkout.stripe.com/checkout.js" class="stripe-button" 
      data-key="<%= Rails.configuration.stripe[:publishable_key] %>" 
      data-description="A month's subscription" 
      data-amount="1000" 
      data-locale="auto"></script> 
<% end %> 

我是新來的條紋連接,並一直停留在這個部分了一段時間。快把我逼瘋。我在用戶配置文件上有一個「立即付款」按鈕,應該允許「用戶A」支付「用戶B」,同時向我發送申請費。我如何解決這個錯誤?這是我的付款流程中的主要問題(允許用戶通過他們的個人資料中的按鈕付款)?

UPDATE:

CONNECTED_STRIPE_ACCOUNT_ID尚未初始化,因爲它必須與所連接的用戶進行更換stripe_user_id。在我的數據庫中,這被保存爲「uid」。所以現在這個問題是把它叫做我的應用程序。

更新2:

當用戶連接,其發佈的關鍵,並帶用戶ID(UID在我的DB)保存到我的數據庫。看起來像這樣:

name: "Testguy", publishable_key: "pk_test_6IgEWlj4y##########", provider: "stripe_connect", uid: "acct_19l9#########", access_code: "sk_test_vldP#########">]>

現在,我如何在我的應用程序中調用他們的用戶ID?

Stripe::Account.retrieve(UID) 

得到初始化錯誤

uninitialized constant ChargesController::UID

+0

'CONNECTED_STRIPE_ACCOUNT_ID'沒有被定義..你可以初始化初始化文件或你的控制器(壞主意),另一種方式使用環境文件和調用'ENV ['CONNECTED_STRIPE_ACCOUNT_ID']' –

+0

感謝您的回覆。我對ruby/rails也有點新鮮感,並且很容易混淆。例如,我會進入初始化器/ stripe.rb並添加什麼? CONNECTED_STRIPE_ACCOUNT_ID屬於接收交易中資金的用戶是否正確?或者我錯了?在我的困惑中,我一直認爲CONNECTED_STRIPE ....只在用戶收到錢時纔會發生,所以我怎樣才能在初始化程序中定義它? – ChrisJC2017

+0

你可以閱讀本[使用環境文件在軌道上的ruby](http://railsapps.github.io/rails-environment-variables.html) –

回答

2

你需要通過連接帳戶的ID替換CONNECTED_STRIPE_ACCOUNT_ID。帳戶ID是以"acct_"開頭的字符串,後跟隨機字母數字字符。

當帳戶使用OAuth flow連接到您平臺的帳戶時,您會在流程的last stepstripe_user_id字段中獲得其帳戶ID。你應該把這個ID保存在數據庫中。

然後,您可以使用ID來處理費用或使用Stripe-Account標頭代表此帳戶發出任何API請求。

+0

感謝您的回覆。該信息保存到我的數據庫中。現在我不知道如何將條紋用戶標識調用到我的應用程序中。我的收費控制器有點混亂嗎?我已經更新了我的問題,並提供了更多信息。 – ChrisJC2017

相關問題