modifikasi script dibawah ini agar jika sistem menerima data transaction_amount dan commision_amount dari merchant maka secara otomatis sistem akan mengkonversi nilainya dengan kurs USD ke IDR sesuai kurs real time kemudian tulis ulang script lengkap termasuk yang sudah dimodifikasi dalam format text,                                                                                                                                                                                                   <?php

function cmdcp_postback_api($data)
{
    global $wpdb;
    $wp_prefix = $wpdb->prefix;

    $data['merchant_transaction_id'] = trim($data['merchant_transaction_id']);
    $data['transaction_amount'] = trim($data['transaction_amount']);
    $data['commission_amount'] = trim($data['commission_amount']);
    $data['transaction_status'] = trim($data['transaction_status']);
    $data['click_id'] = trim($data['click_id']);
    $data['transaction_date'] = trim($data['transaction_date']);

    $error = array();
    $data['transaction_date'] = urldecode($data['transaction_date']);
    $params = array('merchant_transaction_id' => $data['merchant_transaction_id'], 'store' => $data['store'], 'transaction_amount' => $data['transaction_amount'], 'transaction_status' => $data['transaction_status'], 'commission_amount' => $data['commission_amount'], 'user' => $data['user'], 'click_id' => $data['click_id'], 'transaction_date' => $data['transaction_date'],'Version' => $data['version']);
    if ($data['version'] == 'v2') {
        $data['token'] = trim($data['token']);
        $params['token'] = $data['token'];
    }
    $req_params = maybe_serialize($params);

    if (!isset($data['click_id']) and (!isset($data['user']) or !isset($data['store']))) {
        $error['Missing parameter(s)'][] = "should include [click_id] or both [store] and [user]";
    } elseif (isset($data['click_id']) and (!is_numeric($data['click_id']) or cmdcp_get_clickdata($data['click_id']) === false)) {
        $error['Invalid parameter(s)'][] = "[click_id], id does not exist";
    } elseif (isset($data['user']) and get_userdata($data['user']) === false) {
        $error['Invalid parameter(s)'][] = "[user], id does not exist";
    } elseif (isset($data['store']) and empty(get_term_by('id', $data['store'], 'stores'))) {
        $error['Invalid parameter(s)'][] = "[store], id does not exist";
    }

    if ($data['version'] == 'v1' && get_option('cmdcp_use_old_version', 'yes') != 'yes') {
        $error['Check Api Version'][] = "Please update Postback to latest version";
    }

    if ($data['version'] == 'v2') {
        $token_name = array_search($data['token'], get_option('cmdcp_postback_token', array()));
        if (!$token_name) {
            $error['Check Token'][] = "[token], Invalid token. Please enter valid token";
        }
    }

    if (!isset($data['merchant_transaction_id']) or empty($data['merchant_transaction_id'])) {
        $error['Missing parameter(s)'][] = "[merchant_transaction_id]";
    }

    if (!is_numeric($data['transaction_amount'])) {
        $error['Invalid parameter(s)'][] = "[transaction_amount], it should be numeric value";
    }

    if (!is_numeric($data['commission_amount'])) {
        $error['Invalid parameter(s)'][] = "[commission_amount], it should be numeric value";
    }

    if (!in_array($data['transaction_status'], array('approved', 'rejected', 'pending'))) {
        $error['Invalid parameter(s)'][] = "[transaction_status], it should be one of these three values {approved, rejected, pending}";
    }

    if (!is_numeric($data['transaction_date']) and !strtotime($data['transaction_date'])) {
        $error['Invalid parameter(s)'][] = "[transaction_date], it should be a valid date format";
    }

    if (empty($error)) {
        if ($data['click_id']) {
            $clickdata = cmdcp_get_clickdata($data['click_id']);
            $data['user']  = $clickdata->user;
            $data['store'] = $clickdata->store;
        }

        $cashback_options = get_option('cashback_options', array('details' => [], 'message' => [], 'value' => [], 'type' => []));
        $type = $cashback_options['type'][$data['store']] ?? '';
        $value = $cashback_options['value'][$data['store']] ?? '';
        $source = $data['version'] == 'v2' ? 'Postback (' . $token_name . ')' : 'Postback (V1)';

        if ($type == 'fixed')                      $cashback_amount = $value;
        elseif ($type == 'percent')                $cashback_amount = $value * $data['transaction_amount'] / 100;
        elseif ($type == 'commission_percent')     $cashback_amount = $value * $data['commission_amount'] / 100;
        else                                       $cashback_amount = 0;

        if (is_numeric($data['transaction_date'])) $transaction_date = get_option('cmdcp_use_wordpress_time') == 'on' ? wp_date('Y-m-d H:i:s', $data['transaction_date']) : date('Y-m-d H:i:s', $data['transaction_date']);
        else                                       $transaction_date = get_option('cmdcp_use_wordpress_time') == 'on' ? wp_date('Y-m-d H:i:s', strtotime($data['transaction_date'])) : date('Y-m-d H:i:s', strtotime($data['transaction_date']));

        $sql = "INSERT INTO `{$wp_prefix}cashback_transactions` (
                                `id`,
                                `merchant_transaction_id`,
                                `user`,
                                `store`,
                                `source`,
                                `transaction_amount`,
                                `commission_amount`,
                                `transaction_status`,
                                `transaction_date`,
                                `cashback_amount`,
                                `comment`
                            ) VALUES (
                                NULL,
                                '{$data['merchant_transaction_id']}',
                                {$data['user']},
                                {$data['store']},
                                '$source',
                                {$data['transaction_amount']},
                                {$data['commission_amount']},
                                '{$data['transaction_status']}',
                                '$transaction_date',
                                $cashback_amount,
                                ''
                            ) ON DUPLICATE KEY UPDATE
                                user = VALUES(user),
                                store = VALUES(store),
                                source = VALUES(source),
                                transaction_amount = VALUES(transaction_amount),
                                commission_amount = VALUES(commission_amount),
                                transaction_status = VALUES(transaction_status),
                                transaction_date = VALUES(transaction_date),
                                cashback_amount = VALUES(cashback_amount)";
        if ($wpdb->query($sql) === false) {
            $error[] = "Error:" . $wpdb->print_error();;
        } else {
            $results = array(
                'id' => $data['merchant_transaction_id'],
                'store' => $data['store'],
                'transaction_amount' => $data['transaction_amount'],
                'transaction_status' => $data['transaction_status'],
                'cashback_amount' => $cashback_amount,
            );
            cmdcp_transactions_mail((object)$results, get_userdata($data['user']));
        }
    }
    $details = maybe_serialize(empty($error) ? array("Valid callback" => "Successfully Updated/Inserted into transactions table") : $error);
    $status = empty($error) ? "success" : "error";
    $current_time = (get_option('cmdcp_use_wordpress_time') == 'on' ? "'" . current_time('mysql') . "'" : "NOW()");
    $sql = "INSERT INTO `{$wp_prefix}cashback_postback_logs` (`id`, `request_parameters`, `response_status`, `details`, `logtime`) VALUES ( NULL,'$req_params','$status', '$details', $current_time) ";
    $wpdb->query($sql);

    header("Content-Type: application/json");
    $response = array('status' => $status);
    if (empty($error)) $response['parameters'] = $params;
    else $response['details'] = $error;

    return new WP_REST_Response(
        $response,
        200,
        array('Cache-Control' => 'no-cache, no-store, must-revalidate', 'Pragma' => 'no-cache', 'Expires' => '0', 'Content-Transfer-Encoding' => 'UTF-8')
    );
}

