Skip to content

Instantly share code, notes, and snippets.

@andronex
Last active December 10, 2023 18:20
Show Gist options
  • Save andronex/c952785ee4ebf45582e7c63227377bbe to your computer and use it in GitHub Desktop.
Save andronex/c952785ee4ebf45582e7c63227377bbe to your computer and use it in GitHub Desktop.
Интеграция сайта на MODX с сервисом "Долями" от "Тинькофф". Долями — BNPL сервис для оплаты частями покупок в интернет-магазинах партнерах за 4 равных платежа. https://dolyame.ru/
<?php
/*
* API для Долями
* by [email protected] / andronex
* интеграция от 20 т.р.
*/
use VKolegov\DolyameAPI\DolyameAPIClient;
use VKolegov\DolyameAPI\Entities\OrderInfo;
use VKolegov\DolyameAPI\Entities\OrderItems;
use VKolegov\DolyameAPI\Entities\RefundResponse;
use VKolegov\DolyameAPI\Entities\ClientInfo;
use VKolegov\DolyameAPI\Exceptions\DolyameRequestException;
use VKolegov\DolyameAPI\Requests\CommitOrderRequest;
use VKolegov\DolyameAPI\Requests\CreateOrderRequest;
use VKolegov\DolyameAPI\Requests\RefundRequest;
/** @var modX $modx */
define('MODX_API_MODE', true);
require_once dirname(dirname(dirname(dirname(__FILE__)))) . '/index.php';
$modx->getService('error', 'error.modError');
$modx->setLogLevel(modX::LOG_LEVEL_ERROR);
$modx->setLogTarget('FILE');
$modx->getService('rest', 'rest.modRestService');
require_once 'vendor/autoload.php';
$path_sert = dirname(dirname(dirname(dirname(dirname(__FILE__)))));
$dolyame_login = $modx->getOption('dolyame_login');
$dolyame_pass = $modx->getOption('dolyame_pass');
if(!$dolyame_login || !$dolyame_pass){
$modx->error->addField('dolyame_login', 'empty');
$modx->error->addField('dolyame_pass', 'empty');
echo json_encode($modx->error->process('Не заполнен логин и пароль от сервиса'));
$modx->rest->sendUnauthorized(true);
}
$action = (string)$_REQUEST['action'];
if(!$action){
$modx->error->addField('action', 'empty');
echo json_encode($modx->error->process('Не указан метод'));
$modx->rest->sendUnauthorized(true);
}
if(!file_exists($path_sert . '/open-api-cert.pem')){
$modx->error->addField('open-api-cert.pem', 'empty');
echo json_encode($modx->error->process('Нет сертифката open-api-cert.pem'));
$modx->rest->sendUnauthorized(true);
}
if(!file_exists($path_sert . '/private.key')){
$modx->error->addField('private.key', 'empty');
echo json_encode($modx->error->process('Нет сертифката private.key'));
$modx->rest->sendUnauthorized(true);
}
function phone_format($phone)
{
$phone = trim($phone);
$res = preg_replace(
array(
'/[\+]?([7|8])[-|\s]?\([-|\s]?(\d{3})[-|\s]?\)[-|\s]?(\d{3})[-|\s]?(\d{2})[-|\s]?(\d{2})/',
'/[\+]?([7|8])[-|\s]?(\d{3})[-|\s]?(\d{3})[-|\s]?(\d{2})[-|\s]?(\d{2})/',
'/[\+]?([7|8])[-|\s]?\([-|\s]?(\d{4})[-|\s]?\)[-|\s]?(\d{2})[-|\s]?(\d{2})[-|\s]?(\d{2})/',
'/[\+]?([7|8])[-|\s]?(\d{4})[-|\s]?(\d{2})[-|\s]?(\d{2})[-|\s]?(\d{2})/',
'/[\+]?([7|8])[-|\s]?\([-|\s]?(\d{4})[-|\s]?\)[-|\s]?(\d{3})[-|\s]?(\d{3})/',
'/[\+]?([7|8])[-|\s]?(\d{4})[-|\s]?(\d{3})[-|\s]?(\d{3})/',
),
array(
'+7$2$3$4$5',
'+7$2$3$4$5',
'+7$2$3$4$5',
'+7$2$3$4$5',
'+7$2$3$4',
'+7$2$3$4',
),
$phone
);
return $res;
}
$Dolyame = new DolyameAPIClient($dolyame_login, $dolyame_pass, $path_sert . '/open-api-cert.pem', $path_sert . '/private.key');
// объект товаров в заказе
$Items = new OrderItems();
// объект заказа
$Order = new CreateOrderRequest(time(), 0, $Items);
// объект клиента
$ClientInfo = new ClientInfo();
// установка УРЛОВ возврата и уведомлений
$Order->setNotificationURL($modx->getOption('site_url') . 'assets/components/dolyame/action.php?action=notify');
$Order->setFailURL($modx->getOption('site_url') . 'assets/components/dolyame/action.php?action=fail');
$Order->setSuccessURL($modx->getOption('site_url') . 'assets/components/dolyame/action.php?action=success');
switch($action){
case 'create':
$get = $modx->sanitize($_REQUEST, $modx->sanitizePatterns);
if(!$get['idPage']){
$modx->error->addField('idPage', 'empty');
die(json_encode($modx->error->process('Не указана услуга')));
}
if(!$serv_res = $modx->getObject('modResource', (int)$get['idPage'])){
$modx->error->addField('idPage', 'empty');
die(json_encode($modx->error->process('Не указана услуга')));
}
$service = $serv_res->getTVValue('offer');
$service = json_decode($service, true);
if(!$service || !isset($service[0]['dolyame_name'])){
$modx->error->addField('dolyame_name', 'empty');
die(json_encode($modx->error->process('Не указана услуга')));
}
$service_name = $service[0]['dolyame_name']?:$service[0]['title'];
$service_price = round(($service[0]['dolyame_price']?:0) / 4) * 4;
if(!$service[0]['dolyame_published']){
$modx->error->addField('dolyame_published', '0');
die(json_encode($modx->error->process('Оффер неактивен')));
}
// print_r($service_name);
// print_r($service_price);
if($get['fio'] || $get['name']){
$ClientInfo->setFirstName($get['fio']?:$get['name']);
}
if($get['phone']){
$ClientInfo->setPhone(phone_format($get['phone']));
}
if($get['email']){
$ClientInfo->setEmail($get['email']);
}
if($service_price){
$Order->setAmount($service_price);
}
else{
$modx->error->addField('amount', 'empty');
die(json_encode($modx->error->process('Не указана стоимость')));
}
$modx->addPackage('mercidolyame', MODX_CORE_PATH . 'components/mercidolyame/model/');
$ItemsAdd = $Items->fromArray([['name' => $service_name, 'quantity' => 1, 'price' => $service_price, 'tax' => ($service[0]['dolyame_tax']?:'20%'), 'payment_method' => 'CARD']]);
$Order->setItems($ItemsAdd);
$Order->setClientInfo($ClientInfo);
// print_r($get);
// print_r($Order->toArray());
$data_order = $Order->toArray();
$DolyameOrder = $modx->newObject('DolyameOrder');
$DolyameOrder->set('order_id', $data_order['order']['id']);
$DolyameOrder->set('fio', ($data_order['client_info']['first_name']?:'') . ($data_order['client_info']['middle_name']?' ' . $data_order['client_info']['middle_name']:'') . ($data_order['client_info']['last_name']?' ' . $data_order['client_info']['last_name']:''));
$DolyameOrder->set('price', $data_order['order']['amount']);
$DolyameOrder->set('page_id', (int)$get['idPage']);
$DolyameOrder->set('order_name', $service_name);
$DolyameOrder->set('phone', $data_order['client_info']['phone']);
$DolyameOrder->set('email', $data_order['client_info']['email']);
$DolyameOrder->set('createdon', time());
$DolyameOrder->save();
$newOrder = $Dolyame->createOrder($Order);
if($newOrder->getStatus()){
$DolyameOrder->set('status', $newOrder->getStatus());
$DolyameOrder->set('url', $newOrder->getLink());
$DolyameOrder->save();
}
// print_r($Dolyame->createOrder($Order));
echo json_encode($modx->error->process('', true, ['url' => $newOrder->getLink()]));
die();
break;
case 'fail':
$modx->sendRedirect($modx->makeUrl(3));
break;
case 'success':
$modx->sendRedirect($modx->makeUrl(3));
break;
case 'notify':
// $modx->log(1, print_r($_REQUEST, true));
// $modx->log(1, print_r(file_get_contents("php://input"), true));
$data = json_decode(file_get_contents("php://input"), true);
if($data){
$modx->addPackage('mercidolyame', MODX_CORE_PATH . 'components/mercidolyame/model/');
if($orderObj = $modx->getObject('DolyameOrder', ['order_id' => $data['id']])){
$orderObj->set('status', $data['status']);
if($data['status'] == 'completed'){
$orderObj->set('payment', 1);
}
$orderObj->save();
}
}
die('OK');
break;
case 'cancel':
$modx->addPackage('mercidolyame', MODX_CORE_PATH . 'components/mercidolyame/model/');
$get = $modx->sanitize($_REQUEST, $modx->sanitizePatterns);
if(isset($get['order_id']) && $get['order_id']){
$DolyameOrder = $modx->getObject('DolyameOrder', ['order_id' => $get['order_id']]);
if($DolyameOrder){
$DolyameOrderResult = $Dolyame->cancelOrder((string)$get['order_id']);
if($DolyameOrderResult->getStatus()){
$DolyameOrder->set('status', $DolyameOrderResult->getStatus());
$DolyameOrder->save();
}
}
}
die('<script>window.close();</script>');
break;
case 'commit':
$modx->addPackage('mercidolyame', MODX_CORE_PATH . 'components/mercidolyame/model/');
$get = $modx->sanitize($_REQUEST, $modx->sanitizePatterns);
if(isset($get['order_id']) && $get['order_id']){
$DolyameOrder = $modx->getObject('DolyameOrder', ['order_id' => $get['order_id']]);
if($DolyameOrder){
// объект коммита заказа
$Order = new CommitOrderRequest((string)$get['order_id'], $DolyameOrder->price, $Items);
$ItemsAdd = $Items->fromArray([['name' => $DolyameOrder->order_name, 'quantity' => 1, 'price' => $DolyameOrder->price, 'tax' => 'VAT 20%', 'payment_method' => 'CARD']]);
$Order->setItems($ItemsAdd);
$DolyameOrderResult = $Dolyame->commitOrder($Order);
if($DolyameOrderResult && $DolyameOrderResult->getStatus()){
$DolyameOrder->set('status', $DolyameOrderResult->getStatus());
$DolyameOrder->save();
}
}
}
die('<script>window.close();</script>');
break;
}
echo json_encode($modx->error->process('Нет такого метода'));
$modx->rest->sendUnauthorized(true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment