2

I have a form, which sends data through a Jquery Ajax Call over to a controller function, which processes the data, and then gets a response from a 3rd party API.

Data is sent via POST to the controller and then such data is sent via Curl to teh 3rd party API, which then is returned in a string, and then converted to an array, then finally json encoded.

I'm stuck where I'm able to send back the array using " echo json_encode($array); ", however I cant seem to figure out how to work the array.

What i want to finally do, is if the "result" key on the array = "failed", then I would append a message to a div, if it equals "result" = "success", then I would append a message and run a second function.

The response Array im returning is: {"transaction_id":"8267_03-01-12_16:02:58_0","action":"payment","result":"failed","errors":"98","errors_meaning":"(98)","customer_errors_meaning":"","processing_time":"0.0117"}

Heres the code I have

The Form & Jquery Code:

<div class="creditcard">
    <form method="post">
        <strong>Credit Card Type:</strong>
        <select name="cc_type" id="cc_type" size="1">
            <option value="VISA">Visa</option>
            <option value="MASTERCARD">MasterCard</option>
        </select>

        <strong>Credit Card Number:</strong>
        <input type="text" name="cc_number" id="cc_number" size="24" value="">

        <strong>Credit Card Expiration Date:</strong>
        <select name="cc_expdate_month" id="cc_expdate_month">
            <option value="01" >01</option>
            <option value="02" >02</option>
            <option value="03" >03</option>
            <option value="04" >04</option>
            <option value="05" >05</option>
            <option value="06" >06</option>
            <option value="07" >07</option>
            <option value="08" >08</option>
            <option value="09" >09</option>
            <option value="10" >10</option>
            <option value="11" >11</option>
            <option value="12" >12</option>
        </select> / <select name="cc_expdate_year" id="cc_expdate_year">
            <option value="06" >2006</option>
            <option value="07" >2007</option>
            <option value="08" >2008</option>
            <option value="09" >2009</option>
            <option value="10" >2010</option>
            <option value="11" >2011</option>
            <option value="12" >2012</option>
            <option value="13" >2013</option>
            <option value="14" >2014</option>
            <option value="15" >2015</option>
            <option value="16" >2016</option>
        </select>

        <strong>Credit Card Security Code:</strong>
        <input type="text" name="cc_security_code" id="cc_security_code" size="4" maxlength="4" value="">
        <input type="hidden" name="business" id="business" value="[email protected]" />
        <input type="hidden" name="action" id="action" value="payment" />
        <input type="hidden" name="vericode" id="vericode" value="xxxxxxxx" />

        <input type="hidden" name="first_name" id="first_name" value="testname" />
        <input type="hidden" name="last_name" id="last_name" value="testlname" />

        <input type="hidden" name="address" id="address" value="testaddress" />
        <input type="hidden" name="city" id="city" value="testcity" />
        <input type="hidden" name="state_or_province" id="state_or_province" value="AL" />
        <input type="hidden" name="zip_or_postal_code" id="zip_or_postal_code" value="12345" />
        <input type="hidden" name="country" id="country" value="US" />
        <input type="hidden" name="shipping_address" id="shipping_address" value="testaddress" />
        <input type="hidden" name="shipping_city" id="shipping_city" value="testcity" />
        <input type="hidden" name="shipping_state_or_province" id="shipping_state_or_province" value="AL" />
        <input type="hidden" name="shipping_zip_or_postal_code" id="shipping_zip_or_postal_code" value="12345" />
        <input type="hidden" name="shipping_country" id="shipping_country" value="US" />
        <input type="hidden" name="phone" id="phone" value="1234567890" />
        <input type="hidden" name="email" id="email" value="[email protected]" />

        <input type="hidden" name="item_name" id="item_name" value="xxxx.xxx" />
        <input type="hidden" name="item_code" id="item_code" value="Product Name 3," />
        <input type="hidden" name="quantity" id="quantity" value="1" />
        <input type="hidden" name="amount" id="amount" value="205.00" />
        <input type="hidden" name="transaction_id" id="transaction_id" value="1060" />
        <input type="hidden" name="shipment" id="shipment" value="yes" />
        <input type="hidden" name="shipment_method" id="shipment_method" value="USPS Priority Mail" />
        <input type="hidden" name="ip_address" id="ip_address" value="123.45.67.89" />
        <input type="hidden" name="test_php" value="1" />

        <input type="submit" value="Authorize Credit Card" name="authorize_cc" id="authorize_cc" />
        <div class="authorization_result"></div>
    </form>
</div>
<script defer="defer" type="text/javascript">
$(document).ready(function() {

$('#authorize_cc').click(function() {
    var postData = {
        'authorize' : 1 ,
        'cc_type' : $("#cc_type").val(),
        'cc_number' : $("#cc_number").val(),
        'cc_expdate_month' : $("#cc_expdate_month").val(),
        'cc_expdate_year' : $("#cc_expdate_year").val(),
        'cc_security_code' : $("#cc_security_code").val(),
        'business' : $("#business").val(),
        'action' : $("#action").val(),
        'vericode' : $("#vericode").val(),
        'first_name' : $("#first_name").val(),
        'last_name' : $("#last_name").val(),
        'address' : $("#address").val(),
        'city' : $("#city").val(),
        'state_or_province' : $("#state_or_province").val(),
        'zip_or_postal_code' : $("#zip_or_postal_code").val(),
        'country' : $("#country").val(),
        'shipping_address' : $("#shipping_address").val(),
        'shipping_city' : $("#shipping_city").val(),
        'shipping_state_or_province' : $("#shipping_state_or_province").val(),
        'shipping_zip_or_postal_code' : $("#shipping_zip_or_postal_code").val(),
        'shipping_country' : $("#shipping_country").val(),
        'phone' : $("#phone").val(),
        'email' : $("#email").val(),
        'item_name' : $("#item_name").val(),
        'item_code' : $("#item_code").val(),
        'quantity' : $("#quantity").val(),
        'amount' : $("#amount").val(),
        'transaction_id' : $("#transaction_id").val(),
        'shipment' : $("#shipment").val(),
        'shipment_method' : $("#shipment_method").val(),
        'ip_address' : $("#ip_address").val(),
    };

    $.ajax({
            url: "<?php echo base_url().'admin/creditcard/authorize';?>",
            type:'POST',
            data: postData,
            dataType: 'json',
            success: function(output_string){
                $(".authorization_result").append(output_string);

            }
            }); // End of ajax call
    return false;       
}); 
});

