Table of Contents
Key Takeaways
Chapter Two
Project Scenario
Database Structure
E-R Diagram:
Database Schema:
User Interface
Project Workflow
Setup PayPal Sandbox
1. Enable PDT and Settings
2. Enable IPN and Settings
2. Create a PayPal Button and PayPal Parameter Settings
PDT Handler Function
Source Code:
Explanation:
IPN Handler Function
Frequently Asked Questions about Registering for PayPal
How can I ensure my PayPal account is secure during registration?
Can I register for a PayPal account without a credit card?
What are the benefits of registering for a PayPal business account over a personal account?
How can I register for a PayPal account if I live outside the United States?
What information do I need to provide when registering for a PayPal account?
How long does it take to register for a PayPal account?
Can I register for more than one PayPal account?
What happens if I forget my PayPal password during registration?
Can I change the information I provided during PayPal registration?
Is there a fee to register for a PayPal account?
Home Web Front-end JS Tutorial REGISTER WITH PAYPAL TUTORIAL (2/3): A Real Register with PayPal Project

REGISTER WITH PAYPAL TUTORIAL (2/3): A Real Register with PayPal Project

Feb 23, 2025 am 09:34 AM

REGISTER WITH PAYPAL TUTORIAL (2/3): A Real Register with PayPal Project

Explain how PayPal works (IPN and PDT process). Chapter One Chapter Three

Key Takeaways

  • The tutorial covers the process of setting up a PayPal account and integrating it with a registration form and database for a real project.
  • The project involves creating a registration form, redirecting users to PayPal for payment, and implementing PayPal IPN as a backup plan in case users close their browser.
  • The tutorial provides detailed instructions on setting up the database structure and user interface, creating a PayPal button, setting up a PayPal Sandbox account, and coding PDT and IPN handlers.
  • The tutorial also answers frequently asked questions about registering for PayPal, including ensuring account security, registering without a credit card, benefits of a business account, and registering from outside the United States.

Chapter Two

This chapter introduces a real project: “registration with payment”, from start to end, for better explaining PayPal account setup and integration with register form and database.

Project Scenario

  1. First, we have a registration form.
  2. After completing the form correctly (all validation passed), user clicks on the ‘Register’ button.
  3. Then redirect to PayPal, user pay for the registration fee.
  4. After paid, PayPal will redirect to result page, and 10 seconds auto redirect back to our website, then PayPal PDT will process the payment record.
  5. But, user may close the browser, so we have to implement PayPal IPN for backup plan.

Database Structure

E-R Diagram:

REGISTER WITH PAYPAL TUTORIAL (2/3): A Real Register with PayPal Project
  1. temp_register table: temporary store user account and user password, wait for payment. If paid, the tuple will be deleted, and move to users table.
  2. payment table: uid is a foreign key referencing to users, to connect user information and payment information.
  3. users table: store users information, token is verification token in confirmation email. If user verified their user account, verified will be set as 1.

Database Schema:

<span>CREATE TABLE IF NOT EXISTS <span>`payment`</span> (
</span><span><span>`payId`</span> int(11) NOT NULL AUTO_INCREMENT,
</span><span><span>`timestamp`</span> bigint(20) DEFAULT NULL,
</span><span><span>`paid`</span> float DEFAULT NULL COMMENT 'user paid amount returned by paypal',
</span><span><span>`bankFee`</span> float DEFAULT NULL,
</span><span><span>`currency`</span> varchar(4) DEFAULT NULL,
</span><span><span>`txnId`</span> varchar(32) DEFAULT NULL COMMENT 'Transaction ID: specify single unique transaction from paypal. if this field is NOT NULL, means this payment has been process already. So if IPN returns to PHP, we can refuse to update our database.',
</span><span><span>`status`</span> varchar(16) DEFAULT NULL,
</span><span><span>`uid`</span> int(11) DEFAULT NULL COMMENT 'FK to users PK',
</span><span>PRIMARY KEY (<span>`payId`</span>),
</span><span>KEY <span>`uid`</span> (<span>`uid`</span>)
</span><span>) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
</span><span>CREATE TABLE IF NOT EXISTS <span>`temp_register`</span> (
</span><span><span>`tRegId`</span> int(11) NOT NULL AUTO_INCREMENT,
</span><span><span>`fullName`</span> varchar(255) DEFAULT NULL,
</span><span><span>`uAcc`</span> varchar(255) DEFAULT NULL,
</span><span><span>`uPwd`</span> varchar(32) DEFAULT NULL,
</span><span>PRIMARY KEY (<span>`tRegId`</span>)
</span><span>) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='prevent unpaid user take uAcc(UNIQUE) in our users table' AUTO_INCREMENT=1 ;
</span><span>CREATE TABLE IF NOT EXISTS <span>`users`</span> (
</span><span><span>`uid`</span> int(11) NOT NULL AUTO_INCREMENT,
</span><span><span>`fullName`</span> varchar(255) DEFAULT NULL,
</span><span><span>`uAcc`</span> varchar(255) NOT NULL,
</span><span><span>`uPwd`</span> varchar(32) NOT NULL,
</span><span><span>`token`</span> varchar(32) DEFAULT NULL,
</span><span><span>`verified`</span> tinyint(1) NOT NULL DEFAULT '0',
</span><span><span>`priviledge`</span> enum('delegate','admin','developer') NOT NULL DEFAULT 'delegate',
</span><span>PRIMARY KEY (<span>`uid`</span>)
</span><span>) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
</span><span>ALTER TABLE <span>`payment`</span>
</span><span>ADD CONSTRAINT <span>`payment_ibfk_1`</span> FOREIGN KEY (<span>`uid`</span>) REFERENCES <span>`users`</span> (<span>`uid`</span>);</span>
Copy after login
Copy after login

