Download the PHP package appssquare/payment-validator without Composer

On this page you can find all versions of the php package appssquare/payment-validator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package payment-validator

Payment Validator

Signature and authenticity validation for payment gateway callbacks, in PHP.

Every gateway signs its webhooks differently — different field sets, different orders, different hashes, and in HyperPay's case no signature at all — but every integration needs the same answer: did this really come from the gateway, and was it altered on the way here? This package gives that answer through one interface, with the per-gateway details isolated so a new gateway is usually a few lines of configuration rather than a new integration.

Ships with Paymob, Kashier, Fawry, PayTabs, PayPal, HyperPay and EasyKash — HMAC, plain-hash, RSA and AEAD schemes alike.

Requirements

PHP 8.2+ with ext-hash, ext-json and ext-openssl.

Installation

Quick start

Every gateway is registered lazily, so a secret you never use is never read and a misconfigured gateway only raises when that gateway is actually called.

Prefer exceptions?

Just want a boolean?

Building the payload

A callback reaches you as a body, a set of headers, or a query string, and the right constructor matters — some gateways sign the raw bytes, and some sign the query parameters in the order they sent them.

Constructor Use for
Payload::fromJson($body, $headers) JSON webhooks. Keeps the raw body intact.
Payload::fromRawBody($body, $headers) Any raw body, JSON or not (HyperPay's hex ciphertext).
Payload::fromQueryString($query, $headers) Browser redirects / response callbacks.
Payload::fromArray($data, $headers) An already-decoded array.

Reading the result

ValidationResult never carries the expected signature or the secret, because results get logged.

context() is where the useful debugging lives: missing_fields when a signed field never arrived, channel for the callback type that matched, signature_keys for Kashier, cert_url for PayPal, and the decrypted payload for HyperPay.

The gateways

Paymob — HMAC-SHA512

Twenty fields concatenated in lexicographical order with no separator, hashed with the HMAC value from Dashboard → Settings → Account Info (not the API key). Two callback channels are covered and routed automatically:

Channel Fires on
transaction TRANSACTION processed callbacks and the browser response callback
card_token TOKEN callbacks, when a card is saved

Both the POST webhook (fields nested under obj) and the GET redirect (fields flattened, with dots that PHP rewrites to underscores) are recognised from the same validator.

Kashier — HMAC-SHA256

Signed with the payment API key. Two channels:

Fawry — SHA-256 with the key appended

Fawry does not HMAC. It concatenates seven fields, appends your secure key, and takes a plain SHA-256 of the result, delivered as messageSignature:

The V2 server notification and the hosted checkout's chargeResponse carry the same fields, so one validator answers for both — only the Payload constructor differs:

Two details cause almost every mismatch, and both are handled for you:

V1 notifications are deliberately not shipped. They are md5(secureKey + …) — a broken digest, and the key placement that invites a length-extension attack. If your account is still on V1, wire it up explicitly so the choice is visible in your code rather than hidden in a dependency:

Then ask Fawry to move you to V2.

PayTabs — HMAC-SHA256

Signed with the profile Server Key (not the Client Key). The two channels sign quite different things, and are routed automatically:

The "drop falsy values" step is PayTabs' own array_filter(), which removes "", 0 and the string "0". Surprising as a general rule, but it is the rule the gateway signs by, so the package reproduces it rather than improving on it.

PayPal — RSA-SHA256, verified locally

PayPal signs asymmetrically: there is no shared secret, only a public certificate to check against. What it signs is four pipe-joined parts —

— with PAYPAL-TRANSMISSION-SIG holding the RSA-SHA256 signature over that string, under the certificate named by PAYPAL-CERT-URL.

The webhookId is the subscription ID from the developer dashboard, and it must be the one for this endpoint — it is inside the signed message, so a webhook minted for another subscription cannot be replayed against yours. It is an identifier rather than a secret.

Verifying here rather than calling PayPal's verify-webhook-signature endpoint is what PayPal itself recommends: no extra round trip on the webhook path, no API credentials needed to check a signature, and no outage in your handler when that endpoint is slow.

Two things to know:

Supply your own resolver to add caching, use your HTTP client, or pin the certificate so the webhook path makes no outbound request at all:

Anything implementing CertificateResolver works; returning null means "do not trust this request".

PayPal's legacy IPN is not covered. It is not a signature scheme — you post the message back to PayPal and wait for VERIFIED — so it needs an HTTP round trip against PayPal rather than a local check. Use PayPal's own IPN listener guidance for that, or move the integration to REST webhooks.

HyperPay — AES-256-GCM

HyperPay does not sign its webhooks; it encrypts them. The body is hex ciphertext, with the nonce and authentication tag in X-Initialization-Vector and X-Authentication-Tag. GCM authenticates as it decrypts, so a body that opens cleanly under your key provably came from HyperPay unmodified — the same guarantee an HMAC gives, reached a different way.

The decrypted notification comes back on the result, so you do not decrypt twice:

Use the webhook decryption key from Administration → Webhooks, hex encoded as HyperPay presents it. 16-, 24- and 32-byte keys all work; the cipher is chosen from the key length.

EasyKash — HMAC-SHA256

Confirm the signed field list against your own EasyKash documentation. EasyKash issues per-merchant integration contracts, and the signed field set differs between Direct Pay versions and product profiles — unlike Paymob or Kashier there is no single published list that holds for every merchant. The package ships the common Direct Pay v2 set as a default, and expects you to override it when yours differs.

Or from configuration:

Field lookup is case-insensitive here, because EasyKash mixes Amount and amount across channels. If your account also presents a static token header, both checks can be wired together:

Adding a gateway

The package is built so that gateway N+1 does not require changing it. Reach for the lowest-effort level that fits.

1. Configuration only

Most gateways are "concatenate these fields, hash, compare":

If the gateway hashes the key into the message instead of HMACing it — anything whose docs read SHA-256(fields… + secretKey), as Fawry's does — reach for GenericHashValidator, which takes the same arguments plus where the key goes:

2. A different signing string

Swap the serializer. Four ship with the package, and they cover most schemes:

Serializer Signs
ConcatenatedFieldSerializer Named fields in order, optional glue, alias and case-insensitive lookup
QueryStringSerializer The payload re-encoded as a query string, optionally sorted and empties dropped
TemplateSerializer A literal formula: "/?payment={merchantId}.{orderId}.{amount}"
RawBodySerializer The untouched request body

SignatureLocation covers a body field (field(), dot notation allowed), a header (header(), case-insensitive, optional scheme prefix), several places at once (firstOf()), and anything stranger (custom()).

3. A different value format

Gateways disagree on how values become strings — true vs 1 vs Y, how many decimal places an amount carries. Two normalizers ship with the package:

Normalizer Does
DefaultValueNormalizer JSON spellings: true/false, null as empty. numericBooleans() switches to 1/0
DecimalAmountNormalizer Re-imposes fixed decimals on named money fields, so 250.0 signs as 250.00

Both are injected, so a gateway that disagrees needs your own ValueNormalizer rather than a fork:

4. A recipe of your own

Extend AbstractHmacValidator and declare what is signed and where the signature lives. The template — locate, rebuild, hash, compare in constant time — is inherited:

5. Not an HMAC at all

Implement SignatureValidator directly — three methods. HyperPay does this for AEAD decryption, and PayPal for public-key verification; neither has a signing string to rebuild, so neither fits the template.

Several callback types under one name

Wrap the channels in a CompositeValidator and the right one is picked per request, with the match reported as context('channel'):

Each channel still verifies its own signature in full, so trying several is not a weakening.

Config-driven registration

To drive a new gateway from config/payments.php like the built-in ones, teach the factory:

Overriding a built-in gateway

If a gateway changes its scheme before this package catches up, replace it in your application — no fork required:

One endpoint for every gateway

Convenient, but it resolves every registered gateway to ask, and it returns the first one that recognises the payload rather than the one that verifies. Route per gateway when you can.

That distinction matters where two gateways sign a similarly shaped payload. Kashier's redirect claims any payload with a signature field, so a PayTabs return would be handed to Kashier and rejected. PayTabs' return channel narrows itself by also requiring tranRef or cartId, but the general point stands: a shared endpoint is a convenience, and per-gateway routes are what you want in production.

Security notes

A note on decimals

Gateways sign the text they sent. If a gateway sends "amount": 100.00 as a JSON number, PHP decodes it to 100.0 and re-renders it as "100", which will not reproduce the signature.

Fawry is the built-in gateway this bites, and it is handled — a DecimalAmountNormalizer restores the two decimals its recipe specifies. For a gateway of your own, the same three options apply: sign the raw body (RawBodySerializer), name the money fields (new DecimalAmountNormalizer(['amount'])), or write a ValueNormalizer that formats values exactly the way the gateway does.

Testing

465 unit tests cover the seven gateways against signatures computed independently from each gateway's published recipe — so the tests check the implementation against the spec, not against itself. Alongside the happy paths they assert that tampering with any signed field is caught, that a wrong secret is rejected, that truncated, extended, type-juggled and non-string signatures are refused, that PayPal refuses a certificate URL pointed anywhere but PayPal, and that no secret reaches a log, a dump or a stack trace.

No test touches the network: PayPal's certificates are generated in-process, and the default HTTPS transport is exercised through a substituted stream wrapper.

Continuous integration

.github/workflows/ci.yml runs the suite on every push and pull request against PHP 8.4. The static-analysis step is present but commented out, ready for when you add Psalm.

License

MIT. See LICENSE.


All versions of payment-validator with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
ext-hash Version *
ext-json Version *
ext-openssl Version *
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package appssquare/payment-validator contains the following files

Loading the files please wait ...