Stripping [ ] from JSON arrays

[email protected] (The Doctor) Tue, 10 Jan 2023 01:17:47 -0000 (UTC)
Newsgroups comp.lang.php,comp.lang.javascript
Organization NetKnow News
Message-ID <[email protected]>
This morning I got this from Moeris:


Here's an example working one:

{
"store_id":"store3",
"checkout_id":"chkt23NGFtore3",
"api_token":"yesguy",
"txn_total":"189.00",
"environment":"qa",
"action":"preload"
}

You repeated the same items section 3 times and your commas and braces are
incorrectly placed. Correct your preload, and let me know if you still get a
blank screen.

Follow the formatting shown here for the preload:
https://developer.moneris.com/sitecore/media%20library/Hidden/MCO/Preload%20Request?sc_lang=en


now given code processor



<?=session_start();
error_reporting(E_ALL);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Professional Development Solutions - Online Form</title>
<meta name="description" content="PD Solutions is an educational company that delivers on-line webinars and webcasts to various organizations or people" />
<meta name="robots" content="index, follow" />
<link rel="stylesheet" type="text/css" href="css/css.css"/>
<link rel="stylesheet" type="text/css" href="css/css2.css"/>
    <link rel="stylesheet" href="css/uniform.default.css" type="text/css" media="screen">
    <link rel="stylesheet" href="css/uniform.agent.css" type="text/css" media="screen">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">


</head>
<body>
<?php 
 if(!empty($_SESSION['sessiondata'])){
 }
?>
<div id="wrapper">
 <!--header begins-->
 <?php
  include("includes/header.php");
 ?> 
<!--header ends-->
<FORM ACTION="https://gatewayt.moneris.com/chktv2/request/request.php" method=post>
<?php 
$myObj = [];
$cartarray= [];
$itemssubarray= [];
$storevalues= [];
$storevalues2= [];
$contact_details=[];
$shipping_details=[];
$billing_details=[];
$arr2 =array();
?>
<center>
<!-- Store Settings-->
<!------- DEFINE CHARGE TOTAL HERE --->
<br/><br/><h3>Your Purchase Total Is: </h3> <br/> <h2>$<?=$_POST['charge_total']?></h2> 
<!-- Unique Order ID -->
<!-- Additional Optional Details -->
<!-- Item Information -->
<?php 

$items_count = 1;
$subtotal = 0;
$subtotala = 0;
$arr = array();
if (isset($_POST["submit"])){
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);
    
    $itemssubarray[] = array(
		"items"=>array(
         'url' => "https://www.pdsolutions.ca/images/procircle.png",
	'description' => $_POST['description'][$key],
	'product_code' => $_POST['id'][$key],
       'unit_cost' => $unit_cost,
	'quantity' => $quantity,
		),
    );
}
 
  $subtotala += ($unit_cost * $value);
  $items_count++;
}
  $cartarray[] = array ("cart"=>array($itemssubarray));

//  array_push($cartarray,$itemssubasrray);
 
    $cartarray[] = array(
	'subtotal' => $subtotala,
	"tax"=>array(
          'amount' => ($subtotala * 0.05),
          'description' => "GST",
	  'rate' => "0.05",
          ),
    );
    array_push($arr, $cartarray);
}    

?>
<?php 

if (isset($_POST["submit"])){
(array_push($storevalues,[
//'sessValue' => $_POST['sessionid1'],
'username'=> "demouser",
'password' => "password",
'store_id' => "store3",
'checkout_id' => "chkt23NGFtore3",
'api_token' => "yesguy",
'txn_total' => $_POST['charge_total'],
'environment' => "qa",
'action' => "preload",
]));

    $contact_details[] = array(
	    "contact_details"=>array(
'first_name' =>$_POST['bill_first_name'],
'last_name' => $_POST['bill_last_name'],
'email' => $_POST['email'],
'phone' => $_POST['bill_phone'],
	    )
);
    array_push($arr2, $contact_details);

	$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'],

		)
	);	

?>
<?php 
$myObj = array_merge($storevalues,$cartarray,$contact_details,$billing_details);
//$storevalues2 = substr($storevalues,1,strlen($storevalues) -2);
//$myObj = $storevalues;
$myJSON =  json_encode($myObj);
echo $myJSON;
}

?>
<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" async></script>

 <script>
        var myCheckout = new monerisCheckout();
        myCheckout.setMode("qa");
        myCheckout.setCheckoutDiv("monerisCheckout");
        monerisCheckout.startCheckout('<?php echo $token ?>');
 
        var myPageLoad = function(data) {
            console.log(data);
            const obj = JSON.parse(data);
            console.log(obj.ticket);
 
        };
 
        var myCancelTransaction = function(data) {
            console.log(data);
            const obj = JSON.parse(data);
            console.log(obj.ticket);
        };
 
        var myErrorEvent = function(data) {
            console.log(data);
            const obj = JSON.parse(data);
            console.log(obj.ticket);
        };
 
        var myPaymentReceipt = function(data) {
            console.log(data);
            const obj = JSON.parse(data);
            console.log(obj.ticket);
        };
 
        var myPaymentComplete = function(data) {
            console.log(data);
            const obj = JSON.parse(data);
            console.log(obj.ticket);
        };
 
        /**
        * Set callbacks in JavaScript:
        */
        myCheckout.setCallback("page_loaded",myPageLoad);
        myCheckout.setCallback("cancel_transaction",myCancelTransaction);
        myCheckout.setCallback("error_event", myErrorEvent);
        myCheckout.setCallback("payment_receipt",myPaymentReceipt);
        myCheckout.setCallback("payment_complete", myPaymentComplete);
    </script>
