<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
/*
 * ==========================================================
 * AJAX.PHP
 * ==========================================================
 *
 * AJAX functions. This file must be executed only via AJAX. � 202Cryptopaymentin. All rights reserved.
 *
 */

$_POST = json_decode($_POST['data'], true);
require_once('functions.php');
crpay_cloud_load();


switch ($_POST['function']) {
    case 'installation':
        die(crpay_json_response(crpay_installation($_POST['installation_data'])));
    case 'login':
        die(crpay_json_response(crpay_login($_POST['username'], $_POST['password'], crpay_post('code'))));
    case 'get-balances':
        die(crpay_json_response(crpay_crypto_balances()));
    case 'get-settings':
        die(crpay_json_response(crpay_settings_get_all()));
    case 'save-settings':
        die(crpay_json_response(crpay_settings_save($_POST['settings'])));
    case 'get-transactions':
        die(crpay_json_response(crpay_transactions_get_all(crpay_post('pagination', 0), crpay_post('search'), crpay_post('status'), crpay_post('cryptocurrency'), crpay_post('date_range'), crpay_post('checkout_id'))));
    case 'get-transaction':
        die(crpay_json_response(crpay_transactions_get($_POST['transaction_id'])));
    case 'download-transactions':
        die(crpay_json_response(crpay_transactions_download(crpay_post('search'), crpay_post('status'), crpay_post('cryptocurrency'), crpay_post('date_range'))));
    case 'check-transaction':
        die(crpay_json_response(crpay_transactions_check_single($_POST['transaction'])));
    case 'check-transactions':
        die(crpay_json_response(crpay_transactions_check($_POST['transaction_id'])));
    case 'update-transaction':
        die(crpay_json_response(crpay_transactions_update($_POST['transaction_id'], $_POST['values'])));
    case 'create-transaction':
        die(crpay_json_response(crpay_transactions_create($_POST['amount'], $_POST['cryptocurrency_code'], crpay_post('currency_code'), crpay_post('external_reference'), crpay_post('title'), crpay_post('note', crpay_post('description')), crpay_post('url'), crpay_post('billing', ''), crpay_post('vat'), crpay_post('checkout_id'), crpay_post('user_details'), crpay_post('discount_code'), crpay_post('type', 1)))); // temp rimuovo crpay_post('description')
    case 'cancel-transaction':
        die(crpay_json_response(crpay_transactions_cancel($_POST['transaction'])));
    case 'get-checkouts':
        die(crpay_json_response(crpay_checkout_get(crpay_post('checkout_id', 0))));
    case 'save-checkout':
        die(crpay_json_response(crpay_checkout_save($_POST['checkout'])));
    case 'delete-checkout':
        die(crpay_json_response(crpay_checkout_delete($_POST['checkout_id'])));
    case 'get-fiat-value':
        die(crpay_json_response(crpay_crypto_get_fiat_value($_POST['amount'], $_POST['cryptocurrency_code'], $_POST['currency_code'])));
    case 'cron':
        die(crpay_json_response(crpay_cron()));
    case 'invoice':
        die(crpay_json_response(crpay_transactions_invoice($_POST['transaction_id'])));
    case 'invoice-user':
        die(crpay_json_response(crpay_transactions_invoice_user($_POST['encrypted_transaction_id'], crpay_post('billing_details'))));
    case 'update':
        die(crpay_json_response(crpay_update($_POST['domain'])));
    case 'evc':
        die(crpay_json_response(crpay_ve($_POST['code'], $_POST['domain'], crpay_post('a'))));
    case 'vat':
        die(crpay_json_response(crpay_vat($_POST['amount'], crpay_post('country_code'), crpay_post('currency'), crpay_post('vat_number'))));
    case 'vat-validation':
        die(crpay_json_response(crpay_vat_validation($_POST['vat_number'])));
    case 'email-test':
        die(crpay_json_response(crpay_email_notification('This is a test', 'Lorem ipsum dolor sit amet tempor.')));
    case 'get-tokens':
        require_once(__DIR__ . '/web3.php');
        die(crpay_json_response(crpay_eth_get_contract()));
    case 'payment-link':
        die(crpay_json_response(crpay_payment_link($_POST['transaction_id'])));
    case 'refund':
        die(crpay_json_response(crpay_transactions_refund($_POST['transaction_id'])));
    case 'get-exchange-rates':
        die(crpay_json_response(crpay_exchange_rates($_POST['currency_code'], $_POST['cryptocurrency_code'])));
    case 'get-usd-rates':
        die(crpay_json_response(crpay_usd_rates(crpay_post('currency_code'))));
    case 'exchange-quotation':
        die(crpay_json_response(crpay_exchange_quotation($_POST['send_amount'], $_POST['send_code'], $_POST['get_code'])));
    case 'exchange-is-payment-completed':
        die(crpay_json_response(crpay_exchange_is_payment_completed($_POST['external_reference_base64'])));
    case 'exchange-finalize':
        die(crpay_json_response(crpay_exchange_finalize($_POST['external_reference_base64'], crpay_post('identity'), crpay_post('manual'), crpay_post('user_payment_details'))));
    case 'exchange-finalize-manual':
        die(crpay_json_response(crpay_exchange_finalize_manual($_POST['amount'], $_POST['cryptocurrency_code'], $_POST['currency_code'], $_POST['external_reference'], $_POST['note'], crpay_post('identity'), crpay_post('user_payment_details'))));
    case 'email-verification':
        die(crpay_json_response(crpay_exchange_email_verification($_POST['email'], crpay_post('saved_email'), crpay_post('verification_code'))));
    case 'validate-address':
        die(crpay_json_response(crpay_crypto_validate_address($_POST['address'], $_POST['cryptocurrency_code'])));
    case 'complycube':
        die(crpay_json_response(crpay_complycube($_POST['first_name'], $_POST['last_name'], $_POST['email'])));
    case 'complycube-create-check':
        die(crpay_json_response(crpay_complycube_create_check($_POST['client_id'], $_POST['live_photo_id'], $_POST['document_id'])));
    case 'complycube-check':
        die(crpay_json_response(crpay_complycube_check($_POST['check_id'], $_POST['email'])));
    case 'cloud':
        require_once(__DIR__ . '/cloud/functions.php');
        die(crpay_json_response(crpay_cloud_ajax($_POST['action'], crpay_post('arguments'))));
    case 'get-explorer-link':
        die(crpay_json_response(crpay_crypto_get_explorer_link($_POST['hash'], $_POST['cryptocurrency_code'])));
    case 'get-network-fee':
        die(crpay_json_response(crpay_crypto_get_network_fee($_POST['cryptocurrency_code'], crpay_post('returned_currency_code'))));
    case 'update-license-key-status':
        die(crpay_json_response(crpay_shop_license_key_update_status($_POST['transaction_id'], $_POST['status'])));
    case 'apply-discount':
        die(crpay_json_response(crpay_shop_discounts_apply($_POST['discount_code'], $_POST['checkout_id'], $_POST['amount'])));
    case 'validate-license-key':
        die(crpay_json_response(crpay_shop_license_key_validate($_POST['license_key'])));
    case 'encryption':
        die(crpay_json_response(crpay_encryption($_POST['string'], crpay_post('encrypt', true))));
    case 'get-customer':
        die(crpay_json_response(crpay_customers_get(crpay_post('customer_id'))));
    case 'analytics':
        die(crpay_json_response(crpay_shop_analytics(crpay_post('status', 'C'), crpay_post('currency'), crpay_post('date_range'), crpay_post('checkout_id'))));
    case 'delete-file':
        die(crpay_json_response(crpay_file_delete($_POST['file_name'], crpay_post('folder'))));
    case 'shop-delete-downloads':
        die(crpay_json_response(crpay_shop_downloads_delete($_POST['file_names'])));
    case 'shop-downloads':
        die(crpay_json_response(crpay_shop_downloads($_POST['encrypted_transaction_id'], true)));
    case '2fa':
        die(crpay_json_response(crpay_2fa(crpay_post('code'))));
    default:
        die(crpay_json_response('No function with name "' . $_POST['function'] . '"', false));
}

function crpay_json_response($response, $success = true) {
    return json_encode(['success' => $success, 'response' => $response]);
}

function crpay_post($key, $default = false) {
    return isset($_POST[$key]) ? ($_POST[$key] == 'false' ? false : ($_POST[$key] == 'true' ? true : $_POST[$key])) : $default;
}

function crpay_security_error() {
    $admin_functions = ['delete-file', 'analytics', 'get-customer', 'encryption', 'update-license-key-status', 'refund', 'get-transaction', 'payment-link', 'email-test', 'update-transaction', 'invoice', 'download-transactions', 'get-settings', 'save-settings', 'update', 'get-balances', 'get-transactions', 'get-checkouts', 'save-checkout', 'delete-checkout'];
    $agent_forbidden_functions = ['delete-file', 'save-settings', 'save-checkout', 'delete-checkout'];
    $verify = crpay_verify_admin();
    return in_array($_POST['function'], $admin_functions) && ($verify === false || ($verify === 'agent' && in_array($_POST['function'], $agent_forbidden_functions)));
}

?>