Files

35 lines
1.0 KiB
PHP

<?php
declare( strict_types=1 );
namespace StreamGoals\Core;
/**
* Computes the amount a donor must pay so the recipient nets the intended base
* after PayPal's fee: total = ceil((base + fixed) / (1 - pct)).
*
* Percent is expressed in basis points (349 = 3.49%) to avoid floats.
*/
final class FeeCalculator {
public function __construct( public readonly int $percentBps = 349, public readonly int $fixedMinor = 49 ) {
if ( $percentBps < 0 || $percentBps >= 10000 || $fixedMinor < 0 ) {
throw new \InvalidArgumentException( 'Invalid fee rates.' );
}
}
/** Total the donor pays so the recipient nets $baseMinor. */
public function grossUp( int $baseMinor ): int {
if ( $baseMinor <= 0 ) {
return 0;
}
$num = ( $baseMinor + $this->fixedMinor ) * 10000;
$den = 10000 - $this->percentBps;
return intdiv( $num + $den - 1, $den ); // integer ceil
}
/** The extra fee-cover portion for a base amount. */
public function feeFor( int $baseMinor ): int {
return $this->grossUp( $baseMinor ) - $baseMinor;
}
}