Created
August 20, 2014 11:04
-
-
Save deanc/d59081665da519847884 to your computer and use it in GitHub Desktop.
A simple class to create a paypal secure button
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace SkinBootstrap\Utility; | |
class Paypal { | |
public static function createButton($id, $name, $price, $debug = false) { | |
$username = PAYPAL_API_USERNAME; | |
$password = PAYPAL_API_PASSWORD; | |
$signature = PAYPAL_API_SIGNATURE; | |
if($debug) { | |
$username = PAYPAL_SANDBOX_API_USERNAME; | |
$password = PAYPAL_SANDBOX_API_PASSWORD; | |
$signature = PAYPAL_SANDBOX_API_SIGNATURE; | |
} | |
$sendPayData = array( | |
"METHOD" => "BMCreateButton", | |
"VERSION" => "65.2", | |
"USER" => $username, | |
"PWD" => $password, | |
"SIGNATURE" => $signature, | |
"BUTTONCODE" => "ENCRYPTED", | |
"BUTTONTYPE" => "BUYNOW", | |
"BUTTONSUBTYPE" => "PRODUCTS", | |
"BUTTONCOUNTRY" => "GB", | |
"BUTTONIMAGE" => "reg", | |
"BUYNOWTEXT" => "BUYNOW", | |
"L_BUTTONVAR1" => "item_number=$id", | |
"L_BUTTONVAR2" => "item_name=$name", | |
"L_BUTTONVAR3" => "amount=$price", | |
"L_BUTTONVAR4" => "currency_code=USD", | |
"L_BUTTONVAR5" => "no_shipping=1", | |
"L_BUTTONVAR6" => "no_note=1", | |
"L_BUTTONVAR7" => "notify_url=http://www.abc.co.uk/paypal/ipn.php", | |
"L_BUTTONVAR8" => "cancel_return=http://www.abc.co.uk/paypal/thanks", | |
"L_BUTTONVAR9" => "return=http://www.abc.co.uk/paypal/return.php" | |
); | |
$curl = curl_init(); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); | |
$url = 'https://api-3t.paypal.com/nvp?'; | |
if($debug) { | |
$url = 'https://api-3t.sandbox.paypal.com/nvp?'; | |
} | |
curl_setopt($curl, CURLOPT_URL, $url.http_build_query($sendPayData)); | |
$nvpPayReturn = curl_exec($curl); | |
if($debug) { | |
var_dump($nvpPayReturn); | |
} | |
curl_close($curl); | |
parse_str($nvpPayReturn, $stringBits); | |
if($stringBits['ACK'] == 'Success') { | |
return $stringBits['WEBSITECODE']; | |
} | |
return null; | |
} | |
public static function renderButton($debugMode, $code) { | |
if($debugMode) { | |
//$code = str_replace('www.paypal.com', 'sandbox.paypal.com', $code); | |
} | |
return $code; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment