1. Go to this page and download the library: Download sudiptpa/khalti-sdk-php 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/ */
sudiptpa / khalti-sdk-php example snippets
use Khalti\Config\ClientConfig;
use Khalti\Khalti;
$khalti = Khalti::client(new ClientConfig(
secretKey: $_ENV['KHALTI_SECRET_KEY'],
));
use Khalti\Exception\TransportException;
use Khalti\Http\HttpRequest;
use Khalti\Http\HttpResponse;
use Khalti\Transport\TransportInterface;
final class MyTransport implements TransportInterface
{
public function send(HttpRequest $request, int $timeoutSeconds): HttpResponse
{
// Send request with your preferred HTTP client.
throw new TransportException('Implement transport send logic.');
}
}
$khalti = Khalti::client(
new ClientConfig(secretKey: $_ENV['KHALTI_SECRET_KEY']),
new MyTransport(),
);
use GuzzleHttp\Client;
use Http\Adapter\Guzzle7\Client as GuzzleAdapter;
use Khalti\Config\ClientConfig;
use Khalti\Exception\TransportException;
use Khalti\Http\HttpRequest;
use Khalti\Http\HttpResponse;
use Khalti\Khalti;
use Khalti\Transport\TransportInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Throwable;
final class Psr18Transport implements TransportInterface
{
public function __construct(
private readonly \Psr\Http\Client\ClientInterface $client,
private readonly RequestFactoryInterface $requestFactory,
private readonly StreamFactoryInterface $streamFactory,
) {
}
public function send(HttpRequest $request, int $timeoutSeconds): HttpResponse
{
try {
$psrRequest = $this->requestFactory
->createRequest($request->method, $request->url);
foreach ($request->headers as $name => $value) {
$psrRequest = $psrRequest->withHeader($name, $value);
}
if ($request->body !== '') {
$psrRequest = $psrRequest->withBody($this->streamFactory->createStream($request->body));
}
$psrResponse = $this->client->sendRequest($psrRequest);
} catch (Throwable $e) {
throw new TransportException('PSR-18 transport failed.', 0, $e);
}
$headers = [];
foreach ($psrResponse->getHeaders() as $name => $values) {
$headers[strtolower($name)] = implode(', ', $values);
}
return new HttpResponse(
$psrResponse->getStatusCode(),
(string) $psrResponse->getBody(),
$headers
);
}
}
$psr18 = new GuzzleAdapter(new Client());
$khalti = Khalti::client(
new ClientConfig(secretKey: $_ENV['KHALTI_SECRET_KEY']),
new Psr18Transport($psr18, $requestFactory, $streamFactory),
);
use Khalti\Transport\CurlTransport;
$khalti = Khalti::client(
new ClientConfig(secretKey: $_ENV['KHALTI_SECRET_KEY']),
new CurlTransport(),
);
use Illuminate\Support\Facades\Http;
use Khalti\Exception\TransportException;
use Khalti\Http\HttpRequest;
use Khalti\Http\HttpResponse;
use Khalti\Transport\TransportInterface;
use Throwable;
final class LaravelTransport implements TransportInterface
{
public function send(HttpRequest $request, int $timeoutSeconds): HttpResponse
{
try {
$response = Http::timeout($timeoutSeconds)
->withHeaders($request->headers)
->send($request->method, $request->url, ['body' => $request->body]);
} catch (Throwable $e) {
throw new TransportException('Laravel HTTP transport failed.', 0, $e);
}
$headers = [];
foreach ($response->headers() as $name => $values) {
$headers[strtolower($name)] = implode(', ', $values);
}
return new HttpResponse($response->status(), $response->body(), $headers);
}
}