164 lines
5.1 KiB
PHP
164 lines
5.1 KiB
PHP
|
<?php
|
||
|
|
||
|
/**
|
||
|
* PHP Mikrotik Billing (https://github.com/hotspotbilling/phpnuxbill/)
|
||
|
*
|
||
|
* Payment Gateway ioTec Pay
|
||
|
*
|
||
|
* Created for ioTec Pay v1 Collection API
|
||
|
*/
|
||
|
|
||
|
function iotec_validate_config()
|
||
|
{
|
||
|
global $config;
|
||
|
$config['iotec_client_id'] = $config['iotec_client_id'] ?? 'your_client_id_here';
|
||
|
$config['iotec_client_secret'] = $config['iotec_client_secret'] ?? 'your_client_secret_here';
|
||
|
$config['iotec_wallet_id'] = $config['iotec_wallet_id'] ?? 'your_wallet_id_here';
|
||
|
|
||
|
if (empty($config['iotec_client_id']) || empty($config['iotec_client_secret']) || empty($config['iotec_wallet_id'])) {
|
||
|
Message::sendTelegram("ioTec payment gateway not configured");
|
||
|
http_response_code(400);
|
||
|
header('Content-Type: application/json');
|
||
|
echo json_encode(['status' => 'error', 'message' => 'ioTec Pay not configured']);
|
||
|
exit;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function iotec_show_config()
|
||
|
{
|
||
|
global $ui;
|
||
|
$ui->assign('_title', 'ioTec Pay - Payment Gateway');
|
||
|
$ui->assign('env', [
|
||
|
['id' => 'Sandbox', 'name' => 'Sandbox (Testing)'],
|
||
|
['id' => 'Live', 'name' => 'Live (Production)']
|
||
|
]);
|
||
|
$ui->display('iotec.tpl');
|
||
|
}
|
||
|
|
||
|
function iotec_save_config()
|
||
|
{
|
||
|
global $admin, $_L;
|
||
|
$iotec_client_id = _post('iotec_client_id');
|
||
|
$iotec_client_secret = _post('iotec_client_secret');
|
||
|
$iotec_wallet_id = _post('iotec_wallet_id');
|
||
|
$iotec_env = _post('iotec_env');
|
||
|
|
||
|
$settings = [
|
||
|
'iotec_client_id' => $iotec_client_id,
|
||
|
'iotec_client_secret' => $iotec_client_secret,
|
||
|
'iotec_wallet_id' => $iotec_wallet_id,
|
||
|
'iotec_env' => $iotec_env
|
||
|
];
|
||
|
|
||
|
foreach ($settings as $key => $value) {
|
||
|
$d = ORM::for_table('tbl_appconfig')->where('setting', $key)->find_one();
|
||
|
if ($d) {
|
||
|
$d->value = $value;
|
||
|
$d->save();
|
||
|
} else {
|
||
|
$d = ORM::for_table('tbl_appconfig')->create();
|
||
|
$d->setting = $key;
|
||
|
$d->value = $value;
|
||
|
$d->save();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
_log('[' . $admin['username'] . ']: ioTec Pay ' . $_L['Settings_Saved_Successfully'], 'Admin', $admin['id']);
|
||
|
header('Content-Type: application/json');
|
||
|
echo json_encode(['status' => 'success', 'message' => $_L['Settings_Saved_Successfully']]);
|
||
|
exit;
|
||
|
}
|
||
|
|
||
|
function iotec_create_transaction_json($trx, $user)
|
||
|
{
|
||
|
global $config;
|
||
|
header('Content-Type: application/json');
|
||
|
|
||
|
// Validate required inputs
|
||
|
if (empty($trx['price']) || empty($user['phonenumber']) || empty($user['username']) || empty($trx['plan_name'])) {
|
||
|
http_response_code(400);
|
||
|
echo json_encode([
|
||
|
'status' => 'error',
|
||
|
'message' => 'Missing required input: price, phonenumber, username, or plan_name'
|
||
|
]);
|
||
|
exit;
|
||
|
}
|
||
|
|
||
|
$externalId = uniqid('bill_');
|
||
|
|
||
|
$tokenResponse = json_decode(Http::postData(iotec_get_server('auth') . 'connect/token', [
|
||
|
'client_id' => $config['iotec_client_id'],
|
||
|
'client_secret' => $config['iotec_client_secret'],
|
||
|
'grant_type' => 'client_credentials'
|
||
|
], [
|
||
|
'Content-Type: application/x-www-form-urlencoded'
|
||
|
]), true);
|
||
|
|
||
|
if (empty($tokenResponse['access_token'])) {
|
||
|
http_response_code(500);
|
||
|
echo json_encode([
|
||
|
'status' => 'error',
|
||
|
'message' => 'Failed to authenticate with ioTec',
|
||
|
'response' => $tokenResponse
|
||
|
]);
|
||
|
exit;
|
||
|
}
|
||
|
|
||
|
$json = [
|
||
|
'category' => 'MobileMoney',
|
||
|
'currency' => 'ITX',
|
||
|
'walletId' => $config['iotec_wallet_id'],
|
||
|
'externalId' => $externalId,
|
||
|
'payer' => $user['phonenumber'],
|
||
|
'amount' => $trx['price'],
|
||
|
'payerNote' => 'Payment for ' . $trx['plan_name'],
|
||
|
'payeeNote' => 'Hotspot billing',
|
||
|
'transactionChargesCategory' => 'ChargeCustomer'
|
||
|
];
|
||
|
|
||
|
$result = json_decode(Http::postJsonData(iotec_get_server('api') . 'api/collections/collect', $json, [
|
||
|
'Authorization: Bearer ' . $tokenResponse['access_token'],
|
||
|
'Content-Type: application/json'
|
||
|
]), true);
|
||
|
|
||
|
if (empty($result['id'])) {
|
||
|
http_response_code(502);
|
||
|
echo json_encode([
|
||
|
'status' => 'error',
|
||
|
'message' => 'Failed to create transaction',
|
||
|
'response' => $result
|
||
|
]);
|
||
|
exit;
|
||
|
}
|
||
|
|
||
|
$d = ORM::for_table('tbl_payment_gateway')
|
||
|
->where('username', $user['username'])
|
||
|
->where('status', 1)
|
||
|
->find_one();
|
||
|
|
||
|
if (!$d) {
|
||
|
http_response_code(404);
|
||
|
echo json_encode([
|
||
|
'status' => 'error',
|
||
|
'message' => 'Active payment gateway record not found for user'
|
||
|
]);
|
||
|
exit;
|
||
|
}
|
||
|
|
||
|
$d->gateway_trx_id = $result['id'];
|
||
|
$d->pg_url_payment = 'N/A';
|
||
|
$d->pg_request = json_encode($result);
|
||
|
$d->expired_date = date('Y-m-d H:i:s', strtotime("+6 HOUR"));
|
||
|
$d->save();
|
||
|
|
||
|
echo json_encode([
|
||
|
'status' => 'success',
|
||
|
'message' => 'Transaction created. Authorize on phone.',
|
||
|
'transaction_id' => $result['id'],
|
||
|
'external_id' => $externalId
|
||
|
]);
|
||
|
exit;
|
||
|
}
|
||
|
|
||
|
// Other functions (iotec_payment_notification, iotec_get_status, iotec_get_server, etc.) remain unchanged.
|