21 lines
804 B
PHP
21 lines
804 B
PHP
<?php
|
|
declare( strict_types=1 );
|
|
|
|
namespace StreamGoals\PayPal;
|
|
|
|
/**
|
|
* PayPal-Request-Id (idempotency key) builder. Not derivable from the reusable auto-increment id alone: it is an HMAC
|
|
* keyed with the site's secret salt over kind + donation id + created_at, so a re-used id (after rows are deleted and
|
|
* AUTO_INCREMENT resets) gets a different key, while a retry for the SAME donation stays identical.
|
|
*/
|
|
final class RequestId {
|
|
|
|
public const MAX_LENGTH = 108;
|
|
|
|
public static function make( string $kind, int $donationId, string $createdAt, string $salt ): string {
|
|
$kind = preg_replace( '/[^a-z]/', '', strtolower( $kind ) ) ?: 'req';
|
|
$h = hash_hmac( 'sha256', $kind . '|' . $donationId . '|' . $createdAt, $salt );
|
|
return substr( 'sg-' . $kind . '-' . $h, 0, self::MAX_LENGTH );
|
|
}
|
|
}
|