This repository
This repository
All repositories
branch: master
Switch branches/tags
Nothing to show
Nothing to show
file 329 lines (294 sloc) 11.536 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
<?php
/**
* CreditCardFreezer_AuthorizeDotNet
*
* Extends CreditCardFreezer to add numerous convenience
* routines for creating, updating and deleting credit card
* charges for the Authorize.Net payment gateway using their
* API.
*
* Copyright (c) 2010, Andrew Kandels <me@andrewkandels.com>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @category Encryption
* @package CreditCardFreezer
* @author Andrew Kandels <me@andrewkandels.com>
* @copyright 2010 Andrew Kandels <me@andrewkandels.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://andrewkandels.com/CreditCardFreezer
* @access public
*/


require_once dirname(__FILE__) . '/../CreditCardFreezer.php';

class CreditCardFreezer_AuthorizeDotNet extends CreditCardFreezer
{
    /* Additional Attributes */
    const INVOICE = 30;
    const DESCRIPTION = 31;
    const COMPANY = 32;
    const EMAIL = 33;
    const CUSTOMER_ID = 34;
    const CUSTOMER_IP = 35;

    /* Authorize.NET Configuration */
    const X_VERSION = '3.1';
    const X_DELIM_DATA = 'TRUE';
    const X_DELIM_CHAR = '|';
    const X_RELAY_RESPONSE = 'FALSE';
    const X_METHOD = 'CC';
    const POST_URL = '';

    private $_loginId = null;
    private $_transactionKey = null;
    private $_postUrl = null;

    /**
* Creates a CreditCardFreezer_AuthorizeDotNet object which stores
* credit card and billing information and can make charges through
* the Authorize.Net payment gateway.
*
* @param string Authorize.NET API Login ID
* @param string Authorize.NET API Transaction Key
* @param string Authorize.NET API URL (sandbox, production, etc.)
*/
    public function __construct($loginId, $transactionKey, $postUrl = null)
    {
        parent::__construct();

        if (!function_exists(curl_init)) {
            throw new CreditCardFreezer_Exception(
                'php-curl extension does not exist. This extension is required '
                . 'by CreditCardFreezer_AuthorizeDotNet.'
            );
        }

        $this->_loginId = $loginId;
        $this->_transactionKey = $transactionKey;
        $this->_postUrl = $postUrl;

        if (!$this->_postUrl) {
            $this->_postUrl = self::POST_URL;
        }
    }

    /**
* Default text labels to describe the class constants.
* @return array
*/
    protected function _getTextLabels()
    {
        return array_merge(parent::_getTextLabels(), array(
            self::INVOICE => 'invoice',
            self::DESCRIPTION => 'description',
            self::COMPANY => 'company',
            self::EMAIL => 'email',
            self::CUSTOMER_ID => 'customer_id',
            self::CUSTOMER_IP => 'customer_ip'
        ));
    }

    /**
* Processes a one-time charge through the Authorize.NET
* payment gateway.
*
* @param float Amount to charge
* @return integer Transaction ID
* @throws CreditCardException
*/
    public function charge($amount)
    {
        if ($amount < 1) {
            throw new CreditCardFreezer_Exception(
                'Charge amount should be more than one dollar.'
            );
        }

        $params = $this->_getParams(array(
            'x_amount' => $amount,
            'x_recurring_billing' => 'FALSE',
            'x_type' => 'AUTH_CAPTURE'
        ));

        $response = $this->_sendRequest($params);

        return $response['transactionId'];
    }

    /**
* Adds an Authorize.net recurring subscription, which is a repeating charge
* every X number of months until deleted or the customer cancels.
*
* @param float Amount to charge
* @param integer Monthly increment (1 = charge once per month)
* @param timestamp Start date (date of first charge, must be in future,
* defaults to tomorrow)
* @return integer Transaction ID
* @throws CreditCardException
*/
    public function addRecurring($amount, $months = 1, $ts = null)
    {
        if ($amount < 1) {
            throw new CreditCardFreezer_Exception(
                'Charge amount should be more than one dollar.'
            );
        }

        $params = $this->_getParams(array(
            'x_amount' => $amount,
            'x_recurring_billing' => 'TRUE',
            'x_type' => 'AUTH_CAPTURE'
        ));

        $response = $this->_sendRequest($params);

        return $response['transactionId'];
    }