function cmdcp_postback_page()
{
    //Bootstrap CSS
    wp_register_style('bootstrap.min', get_template_directory_uri() . '/inc/assets/css/bootstrap.min.css');
    wp_enqueue_style('bootstrap.min');
    //Custom CSS
    wp_register_style('cmdcp_css', plugins_url('assets/css/cmdcp.css', __FILE__));
    wp_enqueue_style('cmdcp_css');
    //DataTable CSS
    wp_register_style('datatable_css', plugins_url('assets/extensions/DataTables/datatables.min.css', __FILE__));
    wp_enqueue_style('datatable_css');

    // Display
?>
    <div class="wrap">

        <h1><?= __('Postback', 'clipmydeals-cashback') ?></h1>

        <hr />

        <div class="card p-0 mb-3" style="max-width:100%">
            <div class="card-header"><?= __('Create Postback URL &amp; Pixel', 'clipmydeals-cashback') ?></div>
            <div class="card-body">
                <form>
                    <div class="row">
                        <div class="form-group col-md-3">
                            <label for="click"><strong><?= __('Sub ID Variable', 'clipmydeals-cashback') ?></strong> <small class="text-danger">*</small></label>
                            <input type="text" class="form-control" name="click" id="click" />
                            <small>
                                <?= __("Affiliate Program's Variable which contains the Sub-ID value. This should be the same Sub-ID where you passed [click] shortcode in affiliate links.", 'clipmydeals-cashback') ?>
                            </small>
                        </div>

                        <div class="form-group col-md-3">
                            <label for="merchant_transaction_id">
                                <strong> <?= __('Transaction ID Variable', 'clipmydeals-cashback') ?></strong>
                                <small class="text-danger">*</small>
                            </label>
                            <input type="text" class="form-control" name="merchant_transaction_id" id="merchant_transaction_id" />
                            <small>
                                <?= __("Affiliate Program's Variable which contains a unique transaction ID. This should ideally be transaction ID from the store, or a unique record identifier in your affiliate panel", 'clipmydeals-cashback') ?>
                            </small>
                        </div>

                        <div class="form-group col-md-3">
                            <label for="transaction_amount">
                                <strong><?= __("Transaction Amount Variable", 'clipmydeals-cashback') ?></strong>
                                <small class="text-danger">*</small></label>
                            <input type="text" class="form-control" name="transaction_amount" id="transaction_amount" />
                        </div>

                        <div class="form-group col-md-3">
                            <label for="commission_amount">
                                <strong><?= __("Commission Amount Variable", 'clipmydeals-cashback') ?></strong>
                                <small class="text-danger">*</small></label>
                            <input type="text" class="form-control" name="commission_amount" id="commission_amount" />
                        </div>

                        <div class="form-group col-md-3">
                            <label for="transaction_date"><strong><?= __("Transaction Date Variable", 'clipmydeals-cashback') ?></strong></label>
                            <input type="text" class="form-control" name="transaction_date" id="transaction_date" placeholder="<?= __("Default: Current Date", 'clipmydeals-cashback') ?>" />
                            <small><?= __("Affiliate Program's Variable which gives the Transaction Date in a valid date format or in UNIX Timestamp", 'clipmydeals-cashback') ?></small>
                        </div>

                        <div style="margin-bottom:1.5rem;" id="changecolumn" class="col-md-3">
                            <strong><?= __('Auto-Approve Transactions?', 'clipmydeals-cashback') ?></strong><br />
                            <div class="form-check-inline">
                                <label class="form-check-label">
                                    <input type="radio" class="form-check-input" name="transaction_status" id="transaction_status_approved" value="yes">
                                    <?= __('Yes', 'clipmydeals-cashback') ?>
                                </label>
                            </div>
                            <div class="form-check-inline">
                                <label class="form-check-label">
                                    <input type="radio" class="form-check-input" name="transaction_status" id="transaction_status_pending" value="no" checked>
                                    <?= __('No', 'clipmydeals-cashback') ?>
                                </label>
                            </div>
                        </div>
                        <div class="form-group col-md-3">
                            <label for="postback_version">
                                <strong><?= __("Postback Version", 'clipmydeals-cashback') ?></strong>
                                <small class="text-danger">*</small></label>
                            <select class="form-control" name="postback_version" id="postback_version" onchange="showTokens(this.value)">
                                <option value="v2">v2 (<?= __("recommended", 'clipmydeals-cashback') ?>)</option>
                                <option value="v1">v1</option>
                            </select>
                        </div>
                        <div class="form-group col-md-3" id="token_field">
                            <label for="token_name">
                                <strong><?= __("Token", 'clipmydeals-cashback') ?></strong>
                                <small class="text-danger">*</small></label>
                            <select class="form-control" name="token_name" id="token_name">
                                <?php
                                $tokens = get_option('cmdcp_postback_token', array());
                                foreach ($tokens as $name => $token) {
                                    echo "<option value='" . $token . "'>" . $name . "</option>";
                                }
                                ?>
                            </select>
                            <small><?= __("You can create different tokens for each Affiliate Network/program/campaign to identify which of those generated the postback.", 'clipmydeals-cashback') ?><a href="admin.php?page=postback-settings" target="_blank"><?= __('Add New Token', 'clipmydeals-cashback') ?></a></small>
                        </div>

                    </div>
                </form>
                <div id="error" class="alert alert-danger d-none"><?= __("Error Message", 'clipmydeals-cashback') ?></div>
            </div>
            <div class="card-footer">
                <button class="button button-primary" onclick="generatePostback()"><?= __("Create", 'clipmydeals-cashback') ?></button>
            </div>
        </div>

        <script type="text/javascript">
            function generatePostback() {
                // Values
                var transaction_status = jQuery('input[name="transaction_status"]:checked').val();
                var click = jQuery('#click').val().trim();
                var merchant_transaction_id = jQuery('#merchant_transaction_id').val().trim();
                var transaction_amount = jQuery('#transaction_amount').val().trim();
                var commission_amount = jQuery('#commission_amount').val().trim();
                var transaction_date = jQuery('#transaction_date').val().trim();
                var postback_version = jQuery('#postback_version').val().trim();
                var token_name = jQuery('#token_name').val().trim();
                // Validations
                jQuery('#error').addClass('d-none');
                if (click == '') {
                    jQuery('#error').html('Sub-ID variable is mandatory. Because It will contain the ID of the Click.');
                    jQuery('#error').removeClass('d-none');
                } else if (merchant_transaction_id == '') {
                    jQuery('#error').html('Transaction ID variable is mandatory. You will need this to identify the transaction in case of corrections/disputes.');
                    jQuery('#error').removeClass('d-none');
                } else if (transaction_amount == '') {
                    jQuery('#error').html('Transaction Amount variable is mandatory.');
                    jQuery('#error').removeClass('d-none');
                } else if (commission_amount == '') {
                    jQuery('#error').html('Commission Amount variable is mandatory.');
                    jQuery('#error').removeClass('d-none');
                } else if (postback_version == 'v2' && token_name == '') {
                    jQuery('#error').html('Token variable is mandatory for V2 version.');
                    jQuery('#error').removeClass('d-none');
                }
                // Generate
                var url = `<?= get_bloginfo('url') ?>/wp-json/cashback/${postback_version}/postTransaction/?click_id=${click}&merchant_transaction_id=${merchant_transaction_id}&transaction_amount=${transaction_amount}&commission_amount=${commission_amount}${transaction_status == 'yes' ?'&transaction_status=approved':''}${transaction_date ? `&transaction_date=${transaction_date}` : ''}${postback_version=='v2' ? `&token=${token_name}` : ''}`;
                // Print
                jQuery('#postback-url').html(url);
                jQuery('#postback-pixel').html(`<img width="1px" height="1px" src="${url}" />`);
            }

            function showTokens(value) {
                if (value == 'v2') {
                    document.getElementById('token_field').classList.remove('d-none');
                } else {
                    document.getElementById('token_field').classList.add('d-none');
                }
            }
        </script>

        <div class="row">
            <div class="form-group col-md-6">
                <label><?= __("Postback URL", 'clipmydeals-cashback') ?></label>
                <textarea id="postback-url" name="postback-url" class="form-control" rows="5"></textarea>
            </div>
            <div class="form-group col-md-6">
                <label><?= __("Postback Pixel", 'clipmydeals-cashback') ?></label>
                <textarea id="postback-pixel" name="postback-pixel" class="form-control" rows="5"></textarea>
            </div>
        </div>

    </div>
<?php
}

