From 099e0be8f5e3279bc3c1e1925b74fa3619cc6b85 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 20 Jun 2021 18:20:21 +0300 Subject: [PATCH] add anpay --- app/api/apiclient.php | 13 ++-- app/confg.php | 10 ++- app/index.php | 1 + app/libs/logsave.php | 11 +++ app/routes/anpay.php | 154 ++++++++++++++++++++++++++++++++++++++++++ app/routes/order.php | 56 ++++++++++----- 6 files changed, 219 insertions(+), 26 deletions(-) create mode 100644 app/libs/logsave.php create mode 100644 app/routes/anpay.php diff --git a/app/api/apiclient.php b/app/api/apiclient.php index e7b719b..00930d4 100644 --- a/app/api/apiclient.php +++ b/app/api/apiclient.php @@ -225,23 +225,24 @@ function client_find_email_kuri($mail){ # узнаем id клиента по мылу или создаем его function api_add_client($mail){ - $find_mail = dbl_get("SELECT `client_id`, `clienthash` FROM `clients` WHERE `clientmail` = '$mail' LIMIT 1"); + $find_client = dbl_get("SELECT `client_id`, `clientmail`, `clienthash` FROM `clients` WHERE `clientmail` = '$mail' LIMIT 1"); - if (isset($find_mail['client_id'])){ - $result = $find_mail['clienthash']; + if (isset($find_client['client_id'])){ + $result = $find_client; // возвращаем клиента } else { $add['clientmail'] = $mail; $add['clienthash'] = md5($mail); $add['clientdate'] = date("Y-m-d H:i:s"); - $res = db_insert('clients', $add, 'chitatel'); + $res = db_insert('clients', $add, 'chitatel'); // создаем клиента if ($res > 0){ - $result = $add['clienthash']; + $add['client_id'] = $res; + $result = $add; } else{ - $result = false; + $result = false; # клиент не добавлен } } diff --git a/app/confg.php b/app/confg.php index 99a989d..a3f9473 100644 --- a/app/confg.php +++ b/app/confg.php @@ -6,7 +6,7 @@ error_reporting(E_ALL); if (isset($_SERVER['HTTP_HOST'])) - define('SITE', 'http://'.$_SERVER['HTTP_HOST'].'/'); + define('SITE', 'https://'.$_SERVER['HTTP_HOST'].'/'); if (isset($_SERVER['HTTP_HOST'])) define('SITEPATH', $_SERVER['DOCUMENT_ROOT'].'/'); @@ -31,7 +31,13 @@ define('VIEWPATH', SITEPATH.'views/'); define('API', 'http://api.argumenti.ru/'); define('IMGSRV', 'https://imgurl.argumenti.ru/'); -define('PAYSITE', 'https://pay.argumenti.ru/'); //сервис оплаты +/* тестовый магазин ЮКАССА */ +define('YID', '812455'); +define('YKEY', 'test_7asbXctnircUsmb_DhtYWSfxMiWzPgJBDF7OoZ0XfvQ'); + +define('LOGDIR', SITEPATH.'logs/'); // папка куда падают логи + + /** отправка уведомлений */ /*set('noreply', diff --git a/app/index.php b/app/index.php index ee77f67..58dade6 100644 --- a/app/index.php +++ b/app/index.php @@ -12,6 +12,7 @@ require 'routes/pdfserver.php'; require 'routes/email.php'; require 'routes/books.php'; require 'routes/order.php'; +require 'routes/anpay.php'; #api require 'api/apiclient.php'; diff --git a/app/libs/logsave.php b/app/libs/logsave.php new file mode 100644 index 0000000..7051e90 --- /dev/null +++ b/app/libs/logsave.php @@ -0,0 +1,11 @@ +_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; + + +} + diff --git a/app/routes/order.php b/app/routes/order.php index e374363..ef7d814 100644 --- a/app/routes/order.php +++ b/app/routes/order.php @@ -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'; -} \ No newline at end of file +}