Monthly Archive for mars 2008

Cryptage dynamique en php des boutons Paypal

Bonjour à tous,

Paypal dans son interface d’administration ne permet de crypter qu’un bouton à la fois, ce qui n’est pas très pratique quand on à 500 articles à vendre par exemple.

Nous allons donc voir comment crypter dynamiquement tout les boutons paypal buy now d’une page .

  1. Il faut bien entendu dans la page ou va apparaitre l’article à vendre inclure le script qui va crypter nos boutons.
    1. require_once("../paypal/crypt.php"); // représente le chemin du script
  2. Créer un certificat en cliquant ici et récupérer la Private Key (renommée my-prvkey.pem et le Public certificate my-pubcert.pem
  3. Allez dans l’administration de votre compte Paypal cliquez sur préférence puis sur certificats pour site marchand cliquez sur télécharger et récupérez paypal_cert_pem.txt et renommez le en paypal_cert.pem.
  4. Toujours dans la page certificats pour site marchand, cliquez sur Ajouter et sélectionnez votre certificat public généré à l’étape 2. Il vous attribue un numéro de certificat à conserver précieusement et à utiliser dans la variable ‘cert_id’ de l’étape suivante :
  5. passons maintenant au contenu de crypt.php
    1. //Sample PayPal Button Encryption: Copyright 2006,2007 StellarWebSolutions.com
    2. //Not for resale  - license agreement at
    3. //http://www.stellarwebsolutions.com/en/eula.php
    4. //Updated: 2007 04 04
    5.  
    6. #Set home directory for OpenSSL
    7. putenv("HOME=~");
    8.  
    9. # private key file to use
    10. $MY_KEY_FILE = "/var/www/vhosts/votresite.com/httpdocs/paypal/my-prvkey.pem";
    11.  
    12. # public certificate file to use
    13. $MY_CERT_FILE = "/var/www/vhosts/votresite.com/httpdocs/paypal/my-pubcert.pem";
    14.  
    15. # Paypal’s public certificate
    16. $PAYPAL_CERT_FILE = "/var/www/vhosts/votresite.com/httpdocs/paypal/paypal_cert.pem";
    17.  
    18. # path to the openssl binary
    19. $OPENSSL = "/usr/bin/openssl";
    20.  
    21. function paypal_encrypt($hash)
    22.         {
    23.                 //Sample PayPal Button Encryption: Copyright 2006,2007 StellarWebSolutions.com
    24.                 //Not for resale - license agreement at
    25.                 //http://www.stellarwebsolutions.com/en/eula.php
    26.  
    27.                 global $MY_KEY_FILE;
    28.                 global $MY_CERT_FILE;
    29.                 global $PAYPAL_CERT_FILE;
    30.                 global $OPENSSL;
    31.  
    32.                 if (!file_exists($MY_KEY_FILE)) {
    33.                         echo "ERROR: MY_KEY_FILE $MY_KEY_FILE not found\n";
    34.                 }
    35.                 if (!file_exists($MY_CERT_FILE)) {
    36.                         echo "ERROR: MY_CERT_FILE $MY_CERT_FILE not found\n";
    37.                 }
    38.                 if (!file_exists($PAYPAL_CERT_FILE)) {
    39.                         echo "ERROR: PAYPAL_CERT_FILE $PAYPAL_CERT_FILE not found\n";
    40.                 }
    41.                 if (!file_exists($OPENSSL)) {
    42.                         echo "ERROR: OPENSSL $OPENSSL not found\n";
    43.                 }
    44.  
    45.                 //Assign Build Notation for PayPal Support
    46.                 $hash[‘bn’]= ‘StellarWebSolutions.PHP_EWP’;
    47.  
    48.                 $openssl_cmd = "$OPENSSL smime -sign -signer $MY_CERT_FILE -inkey $MY_KEY_FILE " .
    49.                                         "-outform der -nodetach -binary | $OPENSSL smime -encrypt " .
    50.                                         "-des3 -binary -outform pem $PAYPAL_CERT_FILE";
    51.  
    52.                 $descriptors = array(
    53.                                 0 => array("pipe", "r"),
    54.                         1 => array("pipe", "w"),
    55.                 );
    56.  
    57.                 $process = proc_open($openssl_cmd, $descriptors, $pipes);
    58.  
    59.                 if (is_resource($process)) {
    60.                         foreach ($hash as $key => $value) {
    61.                                 if ($value != "") {
    62.                                         //echo "Adding to blob: $key=$value\n";
    63.                                         fwrite($pipes[0], "$key=$value\n");
    64.                                 }
    65.                         }
    66.                         fflush($pipes[0]);
    67.                                 fclose($pipes[0]);
    68.  
    69.                         $output = "";
    70.                         while (!feof($pipes[1])) {
    71.                                 $output .= fgets($pipes[1]);
    72.                         }
    73.                         //echo $output;
    74.                         fclose($pipes[1]);
    75.                         $return_value = proc_close($process);
    76.                         return $output;
    77.                 }
    78.                 return "ERROR";
    79.         };
    80. function paypal_button($title,$cost,$iduser) // choississez les variables à transmettre à la fonction (prix, nom de l’objet, etc)
    81.         {
    82.         $form = array(‘cmd’ => ‘_xclick’, //bouton paypal achat immédiat
    83.         ‘business’ => ‘votreadresse@monsite.com’, // adresse de votre compte paypal
    84.         ‘cert_id’ => ‘WWEEDZREXSSS’, // ici mettre le numéro de certificat obtenu dans l’interface d’administration paypal
    85.         ‘item_name’ => $title, //titre de l’objet vendu
    86.         ‘amount’ => $cost, // cout de l’objet
    87.         ’shipping’ => ‘0.00′, //frais de port
    88.         ‘no_shipping’ => ‘0′,
    89.         ‘return’ => ‘http://www.monsite.com/user/?action=user.credits&fin=ok’, // adresse de retour après le paiement
    90.         ‘notify_url’ => ‘http://www.monsite.com/paypal/paypal.php’, // adresse de notification ipn (voir autre tuto sur le site)
    91.         ‘cancel_return’ => ‘http://www.monsite.com/user/?action=user.credits’, // adresse de retour en cas d’annulation du paiement
    92.         ‘no_note’ => ‘1′,
    93.         ‘custom’ => $iduser."/credits/".$title."/".date("Y-m-d H:i:s"), // la variable custom permet de faire passer n’importe quel parametre de votre choix
    94.         ‘currency_code’ => ‘USD’, //Paiement en dollars ici
    95.         ‘tax’ => ‘0.00′,
    96.         ‘lc’ => ‘US’
    97.         );
    98.  
    99.         $encrypted = paypal_encrypt($form);
    100.         return $encrypted;
    101.  
    102.         }
    103.         ?>
  6. Retournons à la page où nous affichons l’article. Il ne reste plus qu’a crypter nos boutons à la demande
    1.  
    2. <form action="https://www.paypal.com/cgi-bin/webscr" method="post">
    3. <input name="cmd" value="_s-xclick" type="hidden" />
    4. <input name="encrypted" value="<? $bouton=paypal_button($vosvariable1,$vosvariable2,$vosvariable3); echo $bouton; ?>" type="hidden" />
    5. <input class="button68" value="Buy" type="submit" />
    6.                                         </form>

    Riens ne vous empêche d’intégrer cela à un for each ou while pour générer x boutons selon vos conditions

Ce tuto est terminé n’hésitez pas à poster un commentaire et si il vous à bien aidé à faire un don :

Si vous désirez que notre entreprise l’installe sur votre site Contactez nous