0
votes

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

1
CONNECTED_STRIPE_ACCOUNT_ID is not defined.. you can initialize that on initialize file or on your controller (bad idea), another way using environment file and call with ENV['CONNECTED_STRIPE_ACCOUNT_ID'] - rails_id
Thanks for your reply. I'm also somewhat new to ruby/rails, and easily confused. So for example, I would go into initializers/stripe.rb and add..what? CONNECTED_STRIPE_ACCOUNT_ID belongs to the user receiving the funds in the transaction correct? or am I wrong? In my confusion I've been led to believe that CONNECTED_STRIPE.... only occurs when a user is receiving money, so how can I define that in the initializer? - ChrisJC2017
I understand using env, but I don't know how to initialize this particular item. I've edited my question at the bottom to be more specific about this. - ChrisJC2017
do you have a stripe account? you have an api_key also you need account_id of stripe - rails_id

1 Answers

2
votes

You need to replace CONNECTED_STRIPE_ACCOUNT_ID by the ID of the connected account. Account IDs are strings that start with "acct_" followed by random alphanumeric characters.

When an account connects to your platform's account using the OAuth flow, you get its account ID in the last step of the flow, in the stripe_user_id field. You should then save this ID in your database.

You can then use the ID to process charges or issue any API request on behalf of this account, using the Stripe-Account header.