</script>

The Controller Function

function authorize(){
// Get variables from POST array
$post_str = "action=payment&business="      .urlencode($this->input->post('business'))
            ."&vericode="                   .urlencode($this->input->post('vericode'))
            ."&item_name="                  .urlencode($this->input->post('item_name'))
            ."&item_code="                  .urlencode($this->input->post('item_code'))
            ."&quantity="                   .urlencode($this->input->post('quantity'))
            ."&amount="                     .urlencode($this->input->post('amount'))
            ."&cc_type="                    .urlencode($this->input->post('cc_type'))
            ."&cc_number="                  .urlencode($this->input->post('cc_number'))
            ."&cc_expdate="                 .urlencode($this->input->post('cc_expdate_year')).urlencode($this->input->post('cc_expdate_month'))
            ."&cc_security_code="           .urlencode($this->input->post('cc_security_code'))
            ."&shipment="                   .urlencode($this->input->post('shipment'))
            ."&first_name="                 .urlencode($this->input->post('first_name'))
            ."&last_name="                  .urlencode($this->input->post('last_name'))
            ."&address="                    .urlencode($this->input->post('address'))
            ."&city="                       .urlencode($this->input->post('city'))
            ."&state_or_province="          .urlencode($this->input->post('state_or_province'))
            ."&zip_or_postal_code="         .urlencode($this->input->post('zip_or_postal_code'))
            ."&country="                    .urlencode($this->input->post('country'))
            ."&shipping_address="           .urlencode($this->input->post('shipping_address'))
            ."&shipping_city="              .urlencode($this->input->post('shipping_city'))
            ."&shipping_state_or_province=" .urlencode($this->input->post('shipping_state_or_province'))
            ."&shipping_zip_or_postal_code=".urlencode($this->input->post('shipping_zip_or_postal_code'))
            ."&shipping_country="           .urlencode($this->input->post('shipping_country'))
            ."&phone="                      .urlencode($this->input->post('phone'))
            ."&email="                      .urlencode($this->input->post('email'))
            ."&ip_address="                 .urlencode($this->input->post('ip_address'))
            ."&website_unique_id="          .urlencode($this->input->post('website_unique_id'));

            // Send URL string via CURL
            $backendUrl = "https://www.veripayment.com/integration/index.php";
            $this->curl->create($backendUrl);
            $this->curl->post($post_str);
            $return_str = $this->curl->execute();

            //-------------------------------------
            // Convert String to array
            //-------------------------------------

            $result = array();
            // Explode array where blanks are found
            $resparray = explode(' ', $return_str);

            if ($resparray)
            {
              // save results into an array
              foreach ($resparray as $resp) {
                $keyvalue = explode('=', $resp);
                if(isset($keyvalue[1])){
                $result[$keyvalue[0]] =  str_replace('"', '', $keyvalue[1]);
                }
              }
            }

            //echo $result array
            echo json_encode($result);
}
4
  • 2
    oh man, nobody wants to go through this wall of code. can you reduce your code to the absolute minimum that demonstrates the problem? Commented Mar 1, 2012 at 21:34
  • I don't see a mention of 'failed' or 'success' anywhere in your $result array. Commented Mar 1, 2012 at 21:38
  • @BrianGlaz it's implicit with the question, we don't actually need those specifics, he just needs to see if a property exists and it's parse-able. Commented Mar 1, 2012 at 21:44
  • +1 for adding the return array! Commented Mar 1, 2012 at 21:50

1 Answer 1

3

Give a conditional look at your response.

    $.ajax({
        url: "<?php echo base_url().'admin/creditcard/authorize';?>",
        type:'POST',
        data: postData,
        dataType: 'json',
        success: function(output_string){
            if(x = $.parseJSON(output_string)){
              if(x.property == 'true'){ //or  if(x.property != null){
                  $(".authorization_result").append(x);
              }else{
                 $(".authorization_result").append("fail");
               }
            }else{
                 $(".authorization_result").append("fail");
            }
        }
    });
Sign up to request clarification or add additional context in comments.

8 Comments

What does the x mean in this case?
it's the parsed JSON. if the parser completes and doesn't complain, x will be your JSON. if the parser fails, then the script doesn't fail it just bumps. That's why the if conditional doesn't look quite right. And yes there's a more graceful way to do this, I'm just lazy.
so where it says "property" that would be the array key then?
it's an object, but yes, that's the object property you're checking for a value. and 'true' was just any string value... so it could be anything, just needs to match the value of x.property, or you could say != null....
And you may wanna set var x; somewhere above where it's set and used, just for good practices.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.