<?php
/**
 * WHMCS Sample Payment Callback File
 *
 * This sample file demonstrates how a payment gateway callback should be
 * handled within WHMCS.
 *
 * It demonstrates verifying that the payment gateway module is active,
 * validating an Invoice ID, checking for the existence of a Transaction ID,
 * Logging the Transaction for debugging and Adding Payment to an Invoice.
 *
 * For more information, please refer to the online documentation.
 *
 * @see https://developers.whmcs.com/payment-gateways/callbacks/
 *
 * @copyright Copyright (c) WHMCS Limited 2017
 * @license http://www.whmcs.com/license/ WHMCS Eula
 */

/*
 * Copyright (c) 2018-2021 Atipay, (https://www.atipay.net)

 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * */

// Require libraries needed for gateway module functions.
require_once __DIR__ . '/../../../init.php';
require_once __DIR__ . '/../../../includes/gatewayfunctions.php';
require_once __DIR__ . '/../../../includes/invoicefunctions.php';



$params = $_POST;

$gatewayModuleName = basename(__FILE__, '.php');
require_once __DIR__ . "/../$gatewayModuleName/fn.atipay.php";

$gatewayParams = getGatewayVariables($gatewayModuleName);

$checkParams = fn_check_callback_data($params);

$invoiceId = $params['reservationNumber'];
$suffix = "paymentfailed=true";

if ($checkParams['success'] == 1){

    $apiKey = $gatewayParams['apiKey'];
    $transactionId = $params['referenceNumber'];


    $currency = $params['siteCurrency'];

    checkCbInvoiceID($invoiceId, $gatewayModuleName);
    checkCbTransID($transactionId);

    $verify_params = array('apiKey'=>$apiKey,
        'referenceNumber'=>$transactionId
    );


    $results = fn_atipay_get_invoice($invoiceId);

    $amount = $results['total'];
    if ($currency == 'T'){
        $amount = intval($amount * 10);
    }else{
        $amount = intval($amount);
    }

    $vr = fn_atipay_verify_payment($verify_params,$amount);
    if ($vr['success'] == 0){ //veriy failed
        $transactionStatus = $vr['errorMessage'];
        $success = false;
    }else{
        $suffix = "paymentsuccess=true";
        $success = true;
        $transactionStatus = "پرداخت موفق با آتی‌پی";
    }
}else{

    if (isset($checkParams['errors']) && !empty($checkParams)){
        $transactionStatus = $checkParams['errors'];
    }else{
        $transactionStatus = 'خطا در اطلاعات ارسالی از سمت درگاه پرداخت';
    }
    $success = false;
}


logTransaction($gatewayModuleName, $params, $transactionStatus);

if ($success) {

    addInvoicePayment(
        $invoiceId,
        $transactionId,
        $paymentAmount,
        $paymentFee,
        $gatewayModuleName
    );

}

$redurect_url = $gatewayParams['systemurl'] . "viewinvoice.php?id=$invoiceId&$suffix";

header("Location: $redurect_url");