forked from paypal-examples/docs-examples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.php
117 lines (101 loc) · 3.27 KB
/
index.php
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
require __DIR__ . '/../vendor/autoload.php';
use PaypalServerSdkLib\Authentication\ClientCredentialsAuthCredentialsBuilder;
use PaypalServerSdkLib\Environment;
use PaypalServerSdkLib\PaypalServerSdkClientBuilder;
use PaypalServerSdkLib\Models\Builders\OrderRequestBuilder;
use PaypalServerSdkLib\Models\CheckoutPaymentIntent;
use PaypalServerSdkLib\Models\Builders\PurchaseUnitRequestBuilder;
use PaypalServerSdkLib\Models\Builders\AmountWithBreakdownBuilder;
$PAYPAL_CLIENT_ID = getenv('PAYPAL_CLIENT_ID');
$PAYPAL_CLIENT_SECRET = getenv('PAYPAL_CLIENT_SECRET');
$client = PaypalServerSdkClientBuilder::init()
->clientCredentialsAuthCredentials(
ClientCredentialsAuthCredentialsBuilder::init(
$PAYPAL_CLIENT_ID,
$PAYPAL_CLIENT_SECRET
)
)
->environment(Environment::SANDBOX)
->build();
/**
* Create an order to start the transaction.
* @see https://developer.paypal.com/docs/api/orders/v2/#orders_create
*/
function createOrder($cart)
{
global $client;
$orderBody = [
'body' => OrderRequestBuilder::init(
CheckoutPaymentIntent::CAPTURE,
[
PurchaseUnitRequestBuilder::init(
AmountWithBreakdownBuilder::init(
'USD',
'100.00'
)->build()
)->build()
]
)->build()
];
$apiResponse = $client->getOrdersController()->ordersCreate($orderBody);
return handleResponse($apiResponse);
}
/**
* Capture payment for the created order to complete the transaction.
* @see https://developer.paypal.com/docs/api/orders/v2/#orders_capture
*/
function captureOrder($orderID)
{
global $client;
$captureBody = [
'id' => $orderID
];
$apiResponse = $client->getOrdersController()->ordersCapture($captureBody);
return handleResponse($apiResponse);
}
function handleResponse($response)
{
return [
'jsonResponse' => $response->getResult(),
'httpStatusCode' => $response->getStatusCode()
];
}
$endpoint = $_SERVER['REQUEST_URI'];
if ($endpoint === '/') {
try {
$response = [
"message" => "Server is running"
];
header('Content-Type: application/json');
echo json_encode($response);
} catch (Exception $e) {
echo json_encode(['error' => $e->getMessage()]);
http_response_code(500);
}
}
if ($endpoint === '/api/orders') {
$data = json_decode(file_get_contents('php://input'), true);
$cart = $data['cart'];
header('Content-Type: application/json');
try {
$orderResponse = createOrder($cart);
echo json_encode($orderResponse['jsonResponse']);
} catch (Exception $e) {
echo json_encode(['error' => $e->getMessage()]);
http_response_code(500);
}
}
if (str_ends_with($endpoint, '/capture')) {
$urlSegments = explode('/', $endpoint);
end($urlSegments); // Will set the pointer to the end of array
$orderID = prev($urlSegments);
header('Content-Type: application/json');
try {
$captureResponse = captureOrder($orderID);
echo json_encode($captureResponse['jsonResponse']);
} catch (Exception $e) {
echo json_encode(['error' => $e->getMessage()]);
http_response_code(500);
}
}