PHP code example of tigusigalpa / telegram-wallet-php

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

    

tigusigalpa / telegram-wallet-php example snippets




use Tigusigalpa\TelegramWallet\WalletPayClient;
use Tigusigalpa\TelegramWallet\DTO\CreateOrderRequest;
use Tigusigalpa\TelegramWallet\DTO\MoneyAmount;

// Initialize with your API key from Wallet Pay
$client = new WalletPayClient(apiKey: 'YOUR_STORE_API_KEY');

// Create a payment order
$order = $client->createOrder(new CreateOrderRequest(
    amount: new MoneyAmount('USD', '9.99'),
    description: 'Premium subscription for 1 month',
    externalId: 'ORDER-' . uniqid(),      // Your unique order ID
    timeoutSeconds: 3600,                  // 1 hour to pay
    customerTelegramUserId: 123456789,     // Who can pay this order
    autoConversionCurrency: 'USDT',        // Receive payment in USDT
    returnUrl: 'https://t.me/YourBot/YourApp',
    customData: json_encode(['user_id' => 42])
));

// Send this link to your user — they'll pay right in Telegram!
echo $order->directPayLink;

use Tigusigalpa\TelegramWallet\Laravel\Facades\WalletPay;
use Tigusigalpa\TelegramWallet\DTO\CreateOrderRequest;
use Tigusigalpa\TelegramWallet\DTO\MoneyAmount;

$order = WalletPay::createOrder(new CreateOrderRequest(
    amount: new MoneyAmount('USD', '9.99'),
    description: 'Premium subscription',
    externalId: 'SUB-' . $user->id . '-' . time(),
    timeoutSeconds: 3600,
    customerTelegramUserId: $user->telegram_id,
    autoConversionCurrency: 'USDT',
    returnUrl: 'https://t.me/YourBot/YourApp',
    customData: json_encode(['user_id' => $user->id])
));

return response()->json(['pay_url' => $order->directPayLink]);

use Tigusigalpa\TelegramWallet\Webhook\WebhookVerifier;
use Tigusigalpa\TelegramWallet\Enums\WebhookEventType;

$verifier = new WebhookVerifier('YOUR_STORE_API_KEY');

try {
    // Verify signature and parse events in one call
    $events = $verifier->verifyAndParse(
        $_SERVER['REQUEST_METHOD'],
        parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH),
        $_SERVER['HTTP_WALLETPAY_TIMESTAMP'],
        file_get_contents('php://input'),
        $_SERVER['HTTP_WALLETPAY_SIGNATURE']
    );
    
    foreach ($events as $event) {
        if ($event->type === WebhookEventType::ORDER_PAID) {
            // 🎉 Payment successful!
            $customData = json_decode($event->payload->customData, true);
            
            // Now you can:
            // - Update your database
            // - Grant premium access
            // - Send a thank-you message
        }
    }
    
    http_response_code(200);
    echo 'OK';
} catch (InvalidWebhookSignatureException $e) {
    http_response_code(401);
    echo 'Invalid signature';
}

use Tigusigalpa\TelegramWallet\WalletPayClient;
use GuzzleHttp\Client;

// Simple — just your API key
$client = new WalletPayClient(apiKey: getenv('WALLETPAY_API_KEY'));

// Custom — bring your own HTTP client
$client = new WalletPayClient(
    apiKey: getenv('WALLETPAY_API_KEY'),
    timeout: 60,
    httpClient: new Client(['verify' => true])
);



return [
    'api_key' => env('WALLETPAY_API_KEY'),
    'base_url' => env('WALLETPAY_BASE_URL', 'https://pay.wallet.tg'),
    'timeout' => env('WALLETPAY_TIMEOUT', 30),
    'webhook_path' => env('WALLETPAY_WEBHOOK_PATH', '/webhook/walletpay'),
];



// Example: Store processed event IDs in database
if (ProcessedWebhookEvent::where('event_id', $event->eventId)->exists()) {
    return; // Already processed
}

