<?php

class Am_Paysystem_Premiumwebcart extends Am_Paysystem_Abstract{
    const PLUGIN_STATUS = self::STATUS_BETA;
    const PLUGIN_REVISION = '4.2.14.3b7ee0a';

    protected $defaultTitle = 'Premium Web Cart';
    protected $defaultDescription = 'All major credit cards accepted';
    
    
    const URL = "http://www.secureinfossl.com/";
    const API_URL = "https://www.secureinfossl.com/api";
    const PROFILE_ID = 'premiumwebcart_profile_id';
    
    public function _initSetupForm(Am_Form_Setup $form)
    {
        $form->addText('merchant_id')->setLabel(array('Merchant ID', 'Your PremiumWebCart Merchant ID'));
        $form->addText('api_signature', array('size' => 60))->setLabel(array('API Sinature', 'You can get it from Home >> Cart Settings >> Advance Integration >> API Integration'));
    }
    
    public function init()
    {
        parent::init();
        $this->getDi()->billingPlanTable->customFields()
            ->add(new Am_CustomFieldText('premiumwebcart_id', "Premium Web Cart  Product Link ID", 
            "This is the Product  Link ID which is available in the Edit Product Screen."));
    }
    
    
    public function _process(Invoice $invoice, Am_Request $request, Am_Paysystem_Result $result)
    {
        $a = new Am_Paysystem_Action_Redirect(self::URL);
        $a->con         =   'my_cart';
        $a->met         =   'addToCart';
        $a->pid         =   $invoice->getItem(0)->getBillingPlanData('premiumwebcart_id');
        $a->pquantity   =   1;
        $a->clearcart   =   1;
        $a->action      =   2;
        $a->fname       =   $invoice->getFirstName();
        $a->lname       =   $invoice->getLastName();
        $a->email       =   $invoice->getEmail();
        $a->baddress1   =   $invoice->getStreet();
        $a->bcity       =   $invoice->getCity();
        $a->bzip        =   $invoice->getZip();
        $a->bstate      =   $invoice->getState();
        $a->bcountry    =   $invoice->getCountry();
        $a->custom1     =   $invoice->public_id;
        $result->setAction($a);
    }
    public function createTransaction(Am_Request $request, Zend_Controller_Response_Http $response, array $invokeArgs)
    {
        return new Am_Paysystem_Transaction_Premiumwebcart($this, $request, $response, $invokeArgs);
        
    }
    public function getRecurringType()
    {
        return self::REPORTS_REBILL;
    }
    public function apiRequest($method, $vars)
    {
        $req = new Am_HttpRequest(self::API_URL."/".$method.".html", Am_HttpRequest::METHOD_POST);
        $req->addPostParameter('merchantid', $this->getConfig('merchant_id'));
        $req->addPostParameter('signature', $this->getConfig('api_signature'));
        foreach($vars as $k=>$v){
            $req->addPostParameter($k, $v);
        }
        $req->send();
        $resp = $req->getBody();
        if(!$resp)
            throw new Am_Exception_InputError('PWC: got empty response from API server');
        $xml = simplexml_load_string($resp);
        if($xml->error)
            throw new Am_Exception_InputError('PWC: Got error from API: '.$xml->error->errortext);
        
        return $xml;
    }
    public function cancelAction(Invoice $invoice, $actionName, Am_Paysystem_Result $result)
    {
        $id = $invoice->data()->get(self::PROFILE_ID);
        if (!$id)
            throw new Am_Exception_InputError("No external id recorded for invoice [".$invoice->public_id."]");
        $resp = $this->apiRequest('suspendSubscription', array('profileid'=>$id));
        if($resp->recurring->status == 'suspended')
            echo "Order Cancelled";
        else throw new Am_Exception_InputError("PWC: Unknown response from API");
    }

    public function getReadme(){
        $rootURL = $this->getDi()->config->get('root_url');
        return <<<CUT
<b>PremiumWebCart payment plugin configuration</b>
        
1. Configure "premiumwebcart" payment plugin at aMember CP -> Setup/Configuration -> PremiumWebCart
   Make sure you set the same API Key in aMember CP and PWC
   Home >> Cart Settings >> Advance Integration >> API Integration
   
2. Create equivalents for all aMember products in  PWC .
   Make sure it has the same subscription terms (period, price) as aMember
   Products. Set "Thanks URL" for all PWC  products to 
   $rootURL/thanks
   Write down Product Link ID of all PWC products. 
   
3. Visit aMember CP -> Manage Products, click "Edit" on each product
   and enter "PremiumWebCart Product Link ID" for each corresponding billing plan, 
   then click "Save".
        
4. Set Premium Web Cart Instant Payment Notification URL to $rootURL/payment/premiumwebcart/ipn  
   at Home >> Cart Settings >> Advance Integration >> PWC IPN
   
5. Try your integration - go to aMember signup page, and try to make new signup.

CUT;
    }


    
}

class Am_Paysystem_Transaction_Premiumwebcart extends Am_Paysystem_Transaction_Incoming
{
    const STATUS_SUCCESS ='success';
    function __construct(Am_Paysystem_Abstract $plugin, Am_Request $request, Zend_Controller_Response_Http $response, $invokeArgs)
    {
        parent::__construct($plugin, $request, $response, $invokeArgs);
    }

    public function getUniqId()
    {
        return $this->request->get("order_unique_id");
    }

    public function findInvoiceId()
    {
        return $this->request->get('custom1');
    }
    public function validateSource()
    {
        $this->_checkIp('72.32.221.227');
        return true;
    }

    public function validateStatus()
    {
        return ($this->request->get('txn_status') == self::STATUS_SUCCESS);
    }

    public function validateTerms()
    {
        return true;
    }
    
    public function processValidated()
    {
        $this->invoice->addPayment($this);
        if($profile_id = $this->request->get('profile_id'))
            $this->invoice->data()->set(Am_Paysystem_PremiumWebCart::PROFILE_ID, $profile_id)->update();
    }
    
    
    
}
?>
