Bonjour,

Je suis actuellement en train de m'attarder sur le tutoriel "Paypal Express Checkout". Je n'ai pas rencontré de problèmes majeurs jusqu'à la moitié de la vidéo.

Mon code fonctionne initialement bien, sauf quand je rajoute ma boucle pour obtenir un détail des informations sur les produits qui seront achetés.

J'ai cherché pendant de longues minutes et essayé d'adapter le code mais sans grand succès.

<?php
$products = array(
    array(
    "name" => "Générateur d'énergie quantique",
    "price" => 10.0,
    "priceTVA" => 12.0,
    "count" => 1
    ),
    array(
    "name" => "Hyperdrive T14",
    "price" => 25.5,
    "priceTVA" => 30.50,
    "count" => 2
    )
);

$total = 61.0;
$totalttc = 61.0;
$port = 10.0;

$mmddpp = "BLABLABLAIRBLAIR";
$identi = "seller-bambou_api1.hotmail.com";
$signature = "BLOPBLOPBLABLA";

$params = array(
    'METHOD' => 'SetExpressCheckout',
    'VERSION' => '74',
    'USER' => $identi,
    'SIGNATURE' => $signature,
    'PWD' => $mmddpp,
    'RETURNURL' => 'http://localhost/oui.php',
    'CANCELURL' => 'http://localhost/oui.php',
    'PAYMENTREQUEST_0_AMT' => $totalttc + $port,
    'PAYMENTREQUEST_0_CURRENCYCODE' => 'EUR',
    'PAYMENTREQUEST_0_SHIPPINGAMT' => $port,
    'PAYMENTREQUEST_0_ITEMAMT' => $totalttc
);

$params = http_build_query($params);
$endpoint = 'https://api-3T.sandbox.paypal.com/nvp';
$curl = curl_init();
curl_setopt_array($curl,array(
    CURLOPT_URL => $endpoint,
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => $params,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_SSL_VERIFYHOST => false,
    CURLOPT_VERBOSE => 1
    ));
$response = curl_exec($curl);
$responseArray = array();
parse_str($response,$responseArray);
if(curl_errno($curl)){
        var_dump(curl_error($curl));
        curl_close($curl);
        die();
}else{
    if($responseArray['ACK'] == 'Success'){

    }else{
        var_dump($responseArray);
        die();
    }
    curl_close($curl);
}?>

<a href="https://www.sandbox.paypal.com/webscr?cmd=_express-checkout&useraction=commit&token=<?php echo $responseArray['TOKEN']; ?>">Payer</a>

Le problème survient lorsque je rajoute ...

foreach($products as $k => $product){
    $params["L_PAYMENTREQUEST_0_NAME$k"] = $product['name'];
    $params["L_PAYMENTREQUEST_0_DESC$k"] = '';
    $params["L_PAYMENTREQUEST_0_AMT$k"] = $product['priceTVA'];
    $params["L_PAYMENTREQUEST_0_QTY$k"] = $product['count'];
    }

J'ai regardé sur les sujets du forum, mais je n'ai pas rencontré quelqu'un qui avait eu le même problème. Quelqu'un aurait une petite idée ? Merci d'avance :)

6 réponses


Huggy
Réponse acceptée

Le message est clair, le montant total doit correspondre à la somme des montants des articles

J'espère pour toi que ce ne sont pas tes vrais identifiants !!!

edit : seller.... c'est forcément un compte de démo

C'est quoi le message d'erreur ?
erreur php ou erreur paypal ?

il te faut un indice 0,1,2,3....

$i = 0;
foreach($products as $k => $product){
  $params["L_PAYMENTREQUEST_0_NAME$i"] = $product['name'];
  $params["L_PAYMENTREQUEST_0_DESC$i"] = '';
  $params["L_PAYMENTREQUEST_0_AMT$i"] = $product['priceTVA'];
  $params["L_PAYMENTREQUEST_0_QTY$i"] = $product['count'];
  $i = $i + 1;
}

C'est un test, j'ai donc créé un compte "bidon" :)
Oops, j'ai oublié d'indiquer le message d'erreur.

array (size=9)
  'TIMESTAMP' => string '2015-05-03T12:20:11Z' (length=20)
  'CORRELATIONID' => string 'ec1c3b3272ae1' (length=13)
  'ACK' => string 'Failure' (length=7)
  'VERSION' => string '74' (length=2)
  'BUILD' => string '16481822' (length=8)
  'L_ERRORCODE0' => string '10413' (length=5)
  'L_SHORTMESSAGE0' => string 'Transaction refused because of an invalid argument. See additional error messages for details.' (length=94)
  'L_LONGMESSAGE0' => string 'The totals of the cart item amounts do not match order amounts.' (length=63)
  'L_SEVERITYCODE0' => string 'Error' (length=5)

La solution d'incrémentation, je viens de la tester. Il n'y a pas de différence :/

Tout bêtement ...
Merci :)