// Process the event
processPayment($event);

// Mark as processed
ProcessedWebhookEvent::create(['event_id' => $event->eventId]);

// config/app.php
'providers' => [
    Tigusigalpa\TelegramWallet\Laravel\WalletPayServiceProvider::class,
],

// config/app.php
'aliases' => [
    'WalletPay' => Tigusigalpa\TelegramWallet\Laravel\Facades\WalletPay::class,
],

protected $middlewareAliases = [
    'walletpay.webhook' => \Tigusigalpa\TelegramWallet\Laravel\Http\Middleware\VerifyWalletPayWebhook::class,
];

// routes/api.php
use Tigusigalpa\TelegramWallet\Webhook\WebhookVerifier;

Route::post('/webhook/walletpay', function (Request $request, WebhookVerifier $verifier) {
    $events = $verifier->parseWebhookEvents($request->getContent());
    
    foreach ($events as $event) {
        Log::info('Payment webhook', [
            'event_id' => $event->eventId,
            'type' => $event->type->value,
        ]);
    }
    
    return response('OK', 200);
})->middleware('walletpay.webhook');



namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Tigusigalpa\TelegramWallet\Laravel\Facades\WalletPay;
use Tigusigalpa\TelegramWallet\DTO\CreateOrderRequest;
use Tigusigalpa\TelegramWallet\DTO\MoneyAmount;
use Tigusigalpa\TelegramWallet\Enums\WebhookEventType;
use Tigusigalpa\TelegramWallet\Webhook\WebhookVerifier;

class PaymentController extends Controller
{
    public function createPayment(Request $request)
    {
        $user = $request->user();
        
        $order = WalletPay::createOrder(new CreateOrderRequest(
            amount: new MoneyAmount('USD', '9.99'),
            description: 'Premium subscription',
            externalId: 'SUB-' . $user->id . '-' . time(),
            timeoutSeconds: 3600,
            customerTelegramUserId: $user->telegram_id,
            autoConversionCurrency: 'USDT',
            returnUrl: 'https://t.me/YourBot/YourApp',
            customData: json_encode(['user_id' => $user->id])
        ));
        
        // Save to your database
        Payment::create([
            'user_id' => $user->id,
            'wallet_pay_order_id' => $order->id,
            'amount' => $order->amount->amount,
            'status' => 'pending',
        ]);
        
        return response()->json(['pay_url' => $order->directPayLink]);
    }
    
    public function webhook(Request $request, WebhookVerifier $verifier)
    {
        $events = $verifier->parseWebhookEvents($request->getContent());
        
        foreach ($events as $event) {
            if ($event->type === WebhookEventType::ORDER_PAID) {
                $customData = json_decode($event->payload->customData, true);
                
                // Update payment status
                Payment::where('wallet_pay_order_id', $event->payload->id)
                    ->update(['status' => 'paid']);
                
                // Grant premium access
                User::find($customData['user_id'])
                    ->update(['is_premium' => true]);
            }
        }
        
        return response('OK', 200);
    }
}

use Tigusigalpa\TelegramWallet\Exceptions\OrderNotFoundException;
use Tigusigalpa\TelegramWallet\Exceptions\RateLimitException;
use Tigusigalpa\TelegramWallet\Exceptions\WalletPayException;

try {
    $order = $client->getOrderPreview('123456');
} catch (OrderNotFoundException $e) {
    // Order doesn't exist
    return response()->json(['error' => 'Order not found'], 404);
} catch (RateLimitException $e) {
    // Too many requests — back off and retry
    return response()->json(['error' => 'Try again later'], 429);
} catch (WalletPayException $e) {
    // Something else went wrong
    Log::error('Wallet Pay error', ['message' => $e->getMessage()]);
    return response()->json(['error' => 'Payment error'], 500);
}

$externalId = 'ORDER-' . $userId . '-' . $productId . '-' . time();
bash
composer 
bash
php artisan vendor:publish --tag=walletpay-config