User Interface

REGISTER WITH PAYPAL TUTORIAL (2/3): A Real Register with PayPal Project

Project Workflow

  1. Fill the form, and all inputs are validated.
  2. Click Register button, redirect to PayPal.
  3. PayPal Sandbox ‘Pay Now’: REGISTER WITH PAYPAL TUTORIAL (2/3): A Real Register with PayPal Project
  4. Redirect PayPal result page.
  5. Wait for redirect (PDT works) show success/fail page, or close browser (IPN works).

Setup PayPal Sandbox

Before we start coding, we need to setup PayPal Sandbox account first. Suppose you have registered a PayPal developer account, and have created one business user account and one buyer account in Sandbox.REGISTER WITH PAYPAL TUTORIAL (2/3): A Real Register with PayPal Project Then select business account, and click on the ‘Enter Sandbox Test Site’ Button. You can see the main panel pop-out page: REGISTER WITH PAYPAL TUTORIAL (2/3): A Real Register with PayPal Project Then you can see all information and selling preference settings. REGISTER WITH PAYPAL TUTORIAL (2/3): A Real Register with PayPal Project So let’s setup all the three options one by one in order.

1. Enable PDT and Settings

Setup your PDT handler function calling URL. REGISTER WITH PAYPAL TUTORIAL (2/3): A Real Register with PayPal Project

2. Enable IPN and Settings

Setup your IPN handler function calling URL. REGISTER WITH PAYPAL TUTORIAL (2/3): A Real Register with PayPal Project

2. Create a PayPal Button and PayPal Parameter Settings

