Re: Stripping a JSON of extra characters in PHP

[email protected] (The Doctor) Mon, 6 Feb 2023 15:28:19 -0000 (UTC)
Newsgroups comp.lang.php
Organization NetKnow News
Message-ID <[email protected]>
In article <[email protected]>,
J.O. Aho <[email protected]> wrote:
>On 06/02/2023 02:31, The Doctor wrote:
>> And in the case of recurring items when being puleld from
>> a form getting parsed?
>
>If you are thinking of post value which is an array, then the simplest 
>would be:
>$a->cart = (object)$_POST['cart']; // this may give you object you don't 
>want
>
>Just keep in mind that the data is unsafe to use, you should do the long 
>way and validate the data and as you validate you can then add the 
>values to a temp variable and if all the cart items passes validation, 
>then add it to the main json object  (in this silly example the $a).
>
>If you get a new cart posted (updates), then  replace the current one 
>you have in the session, don't forget to validate the data before you 
>replace the cart section.  Only have the latest validated one
>$a->cart = $validated_shopping_cart; // validated_shopping_cart is a 
>json object
>
>for duplicate entries you have to decide what to do, cast an error or 
>merge them together, that is up to you, but do not have duplicate items 
>in your json object.
>
>I hope that was the answer you was looking for.
>

The cart is being derived from a previous form being passed into PHP!


Just an update


<?=session_start();
error_reporting(E_ALL);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
</head>
<?php 
 if(!empty($_SESSION['sessiondata'])){
 }
?>
<?php 
$myObj = [];
$cart = [];
$items = [];
$itemssubarray = [];
$storevalues= [];
$contact_details=[];
$shipping_details=[];
$billing_details=[];
$arr2 =array();

$items_count = 0;
$newtotal = 0;
$subtotal = 0;
$arr = array();
if (isset($_POST["submit"])){

$arr=array(
'store_id' => "store3",
'api_token' => "yesguy",
'checkout_id' => "chktGLFZQtore3",
'txn_total' => $_POST['charge_total'],
'environment' => "qa",
'action' => "preload",
'cart' => array( 
(foreach ($_POST['quantity'] as $key => $value) {

  if( $value > 0){

    if(isset($_POST['with_gst'][$key])){
      $unit_cost = 63.00;
    }else{
      $unit_cost = 60.00; //no gst included
    }

    $subtotal = ($unit_cost * $value);
    $quantity = ($value);

	$cart['items'][] =array(
         'url' => "https://www.pdsolutions.ca/images/newwhiteheader.png",
        'description' => $_POST['description'][$key],
        'product_code' => $_POST['id'][$key],
       'unit_cost' => $unit_cost,
       'quantity' => $quantity,
	),
    $newtotal += $subtotal;
    $items_count++;
    array_push(items,itemsubarray);
  	}
  }
         
        'subtotal' => $newtotal,
    if(isset($_POST['with_gst'][$key])){
        'tax'=>array(
          'amount' => ($newtotal * 0.05),
          'description' => "GST",
	  'rate' => "5.00",
          ),
    })
),
'contact_details'=>array(
	'first_name' =>$_POST['bill_first_name'],
	'last_name' => $_POST['bill_last_name'],
	'email' => $_POST['email'],
	'phone' => $_POST['bill_phone'],
	    ),
'billing_details'=>array(
	'address_1' => $_POST['bill_address_one'],
	'city' => $_POST['bill_city'],
	'province' => $_POST['bill_state_or_province'],
	'country' => "CA",
	'postal_code' => $_POST['bill_postal_code'],
	),

);

}


//	$billing_details[] = array (
//		"billing_details"=>array(
//'address_1' => $_POST['bill_address_one'],
//'city' => $_POST['bill_city'],
//'province' => $_POST['bill_state_or_province'],
//'country' => "CA",
//'postal_code' => $_POST['bill_postal_code'],

//		)



$myObj = $arr;
$myJSON =  json_encode($myObj);

echo $myJSON;

// Prepare new cURL resource
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://gatewayt.moneris.com/chkt/request/request.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $myJSON);
 
$array_length = count($arr, COUNT_RECURSIVE);
 
// Set HTTP Header for POST request 
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json' ,'Content-Length: ' . strlen($myJSON) 
 )
);
  
// Submit the POST request
$result = curl_exec($ch);
 
// Close cURL session handle
curl_close($ch);
print_r($arr);
print_r($result);
$response_data = json_decode($result);
print_r($response_data);
 
 
$result_data = json_decode($result, true);
print_r($result_data);
 
if( isset($result_data['response']['error']) &&  ( ! (bool) $result_data['response']['success'] ) ){
    echo 'error message';
}
$token = $result_data['response']['ticket'];
?>

<body>
                <div id="outerDiv" style="width:400px;height:300px">
        <div id="monerisCheckout"></div>
    </div>

 <script src="https://gatewayt.moneris.com/chktv2/js/chkt_v2.00.js"></script>
   <script>
                let myCheckout = new monerisCheckout();
                myCheckout.setMode("qa");
                myCheckout.setCheckoutDiv("monerisCheckout");
                myCheckout.setCallback("page_loaded", testPageLoad);
                myCheckout.setCallback("payment_receipt", testPayReceipt);
                myCheckout.setCallback("payment_complete", testPayComplete);
                myCheckout.setCallback("cancel_transaction", testCancel);
                myCheckout.setCallback("error_event", testCancel);
                myCheckout.setCallback("payment_submitted", testPaymentSubmitted);
                myCheckout.startCheckout('<?php echo $token ?>');
                myCheckout.setCallback("page_closed", testCancel);

                function testPageLoad(res){

                                console.log(res)

                }

                function testPaySubmit(res){

                                console.log(res)

                }

                function testPayReceipt(res){

                                console.log(res)

                }

                function testPayComplete(res){

                                console.log(res)

                }

                function testPaymentSubmitted(res){

                                console.log(res)

                }

                function testCancel(res){

                                console.log(res)

                                //myCheckout.closeCheckout(JSON.parse(res).ticket)

                }

               
        
    </script>
</body>
</html>

75% is working properly.


The cart section is now an issue .

>-- 
>  //Aho
>


-- 
Member - Liberal International This is [email protected] Ici [email protected]
Yahweh, King & country!Never Satan President Republic!Beware AntiChrist rising!
Look at Psalms 14 and 53 on Atheism https://www.empire.kred/ROOTNK?t=94a1f39b 
If their only strength is in enforcing weakness, then they will preside over ruin. -unknown Beware https://mindspring.com