RE: Debug HTTPS Curl Request

Daniel Cole <[email protected]>
Newsgroups gmane.comp.web.curl.php
Message-ID <[email protected]>
Hi Shiplu,

I have tried to replicate the process using the command line version of cURL but I’m afraid I’m a bit out of my depth here. I have attached a copy of the PHP script that is run (note: this is invoked from a separate script). As you will be able to see it deals with cookies and logging in.

If you could please provide some pointers on how to perform this action from the command line would be greatly appreciated.

Thanks,
Daniel

_______________________________________________
http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-php
script.txt (text/plain, 13.5 KB)
<?php
class tp_mods_CreditCardGateway
{
        /**
         * Property: COOKIE
         * -------------------------------------------------------------------------
         * This is the file path to the cookie that is saved.
         * Every new instance of this class will get a new cookie.
         *
         * @access private
         * @type string
         */
        private $COOKIE = '';

        /**
         * Method: Constructor
         * -------------------------------------------------------------------------
         * This is where we will set the name of the new cookie
         *
         * @access public
         * @return void
         */
        public function __construct()
        {
                // NOTE: CookieJar must use relative paths or is wont work.
                $this->COOKIE = '../data/'.time().'.tmp';
        }

        /**
         * Method: login
         * -------------------------------------------------------------------------
         * This method will make the first request to login to the MOTO Interface.
         * Simply returning true or false.
         *
         * @access public
         * @return boolean
         */
        public function login()
        {
                // Make the request
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_VERBOSE, 1);
                curl_setopt_array($ch, array
                (
                        CURLOPT_URL => $GLOBALS['config']->MODS->BB->URL . 'login.s',
                        CURLOPT_POST => 1,
                        CURLOPT_HEADER => 0,
                        CURLOPT_SSL_VERIFYPEER => 0,
                        CURLOPT_RETURNTRANSFER => 1,
                        CURLOPT_FOLLOWLOCATION => 1,
                        CURLOPT_COOKIEJAR => $this->COOKIE,
                        CURLOPT_POSTFIELDS => array
                        (
                                'mappedUrl' => '/BENDIGO',
                                'is-login-request' => 'true',
                                'ownerId' => $GLOBALS['config']->MODS->BB->MERCHANT_ID,
                                'userName' => $GLOBALS['config']->MODS->BB->USERNAME,
                                'password' => $GLOBALS['config']->MODS->BB->PASSWORD,
                                'loginForm' => 'LOG IN'
                        )
                ));
                $data = curl_exec($ch);
                curl_close($ch);

