title: Calculate payment fee from Stripe / PayPal - strange output in function
body: <p>I have the below function that calculates the payment fee that will go to the payment provider (say Stripe or PayPal).</p>
  <pre><code>/**
   * Calculate payment provider fee. As described in this article: https://support.stripe.com/questions/can-i-charge-my-stripe-fees-to-my-customers
   *
   * 
   * @param  integer $amount  The amount you wish to charge
   * @param  float  $fixed   The fixed fee on the gateway
   * @param  float  $percent The percent fee on the gateway (0.029 = 2.9%)
   * @return integer          The fee that the gateway will charge
   */
  
  function calcPaymentFee($amount, $fixed = 0.30, $percent = 0.029) {
      $a = $amount+$fixed;
      return round($a / (1 - $percent) - $amount, 3);
  }
  </code></pre>
  <p>I has been developed based on <a href="https://support.stripe.com/questions/can-i-charge-my-stripe-fees-to-my-customers" rel="nofollow noreferrer">this article from Stripe</a>:</p>
  <blockquote>
  <p>However, as you increase the total amount you ask Stripe to charge, you’re also increasing the Stripe fee, because Stripe’s pricing is a flat fee plus a percentage of the total charge amount. So you’ll need to figure out what gross amount you need to charge such that, after Stripe deducts its fee, you receive some predictable net amount.</p>
  <p>This turns out to be a math problem that isn’t as simple as it sounds, but to save you some calculation:</p>
  <p><a href="https://i.stack.imgur.com/DFeXQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DFeXQ.png" alt="enter image description here" /></a></p>
  </blockquote>
  <p>I believe my function should be working when working with lower percentages (i.e. 2.9%, 5%, etc) - <a href="http://thefeecalculator.com/stripe/" rel="nofollow noreferrer">I tested output according to this tool</a>. But as the percentage increases, the output seems weird, and I think the function is somehow messed up, or I don't properly understand how the fee should be charged. Either case could be imaginable.</p>
  <p>Output:</p>
  <pre><code>calcPaymentFee(100, 0, 0.029) // 2.9%: 2.987 (seems correct, this tool gives same output: http://thefeecalculator.com/stripe/) 
  calcPaymentFee(100, 0, 0.1) // 10%: 11.111 (seems correct)
  calcPaymentFee(100, 0, 0.5) // 50%: 100.0 (?!? - makes no sense)
  </code></pre>
  <p>I don't really understand why the payment fee would be 100. I would imagine it to be something like 50%, if I pass in &quot;0.5&quot; as the third parameter. Maybe I misunderstand something, but it seems odd.</p>
  <h2>What I want to achieve</h2>
  <p>I wish to calculate the fee that Stripe or PayPal will charge, and then put this fee on the customer.</p>
  <p>So if I sell something for 100$, and the Stripe fee is 2.9%, I will charge the customer 102.9$.</p>
  <p><strong>What I currently don't understand</strong>, is if I want sell something for 100$, where I have set the fee to be 50%, I am told to charge 200$.</p>