phparraysparsingpaypal

Parse PayPal items returned values properly


I'm integrating a PayPal payment method on my website, I got it all running just fine, I'm stuck at the point were PayPal sends me to my return URL with information about the customer and items purchased.

I get this following structure on the confirmation array

Array
(
    some customer info
    ...
    [L_NAME0] => Frame%20Rojo
    [L_NAME1] => External%20Hard%20Disk
    [L_NUMBER0] => PD1002
    [L_NUMBER1] => PD1003
    [L_QTY0] => 1
    [L_QTY1] => 1
    [L_TAXAMT0] => 0%2e00
    [L_TAXAMT1] => 0%2e00
    [L_AMT0] => 29%2e00
    [L_AMT1] => 100%2e00
    ...
)

What I'm interested is in saving the whole item list, quantities and prices to my database so I can later keep track of what's been sent and what not.

My issue here is that as you can see, PayPal returns to me a set of values that are names "something+n" (L_NUMBER0 and so on), so, I can't just set up a table on my DDBB as I don't know how many items would an user get. I could save it on 2 tables: purchase and items_per_purchase like structure, but I still face the issue of parsing that array.

What would be the best way to run through it and see how many items per purchase there are to save?

I thought of some kind of bucle which sees:

while(if(isset($_GET['L_NUMBER'.$cont]))) {
    // save to ddbb
    L_NAME.$cont
    L_NUMBER.$cont
    ...
    cont++
} 

...and increment some counter but I would like to know if there's a better solution.


Solution

  • I think your solution is fine, though you don't need an if inside the while test...

    $cont = 0;
    while (isset($_GET['L_NUMBER' . $cont])) {
        // save to database after assembling array keys as follows...
        // L_NAME . $cont
        // L_NUMBER . $cont
        // etc.
    
        cont++;
    }
    

    And you should never trust input from $_GET. I recommend using parameterized queries with PDO.