Sammy30
10/9/2019 - 6:08 PM

class-e2pay-gateway.php Remove Logo

ethis.co.id / httpdocs / wp-content / plugins / e2pay-for-woocommerce / includes / class-e2pay-gateway.php

<?php

/*
 * E2pay Gateway for WooCommerce
 *
 * Description: Official E2pay module for WordPress WooCommerce.
 * Plugin URI: https://www.e2pay.com/
 * Author: E2pay
 * License: The MIT License (MIT)
 *
 */
class E2pay_Gateway extends E2pay_Abstract {

    public $id = 'e2pay';
    public $has_fields = true;
    protected $e2pay_payment_method_selected;

    public function __construct() {
        parent::__construct();

       /* 
       $this->icon = E2P_PLUGIN_URL . '/assets/images/e2pay/e2paylogo.png';
       */
        $this->method_title = __('E2pay', E2P_TEXT_DOMAIN);
        $this->method_description = __('Enables the e2pay-php Payment Solution. The easiest way to monetize your game or web service globally.', E2P_TEXT_DOMAIN);
        $this->title = isset($this->settings['title']) ? $this->settings['title'] : "e2Pay Payment Gateway Solution";
        $this->notify_url = add_query_arg('wc-api', 'E2pay_Gateway', home_url('/'));

        // Our Actions
        add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options'));
        add_action('woocommerce_receipt_' . $this->id, array($this, 'receipt_page'));
        add_action('woocommerce_api_' . $this->id . '_gateway', array($this, 'ipn_response'));

    }

    /**
     * Initial E2pay Configs
     */
    function init_configs($isPingback = false) {
        E2pay_Config::getInstance()->set(array(
            'merchant_code' => $this->settings['merchant_code'],
            'merchant_key' => $this->settings['merchant_key']
        ));
    }

    function getConfigOptionByKey($key) {
        return $this->settings[$key];
    }

    /**
     * @param $order_id
     */
    function receipt_page($order_id) {
        $this->init_configs();
        echo $redirect_form = $this->preparePayment($order_id);
    }

    function dd($data) {
        echo "<pre>";
        print_r($data);
        echo "</pre>";
    }

    function preparePayment($order_id) {
        $isTest = $this->getConfigOptionByKey('test_mode');
        $merchantCode = $this->getConfigOptionByKey('merchant_code');
        $merchantKey = $this->getConfigOptionByKey('merchant_key');
        $refNo = $order_id;

        $paymentId = $_GET['e2pay_payment_method_selected'];

        $getwayUrl = $isTest ? E2pay_Indonesia::URL_PAYMENT_SANDBOX : E2pay_Indonesia::URL_PAYMENT_PRODUCTION;

        $order = wc_get_order( $order_id );
        $amount = $order->get_total();
        $hashAmount = str_replace(".", "", str_replace(",", "", $amount));

        $currency = $order->get_data()['currency'];

        $str = $merchantKey  . $merchantCode . $refNo . $hashAmount . $currency;

        $signature = new E2pay_Signature($str);
        $requestSignature = $signature->getSignature();

        $userInfo = $this->get_billing_address($order);

        $backendUrl = $this->notify_url;
        $responseUrl  = $this->notify_url . '&responseURL=true';

        $data = array(
            'MerchantCode'  => $merchantCode,
            'PaymentId'     => $paymentId,
            'RefNo'         => $refNo,
            'Amount'        => $hashAmount,
            'Currency'      => $currency,
            'ProdDesc'      => 'Order #'. $order_id,
            'UserName'      => $userInfo['billing_first_name'] . ' ' . $userInfo['billing_last_name'],
            'UserEmail'     => $userInfo['billing_email'],
            'UserContact'   => $userInfo['billing_phone'],
            'Lang'          => 'UTF-8',
            'Signature'     => $requestSignature,
            'ResponseURL'   => $responseUrl,
            'BackendURL'    => $backendUrl,
        );

        $wiget = new E2pay_Wiget($data);
        $wiget->setAction($getwayUrl);

        return $wiget->generateRedirectForm();
    }


    /**
     * Process the order after payment is made
     * @param int $order_id
     * @return array
     */
    function process_payment($order_id) {
        $order = wc_get_order($order_id);

        $payment_method_selected = $_POST['e2pay_bank_payment_method'];

        $_SESSION['posted_payment_type'] = $_POST['e2pay_bank_payment_method'];

        return array(
            'result' => 'success',
            'redirect' => add_query_arg(
                'key',
                $order->order_key,
                add_query_arg(
                    array(
                        'order-pay' => !method_exists($order, 'get_id') ? $order->id : $order->get_id(),
                        'e2pay_payment_method_selected' => $payment_method_selected,
                    ),
                    $order->get_checkout_payment_url(true)
                )
            )
        );
    }

    /**
     * Check the response from E2pay's Servers
     */
    function ipn_response() {
        global $woocommerce;

        $order = wc_get_order($refNo = $_POST['RefNo']);

        if (!$order) {
            die('The order is Invalid!');
        }

        $merchantKey = $this->getConfigOptionByKey('merchant_key');
        $transaction = new E2pay_Transaction($_POST);
        $transaction->setMerchantKey($merchantKey);
        $transaction->setSignature($_POST['Signature']);

        if($transaction->isPaid()) {
            $order->add_order_note(__('The Order has been Paided by e2Pay Payment Gateway - Transaction Id: ' . $_POST['TransId'], E2P_TEXT_DOMAIN));
            $order->payment_complete();

            $woocommerce->cart->empty_cart();

            if(isset($_REQUEST['responseURL']) && $_REQUEST['responseURL'] == 'true') {
                status_header(200);
                wp_redirect( $this->get_return_url( $order ) );

                // Return thank you page redirect
                return array(
                    'result'   => 'success',
                    'redirect' => $this->get_return_url( $order ),
                );
            } else {
                die('RECEIVEOK');
            }
        } elseif ($transaction->isWaitingPayment()) {
            $order->add_order_note(__('The Order is waiting payment via e2Pay Payment Gateway - Transaction Id: ' . $_POST['TransId'], E2P_TEXT_DOMAIN));
            $order->update_status('on-hold');
            wp_redirect( $this->get_return_url( $order ) );
        } else {
            $order->cancel_order(__('Reason: ' . $_POST['ErrDesc'], E2P_TEXT_DOMAIN));
            return wp_redirect(WC()->cart->get_checkout_url());
        }
    }
}