REGISTER WITH PAYPAL TUTORIAL (2/3): A Real Register with PayPal Project REGISTER WITH PAYPAL TUTORIAL (2/3): A Real Register with PayPal Project After you saving your change, you can see the source code of your PayPal Button:
<span>CREATE TABLE IF NOT EXISTS <span>`payment`</span> (
</span><span><span>`payId`</span> int(11) NOT NULL AUTO_INCREMENT,
</span><span><span>`timestamp`</span> bigint(20) DEFAULT NULL,
</span><span><span>`paid`</span> float DEFAULT NULL COMMENT 'user paid amount returned by paypal',
</span><span><span>`bankFee`</span> float DEFAULT NULL,
</span><span><span>`currency`</span> varchar(4) DEFAULT NULL,
</span><span><span>`txnId`</span> varchar(32) DEFAULT NULL COMMENT 'Transaction ID: specify single unique transaction from paypal. if this field is NOT NULL, means this payment has been process already. So if IPN returns to PHP, we can refuse to update our database.',
</span><span><span>`status`</span> varchar(16) DEFAULT NULL,
</span><span><span>`uid`</span> int(11) DEFAULT NULL COMMENT 'FK to users PK',
</span><span>PRIMARY KEY (<span>`payId`</span>),
</span><span>KEY <span>`uid`</span> (<span>`uid`</span>)
</span><span>) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
</span><span>CREATE TABLE IF NOT EXISTS <span>`temp_register`</span> (
</span><span><span>`tRegId`</span> int(11) NOT NULL AUTO_INCREMENT,
</span><span><span>`fullName`</span> varchar(255) DEFAULT NULL,
</span><span><span>`uAcc`</span> varchar(255) DEFAULT NULL,
</span><span><span>`uPwd`</span> varchar(32) DEFAULT NULL,
</span><span>PRIMARY KEY (<span>`tRegId`</span>)
</span><span>) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='prevent unpaid user take uAcc(UNIQUE) in our users table' AUTO_INCREMENT=1 ;
</span><span>CREATE TABLE IF NOT EXISTS <span>`users`</span> (
</span><span><span>`uid`</span> int(11) NOT NULL AUTO_INCREMENT,
</span><span><span>`fullName`</span> varchar(255) DEFAULT NULL,
</span><span><span>`uAcc`</span> varchar(255) NOT NULL,
</span><span><span>`uPwd`</span> varchar(32) NOT NULL,
</span><span><span>`token`</span> varchar(32) DEFAULT NULL,
</span><span><span>`verified`</span> tinyint(1) NOT NULL DEFAULT '0',
</span><span><span>`priviledge`</span> enum('delegate','admin','developer') NOT NULL DEFAULT 'delegate',
</span><span>PRIMARY KEY (<span>`uid`</span>)
</span><span>) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
</span><span>ALTER TABLE <span>`payment`</span>
</span><span>ADD CONSTRAINT <span>`payment_ibfk_1`</span> FOREIGN KEY (<span>`uid`</span>) REFERENCES <span>`users`</span> (<span>`uid`</span>);</span>
Copy after login
Copy after login
It is easy to find out that the button is actually a form, so we need to POST data using its inputs. When we generate our ‘register’ button, the redirect URL, should contain ‘&cmd=_s-xclick’ and ‘&hosted_button_id=HA9DZBCKXKCL2’. Now, PayPal Sandbox account has been setup. Then start to code your PDT and IPN handlers.

PDT Handler Function

Source Code:

Copy after login

Explanation:

PayPal call PDTHandler() function, then this handler function process _PDT(). As you see, it receives parameters provided by PayPal, from the URL ($_GET). So we POST token and tx back to PayPal, via fsock ssl. Then PayPal will return payment record, and payment result (SUCCESS/FAIL). _PDT() passes those data to _validatePaypal() which saves data into database. Then, page redirect according to the return.

IPN Handler Function

Source Code:

<span>/**
</span><span>* PAYPAL: PDT HANDLER:
</span><span>* ====================
</span><span>* called by PayPal, send tokens back
</span><span>* get payment details and payment result
</span><span>* <span>@return $ret array contains result true/false, and user account or error message
</span></span><span>*/
</span><span>private function _PDT()
</span><span>{
</span><span>// some indexes can not be missing:
</span><span>$ruler = array(
</span><span>'tx', // token from paypal
</span><span>);
</span><span>if(count(array_diff($ruler, array_keys($_GET))))
</span><span>{
</span><span>return array('result' => false, 'error' => 'Index missing ... ', 'index' => $_GET, 'missing' => array_diff($ruler, array_keys($_GET)));
</span><span>}
</span><span>// read the post from PayPal system and add 'cmd'
</span><span>$req = 'cmd=_notify-synch';
</span><span>// get token and prepare request url (send back to paypal)
</span><span>$tx_token = $_GET['tx'];$auth_token = "_PJaHiwRfwMmWzW-9nuPuSguYxC-1d9KpxaasaNANtIvyOcmqY6jXNkRmxW";
</span><span>// $auth_token = "OxDenzKmrWPyEXU0YzIg2zs-VAe7ufCADyjbfxF_RpREL4rLEslZrSa21R4";
</span>
<span>$req .= "&tx=<span><span>$tx_token</span>&at=<span>$auth_token</span>"</span>;
</span>
<span>// post back to PayPal system to validate
</span>
<span>$header = "POST /cgi-bin/webscr HTTP/1.0rn";
</span>
<span>$header .= "Host: www.sandbox.paypal.comrn";
</span><span>// $header .= "Host: www.paypal.comrn";
</span>
<span>$header .= "Content-Type: application/x-www-form-urlencodedrn";
</span><span>$header .= "Content-Length: " . strlen($req) . "rnrn";
</span>
<span>$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30); // open socket
</span><span>// $fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30); // open socket
</span>
<span>if (!$fp)
</span><span>{
</span><span>// HTML FAIL
</span><span>return array('result' => false, 'error' => 'HTTP error ... ');
</span><span>}
</span><span>else
</span><span>{
</span><span>fputs ($fp, $header . $req);
</span><span>// read the body data
</span><span>$res = '';
</span><span>$headerdone = false;
</span><span>while (!feof($fp))
</span><span>{
</span><span>$line = fgets ($fp, 1024);
</span><span>if (strcmp($line, "rn") == 0)
</span><span>{
</span><span>$headerdone = true; // read the header
</span><span>}
</span><span>else if ($headerdone)
</span><span>{
</span><span>$res .= $line; // header has been read. now read the contents
</span><span>}
</span><span>}
</span>
<span>// parse the data
</span><span>$lines = explode("n", $res);
</span><span>$keyarray = array();
</span><span>if (strcmp ($lines[0], "SUCCESS") == 0)
</span><span>{
</span><span>for ($i=1; $i_validatePaypal($keyarray);
</span><span>}
</span><span>// log for manual investigation
</span><span>else if (strcmp ($lines[0], "FAIL") == 0)
</span><span>{
</span><span>// skipped
</span><span>return array('result' => false, 'error' => 'Transaction failed ... ');
</span><span>}
</span><span>}
</span><span>fclose ($fp);
</span><span>return $ret;
</span><span>}</span>
Copy after login