function cmdcp_register_postback_api()
{
    register_rest_route('cashback/v1', 'postTransaction', array(
        'methods'  => 'GET,POST',
        'callback' => 'cmdcp_postback_api',
        'permission_callback' => '__return_true',
        'args' => array(
            'merchant_transaction_id' => array(),
            'transaction_amount' => array('default' => 0),
            'commission_amount' => array('default' => 0),
            'user' => array(),
            'store' => array(),
            'transaction_status' => array('default' => 'pending'),
            'click_id' => array(),
            'transaction_date' => array('default' => date('Y-m-d H:i:s')),
            'version' => array('default' => 'v1')
        ),
    ));

    register_rest_route('cashback/v2', 'postTransaction', array(
        'methods'  => 'GET,POST',
        'callback' => 'cmdcp_postback_api',
        'permission_callback' => '__return_true',
        'args' => array(
            'merchant_transaction_id' => array(),
            'transaction_amount' => array('default' => 0),
            'commission_amount' => array('default' => 0),
            'user' => array(),
            'store' => array(),
            'transaction_status' => array('default' => 'pending'),
            'click_id' => array(),
            'transaction_date' => array('default' => date('Y-m-d H:i:s')),
            'token' => array(),
            'version' => array('default' => 'v2')
        ),
    ));
}
?>