This repository has been archived by the owner on Sep 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.php
executable file
·81 lines (68 loc) · 2.5 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
<?php
require 'vendor/autoload.php';
require_once("includes/braintree_init.php");
$gateway = new Braintree_Gateway([
'environment' => getenv('BT_ENVIRONMENT'),
'merchantId' => getenv('BT_MERCHANT_ID'),
'publicKey' => getenv('BT_PUBLIC_KEY'),
'privateKey' => getenv('BT_PRIVATE_KEY')
]);
$app = new \Slim\Slim();
$app->config([
'templates.path' => 'templates',
]);
$app->get('/', function () use ($app) {
$app->redirect('/checkouts');
});
$app->get('/checkouts', function () use ($app, $gateway) {
$app->render('checkouts/new.php', [
'client_token' => $gateway->clientToken()->generate(),
]);
});
$app->post('/checkouts', function () use ($app, $gateway) {
$result = $gateway->transaction()->sale([
"amount" => $app->request->post('amount'),
"paymentMethodNonce" => $app->request->post('payment_method_nonce'),
'options' => [
'submitForSettlement' => True
]
]);
if($result->success || $result->transaction) {
$app->redirect('/checkouts/' . $result->transaction->id);
} else {
$errorString = "";
foreach($result->errors->deepAll() AS $error) {
$errorString .= 'Error: ' . $error->code . ": " . $error->message . "\n";
}
$_SESSION["errors"] = $errorString;
$app->redirect('/checkouts');
}
});
$app->get('/checkouts/:transaction_id', function ($transaction_id) use ($app, $gateway) {
$transaction = $gateway->transaction()->find($transaction_id);
$transactionSuccessStatuses = [
Braintree\Transaction::AUTHORIZED,
Braintree\Transaction::AUTHORIZING,
Braintree\Transaction::SETTLED,
Braintree\Transaction::SETTLING,
Braintree\Transaction::SETTLEMENT_CONFIRMED,
Braintree\Transaction::SETTLEMENT_PENDING,
Braintree\Transaction::SUBMITTED_FOR_SETTLEMENT
];
if (in_array($transaction->status, $transactionSuccessStatuses)) {
$header = "Sweet Success!";
$icon = "success";
$message = "Your test transaction has been successfully processed. See the Braintree API response and try again.";
} else {
$header = "Transaction Failed";
$icon = "fail";
$message = "Your test transaction has a status of " . $transaction->status . ". See the Braintree API response and try again.";
}
$app->render('checkouts/show.php', [
'transaction' => $transaction,
'header' => $header,
'icon' => $icon,
'message' => $message
]);
});
$app->run();