-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaypal-transaction-complete.php
163 lines (140 loc) · 4.56 KB
/
paypal-transaction-complete.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
use PayPalCheckoutSdk\Orders\OrdersGetRequest;
use PayPalCheckoutSdk\Core\ProductionEnvironment;
use PayPalCheckoutSdk\Core\SandboxEnvironment;
use PayPalCheckoutSdk\Core\PayPalHttpClient;
function failWithMsg($msg)
{
$output = new stdClass();
$output->status = 'NOK';
$output->error = $msg;
echo json_encode($output);
exit();
}
function success($paypal, $riders)
{
$output = new stdClass();
$output->status = 'OK';
$paymentData = new stdClass();
$paymentData->id = $paypal->id;
$paymentData->amount = $paypal->purchase_units[0]->amount;
$output->paymentData = $paymentData;
$output->riders = $riders;
echo json_encode($output);
exit();
}
require_once('RegistrationSaver.php');
header('Content-Type: application/json');
/***********************************************************************************
* Request stuff
**********************************************************************************/
if (!isset($_GET['key'])) {
failWithMsg('No event key given');
}
try {
$config = PaymentConfigList::getConfig($_GET['key']);
} catch (Exception $e) {
failWithMsg("Bad key: '$key'");
}
$_POST = json_decode(file_get_contents('php://input'), true);
$orderId = isset($_POST['orderID']) ? $_POST['orderID'] : null;
if (!$orderId && $config->paymentType == PaymentConfigList::PAYMENT_MANDATORY) {
// Payment doesn't seem to have worked
failWithMsg('No order id given');
}
if ($orderId && $config->paymentType == PaymentConfigList::PAYMENT_NONE) {
// Payment info given when none is requested
failWithMsg("Got payment info when none was requested. Order id: '$orderId'");
}
$registrarDetails = isset($_POST['registrarDetails']) ? $_POST['registrarDetails'] : null;
if (!$registrarDetails) {
failWithMsg('No registrar details given');
}
$riderDetails = isset($_POST['riderDetails']) ? $_POST['riderDetails'] : null;
if (!$riderDetails) {
failWithMsg('No rider details given');
}
// Necessary?
$orderDetails = isset($_POST['orderDetails']) ? $_POST['orderDetails'] : null;
/***********************************************************************************
* Log all request data before processing
**********************************************************************************/
$logPath = realpath(getcwd(). '/'. $config->logFile);
$fh = fopen($logPath, 'a');
fclose($fh);
$rawLog = file_get_contents($logPath);
if (!$rawLog) {
$log = [];
} else {
$log = json_decode($rawLog);
}
$entry = new stdClass();
$entry->date = date('Y-m-d H:i:s');;
$entry->orderId = $orderId;
$entry->registrarDetails = $registrarDetails;
$entry->riderDetails = $riderDetails;
$entry->orderDetails = $orderDetails;
$log[] = $entry;
file_put_contents($config->logFile, json_encode($log, JSON_PRETTY_PRINT));
/***********************************************************************************
* Paypal stuff
**********************************************************************************/
if ($orderId) {
if (REGISTRATION_USE_PRODUCTION) {
$client = new PayPalHttpClient(
new ProductionEnvironment(
$config->paypalClientId,
$config->paypalSecret
));
} else {
$client = new PayPalHttpClient(
new SandboxEnvironment(
$config->paypalClientId,
$config->paypalSecret
));
}
$paypalValidationResponse = $client->execute(new OrdersGetRequest($orderId));
if ($paypalValidationResponse->result->status !== 'COMPLETED') {
// Payment doesn't seem to have worked
failWithMsg('Payment not found');
}
$paymentDetails = $paypalValidationResponse->result;
} else {
$paymentDetails = null;
}
/***********************************************************************************
* Spreadsheet stuff
**********************************************************************************/
$errorMessage = "";
if (!file_exists(CREDENTIALS_PATH)) {
$errorMessage = "No token file";
} else {
try {
$accessToken = json_decode(file_get_contents(CREDENTIALS_PATH), true);
} catch (Exception $e) {
$errorMessage = "Could not decode the Google API token";
}
}
if ($errorMessage) {
failWithMsg($errorMessage);
}
$client = Helpers::getGoogleClientForWeb($accessToken);
$sheetsService = new Google_Service_Sheets($client);
$logger = new Logger();
$spreadsheetId = $config->spreadSheetId;
try {
RegistrationSaver::save(
$sheetsService,
$logger,
$spreadsheetId,
$paymentDetails,
$registrarDetails,
$riderDetails,
$config->competitions,
$config->additionalTextFields
);
} catch (Google_Service_Exception $e) {
failWithMsg($e->getMessage());
}
// All good!
success($paymentDetails, $riderDetails);