简体   繁体   中英

how to retrieve multiple cart item values from response array paypal

I am using the classic API and can do most things however I have encountered a problem I cannot seem to get around. I am using PHP and after I get a successful "ACK" from GetExpressCheckoutDetails I try to loop through the response array to get multiple items but it will not accept my code. I can var_dump the response array and it clearly shows the keys and values and I can test output of the values and it looks like this

Item Name = L_PAYMENTREQUEST_0_NUMBER0

Item Name = 45065

Item Name = L_PAYMENTREQUEST_0_NUMBER1

Item Name = 16030

My for loop looks like this where "$httpParsedResponseAr" is the response array from PayPal

$index = 0;
foreach ($httpParsedResponseAr as $key => $value){                  
    if ($key == 'L_PAYMENTREQUEST_0_NAME{$index}'){
      echo '<tr> ';
      echo '<td align="center" bgcolor="#39AD43"> 
              <p>Item Name = '.$key.'</p>
              <p>Item Name = '.$value.'</p>
            </td>';
     echo '</tr>';'
     } 
     $index++;
}

the above returns nothing however,

It will let me "hard-code" the key string like below and return the correct value

$index = 0;
foreach ($httpParsedResponseAr as $key => $value){                  
    if ($key == 'L_PAYMENTREQUEST_0_NAME0'){
      echo '<tr> ';
      echo '<td align="center" bgcolor="#39AD43"> 
              <p>Item Name = '.$key.'</p>
              <p>Item Name = '.$value.'</p>
            </td>';
     echo '</tr>';'
     } 
     $index++;
}

My problem is that I will not know how many items are included in the response (added to buyers cart) so do not know how many index numbers to use so that is why the foreach loop.

I have tried this also

        if ($key == 'L_PAYMENTREQUEST_0_NAME'.$index){

How do I retrieve these multiple values?

I have tried all different combinations for the string but they are not working. What am I doing wrong?

Thanks in advance

The index incremental operator is misplaced and I have replaced 'L_PAYMENTREQUEST_0_NAME{$index}' with L_PAYMENTREQUEST_0_NAME$index or you can also use "L_PAYMENTREQUEST_0_NAME{$index}" .

Try this which worked for me:

$index = 0;
foreach ($httpParsedResponseAr as $key => $value){                  
    if ($key == "L_PAYMENTREQUEST_0_NAME$index"){
      echo '<tr>';
      echo '<td align="center" bgcolor="#39AD43"> 
              <p>Item Name = '.$key.'</p>
              <p>Item Value= '.$value.'</p>
            </td>';
      echo '</tr>';
      $index++;
     } 
}

This is what I passed in SETEC:

  . "&METHOD=SetExpressCheckout" 
       . "&VERSION=115.0" 
       . "&RETURNURL=http://localhost/justbuy/ec_check.php"
       . "&CANCELURL=http://localhost/justbuy/ec_check.php"
       . "&PAYMENTREQUEST_0_CURRENCYCODE=USD"
       . "&PAYMENTREQUEST_0_ITEMAMT=99.30"
       . "&PAYMENTREQUEST_0_TAXAMT=2.58"
       // . "&localecode=zh_HK"
       . "&PAYMENTREQUEST_0_AMT=101.88"
       . "&PAYMENTREQUEST_0_CURRENCYCODE=USD"
       . "&PAYMENTREQUEST_0_PAYMENTACTION=Sale"
       . "&SOLUTIONTYPE=Sole"

       . "&L_PAYMENTREQUEST_0_NAME0=1 Decaf Kona Blend Coffee  "
       . "&L_PAYMENTREQUEST_0_NUMBER0=623083"
       . "&L_PAYMENTREQUEST_0_QTY0=1"
       . "&L_PAYMENTREQUEST_0_AMT0=9.95 "
       . "&L_PAYMENTREQUEST_0_QTY0=2"

        . "&L_PAYMENTREQUEST_0_NAME1=Coffee Filter bags  "
       . "&L_PAYMENTREQUEST_0_NUMBER1=623084"
       . "&L_PAYMENTREQUEST_0_QTY1=1"
       . "&L_PAYMENTREQUEST_0_AMT1=39.70"
       . "&L_PAYMENTREQUEST_0_QTY1=2";

and the response when I print the GETEC response:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM