add anpay

This commit is contained in:
Your Name
2021-06-20 18:20:21 +03:00
parent d328ffa7b0
commit 099e0be8f5
6 changed files with 219 additions and 26 deletions

154
app/routes/anpay.php Normal file
View File

@@ -0,0 +1,154 @@
<?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;
}

View File

@@ -5,19 +5,13 @@
* оформление заказа
*/
function checkout_kuri($priceslug, $buy_type = 1){
function checkout_kuri($priceslug, $delivery_id = 1){
$price_id = db_get("SELECT * FROM `price` WHERE `priceslug` = '$priceslug' LIMIT 1");
if (isset($price_id['price_id'])) {
if ($buy_type == 1){ // тип товара
if ($delivery_id == 1){ // тип товара
$price = $price_id['price']; // электронная книжка
}
else {
@@ -30,7 +24,7 @@ function checkout_kuri($priceslug, $buy_type = 1){
$client = client_find_id(CLIENT_ID);
if (isset($client['result']['client_id'])){
$clienthash = $client['result']['clienthash'];
$client = $client['result'];
}
}
@@ -43,16 +37,42 @@ function checkout_kuri($priceslug, $buy_type = 1){
$error_mail = True;
}
else {
# получаем хэш клиента чтобы отправить на оплату
$clienthash = api_add_client($clientmail);
# ищем или создаем клиента
$client = api_add_client($clientmail);
}
}
if (isset($clienthash) and $clienthash) {
$paylink = PAYSITE."anpay/$priceslug/$clienthash";
header("Location: $paylink");
return True;
if (isset($client['client_id'])) {
#создаем заказ
$add['client_id'] = $client['client_id'];
$add['buy_email'] = $client['clientmail'];
$add['buy_price'] = $price;
$add['price'] = $price;
$add['buy_status'] = 0;
$add['mail_status'] = 0;
$add['number_id'] = $price_id['price_id'];
$add['pay_id'] = 3; // оплата через Юкассу
$add['delivery_id'] = $delivery_id;
$add['date_start'] = date('Y-m-d H:i:s');
$add['buy_active'] = 0;
$order_id = db_insert('anbuy', $add, 'chitatel'); // добавляем в базу заказ
if (is_array($order_id)){ // неудалось создать заказ
$error_mail = True;
}
else { // заказ создался
return anpay_kuri($order_id);
}
}
@@ -61,9 +81,9 @@ function checkout_kuri($priceslug, $buy_type = 1){
$error_price = True;
}
$submit_link = SITE."checkout/$priceslug/$buy_type";
$submit_link = SITE."checkout/$priceslug/$buy_type";
include VIEWPATH.'checkout.phtml';
include VIEWPATH.'checkout.phtml';
}
}