PayPal with PHP


Create a new PHP file named config.php contains configuration information for paypal paypal as below:

<?php
$config ['authtoken'] = 'Your Token';
$config ['posturl'] = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
$config ['business'] = 'Your Email Business';
$config ['returnurl'] = 'http://localhost:8082/paypalwithphp/success.php';
?>

Create a new PHP file named payment.php. This page contains information the products will submit via PayPal

<?php
require_once 'config.php';
?>
<form method="post" action="<?php echo $config ['posturl']; ?>">

	<input type="hidden" name="upload" value="1" />
	<input type="hidden" name="return" value="<?php echo $config ['returnurl']; ?>" />
	<input type="hidden" name="cmd" value="_cart" />
	<input type="hidden" name="business" value="<?php echo $config ['business']; ?>" />

	<!-- Product 1  -->
	<input type="hidden" name="item_name_1" value="Product 1" />
	<input type="hidden" name="item_number_1" value="p1" />
	<input type="hidden" name="amount_1" value="2" />
	<input type="hidden" name="quantity_1" value="3" />

	<!-- Product 2  -->
	<input type="hidden" name="item_name_2" value="Product 2" />
	<input type="hidden" name="item_number_2" value="p2" />
	<input type="hidden" name="amount_2" value="3" />
	<input type="hidden" name="quantity_2" value="4" />

	<!-- Product 3  -->
	<input type="hidden" name="item_name_3" value="Product 3" />
	<input type="hidden" name="item_number_3" value="p3" />
	<input type="hidden" name="amount_3" value="3" />
	<input type="hidden" name="quantity_3" value="2" />

	<input type="image" src="https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif">

</form>




Create new PHP file named paypal.php. This file contains methods for get payment information after payment

<?php
function getListProducts($result) {
	$i = 1;
	$data = array ();
	foreach ( $result as $key => $value ) {
		if (0 === strpos ( $key, 'item_number' )) {
			$product = array (
					'item_number' => $result ['item_number' . $i],
					'item_name' => $result ['item_name' . $i],
					'quantity' => $result ['quantity' . $i],
					'mc_gross' => $result ['mc_gross_' . $i]
			);
			array_push ( $data, $product );
			$i ++;
		}
	}
	return $data;
}

function verifyWithPayPal($tx) {
	require_once 'config.php';
	$token = $config ['authtoken'];
	$paypal_url = $config ['posturl'] . '?cmd=_notify-synch&tx=' . $tx . '&at=' . $token;
	$curl = curl_init ( $paypal_url );
	$data = array (
			"cmd" => "_notify-synch",
			"tx" => $tx,
			"at" => $token
	);
	$data_string = json_encode ( $data );
	curl_setopt ( $curl, CURLOPT_HEADER, 0 );
	curl_setopt ( $curl, CURLOPT_POST, 1 );
	curl_setopt ( $curl, CURLOPT_POSTFIELDS, $data_string );
	curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, 0 );
	curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
	$headers = array (
			'Content-Type: application/x-www-form-urlencoded',
			'Host: www.sandbox.paypal.com',
			'Connection: close'
	);
	curl_setopt ( $curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 );
	curl_setopt ( $curl, CURLOPT_HTTPHEADER, $headers );
	$response = curl_exec ( $curl );
	$lines = explode ( "\n", $response );
	$keyarray = array ();
	if (strcmp ( $lines [0], "SUCCESS" ) == 0) {
		for($i = 1; $i < count ( $lines ); $i ++) {
			list ( $key, $val ) = explode ( "=", $lines [$i] );
			$keyarray [urldecode ( $key )] = urldecode ( $val );
		}
		$keyarray ['listProducts'] = getListProducts ( $keyarray );
	}
	return $keyarray;
}
?>




Create new PHP file named success.php. This page shows the payment information you just created

<?php
error_reporting ( 0 );
require_once 'paypal.php';
$result = verifyWithPayPal($_REQUEST ['tx']);
?>
First name: <?php echo $result['first_name']; ?>
<br>
Last name: <?php echo $result['last_name']; ?>
<br>
Payment status: <?php echo $result['payment_status']; ?>
<br>
Business: <?php echo $result['business']; ?>
<br>
Payer email: <?php echo $result['payer_email']; ?>
<br>
Payment gross: <?php echo $result['payment_gross']; ?>
<br>
Currency: <?php echo $result['mc_currency']; ?>
<br>
Address street: <?php echo $result['address_street']; ?>
<br>
Address city: <?php echo $result['address_city']; ?>
<br>
Address state: <?php echo $result['address_state']; ?>
<br>
Address country: <?php echo $result['address_country']; ?>
<br>
Transaction id: <?php echo $result['txn_id']; ?>
<br>
Payment fee: <?php echo $result['payment_fee']; ?>
<br><br>
<b>List Products</b>
<?php
foreach ( $result['listProducts'] as $product ) {
	echo '<br>Product Id: ' . $product['item_number'];
	echo '<br>Product Name: ' . $product['item_name'];
	echo '<br>Quantity: ' . $product['quantity'];
	echo '<br>Gross: ' . $product['mc_gross'];
	echo '<br>==================================';
}
?>




PayPal Configuration

Payment Page

Get Payment Information

Success Page

I recommend you refer to the books below to learn more about the knowledge in this article: