<commit_before><?php
/**
 * Created by PhpStorm.
 * User: Malgin
 * Date: 24.01.15
 * Time: 3:15
 */

/**
 * Class DWRRobokassaService
 *
 * Use to access robokassa data
 */
class DWRRobokassaService
{
    public function __construct($merchantLogin, $responseLanguage)
    {
        $this->merchantLogin = $merchantLogin;
        $this->responseLanguage = $responseLanguage;
    }

    public function getAvailableCurrencies()
    {
        $curl = curl_init();

        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_URL, DWR_ROBOKASSA_GET_CURRENCIES_URL . '?MerchantLogin=' . $this->merchantLogin . '&Language=' . $this->responseLanguage);

        $result = curl_exec($curl);

        curl_close($curl);

        if (is_object($result) && $result->GetCurrenciesResult->Result->Code != 0) {
            error_log("Error occured while requesting available merchant currencies: " . $result->GetCurrenciesResult->Result->Description);
            return false;
        } else {
            return new \SimpleXMLElement($result);
        }
    }

    public function processResult($invId, $signatureValue)
    {
        global $wpdb;

        if (!$invId OR !$signatureValue) {
            error_log("No $invId or $signatureValue provided.");
            die("No $invId or $signatureValue provided.");
        }

        $table_donations = $wpdb->prefix . DWR_DONATIONS_TABLE_NAME;
        $transaction = $wpdb->get_row($wpdb->prepare("SELECT * FROM `" . $table_donations . "` WHERE `id` = %d", $invId));

        $merchant_pass_two = get_option('dwr_merchant_pass_two');

        if (!$merchant_pass_two) {
            error_log("Merchant Pass #2 is not set in admin panel.");
            die("Internal error");
        }

        $mySignatureValue = strtolower(md5("{$transaction->amount}:$invId:$merchant_pass_two"));
        $signatureValue = strtolower($signatureValue);

        if ($signatureValue === $mySignatureValue) {
            echo "OK$invId";
            // TODO: implement mail delivery if required
            // wp_mail("<EMAIL>", "Domation arrived", "Details");

            // update transaction in the db
            $wpdb->update(
                $table_donations,
                array('accomplished' => 1, 'finish_date' => current_time('mysql', 1)),
                array('id' => $invId),
                array('%d', '%s'),
                array('%d')
            );<commit_msg>Add check for existtance of transation before processing it<commit_after><?php
/**
 * Created by PhpStorm.
 * User: Malgin
 * Date: 24.01.15
 * Time: 3:15
 */

/**
 * Class DWRRobokassaService
 *
 * Use to access robokassa data
 */
class DWRRobokassaService
{
    public function __construct($merchantLogin, $responseLanguage)
    {
        $this->merchantLogin = $merchantLogin;
        $this->responseLanguage = $responseLanguage;
    }

    public function getAvailableCurrencies()
    {
        $curl = curl_init();

        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_URL, DWR_ROBOKASSA_GET_CURRENCIES_URL . '?MerchantLogin=' . $this->merchantLogin . '&Language=' . $this->responseLanguage);

        $result = curl_exec($curl);

        curl_close($curl);

        if (is_object($result) && $result->GetCurrenciesResult->Result->Code != 0) {
            error_log("Error occured while requesting available merchant currencies: " . $result->GetCurrenciesResult->Result->Description);
            return false;
        } else {
            return new \SimpleXMLElement($result);
        }
    }

    public function processResult($invId, $signatureValue)
    {
        global $wpdb;

        if (!$invId OR !$signatureValue) {
            error_log("No $invId or $signatureValue provided.");
            die("No $invId or $signatureValue provided.");
        }

        $table_donations = $wpdb->prefix . DWR_DONATIONS_TABLE_NAME;
        $transaction = $wpdb->get_row($wpdb->prepare("SELECT * FROM `" . $table_donations . "` WHERE `id` = %d", $invId));

        if ($transaction === null) {
            error_log("Transaction with id $invId was not found in database");
            die("Internal error");
        }

        $merchant_pass_two = get_option('dwr_merchant_pass_two');

        if (!$merchant_pass_two) {
            error_log("Merchant Pass #2 is not set in admin panel.");
            die("Internal error");
        }

        $mySignatureValue = strtolower(md5("{$transaction->amount}:$invId:$merchant_pass_two"));
        $signatureValue = strtolower($signatureValue);

        if ($signatureValue === $mySignatureValue) {
            echo "OK$invId";
            // TODO: implement mail delivery if required
            // wp_mail("<EMAIL>", "Domation arrived", "Details");

            // update transaction in the db
            $wpdb->update(
                $table_donations,
                array('accomplished' => 1, 'finish_date' => current_time('mysql', 1)),
                array('id' => $invId),
                array('%d', '%s'),
                array('%d')
            );
{'fraction_non_alphanumeric': 0.13546991742036965, 'fraction_numerical': 0.005505308690523005, 'mean_word_length': 2.4628999319264806, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5915254', 'n_tokens_mistral': 1548, 'n_tokens_neox': 1454, 'n_words': 312}