Skip to content

Instantly share code, notes, and snippets.

@tetafro
Created August 2, 2016 18:07
Show Gist options
  • Save tetafro/e609163c825776ff3cc651027c18c3b1 to your computer and use it in GitHub Desktop.
Save tetafro/e609163c825776ff3cc651027c18c3b1 to your computer and use it in GitHub Desktop.
PayPal PHP checkout example

Description

PayPal checkout example in PHP. After payment is done PayPal IPN service sends notification to notify_url (or it can be set in PayPal profile).

<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
<title>PayPal shop</title>
</head>
<body>
<table>
<tr>
<th>Товар</th>
<th>Цена</th>
<th>Купить</th>
</tr>
<tr>
<td>Коробка</td>
<td>10$</td>
<td>
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" name="form-pp">
<input type="hidden" name="business" value="[email protected]">
<input type="hidden" name="image_url" value="http://paypal.local/static/logo.png">
<input type="hidden" name="charset" value="utf8">
<input type="hidden" name="item_name" value="Коробка">
<input type="hidden" name="item_number" value="1">
<input type="hidden" name="amount" value="10">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="no_shipping" value="1">
<input type="hidden" name="return" value="http://10.10.10.10/payment-return.php">
<input type="hidden" name="rm" value="2">
<!-- This can be set in PayPal profile -->
<!-- <input type="hidden" name="notify_url" value="http://10.10.10.10/payment-listner.php"> -->
<input type="hidden" name="cancel_return" value="http://10.10.10.10/payment-fail.php">
<input type="submit" value="buy">
</form>
</td>
</tr>
</table>
</body>
</html>
<?php
echo 'The transaction has failed';
<?php
$postdata = ''
$post_json = [];
foreach($_POST as $key => $value) {
$postdata .= $key . '=' . urlencode($value) . '&';
}
$postdata .= 'cmd=_notify-validate';
$curl = curl_init('https://www.sandbox.paypal.com/cgi-bin/webscr');
curl_setopt ($curl, CURLOPT_HEADER, 0);
curl_setopt ($curl, CURLOPT_POST, 1);
curl_setopt ($curl, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($curl, CURLOPT_SSL_VERIFYHOST, 1);
$response = curl_exec($curl);
if($response == 'VERIFIED') {
file_put_contents('log.txt', $_POST['txn_id'].''\n', FILE_APPEND);
}
curl_close($curl);
<?php
$txn_string = file_get_contents('log.txt');
$txn_array = explode("\n", $txn_string);
if(in_array($_POST['txn_id'], $txn_array))
echo 'Success';
else
echo 'Unknown transaction';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment