Codeigniter:无法将错误字符串解析为XML

时间:2018-09-25 01:18:03

标签: php simplexml codeigniter-3 simplexml-load-string

我正在尝试集成我在GitHub上找到的支付网关。我按照说明进行安装。当我运行网站时,出现错误。它说,

  

遇到了未捕获的异常   类型:异常消息:无法将字符串解析为XML   文件名:/Applications/XAMPP/xamppfiles/htdocs/application/libraries/Nab_library.php

我从gitHub下载了zip文件,并安装了它。

class nab_library {
    private $merchant_id;
    private $password;
    private $payment_url;


    public function __construct($params)
    {
        $this->merchant_id = $params['NAB_merchant_id'];
        $this->password = $params['NAB_password'];
        $this->payment_url = $params['NAB_payment_url'];
    }


    public function pay_now($data)
    {
        $nabXml = '<NABTransactMessage>
            <MerchantInfo>
                <merchantID>'.$this->merchant_id.'</merchantID>
                <password>'.$this->password.'</password>
            </MerchantInfo>
            <RequestType>Payment</RequestType>
            <Payment>
                <TxnList count="1">
                    <Txn ID="1">
                        <txnType>0</txnType>
                        <txnSource>23</txnSource>
                        <amount>'.$data['cost'].'</amount>
                        <currency>AUD</currency>
                        <purchaseOrderNo>A'.$data['purchase_id'].'</purchaseOrderNo>
                        <CreditCardInfo>
                            <cardHolderName>test123</cardHolderName>
                            <cardNumber>'.$data['credit_card_no'].'</cardNumber>
                            <cvv>'.$data['security_code'].'</cvv>
                            <expiryDate>'.$data['expiry_month'].'/'.$data['expiry_year'].'</expiryDate>
                        </CreditCardInfo>
                    </Txn>
                </TxnList>
            </Payment>
        </NABTransactMessage>';

        $result = $this->makeCurlCall(
            $this->payment_url, /* CURL URL */
            "POST", /* CURL CALL METHOD */
            array( /* CURL HEADERS */
                /*<?xml version="1.0" encoding="UTF-8" ?>*/
                "Content-Type: text/xml; charset=utf-8",
                "Accept: text/xml",
                "Pragma: no-cache",
                "Content_length: ".strlen(trim($nabXml))
            ),
            null, /* CURL GET PARAMETERS */
            $nabXml /* CURL POST PARAMETERS AS XML */
        );

        $this->payment_process($result);
    }


    private function makeCurlCall($url, $method = "GET", $headers = null, $gets = null, $posts = null) {
        $ch = curl_init();
        if($gets != null)
        {
            $url.="?".(http_build_query($gets));
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        if($posts != null)
        {
            curl_setopt($ch, CURLOPT_POSTFIELDS, $posts);
        }
        if($method == "POST") {
            curl_setopt($ch, CURLOPT_POST, true);
        } else if($method == "PUT") {
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
        } else if($method == "HEAD") {
            curl_setopt($ch, CURLOPT_NOBODY, true);
        }
        if($headers != null && is_array($headers))
        {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        }
        $response = curl_exec($ch);
        $code = curl_getinfo($ch,CURLINFO_HTTP_CODE);

        curl_close($ch);
        return array(
            "code" => $code,
            "response" => $response
        );
    }

    /** 
    * Processes the result obtained from the NAB API.
    * 
    * @access public 
    * @param array
    */
    private function payment_process($result)
    {
        $response = new SimpleXMLElement($result['response']);
        $simple = simplexml_load_string($result['response']);
        $approved = (string) $simple->Payment->TxnList->Txn->approved;
        $responseCode = (string) $simple->Payment->TxnList->Txn->responseCode;

        //Display result based on response code found in result array.
        if($approved == 'Yes' && ($responseCode == '00' || $responseCode == '08')){
            echo 'Success: Payment has been successfully made.';
        } else {
            if($responseCode == '101') {
                echo 'Error: Invalid Credit Card Number - Payment was not processed';
            } else if($responseCode == '109') {
                echo 'Error: Invalid CVV2/CVC2 - Payment was not processed';
            } else if($responseCode == '51') {
                echo 'Error: Insufficient Funds - Payment was not processed';
            } else {
                echo 'Error: Unkown Error - Payment was not processed';
            }
        }
    }
}

/* End of file nab_library.php */
/* Location: ./application/libraries/nab_library.php */
  

视图文件:

 <?php echo form_open('Pay_now/index'); ?> 
            <table>
                <tr>
                    <td><label>Booking ID: </label></td>
                    <td><input type="text" name="purchase_id" value="PO1234"></td>
                </tr>
                <tr>
                    <td><label>Cost: $</label></td>
                    <td><input type="text" name="cost" value="120.00"></td>
                </tr>
                <tr>
                    <td><label>Credit Card Number: </label></td>
                    <td><input type="text" name="credit_card_no" value="4444333322221111"></td>
                </tr>
                <tr>
                    <td><label>Expiry Date: </label></td>
                    <td><input type="text" name="expiry_month" value="05"><input type="text" name="expiry_year" value="16"></td>
                </tr>
                <tr>
                    <td><label>CVV: </label></td>
                    <td><input type="text" name="security_code" value="123"></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td><input type="submit" name="submit" value="Pay"></td>
                </tr>
            </table>
        </form>
  

和控制器:pay_now.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Pay_now extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *      http://example.com/index.php/pay_now
     *  - or -  
     *      http://example.com/index.php/pay_now/index
     *  - or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/pay_now/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    public function index()
    {
        $this->load->library('form_validation');
        $config = array(
                array(
                    'field'   => 'credit_card_no', 
                    'label'   => 'Credit Card Number', 
                    'rules'   => 'required'
                ),
                array(
                    'field'   => 'expiry_month', 
                    'label'   => 'Expiry Month', 
                    'rules'   => 'required'
                ),
                array(
                    'field'   => 'expiry_year', 
                    'label'   => 'Expiry Year', 
                    'rules'   => 'required'
                ),
                array(
                    'field'   => 'security_code', 
                    'label'   => 'CVV', 
                    'rules'   => 'required'
                ),
                array(
                    'field'   => 'purchase_id', 
                    'label'   => 'Purchase Id/Number', 
                    'rules'   => 'required'
                ),
                array(
                    'field'   => 'cost', 
                    'label'   => 'Cost/Amount', 
                    'rules'   => 'required'
                ),
            );

        $this->form_validation->set_rules($config);

        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('pay_now');
        } else {
            $params = array(
                'NAB_merchant_id'   =>  $this->config->item('NAB_merchant_id'),
                'NAB_password'      =>  $this->config->item('NAB_password'),
                'NAB_payment_url'   =>  $this->config->item('NAB_payment_url'),
            );

            $this->load->library('nab_library', $params);

            $data['credit_card_no'] = $this->input->post('credit_card_no');
            $data['expiry_month'] = $this->input->post('expiry_month');
            $data['expiry_year'] = $this->input->post('expiry_year');
            $data['security_code'] = $this->input->post('security_code');
            $data['cost'] = $this->input->post('cost') * 100;
            $data['purchase_id'] = $this->input->post('purchase_id');

            $this->nab_library->pay_now($data);
        }
    }
}

/* End of file pay_now.php */
/* Location: ./application/controllers/pay_now.php */

0 个答案:

没有答案