forked from apoloz/alfa-bank-sdk
103 lines
3.5 KiB
PHP
103 lines
3.5 KiB
PHP
use Apoloz\AlfaBank\Data\NominalAccounts\Identification\AmountDistributionData;
|
|
use Apoloz\AlfaBank\Requests\NominalAccounts\Payouts\PayoffRequest;
|
|
use Apoloz\AlfaBank\Data\NominalAccounts\Payouts\PayoffRequestData;
|
|
use Apoloz\AlfaBank\Data\Common\RecipientData;
|
|
use Apoloz\AlfaBank\Data\NominalAccounts\Payouts\PayoutRequisiteData;
|
|
use Apoloz\AlfaBank\Services\PhpseclibCardEncryptor;
|
|
use Spatie\LaravelData\DataCollection;
|
|
|
|
// --- CONFIGURATION ---
|
|
// Replace these with real data from your system/webhook
|
|
$nominalAccount = '40702810...'; // Ваш Номинальный счет
|
|
$transactionId = '1220112MOCO...'; // ID входящего платежа (из Вебхука или Выписки)
|
|
$beneficiaryId = 'BEN-8d4a...'; // ID Бенефициара (которого создали в примере 17)
|
|
$amount = '100.00'; // Сумма операции (в рублях)
|
|
// Public key for CARD encryption (Download from Bank Portal)
|
|
$cardPublicKeyPath = storage_path('certs/alfa_card_enc_key.pub');
|
|
|
|
// 1. Setup Connector
|
|
$baas = app(AlfaBankConnector::class);
|
|
$baas->login();
|
|
|
|
echo "Connector ready.\n";
|
|
|
|
// ==========================================
|
|
// STEP 1: IDENTIFICATION (Assign funds to Beneficiary)
|
|
// ==========================================
|
|
echo "\n--- Step 1: Identification ---\n";
|
|
|
|
$identifyData = new IdentifyRequestData(
|
|
transactionId: $transactionId,
|
|
accountNumber: $nominalAccount,
|
|
amountDistribution: AmountDistributionData::collect([
|
|
[
|
|
'beneficiaryId' => $beneficiaryId,
|
|
'amount' => $amount
|
|
]
|
|
], DataCollection::class)
|
|
);
|
|
|
|
// Automatic Signing
|
|
$identifyData->sign($baas->getSigner());
|
|
|
|
try {
|
|
$idResponse = $baas->send(new IdentifyTransactionRequest($identifyData));
|
|
echo "Identification Status: " . $idResponse->status() . "\n";
|
|
echo "Funds assigned to beneficiary.\n";
|
|
} catch (\Exception $e) {
|
|
echo "Identification Failed: " . $e->getMessage() . "\n";
|
|
}
|
|
|
|
|
|
// ==========================================
|
|
// STEP 2: PAYOUT (Withdraw to Bank Card)
|
|
// ==========================================
|
|
echo "\n--- Step 2: Payout (Card) ---\n";
|
|
|
|
// A. Encrypt the Card Number (PAN)
|
|
// This is required for security. The raw PAN is never sent.
|
|
if (!file_exists($cardPublicKeyPath)) {
|
|
echo "Error: Card Encryption Key not found at $cardPublicKeyPath\n";
|
|
return;
|
|
}
|
|
|
|
$encryptor = new PhpseclibCardEncryptor(file_get_contents($cardPublicKeyPath));
|
|
$encryptedPan = $encryptor->encrypt('2200000000000000'); // Real PAN here
|
|
|
|
echo "PAN Encrypted.\n";
|
|
|
|
// B. Prepare Payout Request
|
|
$payoutData = new PayoffRequestData(
|
|
type: 'FL_RESIDENT',
|
|
accountNumber: $nominalAccount,
|
|
recipient: new RecipientData($beneficiaryId),
|
|
amount: $amount,
|
|
purpose: 'Выплата средств по договору оферты',
|
|
|
|
// Direct details (Bank Card / PAN)
|
|
payoutRequisite: new PayoutRequisiteData(
|
|
type: 'CARD',
|
|
pan: $encryptedPan
|
|
)
|
|
);
|
|
|
|
// C. Sign and Send
|
|
echo "Signing payout request...\n";
|
|
$payoutData->sign($baas->getSigner());
|
|
|
|
try {
|
|
$payResponse = $baas->send(new PayoffRequest($payoutData));
|
|
$result = $payResponse->dto();
|
|
|
|
echo "Payout Initiated!\n";
|
|
echo "Transaction ID: " . ($result->id ?? 'N/A') . "\n";
|
|
echo "Status: " . ($result->status ?? 'PENDING') . "\n";
|
|
|
|
} catch (\Exception $e) {
|
|
echo "Payout Failed: " . $e->getMessage() . "\n";
|
|
if (method_exists($e, 'getResponse')) {
|
|
echo "Details: " . $e->getResponse()->body() . "\n";
|
|
}
|
|
}
|
|
|