Files
alfa-bank-sdk/examples/17_laravel_tinker_scoring_creation.php

97 lines
3.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* LARAVEL TINKER WORKFLOW: Scoring -> Creation -> Signing
*
* This script is designed to be copy-pasted directly into `php artisan tinker`.
* It demonstrates the new simplified DX with automatic signing.
*
* Prerequisites:
* 1. .env is configured (ALFA_BAAS_CLIENT_ID, ALFA_BAAS_CERT_PATH, etc.)
* 2. Run: php artisan tinker
*/
use Apoloz\AlfaBank\Core\AlfaBankConnector;
use Apoloz\AlfaBank\Requests\NominalAccounts\Beneficiaries\ScoreBeneficiaryRequest;
use Apoloz\AlfaBank\Data\NominalAccounts\Beneficiaries\ScoreBeneficiaryRequestData;
use Apoloz\AlfaBank\Requests\NominalAccounts\Beneficiaries\CreateBeneficiaryRequest;
use Apoloz\AlfaBank\Data\NominalAccounts\Beneficiaries\CreateBeneficiaryRequestData;
use Apoloz\AlfaBank\Data\NominalAccounts\Beneficiaries\AddressData;
// 1. Get Configured Connector
// Laravel automatically injects config from config/alfa-bank.php
$baas = app(AlfaBankConnector::class);
// 2. Authenticate
// Uses Client Credentials (if configured) or Refresh Token logic if you have one.
// For server-to-server tasks, login() is usually sufficient.
echo "Logging in...\n";
$baas->login();
// --- STEP A: SCORING (Check if we can work with this person) ---
echo "--- Step A: Scoring ---\n";
$scoreData = new ScoreBeneficiaryRequestData(
type: 'FL_RESIDENT',
inn: '771234567890',
name: 'Иванов Иван Иванович'
);
try {
$scoreResponse = $baas->send(new ScoreBeneficiaryRequest($scoreData));
$scoreResult = $scoreResponse->dto(); // ScoreBeneficiaryResponseData
echo "Scoring Result: " . $scoreResult->result . "\n";
echo "Description: " . $scoreResult->description . "\n";
if ($scoreResult->result !== 'SUCCESS') {
echo "Stopping: Scoring failed.\n";
return;
}
} catch (\Exception $e) {
echo "Scoring Error: " . $e->getMessage() . "\n";
return;
}
// --- STEP B: CREATION (With Automatic Signing) ---
echo "\n--- Step B: Creation ---\n";
$createData = new CreateBeneficiaryRequestData(
type: 'FL_RESIDENT',
countryOfTaxResidence: 'RU',
name: 'Иванов Иван Иванович', // Must match scoring
inn: '771234567890',
phoneNumber: '79001234567',
email: 'ivanov@example.com',
addresses: AddressData::collect([
new AddressData(
type: 'REGISTRATION_ADDRESS',
address: '123456, г. Москва, ул. Тверская, д. 1',
country: 'RU'
)
])
);
// MAGIC HAPPENS HERE:
// Automatically signs the JSON payload using the connector's certificates.
echo "Signing data...\n";
$createData->sign($baas->getSigner());
try {
$createResponse = $baas->send(new CreateBeneficiaryRequest($createData));
$beneficiary = $createResponse->dto(); // CreateBeneficiaryResponseData
echo "SUCCESS! Beneficiary Created.\n";
echo "ID: " . $beneficiary->id . "\n";
echo "Status: " . $beneficiary->status . "\n";
echo "Request ID: " . $beneficiary->requestId . "\n";
} catch (\Exception $e) {
echo "Creation Error: " . $e->getMessage() . "\n";
if (method_exists($e, 'getResponse')) {
echo "Response Body: " . $e->getResponse()->body() . "\n";
}
}