Create PayPal certificates following instructions: https://developer.paypal.com/docs/classic/paypal-payments-standard/integration-guide/encryptedwebpayments/#id08A3I0P20E9
In PHP:
define('OPENSSL', '/usr/bin/openssl');
class PayPal {
public function __construct() {
$this->key_file = 'my-prvkey.pem';
$this->cert_file = 'my?pubcert.pem';
$this->paypal_key = 'paypal_cert.pem';
$this->button = array(
'cert_id' => 'YOUR CERT ID',
'cmd' => '_xclick',
'business' => 'YOUR PAYPAL EMAIL',
'lc' => 'US',
'item_name' => 'ITEM NAME',
'amount' => 'X',
'currency_code' => 'USD',
'button_subtype' => 'services',
'no_note' => '0',
'bn' => 'PP-BuyNowBF:btn_buynow_LG.gif:NonHostedGuest'
);
}
public function create_form($shipping) {
$this->button['shipping'] = $shipping;
return '<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top"><input name="cmd" type="hidden" value="_s-xclick" />
<input name="encrypted" type="hidden" value="'.$this->encrypt().'" />
<input type="image" src="https://www.paypalobjects.com/en_AU/i/btn/btn_buynow_LG.gif" border="0" name="submit" alt="PayPal — The safer, easier way to pay online.">
<img src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" alt="" width="1" height="1" border="0" /></form>';
}
private function encrypt() {
$data = '';
foreach ($this->button as $key => $value) {
if ($value) $data .= "$key=$value\n";
}
$cmd = '('.OPENSSL." smime -sign -signer {$this->cert_file} -inkey {$this->key_file} " .
"-outform der -nodetach -binary <<_EOF_\n{$data}\n_EOF_\n) | " .
OPENSSL." smime -encrypt -des3 -binary -outform pem {$this->paypal_key}";
exec($cmd, $output, $error);
return implode("\n", $output);
}
}
Then to output the dynamic PayPal form:
$paypal = new Paypal();
echo $paypal->create_form(20);