Paypal without form

This class allows for one's basic usage of PayPal's services. Easy to use, easy to customize, this code allows for the minimum number of clicks.

Its usage requires one to import the PHP class where needed, and then instantiate its objects like below.

Usage:

1

2

3

4

5

<?

$paypal=new PAYPAL("rm@webstorm.ro");

$paypal->addItem("Super Socks Test","10",0);

$paypal->pay();

?>

Below it's the same code with full class source code included (imported) + testing demo. Please do not try to send money to the demo account. Thank you!

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

<?

 

// This is the class that gets the job done

// Do not alter, unless you know what you are doing.

 

class PAYPAL {

 

public $currency_code="USD"; //you can change with any

public $email="your email here"; //the recipient email

private $callback_url=""; //your callback url here

public $paypal_host="https://www.paypal.com/cgi-bin/webscr";

public $items=array();

private $payment_data=array();

 

public function __construct($email=NULL) {

if (strlen($email)>0) $this->email=$email;

}

 

function getData() {

 

$standard_values=array(

"currency_code"=>$this->currency_code,

"cmd" => "_cart",

"upload" => "1",

"business" => $this->email,

"callback_version" => 61,

"callback_timeout" =>3,

"fallback_shipping_option_name_0" => "Option 1",

"fallback_shipping_option_amount_0" => 0,

"fallback_shipping_option_is_default_0" => 1,

"callback_url" => urlencode($this->callback_url),

"notify_url" => urlencode($this->callback_url),

"cancel_return" => urlencode($GLOBALS["srv"])

);

 

$this->payment_data=$standard_values;

 

if (sizeof($this->items)>0) {

 

$itemIndex=1;

foreach($this->items as $item) {

$this->payment_data["item_name_".$itemIndex]=$item["name"];

$this->payment_data["amount_".$itemIndex]=$item["price"];

$this->payment_data["shipping_".$itemIndex]=$item["shipping"];

$itemIndex+=1;}

}

 

 

}

 

function addItem($name,$price,$shipping) {

if ((strlen($name)>1) &amp;&amp; ($price>0))

$this->items[]=array(

"name"=>$name,

"price"=>number_format($price,2),

"shipping"=>$shipping

);

}

 

function pay() {

 

if (sizeof($this->items)==0) return;

$this->getData();

echo $this->paypal_host."?".http_build_query($this->payment_data);

header("Location: ".$this->paypal_host."?".http_build_query($this->payment_data));

die("");

}

}

 

// Basic Usage

// Please DO EDIT the three lines below, to suit your needs.

// Please notice how the example below has two items

$paypal=new PAYPAL("rm@webstorm.ro");

$paypal->addItem("Super Socks Test","10",0);

$paypal->addItem("Super Socks 2","20",0);

$paypal->pay();

?>

Clicking the DEMO button produces the same result as clicking on some instantiated link. You can safely close that window after analyzing its content.

Newsletter

Sign up for inspiration, news and updates