PHP code example of appssquare / payment-validator
1. Go to this page and download the library: Download appssquare/payment-validator library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
if (! $validator->isValid('kashier', $payload)) { … }
// Laravel
$payload = Payload::fromJson($request->getContent(), $request->headers->all());
// A redirect back from the gateway — pass the raw query string, not $_GET,
// so parameter order is preserved.
$payload = Payload::fromQueryString($request->getQueryString());
// Plain PHP
$payload = Payload::fromRawBody(file_get_contents('php://input'), getallheaders());
$result->isValid(); // bool
$result->isInvalid(); // bool
$result->gateway(); // 'paymob'
$result->reason(); // null when valid, a sentence when not
$result->context(); // non-secret diagnostics
$result->contextValue('channel');
$result->throwIfInvalid(); // fluent guard
$result->toArray(); // safe to log as-is
use Cofa\PaymentValidator\Gateways\Paymob\Paymob;
Paymob::validator($hmacSecret)->validate($payload);
use Cofa\PaymentValidator\Gateways\Kashier\Kashier;
// If your redirect URL carries parameters Kashier never signed, exclude them:
Kashier::validator($apiKey, additionalExcluded: ['lang', 'utm_source']);
use Cofa\PaymentValidator\Gateways\Fawry\Fawry;
$fawry = Fawry::validator(getenv('FAWRY_SECURE_KEY'));
$fawry->validate(Payload::fromJson($rawBody)); // server notification
$fawry->validate(Payload::fromQueryString($request->getQueryString())); // return URL
use Cofa\PaymentValidator\Gateways\PayTabs\PayTabs;
PayTabs::validator(getenv('PAYTABS_SERVER_KEY'));
// If your return URL carries parameters PayTabs never signed:
PayTabs::validator($serverKey, additionalExcluded: ['lang', 'utm_source']);
use Cofa\PaymentValidator\Gateways\PayPal\PayPal;
PayPal::validator(getenv('PAYPAL_WEBHOOK_ID'));
use Cofa\PaymentValidator\Support\RemoteCertificateResolver;
PayPal::validator($webhookId, new RemoteCertificateResolver(
allowedHosts: ['paypal.com'],
fetcher: fn (string $url): ?string => $cache->remember($url, 86400,
fn () => $httpClient->get($url)->body()),
));
use Cofa\PaymentValidator\Gateways\EasyKash\EasyKash;
// Default set: easykashRef, Amount, Currency, PaymentMethod, productCode, status
EasyKash::validator($secret);
// Yours, when it differs — no need to edit the package:
EasyKash::validator($secret, ['easykashRef', 'Amount', 'status']);
use Cofa\PaymentValidator\Support\SecretPlacement;
use Cofa\PaymentValidator\Validators\GenericHashValidator;
$validator->register('acmepay', GenericHashValidator::forFields(
gateway: 'acmepay',
secret: getenv('ACMEPAY_SECRET'),
fields: ['merchantRef', 'orderAmount'],
signatureField: 'checksum',
secretPlacement: SecretPlacement::Append, // or Prepend
));
use Cofa\PaymentValidator\Serializers\RawBodySerializer;
use Cofa\PaymentValidator\Support\SignatureLocation;
$validator->register('some-gateway', new GenericHmacValidator(
gateway: 'some-gateway',
secret: $secret,
serializer: new RawBodySerializer(),
signatureLocation: SignatureLocation::header('X-Signature', stripPrefix: 'sha256='),
algorithm: 'sha512',
));
new ConcatenatedFieldSerializer(['amount', 'paid'], ':', $yourNormalizer);
// Or wrap one: amounts to two decimals, everything else the default way.
new ConcatenatedFieldSerializer(
fields: ['ref', 'amount'],
normalizer: new DecimalAmountNormalizer(['amount']),
);
final class MyGatewayValidator extends AbstractHmacValidator
{
public function gateway(): string
{
return 'my-gateway';
}
protected function serializer(Payload $payload): PayloadSerializer
{
// Chosen per payload, so versioned schemes are a branch, not a new class.
return new ConcatenatedFieldSerializer(['ref', 'total']);
}
protected function signatureLocation(): SignatureLocation
{
return SignatureLocation::field('checksum');
}
}
new CompositeValidator('my-gateway', [
'webhook' => new MyWebhookValidator($secret),
'redirect' => new MyRedirectValidator($secret),
]);