PHP code example of guavapay / epg

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

    

guavapay / epg example snippets



GuavaPay\EPG;

$epg = new EPG('USER', 'SECRET', 'BANK_ID', 'SERVICE_ID');

...
use GuavaPay\Exception\GuavaEcomException;
use GuavaPay\Exception\GuavaClientException;

try {
    $orderId = '123456'; // The order ID from your database.
    $amount = 100; // The amount in cents for the payment.
    $currency = 978; // The currency code in ISO 4217 format (Euro in this case).
    $returnUrl = 'http://example.com/paymentResult'; // The URL where the customer will be redirected after a successful payment.
    
    // Create an order using the Electronic Payment Gateway (EPG).
    $order = $epg->createOrder($orderId, $amount, $currency, $returnUrl); 
    
    // Get the EPG order ID for reference. Example: 84c5387a-7824-742b-9567-0c1a0e7e1e23.
    var_dump($order->getOrderId()); 

    // Get the URL for the payment, where the customer should be redirected to make the payment.
    var_dump($order->getFormUrl()); 

} catch (GuavaEcomException $e) { 
    // An error occurred due to a logical issue during the payment process.
    echo $e->getMessage();
} catch (GuavaClientException $e) { 
    // Unable to send the request to the EPG server, possibly due to connectivity issues.
    echo $e->getMessage();
}

...
use GuavaPay\Exception\GuavaEcomException;
use GuavaPay\Exception\GuavaClientException;

try {
    $epgOrder = '84c5387a-7824-742b-9567-0c1a0e7e1e23'; // EPG order ID
    $statusCode = '013'; // status code which was provided by GuavaPay during the integration
    $orderInfo = $epg->getOrderStatus($epgOrder, $statusCode);
    var_dump($orderInfo->getStatus(), $orderInfo->getIsSuccess(), $orderInfo->getAmount());
} catch (GuavaEcomException $e) { 
    // An error occurred due to a logical issue during the payment process.
    echo $e->getMessage();
} catch (GuavaClientException $e) { 
    // Unable to send the request to the EPG server, possibly due to connectivity issues.
    echo $e->getMessage();
}

...
use GuavaPay\Exception\GuavaEcomException;
use GuavaPay\Exception\GuavaClientException;
use GuavaPay\Config\CardConfig;

try {
    $epgOrder = '84c5387a-7824-742b-9567-0c1a0e7e1e23'; // EPG order ID
    $expiry = DateTime::createFromFormat('m/Y', '02/2030');
    $cardConfig = new CardConfig('CardNumber', $expiry, '547', 'CARD HOLDER');

    var_dump($epg->check3dsVersion($epgOrder, $cardConfig)->getVersion()); // int(2)
} catch (GuavaEcomException $e) { 
    // An error occurred due to a logical issue during the payment process.
    echo $e->getMessage();
} catch (GuavaClientException $e) { 
    // Unable to send the request to the EPG server, possibly due to connectivity issues.
    echo $e->getMessage();
}

...
use GuavaPay\Exception\GuavaEcomException;
use GuavaPay\Exception\GuavaClientException;
use GuavaPay\Config\CardConfig;
use GuavaPay\Config\DeviceConfig;

try {
    $expiry = DateTime::createFromFormat('m/Y', '02/2030');
    $cardConfig = new CardConfig('CardNumber', $expiry, '547', 'CARD HOLDER');
    $deviceConfig = new DeviceConfig(true, 'ru-RU', 986, 1024, 0, false, 16);
    $payment = $epg->paymentRequest('84c5387a-7824-742b-9567-0c1a0e7e1e23', $cardConfig, $deviceConfig);

} catch (GuavaEcomException $e) { 
    // An error occurred due to a logical issue during the payment process.
    echo $e->getMessage();
} catch (GuavaClientException $e) { 
    // Unable to send the request to the EPG server, possibly due to connectivity issues.
    echo $e->getMessage();
}

...
use GuavaPay\Exception\GuavaEcomException;
use GuavaPay\Exception\GuavaClientException;

try {
    var_dump($epg->getBalanceStatus(978, '013')->getAmount()); // returns float(133.74)
} catch (GuavaEcomException $e) { 
    // An error occurred due to a logical issue during the payment process.
    echo $e->getMessage();
} catch (GuavaClientException $e) { 
    // Unable to send the request to the EPG server, possibly due to connectivity issues.
    echo $e->getMessage();
}

...
use GuavaPay\Exception\GuavaSignatureException;

try {
    $request = json_decode(file_get_contents('php://input'), true); // get JSON as PHP array from POST request
    if ($request === null && json_last_error() !== JSON_ERROR_NONE) {
        throw new Exception('Invalid JSON request');
    }
    $certPath = "file://./certificate.pem"; // path to your certificate file (.pem) from GuavaPay
    $publicKey = openssl_pkey_get_public($certPath);
    if ($publicKey === false) {
        throw new Exception('Invalid public key'); // if you have an error here, check your certificate file
    }
    $result = $epg->checkSignature($request, $request['signature'], $publicKey);
    echo 'Valid signature!'; // if you see this message, then the signature is valid

    // now you can top up the user's balance, update the order status, etc.
} catch (GuavaSignatureException $e) {
    echo 'Invalid signature!' . $e->getMessage(); // if you see this message, then the signature is invalid
} catch (\Exception $e) {
    echo $e->getMessage() . PHP_EOL; // if you see this message, then an error occurred
}