Skip to content

Commit c070e85

Browse files
committed
Initial commit
0 parents  commit c070e85

11 files changed

+365
-0
lines changed

Diff for: .editorconfig

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
; This file is for unifying the coding style for different editors and IDEs.
2+
; More information at http://editorconfig.org
3+
4+
root = true
5+
6+
[*]
7+
indent_style = space
8+
indent_size = 4
9+
charset = "utf-8"
10+
end_of_line = lf
11+
insert_final_newline = true
12+
trim_trailing_whitespace = true

Diff for: .gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vendor/
2+
.vscode/

Diff for: composer.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"require": {
3+
"paypal/rest-api-sdk-php": "^1.13"
4+
}
5+
}

Diff for: composer.lock

+114
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: create_payments.sql

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CREATE TABLE IF NOT EXISTS `payments` (
2+
`id` int(6) NOT NULL AUTO_INCREMENT,
3+
`transaction_id` varchar(20) NOT NULL,
4+
`payment_amount` decimal(7,2) NOT NULL,
5+
`payment_status` varchar(25) NOT NULL,
6+
`invoice_id` varchar(25) NOT NULL,
7+
`createdtime` datetime NOT NULL,
8+
PRIMARY KEY (`id`)
9+
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

Diff for: src/bootstrap.php

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
use PayPal\Rest\ApiContext;
4+
use PayPal\Auth\OAuthTokenCredential;
5+
6+
require '../vendor/autoload.php';
7+
8+
// For test payments we want to enable the sandbox mode. If you want to put live
9+
// payments through then this setting needs changing to `false`.
10+
$enableSandbox = true;
11+
12+
// PayPal settings. Change these to your account details and the relevant URLs
13+
// for your site.
14+
$paypalConfig = [
15+
'client_id' => 'your-paypal-api-client-id',
16+
'client_secret' => 'your-paypal-api-client-secret',
17+
'return_url' => 'http://example.com/response.php',
18+
'cancel_url' => 'http://example.com/payment-cancelled.html'
19+
];
20+
21+
// Database settings. Change these for your database configuration.
22+
$dbConfig = [
23+
'host' => 'localhost',
24+
'username' => 'user',
25+
'password' => 'secret',
26+
'name' => 'example_database'
27+
];
28+
29+
$apiContext = getApiContext($paypalConfig['client_id'], $paypalConfig['client_secret'], $enableSandbox);
30+
31+
/**
32+
* Set up a connection to the API
33+
*
34+
* @param string $clientId
35+
* @param string $clientSecret
36+
* @param bool $enableSandbox Sandbox mode toggle, true for test payments
37+
* @return \PayPal\Rest\ApiContext
38+
*/
39+
function getApiContext($clientId, $clientSecret, $enableSandbox = false)
40+
{
41+
$apiContext = new ApiContext(
42+
new OAuthTokenCredential($clientId, $clientSecret)
43+
);
44+
45+
$apiContext->setConfig([
46+
'mode' => $enableSandbox ? 'sandbox' : 'live'
47+
]);
48+
49+
return $apiContext;
50+
}

Diff for: src/index.html

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
<title>PayPal REST API Example</title>
8+
</head>
9+
<body>
10+
11+
<form class="paypal" action="request.php" method="post" id="paypal_form">
12+
<input type="hidden" name="item_number" value="123456" / >
13+
<input type="submit" name="submit" value="Submit Payment"/>
14+
</form>
15+
16+
</body>
17+
</html>

Diff for: src/payment-cancelled.html

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
<title>PayPal REST API Example - Cancelled</title>
8+
</head>
9+
<body>
10+
11+
<h1>Payment Cancelled</h1>
12+
13+
</body>
14+
</html>

Diff for: src/payment-successful.html

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
<title>PayPal REST API Example - Success</title>
8+
</head>
9+
<body>
10+
11+
<h1>Successful Payment</h1>
12+
13+
</body>
14+
</html>

Diff for: src/request.php

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
use PayPal\Api\Amount;
4+
use PayPal\Api\Payer;
5+
use PayPal\Api\Payment;
6+
use PayPal\Api\RedirectUrls;
7+
use PayPal\Api\Transaction;
8+
9+
require 'bootstrap.php';
10+
11+
if (empty($_POST['item_number'])) {
12+
throw new Exception('This script should not be called directly, expected post data');
13+
}
14+
15+
$payer = new Payer();
16+
$payer->setPaymentMethod('paypal');
17+
18+
// Set some example data for the payment.
19+
$currency = 'GBP';
20+
$amountPayable = 10.00;
21+
$invoiceNumber = uniqid();
22+
23+
$amount = new Amount();
24+
$amount->setCurrency($currency)
25+
->setTotal($amountPayable);
26+
27+
$transaction = new Transaction();
28+
$transaction->setAmount($amount)
29+
->setDescription('Some description about the payment being made')
30+
->setInvoiceNumber($invoiceNumber);
31+
32+
$redirectUrls = new RedirectUrls();
33+
$redirectUrls->setReturnUrl($paypalConfig['return_url'])
34+
->setCancelUrl($paypalConfig['cancel_url']);
35+
36+
$payment = new Payment();
37+
$payment->setIntent('sale')
38+
->setPayer($payer)
39+
->setTransactions([$transaction])
40+
->setRedirectUrls($redirectUrls);
41+
42+
try {
43+
$payment->create($apiContext);
44+
} catch (Exception $e) {
45+
throw new Exception('Unable to create link for payment');
46+
}
47+
48+
header('location:' . $payment->getApprovalLink());
49+
exit(1);

Diff for: src/response.php

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
use PayPal\Api\Payment;
4+
use PayPal\Api\PaymentExecution;
5+
6+
require 'bootstrap.php';
7+
8+
if (empty($_GET['paymentId']) || empty($_GET['PayerID'])) {
9+
throw new Exception('The response is missing the paymentId and PayerID');
10+
}
11+
12+
$paymentId = $_GET['paymentId'];
13+
$payment = Payment::get($paymentId, $apiContext);
14+
15+
$execution = new PaymentExecution();
16+
$execution->setPayerId($_GET['PayerID']);
17+
18+
try {
19+
// Take the payment
20+
$payment->execute($execution, $apiContext);
21+
22+
try {
23+
$db = new mysqli($dbConfig['host'], $dbConfig['username'], $dbConfig['password'], $dbConfig['name']);
24+
25+
$payment = Payment::get($paymentId, $apiContext);
26+
27+
$data = [
28+
'transaction_id' => $payment->getId(),
29+
'payment_amount' => $payment->transactions[0]->amount->total,
30+
'payment_status' => $payment->getState(),
31+
'invoice_id' => $payment->transactions[0]->invoice_number
32+
];
33+
if (addPayment($data) !== false && $data['payment_status'] === 'approved') {
34+
// Payment successfully added, redirect to the payment complete page.
35+
header('location:payment-successful.html');
36+
exit(1);
37+
} else {
38+
// Payment failed
39+
40+
}
41+
42+
} catch (Exception $e) {
43+
// Failed to retrieve payment from PayPal
44+
45+
}
46+
47+
} catch (Exception $e) {
48+
// Failed to take payment
49+
50+
}
51+
52+
/**
53+
* Add payment to database
54+
*
55+
* @param array $data Payment data
56+
* @return int|bool ID of new payment or false if failed
57+
*/
58+
function addPayment($data)
59+
{
60+
global $db;
61+
62+
if (is_array($data)) {
63+
$stmt = $db->prepare('INSERT INTO `payments` (transaction_id, payment_amount, payment_status, invoice_id, createdtime) VALUES(?, ?, ?, ?, ?)');
64+
$stmt->bind_param(
65+
'sdsss',
66+
$data['transaction_id'],
67+
$data['payment_amount'],
68+
$data['payment_status'],
69+
$data['invoice_id'],
70+
date('Y-m-d H:i:s')
71+
);
72+
$stmt->execute();
73+
$stmt->close();
74+
75+
return $db->insert_id;
76+
}
77+
78+
return false;
79+
}

0 commit comments

Comments
 (0)