Skip to content

CardPay and ComfortPay ​

When a customer pays by card through Tatra banka's CardPay (or ComfortPay, the stored-card variant), the bank can mail the result to the merchant as one line of KEY=value pairs. TatraBankaSimpleMailParser reads it. This recipe parses a successful CardPay payment, a successful ComfortPay payment, and a failed one.

Source: examples/cardpay

The mails ​

text
AMT=44.88 CURR=978 VS=4444255333 RES=OK AC=644311 TRES=OK CID=824452 CC=************1111 TID=11224444 TIMESTAMP=26112016121631 HMAC=b76cb9ddeed7ed0bcf991f19bbbabfb1b76cb9ddeed7ed0bcf991f19bbbabfb1 ECDSA_KEY=1 ECDSA=a5e75a2e2c21081c076caa4a8732e43aa5e75a2e2c21081c076caa4a8732e43aa5e75a2e2c21081c076caa4a8732e43aa5e75a2e2c21081c076caa4a8732e43aa5e75a2e2c21
The other two mails
text
AMT=33.99 CURR=978 VS=5000001234 RES=OK AC=740017 TID=88888888 TIMESTAMP=25112016223023 HMAC=9dfd46cf2af977be8dd4251f4ef92307d95a8903f4738616d8456bd02e858340 ECDSA_KEY=1 ECDSA=3045022100e2c791637534bd57b530b7e42497dc6e33fa9f6c0e3950148c14c988f014ca5f02205918cb783d02d6dad4bea6ed4823a2c833187b979cdced377b3612c939b05f3d
text
AMT=140.92 CURR=978 VS=5555534283 RES=FAIL TRES=FAIL CC=************1111 TID=11224444 TIMESTAMP=24112016170555 HMAC=b76cb9ddeed7ed0bcf991f19bbbabfb1b76cb9ddeed7ed0bcf991f19bbbabfb1 ECDSA_KEY=1 ECDSA=a5e75a2e2c21081c076caa4a8732e43aa5e75a2e2c21081c076caa4a8732e43aa5e75a2e2c21081c076caa4a8732e43aa5e75a2e2c21081c076caa4a8732e43aa5e75a2e2c21

The code ​

php
<?php

declare(strict_types=1);

// Tatra banka mails the result of a CardPay or ComfortPay card payment as one line of KEY=value pairs.

require __DIR__ . '/vendor/autoload.php';

use Tomaj\BankMailsParser\MailContent;
use Tomaj\BankMailsParser\Parser\TatraBanka\TatraBankaSimpleMailParser;
use Tomaj\BankMailsParser\ValueObject\Money;

function show(MailContent $mail): void
{
    foreach (get_object_vars($mail) as $field => $value) {
        $value = match (true) {
            $value instanceof DateTimeInterface => $value->format('Y-m-d H:i:s P'),
            $value instanceof Money => $value->format(),
            default => $value,
        };
        if ($value !== null) {
            printf("  %-20s %s\n", $field, $value);
        }
    }
}

$parser = new TatraBankaSimpleMailParser();

foreach (['cardpay-ok.txt', 'comfortpay-ok.txt', 'comfortpay-fail.txt'] as $file) {
    $mail = $parser->parse((string) file_get_contents(__DIR__ . '/mails/' . $file));

    echo $file, "\n";
    show($mail);

    // res is the bank's result (TRES when present). The mail is not signed for you: verify
    // sign (HMAC) with your gateway key before you trust it, for example with omnipay-tatra.
    echo '  => ', $mail->res === 'OK' ? 'paid' : 'not paid', ", variable symbol {$mail->vs}\n\n";
}

The output ​

text
cardpay-ok.txt
  money                33.99 EUR
  transactionDate      2016-11-25 22:30:23 +00:00
  vs                   5000001234
  sign                 9dfd46cf2af977be8dd4251f4ef92307d95a8903f4738616d8456bd02e858340
  res                  OK
  ac                   740017
  tid                  88888888
  => paid, variable symbol 5000001234

comfortpay-ok.txt
  money                44.88 EUR
  transactionDate      2016-11-26 12:16:31 +00:00
  vs                   4444255333
  cid                  824452
  sign                 b76cb9ddeed7ed0bcf991f19bbbabfb1b76cb9ddeed7ed0bcf991f19bbbabfb1
  res                  OK
  ac                   644311
  cc                   ************1111
  tid                  11224444
  tres                 OK
  => paid, variable symbol 4444255333

comfortpay-fail.txt
  money                140.92 EUR
  transactionDate      2016-11-24 17:05:55 +00:00
  vs                   5555534283
  sign                 b76cb9ddeed7ed0bcf991f19bbbabfb1b76cb9ddeed7ed0bcf991f19bbbabfb1
  res                  FAIL
  cc                   ************1111
  tid                  11224444
  tres                 FAIL
  => not paid, variable symbol 5555534283

The fields ​

KeyPropertyMeaning
AMT + CURRmoneyAmount with . as the decimal mark. CURR is the ISO 4217 number (978 is EUR); a missing CURR is EUR, an unknown one leaves money null and keeps the rest
VSvsVariable symbol you sent to the gateway
RES / TRESres, tresResult, OK or FAIL. When TRES is present, res is the TRES value and tres stores it too
ACacAuthorisation code, only on a successful payment
CIDcidComfortPay customer (stored card) id
CCccMasked card number
TIDtidTransaction id
TXNtxnCardPay transaction type, such as PA
RCrcReturn code
HMAC / SIGNsignThe signature as sent. When both are present, sign is the HMAC
TIMESTAMPtransactionDate14 digits dmYHis in UTC; 10 digits are a Unix timestamp. Missing means null

Only the first contiguous block of KEY=value lines is read, words that are not KEY=value on that line are skipped (so Vysledok: VS=… RES=OK … works), quoted lines starting with > are ignored, and a key repeated with a different value throws InvalidEmailFormatException (see Error handling).

The signature is not verified

The parser copies HMAC/SIGN into sign; it does not check it, because it does not have your gateway key. Verify it with your gateway key before you mark an order paid, the same way you verify the CardPay return URL (for example with omnipay-tatra), and check the mail's DKIM. See Security.

Released under the MIT License.