123456789101112131415161718192021222324252627282930313233343536373839 |
- <?php
- require_once __DIR__ . "/vendor/autoload.php";
- error_reporting(E_ALL);
- setlocale(LC_ALL, 'ru_RU.UTF-8');
- if(empty($_ENV['YOOMONEY_ACCESS_TOKEN']) || empty($_ENV['YOOMONEY_TRANSFER_TO'])) {
- throw new Exception('Environment variables `YOOMONEY_ACCESS_TOKEN` and `YOOMONEY_TRANSFER_TO` must be set');
- }
- if(empty($_ENV['METRICS_URL']) || empty($_ENV['METRICS_USER']) || empty($_ENV['METRICS_PASSWORD']) || empty($_ENV['METRICS_DATABASE'])) {
- throw new Exception('Environment variables `METRICS_URL`, `METRICS_USER`, `METRICS_PASSWORD` and `METRICS_DATABASE` must be set');
- }
- $yooMoney = new YooMoney($_ENV['YOOMONEY_ACCESS_TOKEN']);
- $metrics = new Metrics($_ENV['METRICS_URL'], $_ENV['METRICS_USER'], $_ENV['METRICS_PASSWORD'], $_ENV['METRICS_DATABASE']);
- $info = $yooMoney->getInfo();
- $metrics->send('yoomoney', ['balance' => $info['balance']], ['account' => $info['account']]);
- if(!empty($_ENV['YOOMONEY_TRANSFER_TO_ACCESS_TOKEN'])) {
- $infoTransferTo = (new YooMoney($_ENV['YOOMONEY_TRANSFER_TO_ACCESS_TOKEN']))->getInfo();
- $metrics->send('yoomoney', ['balance' => $infoTransferTo['balance']], ['account' => $infoTransferTo['account']]);
- }
- if($info['balance'] < 2 || $info['balance'] == floor($info['balance'])) {
- exit();
- }
- $transferAmount = $info['balance'] - floor($info['balance']) + 2;
- if($transferAmount > $info['balance']) {
- exit();
- }
- $response = $yooMoney->transfer($_ENV['YOOMONEY_TRANSFER_TO'], $transferAmount, 'Rounding from ' . $info['account'], 'Rounding', 'rounding');
- $yooMoney->confirm($response['request_id']);
|