PHP integrated PayPal payment

Source: Internet
Author: User

PHP integrated PayPal payment

Simply sort out the PHP project and integrate the PayPal payment function.

1. form construction:

1

2

3

4

5

6

7

8

9

10

11

12

<Form method = "post" name = "form" action = "https://www.paypal.com/cgi-bin/webscr">

<Input type = "hidden" name = "rm" value = "2"/>

<Input type = "hidden" name = "cmd" value = "_ xclick"/>

<Input type = "hidden" name = "business" value = "merchant account"/>

<Input type = "hidden" name = "return" value = ""/>

<Input type = "hidden" name = "cancel_return" value = "cancel return URL"/>

<Input type = "hidden" name = "yy_url" value = "the URL used to receive the instant payment notification sent by PayPal, which must be a valid URL"/>

<Input type = "hidden" name = "item_name" value = "item name"/>

<Input type = "hidden" name = "item_number" value = "can be used to track transfer variables for purchase or donation and send them back to you upon payment completion"/>

<Input type = "hidden" name = "amount" value = "item price (total price of all items in the shopping cart"/>

<Input type = "hidden" name = "currency_code" value = "currency"/>

</Form>

Ii. IPN Verification

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

<? Php

Class paypal {

Var $ ipn_data = array (); // array contains the POST values for IPN

Var $ fields = array (); // parameters of the form submitted to Paypal, for example, item_name = iPhone 6, quantity = 1, amount = 499, currency_code = USD, etc,

// Initialization is required before IPN verification is called.

Var $ paypal_url = 'https: // response

  

Function validate_ipn () {// verify IPN

// Parse the paypal URL

$ Url_par = parse_url ($ this-> paypal_url );

// Generate the post string from the _ POST vars aswell as load

// _ POST vars into an arry so we can play with them from the calling

// Script.

$ Post_str = '';

Foreach ($ _ POST as $ field => $ value ){

$ This-> ipn_data ["$ field"] = $ value;

$ Post_str. = $ field. '='. urlencode (stripslashes ($ value )).'&';

}

$ Post_str. = "cmd = _ policy-validate"; // append ipn command

// Open the connection to paypal

$ Fp = fsockopen ($ url_par [host], "80", $ errnum, $ errstr, 30 );

If (! $ Fp ){

// Cocould not open the connection.

Return false;

} Else {

// Post the data back to paypal

Fputs ($ fp, "POST". $ url_par [path]. "HTTP/1.1 \ r \ n ");

Fputs ($ fp, "Host:". $ url_par [host]. "\ r \ n ");

Fputs ($ fp, "Content-type: application/x-www-form-urlencoded \ r \ n ");

Fputs ($ fp, "Content-length:". strlen ($ post_str). "\ r \ n ");

Fputs ($ fp, "Connection: close \ r \ n ");

Fputs ($ fp, $ post_str. "\ r \ n ");

// Loop through the response from the server and append to variable

While (! Feof ($ fp )){

$ This-> validate_ipn_response. = fgets ($ fp, 1024 );

}

Fclose ($ fp); // close connection

}

If (eregi ("VERIFIED", $ this-> validate_ipn_response )){

Return true;

} Else {

Return false;

}

}

}

?>

The above is all the content of this article. I hope you will like it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.