Im using Stripe Connect Standalone for my application. So far the users can successfully connect their stripe account, and payments are coming through into my stripe account. The problem is that I need the users to be able to pay each other, and I retain the application fee.
At the moment, all payments simply come into my (the platforms) account, no application fee is taken, and the end user does not receive any money. after I searched for solutions, I added a stripe header to my charges controller, and used the charge method as displayed on Stripes docs. Once I submit the credit card form, I receive this error:
uninitialized constant ChargesController::CONNECTED_STRIPE_ACCOUNT_ID
this is in response to this line in my charges controller:
Stripe::Account.retrieve(CONNECTED_STRIPE_ACCOUNT_ID)
Here is my 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
initializers/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_###########
charges/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 %>
I'm new to Stripe Connect, and have been stuck on this part for a while. Driving me crazy. I have a "Pay now" button on the users profiles which should allow "User A" to pay "User B", while sending me an application fee. How do I fix this error? Is this the main issue in my payment flow (allowing users to pay each other via a button in their profile)?
UPDATE:
CONNECTED_STRIPE_ACCOUNT_ID hasn't been initialized, because it must be replaced with the connected users stripe_user_id. In my database this is saved as "uid". So now the issue is calling it into my application.
UPDATE 2:
When a user connects, their publishable key, and stripe user Id (uid in my db) are saved to my database. looks like this:
name: "Testguy", publishable_key: "pk_test_6IgEWlj4y##########", provider: "stripe_connect", uid: "acct_19l9#########", access_code: "sk_test_vldP#########">]>
now, how do I call their user ID in my application?
Stripe::Account.retrieve(UID)
gets the uninitialized error
uninitialized constant ChargesController::UID
CONNECTED_STRIPE_ACCOUNT_IDis not defined.. you can initialize that on initialize file or on your controller (bad idea), another way using environment file and call withENV['CONNECTED_STRIPE_ACCOUNT_ID']- rails_idapi_keyalso you need account_id of stripe - rails_id