<?php
/**
 * WHMCS Sample Payment Gateway Module
 *
 * Payment Gateway modules allow you to integrate payment solutions with the
 * WHMCS platform.
 *
 * This sample file demonstrates how a payment gateway module for WHMCS should
 * be structured and all supported functionality it can contain.
 *
 * Within the module itself, all functions must be prefixed with the module
 * filename, followed by an underscore, and then the function name. For this
 * example file, the filename is "gatewaymodule" and therefore all functions
 * begin "atipay_".
 *
 * If your module or third party API does not support a given function, you
 * should not define that function within your module. Only the _config
 * function is required.
 *
 * For more information, please refer to the online documentation.
 *
 * @see https://developers.whmcs.com/payment-gateways/
 *
 * @copyright Copyright (c) WHMCS Limited 2017
 * @license http://www.whmcs.com/license/ WHMCS Eula
 */

if (!defined("WHMCS")) {
    die("This file cannot be accessed directly");
}
/*
 * 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_once 'atipay/fn.atipay.php';
/**
 * Define module related meta data.
 *atipay_
 * Values returned here are used to determine module related capabilities and
 * settings.
 *
 * @see https://developers.whmcs.com/payment-gateways/meta-data-params/
 *
 * @return array
 */
function atipay_MetaData()
{
    return array(
        'DisplayName' => 'Atipay Plugin',
        'APIVersion' => '1.0', // Use API Version 1.1
        'DisableLocalCreditCardInput' => true,
        'TokenisedStorage' => false,
    );
}

/**
 * Define gateway configuration options.
 *
 * The fields you define here determine the configuration options that are
 * presented to administrator users when activating and configuring your
 * payment gateway module for use.
 *
 * Supported field types include:
 * * text
 * * password
 * * yesno
 * * dropdown
 * * radio
 * * textarea
 *
 * Examples of each field type and their possible configuration parameters are
 * provided in the sample function below.
 *
 * @return array
 */
function atipay_config()
{
    return array(
        'FriendlyName' => array(
            'Type' => 'System',
            'Value' => 'شبکه همراه آتی‌پی',
        ),
        'name' => array(
            'Type' => 'System',
            'Value' => 'شبکه همراه آتی‌پی',
        ),
        // a text field type allows for single line text input
        'apiKey' => array(
            'FriendlyName' => 'Api Key',
            'Type' => 'text',
            'Size' => '32',
            'Default' => '',
            'Description' => 'از طریق ایمیل برای شما ارسال شده است.',
        ),
        'siteCurrency' => array(
            'FriendlyName' => 'نوع ارز در سایت',
            'Type' => 'dropdown',
            'Options' => array(
                'R' => 'ریال',
                'T' => 'تومان',
            ),
            'Description' => 'قیمت ها در سایت شما ریال است یا تومان',
        ),
        'sendMobile' => array(
            'FriendlyName' => 'ارسال موبایل به درگاه',
            'Type' => 'yesno',
            'Description' => 'در صورت ارسال شماره موبایل به درگاه مشتری لیست کارت‌های خویش رو مشاهده می‌کند',
        ),
        'mobileFieldName' => array(
            'FriendlyName' => 'نام فیلد موبایل',
            'Type' => 'text',
            'Size' => '32',
            'Default' => '',
            'Description' => 'در صورت خالی بودن از فیلد phone استفاده می شود.',
        ),


    );
}

/**
 * Payment link.
 *
 * Required by third party payment gateway modules only.
 *
 * Defines the HTML output displayed on an invoice. Typically consists of an
 * HTML form that will take the user to the payment gateway endpoint.
 *
 * @param array $params Payment Gateway Module Parameters
 *
 * @see https://developers.whmcs.com/payment-gateways/third-party-gateway/
 *
 * @return string
 */


function atipay_link($params)
{

    $sendMobile = $params['sendMobile'];
    $mobileField = $params['mobileFieldName'];

    // Invoice Parameters
    $invoiceId = $params['invoiceid'];
    $description = $params["description"];
    $phone = '';
    if ($sendMobile == 'on'){
        if (isset($mobileField) && !empty($mobileField)){
            $phone = $params['clientdetails'][$mobileField];
        }else{
            $phone = $params['clientdetails']['phonenumber'];
        }

        $phone_validate = false;
        if (strlen($phone) == 10 && substr($phone, 0, 1) == "9"){
            $phone_validate = true;
        }else if (strlen($phone) == 11 && substr($phone, 0, 2) == "09"){
            $phone_validate = true;
        }else if (strlen($phone) == 12 && substr($phone, 0, 3) == "989"){
            $phone_validate = true;
        }else{
           $phone_validate = false;
        }
    }
    $systemUrl = $params['systemurl'];
    $langPayNow = $params['langpaynow'];
    $moduleName = $params['paymentmethod'];

    $token_params = array(
        'redirectUrl'=>$systemUrl,
        'invoiceNumber'=>$invoiceId,
        'description'=>$description,
        'cellNumber'=>$phone,
    );

    if (!isset($phone) || empty($phone) || $phone == '' || $phone_validate == false){
        unset($token_params['cellNumber']);
    }

    $action = "$systemUrl/modules/gateways/$moduleName/$moduleName.php";
    $htmlOutput = fn_atipay_get_token_form($token_params, $langPayNow, $action);


    return $htmlOutput;
}
