Securing PayPal Checkout
Securing PayPal Checkout
Securing PayPal Checkout
Using the openssl program, enter the following command to generate your private key. The command
generates a 1024-bit RSA private key that is stored in the file my-prvkey.pem:
The public certificate must be in PEM format. To generate your certificate, enter the following openssl
command, which generates a public certificate in the file my-pubcert.pem:
openssl req -new -key my-prvkey.pem -x509 -days 365 -out my-pubcert.pem
3. In the Selling Preferences column, click the Encrypted Payment Settings link.
4. Scroll down the page to the Your Public Certificates section, and click the Add button.
5. Click the Browse button, and select the public certificate that you want to upload to PayPal from
your local computer.
After your public certificate is uploaded successfully, it appears in the Your Public Certificates
section of the Website Payment Certificates page.
You need the certificate ID that PayPal assigned to encrypt your payment buttons by using the
Encrypted Website Payments software provided by PayPal.
3. In the Seller Preferences column, click the Encrypted Payment Settings link.
5. Click the Download button, and save the file in a secure location on your local computer.
require_once(“./PayPalEWP.class.php");
/** For a list of thrown exceptions, refer to the bottom of PayPalEWP.class.php **/
try {
if(!isset($discountAmt)){
$discountAmt='0.00';
}
/** set standard PayPal variables in an array **/
$fields = array("cmd" => "_xclick",
"business" => $paypal_account,
"item_name" => $item_name,
"amount" => $totalcost,
"discount_amount" => $discountAmt,
"on0" => "Transaction ID#",
"on1" => "User ID#",
"os0" => $yourorderid,
"os1" => $loggedinuserid,
"no_shipping" => "1",
"return" => HTTP_PATH."/ComfirmPayment.php",
"cancel_return" => HTTP_PATH."/Cancel.php",
"notify_url" => HTTP_PATH."/Notify.php",
"no_note" => "1",
"currency_code" => "USD",
"bn" => "PP-BuyNowBF"
);
/** Temporary directory for file storage. Make sure it's writable **/
$paypal->setTempDirectory("/tmp");
/** The user certificate and private key you generated **/
$paypal->setUserCertificate("site-public.pem", "site.pem");
PayPalEWP.class.php
<?php
/**
* Class for creating Paypal Encrypted Website Payment buttons
*
* Uses given user certificate, user private key, PayPal
* certificate, and PayPal certificate ID to create form
* content from the given array and then encrypt using
* the above details according to the PayPal EWP standard.
*
* @author Joshua Gilman
* @version 1.0.0
* @package PayPalLib
*
*/
class PayPalEWP
{
private $userCertificate;
private $userCertificateFile;
private $userKey;
private $userKeyFile;
private $paypalCertificate;
private $paypalCertificateFile;
private $paypalCertificateID;
private $tempDirectory;
/**
* Takes the given form fields and encrypts
* them according to the PayPal EWP standard
* returning the encrypted button code.
*
/** Start off the form data with the PayPal certificate ID **/
$plainText .= "cert_id=" . $this->paypalCertificateID;
/** Create the form data by separating each value set by a new line character **/
foreach($fields as $key => $value)
{
$plainText .= "\n{$key}={$value}";
}
/** Create a temporary file to begin storing our data as we process it **/
$dataFile = tempnam($this->tempDirectory, "data");
/** First create a file for storing the plain text value of the form data **/
$fh = fopen($dataFile . "_plain.txt", 'wb');
fwrite($fh, $plainText);
fclose($fh);
/** Parse the signed file between the header and content **/
$signedText = explode("\n\n", file_get_contents($dataFile . "_signed.txt"));
/** Save only the content but base64 decode it first **/
$fh = fopen($dataFile . "_signed.txt", 'wb');
fwrite($fh, base64_decode($signedText[1]));
fclose($fh);
/** Now encrypt the signed file we just wrote to using the PayPal certificate **/
if (!openssl_pkcs7_encrypt($dataFile . "_signed.txt", $dataFile . "_enc.txt", $this-
>paypalCertificate, array(), PKCS7_BINARY))
{
throw new encryptFailedException($dataFile . "_signed.txt", $this-
>paypalCertificateFile);
}
/** Parse the encrypted file between header and content and grab the content **/
$encryptedData = explode("\n\n", file_get_contents($dataFile . "_enc.txt"));
$encText = $encryptedData[1];
/**
* Sets the user certificate to be used with encryption.
*
* @param String $userCertificateFile
* @param String $userKeyFile
* @return Boolean
*/
/** Load the resource handles for both the user certificate and key **/
$cert = openssl_x509_read(file_get_contents($this->userCertificateFile));
$key = openssl_get_privatekey(file_get_contents($this->userKeyFile));
/** Make sure this key and certificate belong together **/
if (!openssl_x509_check_private_key($cert, $key))
{
throw new invalCertKey($this->userCertificateFile, $this->userKeyFile);
}
return TRUE;
}
/**
return TRUE;
}
/**
* Sets the PayPal certificate ID to be used with encryption.
*
* @param String $paypalCertificateID
*/
public function setPaypalCertificateID($paypalCertificateID)
{
$this->paypalCertificateID = $paypalCertificateID;
}
/**
* Sets the temporary directory the encryption process will use.
*
* @param String $tempDirectory
* @return Boolean
*/
public function setTempDirectory($tempDirectory)
return TRUE;
}
}
/** Below are defined exceptions used by the PayPalEWP class **/
/**
* Thrown when an invalid user certificate is used.
*
*/
class invalUserCertException extends Exception
{
private $certFile;
/**
* Thrown when an invalid PayPal certificate is given
*
*/
class invalPaypalCertException extends Exception
{
private $paypalCertFile;
/**
* Thrown when an invalid user key is given
*
*/
class invalKeyException extends Exception
{
private $keyFile;
/**
* Thrown when an invalid user certificate is given
*
*/
class invalCertKey extends Exception
{
private $certFile;
/**
* Thrown when an invalid temporary directory is given
*
*/
class invalTempDirectory extends Exception
{
private $tmpDir;
/**
* Thrown when the signing of a file using a user certificate and user key failed
*
*/
class signFailedException extends Exception
{
private $contentFile;
private $certFile;
private $keyFile;
/**
* Thrown when the encryption of a file using a certificate fails
*
*/
class encryptFailedException extends Exception
{
private $contentFile;
private $certFile;