<?php
$toNumber = '79021234567'; // Номер кошелька получателя
$fromNumberToken = [ // Массив данных, номер кошелька и токен
'79001234567' => '4hjh34bgf50560df90e94051ea5e251g',
'79011234567' => '151ehjh34bgf625118df90e51e940872'
];
$transferLimit = 100; // Лимит срабатывания перевода
$balanceAmount = 50; // Сумма остатка на кошельке
function balance($number, $token) {
$header = array('Accept: application/json',
'Content-Type: application/json',
'Authorization: Bearer ' . $token,
'Host: edge.qiwi.com');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://edge.qiwi.com/funding-sources/v2/persons/' . $number . '/accounts');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
function transfer($number, $token, $amount) {
$id = time() * 1000;
$data =
[
'id' => "$id",
'sum' =>
[
'amount' => $amount,
'currency' => '643'
],
'paymentMethod' =>
[
'type' => 'Account',
'accountId' => '643'
],
'comment' => '',
'fields' =>
[
'account' => '+' . $number
]
];
$data = json_encode($data);
$header = array('Accept: application/json',
'Content-Type: application/json',
'Content-Length: ' . strlen($data),
'Authorization: Bearer ' . $token,
'Host: edge.qiwi.com');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://edge.qiwi.com/sinap/api/v2/terms/99/payments');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
foreach($fromNumberToken as $number => $token) {
$balance = balance($number, $token);
foreach($balance['accounts'] as $account) {
if ($account['alias'] == 'qw_wallet_rub' && $account['balance']['amount'] > $transferLimit) {
transfer($toNumber, $token, $account['balance']['amount'] - $balanceAmount);
}
}
}
?>