【问题标题】:cant add application fee to stripe payment in ruby无法将申请费添加到 ruby​​ 中的条带付款中
【发布时间】:2017-06-18 22:40:33
【问题描述】:

条纹和红宝石的新手。试图在我的条带支付流程中添加申请费。这些费用都通过了我的条带帐户,但我似乎无法弄清楚如何/在哪里添加条带应用程序费用代码。

收费/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

正如我所提到的,支付流程运行良好,Stripe 文档说它必须是一个数字,但如果可能的话,我更喜欢百分比。这是示例中给出的代码:

# 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}
)

我只是不知道在哪里或如何添加申请费代码!我的付款流程的工作原理是用户可以输入他们自己的付款金额,我似乎无法在此处或反映该表单中代码的文档中找到任何答案。我确信它是一个简单的解决方案,但我对使用 Stripe 很陌生。

【问题讨论】:

    标签: ruby-on-rails stripe-payments


    【解决方案1】:

    您只能在使用Connect 创建费用时指定application_fee,可以是directly on a connected account(带有Stripe-Account 标头)或through the platform(带有destination 参数)。

    从您的代码看来,您根本没有使用 Connect,因此无需支付申请费。你能澄清一下你到底想做什么吗?收费的资金应该如何分配?

    【讨论】:

    • 是的,我太新了,我没有意识到我还没有使用 Stripe Connect。我已经开始使用连接,并且遇到了另一个新手问题。或许你知道答案?感谢您的帮助。stackoverflow.com/q/42045316/5644106
    猜你喜欢
    • 2019-12-18
    • 1970-01-01
    • 2013-01-19
    • 2021-11-26
    • 1970-01-01
    • 2018-05-26
    • 2014-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多