<?php

class Am_Paysystem_Eway extends Am_Paysystem_CreditCard
{
    const PLUGIN_STATUS = self::STATUS_BETA;
    const PLUGIN_DATE = '$Date$';
    const PLUGIN_REVISION = '4.2.14.3b7ee0a';

    const GATEWAY_URL = 'https://www.eway.com.au/gateway_cvn/xmlpayment.asp';
    const GATEWAY_URL_TEST = "https://www.eway.com.au/gateway_cvn/xmltest/testpage.asp";

    protected $defaultTitle = "Pay with your Credit Card";
    protected $defaultDescription = "accepts all major credit cards";

    public function storesCcInfo()
    {
        return false;
    }

    public function getRecurringType()
    {
        return self::REPORTS_CRONREBILL;
    }

    public function isConfigured()
    {
        return strlen($this->getConfig('customer_id'));
    }

    public function _initSetupForm(Am_Form_Setup $form)
    {
        $form->addText("customer_id")->setLabel(array('eWAY customer ID',
            'Your unique eWAY customer ID assigned to you when you join eWAY. eg 87654321'));

        $form->addAdvCheckbox("testing")->setLabel("Test Mode Enabled");
    }

    public function getGateway()
    {
        return $this->getConfig('testing') ? self::GATEWAY_URL_TEST : self::GATEWAY_URL;
    }

    public function _doBill(Invoice $invoice, $doFirst, CcRecord $cc, Am_Paysystem_Result $result)
    {
        $xml = new SimpleXMLElement('<ewaygateway></ewaygateway>');
        $xml->ewayCustomerID = $this->getConfig('customer_id');
        $xml->ewayTotalAmount = $invoice->first_total * 100;
        $xml->ewayCustomerFirstName = $cc->cc_name_f;
        $xml->ewayCustomerLastName = $cc->cc_name_l;
        $xml->ewayCustomerEmail = $invoice->getUser()->email;
        $xml->ewayCustomerAddress = $cc->cc_street;
        $xml->ewayCustomerPostcode = $cc->cc_zip;
        $xml->ewayCustomerInvoiceDescription = $invoice->getLineDescription();
        $xml->ewayCustomerInvoiceRef = $invoice->public_id;
        $xml->ewayCardHoldersName = sprintf('%s %s', $cc->cc_name_f, $cc->cc_name_l);
        $xml->ewayCardNumber = $cc->cc_number;
        $xml->ewayCardExpiryMonth = $cc->getExpire('%1$02d');
        $xml->ewayCardExpiryYear = $cc->getExpire('%2$02d');
        ;
        $xml->ewayTrxnNumber = $invoice->public_id;
        $xml->ewayOption1 = '';
        $xml->ewayOption2 = '';
        $xml->ewayOption3 = '';
        $xml->ewayCVN = $cc->getCvv();

        $request = new Am_HttpRequest($this->getGateway(), Am_HttpRequest::METHOD_POST);
        $request->setBody($xml->asXML());
        $request->setHeader('Content-type', 'text/xml');

        $tr = new Am_Paysystem_Transaction_CreditCard_Eway($this, $invoice, $request, $doFirst);
        $tr->run($result);
    }

}

class Am_Paysystem_Transaction_CreditCard_Eway extends Am_Paysystem_Transaction_CreditCard
{

    public function validate()
    {

        $xml = $this->vars;
        if ($xml->ewayTrxnStatus != 'True')
        {
            $this->result->setFailed($xml->ewayTrxnError);
            return;
        }
        $this->result->setSuccess();
    }

    public function parseResponse()
    {
        $this->vars = new SimpleXMLElement($this->response->getBody());
        return $this->vars;
    }

    public function getUniqId()
    {
        return $this->vars->ewayTrxnNumber;
    }

}