Explanation:

PayPal call IPNHandler() function, then this handler function process _ IPN (). As you see, it receives data provided by PayPal, from the request ($_POST). So we POST payment details back to PayPal, via cURL, this is the handshake process. Then PayPal send back payment result (VERIFIED/INVALID). If it’s verified, _IPN() passes those data to _validatePaypal() which saves data into database.

In the zip file, it contains templates, javascript files, css, bootstrap, jquery, debug plugin and smarty plugin, and all core php source code.

Download Project (344KB)

Frequently Asked Questions about Registering for PayPal

How can I ensure my PayPal account is secure during registration?

Security is a top priority when setting up a PayPal account. To ensure your account is secure, use a strong, unique password that includes a mix of letters, numbers, and special characters. Also, make sure to provide accurate personal information during registration. PayPal uses this information to verify your identity and protect your account from fraudulent activities.

Can I register for a PayPal account without a credit card?

Yes, you can register for a PayPal account without a credit card. During the registration process, you can choose to link a bank account instead of a credit card. However, linking a credit card can provide additional payment options and security features.

What are the benefits of registering for a PayPal business account over a personal account?

A PayPal business account offers several benefits over a personal account. These include the ability to accept payments from customers, access to business loans and cash advances, and the option to create and send invoices. A business account also allows you to operate under a company or group name.

How can I register for a PayPal account if I live outside the United States?

PayPal is available in more than 200 countries and regions around the world. To register for a PayPal account outside the United States, visit the PayPal website for your specific country or region. The registration process is similar to that in the United States.

What information do I need to provide when registering for a PayPal account?

When registering for a PayPal account, you will need to provide your legal name, address, phone number, and email address. You will also need to create a password and choose two security questions to help protect your account.

How long does it take to register for a PayPal account?

The registration process for a PayPal account is quick and easy. It typically takes just a few minutes to complete. However, it may take a few days for PayPal to verify your bank account or credit card information.

Can I register for more than one PayPal account?

Yes, you can register for more than one PayPal account. However, each account must have a unique email address and financial information. You can have one Personal account and one Business account.

What happens if I forget my PayPal password during registration?

If you forget your PayPal password during registration, you can reset it by clicking on the “Forgot your password?” link on the login page. You will then be prompted to enter your email address, and PayPal will send you an email with instructions on how to reset your password.

Can I change the information I provided during PayPal registration?

Yes, you can change the information you provided during PayPal registration. To do this, log in to your PayPal account, go to your Profile, and select the information you want to change.

Is there a fee to register for a PayPal account?

No, there is no fee to register for a PayPal account. However, PayPal does charge fees for certain transactions, such as receiving payments for goods and services or sending money to another country.

The above is the detailed content of REGISTER WITH PAYPAL TUTORIAL (2/3): A Real Register with PayPal Project. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The Evolution of JavaScript: Current Trends and Future Prospects The Evolution of JavaScript: Current Trends and Future Prospects Apr 10, 2025 am 09:33 AM

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

See all articles