0
votes

I have an annoying bug when using PayPal PHP API.

In case I use Quantity = 1 for each Item object it works fine. But if I have more than one I always get a sum up error. Code written in Swedish so it Might be a bit hard to follow, Here I add Items:

    $result=mysqli_query($connection,$query);
    if (mysqli_num_rows($result) > 0) {
        $i=0;              
        while($row=mysqli_fetch_array($result)){
              $bestallningrad[] = new Item();
              $bestallningrad[$i]->setName($row['Typ'])
                                 ->setDescription($row['Skick'])
                                 ->setCurrency('SEK')
                                 ->setQuantity($row['Antal'])
                                 ->setPrice($row['Radpris']);
              $i = $i + 1;
        }
    } else {
      mysqli_close($connection);
      $fel=2;
      echo "<SCRIPT>";
           echo "window.location.href=\"varukorg.php?Fel=$fel\";",PHP_EOL;
      echo "</SCRIPT>";
    }

    $varulista = new ItemList();
    $varulista->setItems($bestallningrad);

Here I create the transaction Object:

    $transaktioner = new Transaction();
    $transaktioner->setAmount($belopp)
                  ->setItemList($varulista)
                  ->setDescription("Beställning")
                  ->setInvoiceNumber($bid);

When I have Quatity >1 i get the following error:

    JSON: {
           "intent": "sale","payer": {
           "payment_method": "paypal"
           },
           "redirect_urls": {
           "return_url": "https://teltec.se/teltecsvar.php?lyckad=true",
           "cancel_url": "https://teltec.se/varukorg.php?Fel=5"
           },
           "transactions": [
                            {
                             "amount": {
                                        "currency": "SEK",
                                        "total": "330",
                                        "details": {
                                        "shipping": "8",
                                        "tax": "2",
                                        "subtotal": "320"
                                        }
                            },
                             "item_list": {
                                           "items": [
                                                     {
                                                      "name": "EF 80",
                                                      "description": "Begagnat",
                                                      "currency": "SEK",
                                                      "quantity": "2",
                                                      "price": "320"
               }
           ]
       },
       "description": "Best\u00e4llning",
       "invoice_number": "37"
      }
     ]
    }

Fel: 400 Beskrivning: {"name":"VALIDATION_ERROR","details":[{"field":"transactions[0]","issue":"Item amount must add up to specified amount subtotal (or total if amount details not specified)"}],"message":"Invalid request - see details","information_link":"https://developer.paypal.com/docs/api/payments/#errors","debug_id":"f771187267060"}

1
I solved the problem temporary by splitting up one row in the database that contains more than one item into several Item objects. Looks weird on PayPal side but it works. - M.Landhage

1 Answers

0
votes

I think you need to specify the price per item ("price": "160"). Eg. price * quantity should be equal to subtotal.