<?php

/**
 * Description of SubscriptionController
 *
 * $Author$
 * $Revision$
 * $Id$
 * $Date$
 */
class SubscriptionController 
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
    }

    public function testProcessAction()
    {
        ini_set('date.timezone', 'Europe/Paris');
		
        $initArray = array();

        try
        {
            $wsdl = null;
            //$env = $this->getRequest()->getParam('env', 'preprod');
			$env = 'preprod';
            if ($env == 'preprod')
            {
            	
                $wsdl = 'https://test-ws.hipay.com/soap/subscription?wsdl';
//                $initArray = array(
//                    'wsLogin' => '40955eca75eb5e744925ee8384960bf6',
//                    'wsPassword' => '7c53e516c4a5852664c7e9c798484380',
//                    'websiteId' => 136,
//                    'categoryId' => 132,
//                    'customerEmail' => 'steeve@hipay.com',
//                );

                /** francolive */
/*                $initArray = array(
                    'wsLogin' => '17d61ae5a983aab3fb5cac7827d37a4e',
                    'wsPassword' => '6b9fe1a110c9796bf0d0cd6553a73375',
                    'websiteId' => 133,
                    'categoryId' => 652,
                    'customerEmail' => 'steeve@hipay.com',
                );
*/                /** Juan Test */
                $initArray = array(
                    'wsLogin' => '4f8c3e80fd4e476f1e227c68fefdb3e5',
                    'wsPassword' => '2f0c4099c4254f819bf7246d6202d192',
                    'websiteId' => 345,
                    'categoryId' => 225,
                    'customerEmail' => 'juanchorene@gmail.com',
                );
            }
            else
            {
            	
                $wsdl = 'http://dev.ws.hipay.com/soap/subscription?wsdl';
                $initArray = array(
                    'wsLogin' => 'c297e50d51b00243dfbf72fa5de2320f',
                    'wsPassword' => 'd61107381868a2784bcec829e764d182',
                    'websiteId' => 314,
                    'categoryId' => 658,
                    'customerEmail' => 'steeve001@hipay.com',
                );
            }


            $options = array(
                'compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP,
                'cache_wsdl' => WSDL_CACHE_NONE,
            );

            $client = new SoapClient($wsdl, $options);
            $result = $client->process($initArray + array(
                    'currency' => 'EUR',
                    'rating' => '+18',
                    'locale' => 'fr_FR',
                    'label' => 'test3.juanrene.com', //'Abonnement web service',
                    'customerIpAddress' => '66.249.92.104', //$_SERVER['REMOTE_ADDR'],
//                    'merchantComment' => 'Payer 5 Euros pour 15 jours d\'essai. A l\'issue de cette période votre abonnement sera automatiquement reconduit pour 35 Euros par mois',
                    'merchantReference' => '5028737', // 'REF-ABO15',
                    'urlCallback' => 'http://sd-20614.dedibox.fr/~jabella/notif/index.php', // 'http://www.example.com/callback',

                    'urlAccept' => 'http://sd-20614.dedibox.fr/~jabella/notif/index.php?page=hipayabook',
                    'urlDecline' => 'http://sd-20614.dedibox.fr/~jabella/notif/index.php?page=hipayaboko',
                    'urlCancel' => 'http://sd-20614.dedibox.fr/~jabella/notif/index.php?page=hipayabocancel',

                    /** some parameters */
/*                    'freeData' => array(
                        'param' => array(
                            array(
                                'key' => 'data1',
                                'value' => 'value1',
                            ),
                            array(
                                'key' => 'data2',
                                'value' => 'value2',
                            ),
                            array(
                                'key' => 'data3',
                                'value' => 'value3',
                            ),
                        ),
                    ),
*/
                    /** one parameter */
/*                    'freeData' => array(
                        'param' => array(
                            'key' => null,
                            'value' => null,
                        ),
                    ),
*/
                    'payments' => array(
                        'initial' => array(
                            'name' => 'test3.juanrene.com',
                            'amount' => 7,
                            'recurrence' => array(
//                                'frequency' => 1,
//                                'duration' => 'month',
                                'frequency' => 0,
                                'duration' => 'managed',
                            ),
                            'description' => 'TestJuanABoWS',
                            //'periodType' => 'normal',
                            //'periodType' => 'trial',
                            'periodType' => 'free',
                            'freePeriod' => array(
                                'length' => 45,
                                'timeUnit' => 'day',
                            ),
//                            'executionTimestamp' => date('Y-m-dTH:i:s', strtotime("+45 days")),
//                        ),
/*                        'rebill' => array(
                            'name' => 'Abonnement classique',
                            'description' => 'Abonnement automatiquement reconduit',
                            'amount' => 19,
                            'executionTimestamp' => date('c', strtotime("+2 months", strtotime(date('c')))),
*/                        ),
                        'repayment' => array(
                            array(
                                'name' => 'Paiement N°2',
                                'description' => 'Paiement',
                                'amount' => 67,
                                'executionTimestamp' => date('Y-m-dTH:i:s', strtotime("+45 days")),
                            ),
                        
                    ),

//                    'payments' => array(
//                        'initial' => array(
//                            'name' => 'Prise d\'abonnement',
//                            'description' => 'Période d\'essai de 15 jours',
//                            'amount' => 5,
//                            'recurrence' => array(
//                                'frequency' => 1,
//                                'duration' => 'month',
//                            ),
//                            'periodType' => 'free',
////                            'manualCapture' => 'yes',
//                            'freePeriod' => array(
//                                'length' => 5,
//                                'timeUnit' => 'minute',
//                            ),
////                            'affectedAffiliates' => array(
////                                array('affiliateName' => 'aff1'),
////                            ),
//                        ),
//                        /** 1 seule échéance spécifique */
//                         'repayment' => array(
//                             'name' => 'Paiement N°2',
//                             'description' => 'Paiement récurrent',
//                             'amount' => 20,
//                             'executionTimestamp' => '2010-10-30T09:00:00+00:00',
//                         ),
//
//                         N échéances spécifiques 
/*                        'repayment' => array(
                            array(
                                'name' => 'Paiement N°2',
                                'description' => 'Paiement récurrent',
                                'amount' => 19,
                                'executionTimestamp' => '2012-12-20T00:00:00+00:00',
                            ),
                            array(
                                'name' => 'Paiement N°3',
                                'description' => 'Paiement récurrent',
                                'amount' => 30,
                                'executionTimestamp' => '2013-01-01T09:00:00+00:00',
                            ),
                            array(
                                'name' => 'Paiement N°4',
                                'description' => 'Paiement récurrent',
                                'amount' => 15,
                                'executionTimestamp' => '2013-02-05T09:00:00+00:00',
                            ),
*/                        ),
//                        'rebill' => array(
//                            'name' => 'Abonnement classique',
//                            'description' => 'Abonnement automatiquement reconduit',
//                            'amount' => 19,
//                            'executionTimestamp' => '2013-01-13T09:00:00+00:00',
//                            'affectedAffiliates' => array(
//                                array('affiliateName' => 'aff1'),
//                                array('affiliateName' => 'aff2'),
//                            ),
//                        ),
//                    ),
//                    'affiliateList' => array(
//                        'affiliate' => array(
//                            array(
//                                'name' => 'aff1',
//                                'hipayAccountId' => 66141,
//                                'hipayAccountId' => 144082, // xxxzzzcontact@terrancle.fr
//                                'amount' => '35',
//                                'unitAmount' => 'percent',
//                                'scope' => 'repayment',
//                            ),
//                            array(
//                                'name' => 'aff2',
//                                'hipayAccountId' => 66141,
//                                'hipayAccountId' => 144082, // xxxzzzcontact@terrancle.fr
//                                'amount' => '5',
//                                'unitAmount' => 'fixed',
//                                'scope' => 'repayment',
//                            ),
//                        ),
//                    ),
                ));

//            echo '<pre>LastRequestHeaders: ' . htmlentities($client->getLastRequestHeaders()) . '</pre>';
//            echo '<pre>LastRequest: ' . htmlentities($client->getLastRequest()) . '</pre>';
//            echo '<pre>LastResponseHeaders: ' . htmlentities($client->getLastResponseHeaders()) . '</pre>';
//            echo '<pre>LastResponse: ' . htmlentities($client->getLastResponse()) . '</pre>';
            echo '<pre>$result: ' . var_export($result, true) . '</pre>';
			var_dump($result);
            if (isset($result->redirectUrl))
            {
                //echo '<a href="' . $result->redirectUrl . '/pay-mode/registered" target="_blank">$result: ' . $result->redirectUrl . '</a>';
                echo '<a href="' . $result->redirectUrl . '" target="_blank">$result: ' . $result->redirectUrl . '</a>';
            }
        }
        catch (SoapFault $sf)
        {
//            echo '<pre>LastRequestHeaders: ' . htmlentities($client->getLastRequestHeaders()) . '</pre>';
//            echo '<pre>LastRequest: ' . htmlentities($client->getLastRequest()) . '</pre>';
//            echo '<pre>LastResponseHeaders: ' . htmlentities($client->getLastResponseHeaders()) . '</pre>';
//            echo '<pre>LastResponse: ' . htmlentities($client->getLastResponse()) . '</pre>';
            echo '<pre>$sf: ' . var_export($sf->__toString(), true) . '</pre>';
        }
        catch (Exception $exc)
        {
            echo '<pre>$exc: ' . var_export($exc->__toString(), true) . '</pre>';
        }
        //error_log(str_repeat("\n", 5), 3, '/tmp/my_log.log');
        exit();
    }

}

?>
