Files
anpdf/app/routes/anpay.php
Your Name 099e0be8f5 add anpay
2021-06-20 18:20:21 +03:00

155 lines
3.2 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
use YooKassa\Client;
function anpay_kuri($buy_id){
# получаем инфу о заказе
$buy_find = "
SELECT
`buy_id`,
`buy_status`,
`pricename`,
`anbuy`.`price` as `currprice`
FROM
`anbuy`
LEFT JOIN
`price` ON `anbuy`.`number_id` = `price`.`price_id`
WHERE
`buy_id` = '$buy_id'
LIMIT 1
";
$buy_id = db_get($buy_find);
# заказ не найден
if (!isset($buy_id['buy_id'])){
echo 'заказ не найден!';
return false;
}
# проверяем статус заказа
if ($buy_id['buy_status']) {
echo 'заказ уже оплачен!';
return false;
}
# получить ссылку на оплату
$payment = yk_pay($buy_id['currprice'], $buy_id['pricename']);
if (isset($payment['confirmation']['confirmation_url'])){
$pay_url = $payment['confirmation']['confirmation_url'];
//добавляем shop_id от ЮКАССЫ чтобы следить за статусом оплаты
$upd_shop = "
UPDATE
`anbuy`
SET
`shop_id` = '{$payment->_id}'
WHERE
`buy_id` = '{$buy_id['buy_id']}'
";
db_get($upd_shop, 'chitatel'); # фиксируем shop_id от Юкассы
header("Location: $pay_url"); // перенаправляем на оплату
}
}
# получение результатов оплаты заказа
function resultpay_kuri(){
$source = file_get_contents('php://input');
//$source = file_get_contents('/vhosts/anpay/app/tests/result.json');
$result = json_decode($source, true);
if (isset($result['event'])) {
if ($result['event'] == "payment.succeeded"){
$shop_id = $result['object']['id'];
$find_sql = "SELECT buy_id FROM `anbuy` WHERE `shop_id` = '$shop_id' LIMIT 1";
$find_order = db_get($find_sql);
if (isset($find_order['buy_id'])){
$currdate = date('Y-m-d H:i:s');
$supd = "
UPDATE
`anbuy`
SET
`buy_status` = '1',
`buy_active` = '1',
`date` = '$currdate'
WHERE
`buy_id` = '{$find_order['buy_id']}'";
db_get($supd, 'chitatel');
}
}
}
else {
echo "order not fount";
exit;
}
logsave('yk', $source);
return True;
}
function yk_pay($price, $name){
$client = new Client();
$client->setAuth(YID, YKEY);
$items = array(
'amount' => array(
'value' => 1.0,
'currency' => 'RUB',
),
'confirmation' => array(
'type' => 'redirect',
'return_url' => SITE,
),
'capture' => true,
'description' => $name,
);
$id = uniqid('', true);
$payment = $client->createPayment(
$items, $id
);
return $payment;
}