计算来自 Stripe / PayPal 的支付费用 - 函数中的奇怪输出

Calculate payment fee from Stripe / PayPal - strange output in function

我有以下功能可以计算支付给支付提供商(比如 Stripe 或 PayPal)的支付费用。

/**
 * 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);
}

我是基于this article from Stripe开发的:

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.

This turns out to be a math problem that isn’t as simple as it sounds, but to save you some calculation:

我相信我的函数在使用较低百分比(即 2.9%、5% 等)时应该可以正常工作 - I tested output according to this tool。但是随着百分比的增加,输出看起来很奇怪,我认为这个功能不知何故搞砸了,或者我没有正确理解应该如何收取费用。两种情况都可以想象。

输出:

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)

我不是很明白为什么支付费用会是100,如果我把“0.5”作为第三个参数,我想应该是50%。也许我误解了什么,但看起来很奇怪。

我想达到的目标

我想计算一下Stripe或PayPal会收取的费用,然后把这笔费用转给客户。

所以如果我以 100 美元的价格卖东西,而 Stripe 费用是 2.9%,我将向客户收取 102.9 美元。

我目前不明白,如果我想卖100美元,我设置了50%的费用,我被告知要收200 $.

实际上 50% 的 100 美元费用是正确的。

想象一下这个场景:

  • 客户支付 200 美元。
  • 他们中有 50% 的人拿了银行。 => 100 美元
  • 你得到 100 美元(200 美元的 50%)

下面是一些计算相同内容的基本脚本:

<form method="post" target="<?php echo $_SERVER['PHP_SELF']; ?> ">
    <input type="text" name="amount">
    <input type="submit" name="submit" value="Calculate"> 
</form>

<?php
    $fixed = 0.3;
    $percent = 0.029;        
    function calcPaymentFee($amount) {
        global $fixed, $percent;
        $arr = array();
        $arr['fee'] = ($amount * $percent + $fixed);
        $arr['net'] = $amount - $arr['fee'];
        return $arr;
    }

    function calcTotal($amount) {
        global $fixed, $percent;
        $arr = array();
        $r = (100 - $percent*100) / 100;
        $arr['total'] = ($amount + $fixed) / $r;
        $arr['fee'] = $arr['total'] - $amount;
        return $arr;
    }

    if (isset($_POST['submit']) && !empty($_POST['amount'])) {
        $left = calcPaymentFee($_POST['amount']);
        $right = calcTotal($_POST['amount']);
        printf('Amount: %.2f<br>', $_POST['amount']);
        printf('Fee: %.2f<br>', $left['fee']);
        printf('Net sum: %.2f<br>', $left['net']);
        echo '<br>';
        printf('If you want net sum of %.2f you must ask for:<br>', $_POST['amount']);
        printf('Amount: %.2f<br>', $right['total']);
        printf('Fee: %.2f<br>', $right['fee']);
    }
?> 

输出:

Amount: 100.00
Fee: 3.20
Net sum: 96.80

If you want net sum of 100.00 you must ask for:
Amount: 103.30
Fee: 3.30

总之你在calcPaymentFee函数中的公式有点错误,必须是:

return ($amount * $percent + $fixed);