-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
69 lines (58 loc) · 1.83 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
<?php
require "vendor/autoload.php";
// Load our settings from the .env file
$dotenv = new Dotenv\Dotenv(__DIR__);
$dotenv->load();
// Initialize Slim and set error settings to true
// do change this if you put the app in production
$app = new Slim\App([
'settings' => [
'displayErrorDetails' => true
]
]);
$container = $app->getContainer();
// Add the Slydepay API into the container for easy access in routes
$container['slydepay'] = function() {
return new Slydepay\Slydepay(getenv('EMAIL'), getenv('API_KEY'));
};
$app->post('/order', function($request, $response) {
$items = [];
$ordersRequest = $request->getParam('orders');
foreach ($ordersRequest['items'] as $item) {
$orderItem = new Slydepay\Order\OrderItem(
$item['id'],
$item['name'],
$item['price'],
$item['quantity']
);
array_push($items, $orderItem);
}
$orderItems = new Slydepay\Order\OrderItems($items);
$orderAmount = new Slydepay\Order\OrderAmount(
$orderItems->subTotal(),
$ordersRequest['shipping_cost'],
$ordersRequest['tax_amount']
);
/* we can access slydepay here as a property of the closure
* because we are using the magic __get on the container to
* retrieve properties
*/
try {
$result = $this->slydepay->processPaymentOrder(
$ordersRequest['id'],
$ordersRequest['description'],
$orderAmount,
$orderItems
);
return $response->withJson([
'success' => true,
'redirect' => $result->redirectUrl()
], 200);
} catch (Slydepay\Exception\ProcessPayment $e) {
return $response->withJson([
'success' => false,
'error_message' => $e->getMessage()
], 200);
}
});
$app->run();