首页 文章

无法在红宝石中添加申请费以支付条款

提问于
浏览
0

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

费用/ 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 docs说它必须是一个数字,但如果可能的话我会更喜欢一个百分比 . 这是示例中给出的代码:

# 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非常新 .

1 回答

相关问题