                // Process the request - did we manage to login or not
                if (strpos($data, 'Welcome'))
                {
                        // Okay so we logged in, now we need to grab the stupid CSRF value
                        $this->CSRF = substr($data, strpos($data, "{'csrf':'")+9);
                        $this->CSRF = substr($this->CSRF, 0, strpos($this->CSRF, "'});"));
                        return true;
                }
                else return false;
        }

        /**
         * Method: logout
         * -------------------------------------------------------------------------
         * All this does is deletes the cookie
         *
         * @access public
         * @return boolean
         */
        public function logout()
        {
                unlink($this->COOKIE);
                if (!file_exists($this->COOKIE)) return true;
                else return false;
        }

        /**
         * Method: process_card
         * -------------------------------------------------------------------------
         * This method will process a card and return a response code.
         *
         * @access public
         * @return string
         */
        public function process_card($card_number, $card_expiry_month, $card_expiry_year, $cvv, $amount, $name, $reference)
        {
                // Lets do some validation

                // testing...           return $amount;

                if (!toolBox('ValidLuhn', array($card_number)))
                {
                        return 'INVALID CREDIT CARD NUMBER';
                }

                if (!is_numeric($card_expiry_month) || strlen($card_expiry_month) > 2)
                {
                        return 'INVALID EXPIRY MONTH';
                }

                if (!is_numeric($card_expiry_year) || strlen($card_expiry_year) > 2)
                {
                        return 'INVALID EXPIRY YEAR';
                }

                if (!empty($cvv) && (!is_numeric($cvv) || strlen($cvv) > 4))
                {
                        return 'INVALID CVV NUMBER';
                }

                // Make the request
                $fh = fopen('/tmp/curl.out','w') or die($php_errormsg);
                $ch = curl_init();
                curl_setopt_array($ch, array
                (
                        CURLOPT_VERBOSE => 1,
                        CURLOPT_DEBUGFUNCTION => 1,
                        CURLOPT_STDERR => $fh,
                        CURLOPT_URL => $GLOBALS['config']->MODS->BB->URL . 'initialTransactionEntry.s?csrf='.$this->CSRF,
                        CURLOPT_POST => 1,
                        CURLOPT_HEADER => 0,
                        CURLOPT_SSL_VERIFYPEER => 0,
                        CURLOPT_RETURNTRANSFER => 1,
                        CURLOPT_FOLLOWLOCATION => 1,
                        CURLOPT_COOKIEFILE => $this->COOKIE,
                        CURLOPT_POSTFIELDS => array
                        (
                                'requestType' => 'initTransaction',
                                'orderNumber' => $reference,
                                'amount' => $amount,
                                'avsData.cardholderName' => $name,
                                'cleartextCardNumber' => $card_number,
                                'cardExpiryMonth' => $card_expiry_month,
                                'cardExpiryYear' => $card_expiry_year,
                                'csc' => $cvv,
                                'avsData.address.countryCode' => 'AUS',
                                'submit' => 'Submit'
                        )
                ));
                $data = curl_exec($ch);
                curl_close($ch);

                // Process the request - what response code did we get
                if (strpos($data, '0 - Approved'))
                {
                        return 'APPROVED';
                }
                elseif (strpos($data, '2 - Declined'))
                {
                        return 'DECLINED';
                }
                elseif (strpos($data, 'Invalid card number'))
                {
                        return 'INVALID CARD NUMBER';
                }
                elseif (strpos($data, '4 - Expired Card'))
                {
                        return 'EXPIRED CARD';
                }
                elseif (strpos($data, '5 - Insufficient Funds'))
                {
                        return 'INSUFFICIENT FUNDS';
                }
                elseif (strpos($data, 'Entered amount is invalid.'))
                {
                        return 'AMOUNT IS INVALID';
                }
                elseif (strpos($data, '3 - Timed Out'))
                {
                        return 'TIMED OUT';
                }
                else
                {
                        // Lets send an error report email
                        $mail = new Zend_Mail('utf-8');
                        $mail->setType(Zend_Mime::MULTIPART_RELATED);
                        $mail->setBodyHtml($data,null,Zend_Mime::MULTIPART_RELATED);
                        $mail->setFrom('[email protected]', 'No-Reply');
                        $mail->addTo($GLOBALS['config']->ERROR_TO);
                        $mail->setSubject('TarkaPlus SERVER Bendigo Bank (9 - UNKNOWN ERROR)');
                        $mail->send();

                        return '9 - UNKNOWN ERROR';
                }
        }

        /**
         * Method: refund_card
         * -------------------------------------------------------------------------
         * This will refund a given transaction.
         * You must know the Order Id so that we know we are refunding
         * the correct card. And the amount that you wish to refund.
         *
         * NOTE: That you dont have to refund the whole amount...
         *
         * @access public
         * @return boolean
         */
        public function refund_card($amount, $order_id)
        {
                // Search for the Transaction.
                $ch = curl_init();
                curl_setopt_array($ch, array
                (
                        CURLOPT_URL => $GLOBALS['config']->MODS->BB->URL . 'orderSearch.s?csrf='.$this->CSRF,
                        CURLOPT_POST => 1,
                        CURLOPT_HEADER => 0,
                        CURLOPT_SSL_VERIFYPEER => 0,
                        CURLOPT_RETURNTRANSFER => 1,
                        CURLOPT_FOLLOWLOCATION => 1,
                        CURLOPT_COOKIEFILE => $this->COOKIE,
                        CURLOPT_POSTFIELDS => array
                        (
                                'fromDate' => '',
                                'toDate' => '',
                                'orderId' => $order_id,
                                'acquirerId' => '',
                                'merchantTransactionSource' => '',
                                'transactionState' => '',
                                'submit' => 'Submit'
                        )
                ));
                $html = curl_exec($ch);
                curl_close($ch);

                // Extract the Order System Id
                $order_system_id = toolBox('StrBetween', array
                (
                        $html,
                        '<input type="hidden" name="orderSystemId" value="',
                        '"/>'
                ));

                // Extract the transaction type
                $transaction_type = toolBox('StrBetween', array
                (
                        $html,
                        '<input type="hidden" name="transactionType" value="',
                        '"/>'
                ));

                // Make sure we actually found a card
                if (is_numeric($order_system_id))
                {
                        // Make sure we have a card that can be refunded
                        if ($transaction_type == 'RFND')
                        {
                                // Make the refund.
                                $ch = curl_init();
                                curl_setopt_array($ch, array
                                (
                                        CURLOPT_URL => $GLOBALS['config']->MODS->BB->URL . 'orderDetails.s?csrf='.$this->CSRF,
                                        CURLOPT_POST => 1,
                                        CURLOPT_HEADER => 0,
                                        CURLOPT_SSL_VERIFYPEER => 0,
                                        CURLOPT_RETURNTRANSFER => 1,
                                        CURLOPT_FOLLOWLOCATION => 1,
                                        CURLOPT_COOKIEFILE => $this->COOKIE,
                                        CURLOPT_POSTFIELDS => array
                                        (
                                                'orderSystemId' => $order_system_id,
                                                'orderId' => $order_id,
                                                'transactionType' => 'RFND',
                                                'amount' => $amount,
                                                'submit' => 'Refund'
                                        )
                                ));
                                $data = curl_exec($ch);
                                curl_close($ch);
                        }
                        else
                        {
                                // This card has already been refunded...
                                $data = 'CAN NOT REFUND';
                        }
                }
                else
                {
                        // We could not find an extacrt match to the order id
                        $data = 'ORDER ID NOT FOUND';
                }


                // Process the request - what response code did we get
                if (strpos($data, '0 - Approved'))
                {
                        return '0 - APPROVED';
                }
                elseif($data == 'ORDER ID NOT FOUND')
                {
                        return '1 - ORDER ID NOT FOUND';
                }
                elseif($data == 'CAN NOT REFUND')
                {
                        return '2 - CARD ALREADY REFUNDED';
                }
                else
                {
                        // Lets send an error report email
                        $mail = new Zend_Mail('utf-8');
                        $mail->setType(Zend_Mime::MULTIPART_RELATED);
                        $mail->setBodyHtml($data,null,Zend_Mime::MULTIPART_RELATED);
                        $mail->setFrom('[email protected]', 'No-Reply');
                        $mail->addTo($GLOBALS['config']->ERROR_TO);
                        $mail->setSubject('TarkaPlus SERVER Bendigo Bank (9 - UNKNOWN ERROR)');
                        $mail->send();

                        return '9 - UNKNOWN ERROR';
                }
        }
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.