0

New to stripe, and ruby. Trying to add an application fee to my stripe payment flow. The charges are all making it through to my stripe account, but I can't seem to figure out how/where to add the stripe application fee code.

charges/new.html.erb

<%= form_tag charges_path do %>
  <div id="error_explanation">
    <% if flash[:error].present? %>
      <p><%= flash[:error] %></p>
    <% end %>
  </div>
  <article>
    <%= label_tag(:amount, 'Payment Amount:') %>
    <%= text_field_tag(:amount) %>
  </article>
  <article>
    <%= hidden_field_tag(:stripeToken) %>
  </article>
  <button id='donateButton'>Pay</button>
<% end %>

<script src="https://checkout.stripe.com/checkout.js"></script>

<script>


var handler = StripeCheckout.configure({
  key: '<%= Rails.configuration.stripe[:publishable_key] %>',
  locale: 'auto',
  name: 'Payments',
  description: 'Pay Someone',
  token: function(token) {
    $('input#stripeToken').val(token.id);
    $('form').submit();
  }
});

$('#donateButton').on('click', function(e) {
  e.preventDefault();

  $('#error_explanation').html('');

  var amount = $('input#amount').val();
  amount = amount.replace(/\$/g, '').replace(/\,/g, '')

  amount = parseFloat(amount);

  if (isNaN(amount)) {
    $('#error_explanation').html('<p>Please enter a valid amount in CAD ($).</p>');
  }
  else if (amount < 5.00) {
    $('#error_explanation').html('<p>Wage amount must be at least $5.</p>');
  }
  else {
    amount = amount * 100; // Needs to be an integer!
    handler.open({
      amount: Math.round(amount)
    })
  }
});

// Close Checkout on page navigation
$(window).on('popstate', function() {
  handler.close();
});
</script>

charges_controller.rb

class ChargesController < ApplicationController

 def new
end

def create
  @amount = params[:amount]

  @amount = @amount.gsub('$', '').gsub(',', '')

  begin
    @amount = Float(@amount).round(2)
  rescue
    flash[:error] = 'Charge not completed. Please enter a valid amount in USD ($).'
    redirect_to new_charge_path
    return
  end

  @amount = (@amount * 100).to_i # Must be an integer!

  if @amount < 500
    flash[:error] = 'Charge not completed. Donation amount must be at least $5.'
    redirect_to new_charge_path
    return
  end

  Stripe::Charge.create(
    :amount => @amount,
    :currency => 'usd',
    :source => params[:stripeToken],
    :description => 'Custom donation'
  )

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

As I've mentioned, the payment flow works fine, Stripe docs says it must be a number, but I would prefer a percentage if that is possible. this is the code given in the examples:

# See your keys here: https://dashboard.stripe.com/account/apikeys
Stripe.api_key = "secret key"

# Get the credit card details submitted by the form
token = params[:stripeToken]

# Create the charge with Stripe
charge = Stripe::Charge.create({
    :amount => 1000, # amount in cents
    :currency => "cad",
    :source => token,
    :description => "Example charge",
    :application_fee => 123 # amount in cents
  },
  {:stripe_account => CONNECTED_STRIPE_ACCOUNT_ID}
)

I just dont know where or how to add the application fee code! my payment flow works in that the user can enter their own payment amount, and I cant seem to find any answers here or in the documentation which reflects the code in that form. I'm sure its a simple solution, but I'm very new to working with Stripe.

ChrisJC2017
  • 49
  • 11

1 Answers1

1

You can only specify an application_fee when creating a charge with Connect, either directly on a connected account (with the Stripe-Account header) or through the platform (with the destination parameter).

From your code, it doesn't look like you're using Connect at all, so application fees wouldn't apply. Can you clarify what exactly you're trying to do? How should the funds from the charge be split?

Ywain
  • 16,854
  • 4
  • 51
  • 67
  • Yes, being so new I didnt realize that I wasn't yet using Stripe Connect. I have begun to use connect, and am having another newbie issue. Perhaps you know the answer? Thanks for your help.http://stackoverflow.com/q/42045316/5644106 – ChrisJC2017 Feb 04 '17 at 20:37