PHP code example of machour / konnect

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

    

machour / konnect example snippets




uzzleHttp\Client;
use Machour\Konnect\Gateway;
use Machour\Konnect\ApiException;
use Nyholm\Psr7\Factory\Psr17Factory;

// Use this or bring your own implementation
$psr17Factory = new Psr17Factory();
$client = new Client();
$konnect = new Gateway($psr17Factory, $psr17Factory, $client);

// Mandatory
// Retrieve this from your Konnect dashboard
$apiKey = "6137ad140c181c5eb44a7f88:Rp2dpHPb0mBpj3_51s86zzp3PXs5w1";
$konnect->setApiKey($apiKey);

// By default, the SDK is in sandbox mode.
// To switch to production, use the following
$konnect->setProductionMode();

/**
 * @throws ApiException|\Psr\Http\Client\ClientExceptionInterface
 */
public function initPayment(array $params): array

$response = $konnect->initPayment([
    "amount" => 10000, // millimes
    "type" => "immediate",
    "description" => "payment description",
     "acceptedPaymentMethods" => [
        "wallet",
        "bank_card",
        "e-DINAR"
    ]
    ],
    "lifespan" => 10, // minutes
    "checkoutForm" => true,
    "addPaymentFeesToAmount" => true,
    "firstName" => "John",
    "lastName" => "Doe",
    "phoneNumber" => "22777777",
    "email" => "[email protected]",
    "orderId" => "1234657",
    "webhook" => "https://merchant.tech/api/notification_payment",
    "theme" => "dark"
]);

var_dump($response);
/**
array(2) {
["payUrl"]=>
string(83) "https://gateway.konnect.network/pay?payment_ref=6392d70408ac861bcea30337&theme=dark"
["paymentRef"]=>
string(24) "6392d70408ac861bcea30337"
}
*/

/**
 * @throws ApiException|\Psr\Http\Client\ClientExceptionInterface
 */
public function getPaymentDetails(string $paymentId): array

try {
    $response = $konnect->initPayment([/* ... */]);
    
    } catch (ApiException $e) {
        // HTTP status code
        echo $e->getCode();
        // HTTP status message
        echo $e->getMessage();
        // Konnect API usage errors
        var_dump($e->errors);
    
    } catch (\Psr\Http\Client\ClientExceptionInterface $e) {
        // Transport error, something is wrong with the Konnect API, and they're
        // probably already working on that
    }
}