Files
alfa-bank-sdk/examples/19_laravel_tinker_card_registration_payout.php
Anatoliy Poloz b15d29f8e9 fix(formating)
2026-01-28 03:36:26 +03:00

108 lines
3.1 KiB
PHP

<?php
/**
* LARAVEL TINKER WORKFLOW: Secure Card Registration -> Payout by ID
*
* Scenario:
* 1. User enters Card Number (PAN).
* 2. You encrypt PAN and register it as a "Requisite" in Alfa Bank.
* 3. You save only the `requisiteId` in your database.
* 4. Later, you pay out to this user using only the ID (Zero PAN storage).
*
* Usage: Copy-paste into `php artisan tinker`.
*/
use Apoloz\AlfaBank\Core\AlfaBankConnector;
use Apoloz\AlfaBank\Data\Common\RecipientData;
use Apoloz\AlfaBank\Data\NominalAccounts\Beneficiaries\Requisites\CreateRequisiteRequestData;
use Apoloz\AlfaBank\Data\NominalAccounts\Payouts\PayoffRequestData;
use Apoloz\AlfaBank\Data\NominalAccounts\Payouts\PayoutRequisiteData;
use Apoloz\AlfaBank\Requests\NominalAccounts\Beneficiaries\Requisites\CreateRequisiteRequest;
use Apoloz\AlfaBank\Requests\NominalAccounts\Payouts\PayoffRequest;
use Apoloz\AlfaBank\Services\PhpseclibCardEncryptor;
// --- CONFIGURATION ---
$nominalAccount = '40702810...';
$beneficiaryId = 'BEN-123...';
$rawPan = '4276000000001234'; // Вводится пользователем (не сохранять в БД!)
$cardPubKeyPath = storage_path('certs/alfa_card_enc_key.pub');
// 1. Setup
$baas = app(AlfaBankConnector::class);
$baas->login();
// ==========================================
// STEP 1: SECURE REGISTRATION (Tokenization)
// ==========================================
echo '--- Step 1: Registering Card Requisite ---
';
// A. Encrypt PAN for transport
$encryptor = new PhpseclibCardEncryptor(file_get_contents($cardPubKeyPath));
$encryptedPan = $encryptor->encrypt($rawPan);
// B. Request Creation of Requisite in Alfa Bank
$reqData = new CreateRequisiteRequestData(
type: 'CARD',
pan: $encryptedPan,
beneficiaryId: $beneficiaryId,
isMain: true
);
$reqData->sign($baas->getSigner());
try {
$reqResponse = $baas->send(new CreateRequisiteRequest($reqData));
$requisiteId = $reqResponse->dto()->id; // E.g. "REQ-998877"
echo 'SUCCESS! Card Registered.
';
echo "Requisite ID: $requisiteId (SAVE THIS TO DATABASE)
";
echo 'Mask: ' . substr($rawPan, 0, 6) . '******' . substr($rawPan, -4) . '
';
} catch (\Exception $e) {
echo 'Registration Failed: ' . $e->getMessage() . '
';
return;
}
// ==========================================
// STEP 2: PAYOUT (Using Requisite ID)
// ==========================================
echo "\n--- Step 2: Payout using Requisite ID ---
";
// Now we don't need PAN anymore. We use the ID we just got.
$payoutData = new PayoffRequestData(
type: 'FL_RESIDENT',
accountNumber: $nominalAccount,
recipient: new RecipientData($beneficiaryId),
amount: '100.00',
purpose: 'Выплата вознаграждения',
// Instead of PAN, we provide the Requisite ID
payoutRequisite: new PayoutRequisiteData(
type: 'CARD',
requisiteId: $requisiteId
)
);
$payoutData->sign($baas->getSigner());
try {
$payResponse = $baas->send(new PayoffRequest($payoutData));
$result = $payResponse->dto();
echo 'Payout Sent!
';
echo 'ID: ' . ($result->id ?? 'N/A') . '
';
} catch (\Exception $e) {
echo 'Payout Failed: ' . $e->getMessage() . '
';
}