    /**
* Translates an array of name/value pairs into a urlencoded
* string for use in an HTTP POST or GET request.
*
* @param array Name/value pairs
* @return string URL string
*/
    private function _getPostUrlString(array $params)
    {
        $postData = array();
        foreach ($params as $name => $value) {
            $postData[] = sprintf('%s=%s',
                $name,
                urlencode($value)
            );
        }

        return implode('&', $postData);
    }

    /**
* Submits a request to the Authorize.net API.
*
* @param mixed (string) or (array) post data
* @return integer Transaction ID #
* @throws CreditCardFreezer_Exception
*/
    private function _sendRequest($postData)
    {
        if (is_array($postData)) {
            $postData = $this->_getPostUrlString($postData);
        }

        $request = curl_init($this->_postUrl);
        curl_setopt($request, CURLOPT_HEADER, 0);
        curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($request, CURLOPT_POSTFIELDS, $postData);
        curl_setopt($request, CURLOPT_SSL_VERIFYPEER, FALSE);
        $response = curl_exec($request);
        curl_close ($request);

        if (count($response) < 7) {
            throw new CreditCardException(
                'Authorize.net API request failed. Invalid and unexpected response.'
            );
        }

        list(
            $code,
            $subCode,
            $errorCode,
            $errorText,
            $authCode,
            $avs,
            $transactionId
        ) = explode('|', $response);

        if ($code == 1) {
            return array(
                'code' => $code,
                'subCode' => $subCode,
                'authCode' => $authCode,
                'avs' => $avs,
                'transactionId' => $transactionId
            );
        } else {
            throw new CreditCardException(
                "Authorize.net transaction failed with code $errorCode: $errorText."
            );
        }
    }

    /**
* If you forget to fill in some of the Authorize.net
* required attributes, make some educated guesses. If no
* guess can be made and it's still blank, an exception will
* be thrown.
* @return void
* @throws CreditCardFreezer_Exception
*/
    private function _checkAndFillRequiredAttributes()
    {
        $defaultValues = array(
            self::DESCRIPTION => 'Charge',
            self::INVOICE => '1',
            self::CUSTOMER_IP => $_SERVER['REMOTE_ADDR']
        );

        $requiredAttrs = array();

        foreach ($defaultValues as $attr => $value) {
            if (!$this->get($attr)) {
                $this->set($attr, $value);
            }
        }

        foreach ($requiredAttrs as $attr) {
            if (!$this->get($attr)) {
                throw new CreditCardFreezer_Exception(
                    $attr . ' is a required attribute for an Authorize.net '
                    . 'transaction.'
                );
            }
        }
    }

    /**
* Returns a name/value pair of API parameters that are used
* with all Authorize.net transactions.
*
* @param array Name/value pairs to append/replace
* @return array Merged Name/value pairs
* @throws CreditCardFreezer_Exception
*/
    private function _getParams(array $myParams = array())
    {
        $this->_checkAndFillRequiredAttributes();

        $params = array(
            'x_login' => $this->_loginId,
            'x_tran_key' => $this->_transactionKey,
            'x_version' => self::X_VERSION,
            'x_delim_data' => self::X_DELIM_DATA,
            'x_delim_char' => self::X_DELIM_CHAR,
            'x_relay_response' => self::X_RELAY_RESPONSE,
            'x_method' => self::X_METHOD,
            'x_card_num' => $this->get(self::NUMBER),
            'x_exp_date' => sprintf('%02d/%04d',
                                            $this->get(self::EXPIRE_MONTH),
                                            $this->get(self::EXPIRE_YEAR)
                                       ),
            'x_invoice_num' => $this->get(self::INVOICE),
            'x_description' => $this->get(self::DESCRIPTION),
            'x_first_name' => $this->get(self::FIRST_NAME),
            'x_last_name' => $this->get(self::LAST_NAME),
            'x_company' => $this->get(self::COMPANY),
            'x_address' => $this->get(self::ADDRESS),
            'x_city' => $this->get(self::CITY),
            'x_state' => $this->get(self::STATE),
            'x_zip' => $this->get(self::POSTAL_CODE),
            'x_country' => $this->get(self::COUNTRY),
            'x_phone' => $this->get(self::PHONE),
            'x_email' => $this->get(self::EMAIL),
            'x_cust_id' => $this->get(self::CUSTOMER_ID),
            'x_customer_ip' => $this->get(self::CUSTOMER_IP)
        );

        foreach ($myParams as $name => $value) {
            $params[$name] = $value;
        }

        return $params;
    }
}
Something went wrong with that request. Please try again.