PHP code example of znojil / comgate

1. Go to this page and download the library: Download znojil/comgate 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/ */

    

znojil / comgate example snippets


use Znojil\Comgate\Client;
use Znojil\Comgate\Config;

$config = new Config(
	merchant: 'YOUR_MERCHANT_ID',
	secret: 'YOUR_SECRET',
	test: false // true for test mode
);

$client = new Client($config);

use Znojil\Comgate\DTO\PaymentDTO;
use Znojil\Comgate\Enum\Currency;
use Znojil\Comgate\Request\CreateRequest;

$payment = new PaymentDTO(
	price: 10000, // in cents, 10000 = 100.00 CZK
	curr: Currency::Czk,
	label: 'Order #1234',
	refId: 'order-1234',
	fullName: 'John Doe',
	email: '[email protected]'
);

$result = $client->send(new CreateRequest($payment));

$result->transId; // AB12-CD34-EF56
$result->redirect; // redirect URL for payment gateway

use Znojil\Comgate\Request\CreateRedirectRequest;

$redirectUrl = $client->send(new CreateRedirectRequest($payment));
// redirect the customer to $redirectUrl

use Znojil\Comgate\Request\StatusRequest;

$status = $client->send(new StatusRequest('AB12-CD34-EF56'));

$status->transId; // AB12-CD34-EF56
$status->status; // PaymentStatus enum
$status->price; // int (cents)
$status->curr; // Currency enum

use Znojil\Comgate\Client;
use Znojil\Comgate\Http\Client as ComgateHttpClient;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UriInterface;

class MyCustomHttpClient implements ComgateHttpClient{
	public function send(string $method, string|UriInterface $uri, array $headers = [], mixed $data = null, array $options = []): ResponseInterface{
		// your implementation
	}
}

$client = new Client($config, new MyCustomHttpClient);

use Znojil\Comgate\ServerRequest\PaymentStatusServerRequest;

$status = $client->accept(new PaymentStatusServerRequest);

$status->transId; // AB12-CD34-EF56
$status->status; // PaymentStatus enum
$status->price; // int (cents)
$status->curr; // Currency enum

$status = $client->accept(new PaymentStatusServerRequest, $psrServerRequest);

use Znojil\Comgate\Exception\ApiException;
use Znojil\Comgate\Exception\ClientException;
use Znojil\Comgate\Exception\ServerException;

try{
	$result = $client->send(new CreateRequest($payment));
}catch(ApiException $e){
	echo $e->getMessage(); // error message from Comgate
	echo $e->getCode(); // error code from Comgate
}catch(ServerException $e){
	// Comgate server error (5xx)
}catch(ClientException $e){
	// HTTP client error (4xx)
}