<INPUT TYPE="SUBMIT" NAME="SUBMIT" VALUE="Click to proceed to Secure Page">
</center>
</FORM>

<?php 
    include("includes/footer.php");
?>
</div>
</body>
</html>


This picks up data from a previous form.

Here is and example of what is generated:

[{"username":"demouser","password":"password","store_id":"store3","checkout_id":"chkt23NGFtore3","api_token":"yesguy","txn_total":"189.00","environment":"qa","action":"preload"},{"cart":[[{"items":{"url":"https:\/\/www.pdsolutions.ca\/images\/procircle.png","description":"Protecting Pollinators","product_code":"PP-1294","unit_cost":60,"quantity":"1"}},{"items":{"url":"https:\/\/www.pdsolutions.ca\/images\/procircle.png","description":"Agricultural Health Study","product_code":"AHS-1298","unit_cost":60,"quantity":"1"}},{"items":{"url":"https:\/\/www.pdsolutions.ca\/images\/procircle.png","description":"Pesticide Applicator Records","product_code":"PAR-1302","unit_cost":60,"quantity":"1"}}]]},{"subtotal":180,"tax":{"amount":9,"description":"GST","rate":"0.05"}},{"contact_details":{"first_name":"Dave","last_name":"Yadallee","email":"[email protected]","phone":"7804734587"}},{"billing_details":{"address_1":"","city":"","province":"","country":"CA","postal_code":""}}]

And here is what is being looked for

{
    "store_id":"moneris",
	"api_token":"hurgle",
	    "checkout_id":"chkt5BF66neris",
		"txn_total":"452.00",
		    "environment":"qa",
			"action":"preload",
			    "token": [
				{
					"data_key": "abc123datakey1",
						"issuer_id": "645sddfvdrt4tefd"
						    },
							{
								"data_key": "abc123datakey2",
									"issuer_id": "645sddfvdrt4tefd"
									    },
										{
											"data_key": "abc123datakey3",
												"issuer_id": "645sddfvdrt4tefd"
												    }
													],
													    "ask_cvv":"Y"
														"order_no":"",
														    "cust_id":"chkt - cust - 0303",
															"dynamic_descriptor":"dyndesc",
															    "language":"en",
																"recur":{
																	"bill_now":"true",
																		"recur_amount":"1.00",
																			"start_date":"2021-11-21",
																				"recur_unit":"month",
																					"recur_period":"1",
																						"number_of_recurs":"10"
																						    },
																							"cart":{
																								"items":[
																									    {
																											    "url":"https:\/\/example.com\/examples\/item1.jpg",
																													    "description":"One item",
																															    "product_code":"one_item",
																																	    "unit_cost":"100.00",
																																			    "quantity":"1"
																																					},
																																						    {
																																								    "url":"https:\/\/example.com\/examples\/item2.jpg",
																																										    "description":"Two item",
																																												    "product_code":"two_item",
																																														    "unit_cost":"200.00",
																																																    "quantity":"1"
																																																		},
																																																			    {
																																																					    "url":"https:\/\/example.com\/examples\/item3.jpg",
																																																							    "description":"Three item",
																																																									    "product_code":"three_item",
																																																											    "unit_cost":"100.00",
																																																													    "quantity":"1"
																																																															}
																																																																],
																																																																	"subtotal":"400.00",
																																																																		    "tax":{
																																																																				"amount":"52.00",
																																																																					    "description":"Taxes",
																																																																							"rate":"13.00"
																																																																								}
																																																																								    },
																																																																									"contact_details":{
																																																																										"first_name":"bill",
																																																																											"last_name":"smith",
																																																																												"email":"[email protected]",
																																																																													"phone":"4165551234"
																																																																													    },
																																																																														"shipping_details":{
																																																																															"address_1":"1 main st",
																																																																																"address_2":"Unit 2012",
																																																																																	"city":"Toronto",
																																																																																		"province":"ON",
																																																																																			"country":"CA",
																																																																																				"postal_code":"M1M1M1"
																																																																																				    },
																																																																																					"billing_details":{
																																																																																						"address_1":"1 main st",
																																																																																							"address_2":"Unit 2000",
																																																																																								"city":"Toronto",
																																																																																									"province":"ON",
																																																																																										"country":"CA",
																																																																																											"postal_code":"M1M1M1"
																																																																																											    }
																																																																																											    }

																																																																																											    How do I strip unnecessary [] ?
-- 
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 
Birthdate: 29 Jan 1969 Redhill, Surrey, England  Beware https://mindspring.com