0

I am using an API to make payments.

They want me to pass the productid and quantity on my order page. I am using file_get_contents to get a result from them that I can then use.

But this is where I get stuck: I need to be able to pass multiple products and I don't know how to pass them in an URL as parameters.

This is the layout of the API:

saleData array Array with product information

  • orderData array Array with ordered products

    -- productId integer id of the product

    -- quantity integer quantity of ordered product with id above

This is how I am passing the information to my php script:

productobject:[{"productId":"10","quantity":"1"},{"productId":"11","quantity":"1"},{"productId":"2","quantity":"1"}]

Then in my PHP script I do the following:

$productobj = $_POST['productobject'];
$productobject = json_decode($productobj);

foreach($productobject as $product){
  $producturl .= 'saleData[orderData]productId='.$product->productId.'&saleData[orderData]quantity='.$product->quantity.'';
}

Then I create the api url like this:

$paynltransaction = file_get_contents("https://rest-api.pay.nl/v7/Transaction/start/json/?token=myapitoken&serviceId=SL-9697-8091&amount=".$bedrag."&ipAddress=".get_ip($ip)."&finishUrl=http://www.website.nl/_extern/web/status&paymentOptionId=".$betaalid."".$idealbank."&transaction[description]=BadenInclusief&enduser[initials]=".$voornaam."&enduser[lastName]=".$achternaamnospace."&enduser[emailAddress]=".$mail."&".$producturl."");

It doesn't give me an error but when I check the result later on this is what is stored in the response that contains the product info:

[saleData] => stdClass Object
        (
            [orderData] => Array
                (
                    [0] => 1
                )

            [invoiceDate] => 
            [deliveryDate] => 
        )

It only adds the last quantity and loses the key, it just says [0] instead of [quantity].

What am I doing wrong?

This is what I get with the answer from Fransesco:

 [saleData] => stdClass Object
        (
            [orderData] => Array
                (
                    [0] => stdClass Object
                        (
                            [productId] => 000000
                            [productType] => ROUNDING
                            [description] => Afronding
                            [price] => 330000
                            [quantity] => 1
                            [vatCode] => N
                            [vatPercentage] => 0
                            [discount] => 0
                        )

                )

            [invoiceDate] => 
            [deliveryDate] => 
        )

Updated code:

foreach($productobject as $product){
  $producturl .= 'saleData[orderData][productId]='.$product->productId.'&saleData[orderData][quantity]='.$product->quantity.'';
}
twan
  • 2,450
  • 10
  • 32
  • 92
  • That's not the proper format for array, you are missing some `[]`. Your get params should look like `saleData[orderData][productId]=1&saleData[orderData][quantity]=1` etc... – AbraCadaver Mar 23 '18 at 14:17
  • @AbraCadaver Thank you, I did that but now I am getting the result that I added in my question. Only one product, and the productid is not posted correctly, Why could that be? I posted 3 products with ajax. – twan Mar 23 '18 at 14:33

1 Answers1

0

Every key of every [sub]array needs to be between square brackets:

saleData[orderData][quantity]

As a side note, building a monster of an URL isn't the best way, you have limits on the amount of data you can pass via GET request... see this explanation: What is the maximum length of a URL in different browsers?

Francesco G.
  • 164
  • 1
  • 6
  • Thank you, if I do that my result changes but not in what I want. I added the result in my question. I only get one product like you can see, but I am passing three products to my php script. – twan Mar 23 '18 at 14:24
  • I see the updated results but i can't see how you changed the code... can you show it please? – Francesco G. Mar 23 '18 at 14:32
  • Ok, so, if I'm getting this right, that's not an error on your side but just the way they build the response. To be precise, they send back something like a "receipt" which could contain more than one entry of course, then you'd have: `[1] => stdClass Object ( [productId] => 000001 [2] => stdClass Object ( [productId] => 000002` and so on... am I right? If so, you can't avoid having numbered array and you must change the way you treat the response accordingly (which isn't difficult) – Francesco G. Mar 23 '18 at 15:56