-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrouter.php
141 lines (117 loc) · 4.33 KB
/
router.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
<?php
error_reporting(E_ALL);
date_default_timezone_set('UTC');
// Require composer autoloader
require __DIR__ . '/vendor/autoload.php';
Twig_Autoloader::register();
$loader = new Twig_Loader_Filesystem(__DIR__ . '/templates');
$twig = new Twig_Environment($loader);
// Create Router instance
$router = new AltoRouter();
require __DIR__ . '/gocardless.php';
$gc = new GoCardlessAPI();
// Define routes
$router->map('GET', '/', function() use ($twig, $gc) {
echo $twig->render('index.html', array('users' => $gc->listUsers(), 'customers' => $gc->listCustomers()));
});
$router->map('POST', '/mandates/create', function() {
});
$router->map('POST', '/charge/create', function() use ($gc) {
if (isset($_POST['mid'])) {
$charge = $gc->chargeCustomer($_POST['cid'], $_POST['mid'], 5);
header('Location: /customers/' . $_POST['cid'] . '/bill');
return;
} else if (isset($_POST['cid'])) {
$accounts = $gc->client->customer_bank_accounts()->list()->records();
$creditors = $gc->client->creditors()->list()->records();
if (count($accounts) === 0 || count($creditors) === 0) {
throw new Exception('No bank account/creditor found!');
}
$gc->client->mandates()->create(array(
'links' => array(
'creditor' => $creditors[0]->id(),
'customer_bank_account' => $accounts[0]->id()
)
));
header('Location: /customers/' . $_POST['cid'] . '/bill');
return;
}
throw new Exception('invalid request!');
});
$router->map('GET', '/customers/[a:cid]/bill', function($cid) use ($twig, $gc) {
$customer = $gc->getCustomer($cid);
$mandates = $gc->getCustomerMandates($customer->id())->records();
$mandate = null;
if (count($mandates) > 0) {
$mandate = $mandates[0];
}
$payments = $gc->listPayments($customer->id());
echo $twig->render('bill.html', array(
'payments' => $payments,
'mandates' => $mandates,
'mandate' => $mandate,
'customer' => $customer
));
});
$router->map('GET', '/oneoff', function() use ($twig) {
echo $twig->render('oneoff.html');
});
$router->map('GET', '/bills/paid_success/[a:fid]', function($fid) use ($gc) {
$res = $gc->client->redirect_flows()->complete(array('session_token' => $fid));
echo '<pre>';
print_r($res);
echo '</pre>';
});
$router->map('POST', '/oneoff', function() use ($gc) {
if (!isset($_POST['description'])) {
throw new Exception('Description required!');
}
$description = $_POST['description'];
$creditor_id = $gc->client->creditors()->list()->records()[0]->id();
$uid = uniqid();
$flow = $gc->client->redirect_flows()->create(array(
'session_token' => $uid,
'success_redirect_url' => 'http://localhost:8080/bills/paid_success/' . $uid,
'description' => $description,
'links' => array(
'creditor' => $creditor_id
)
));
print_r($flow);
header('Location: /flows/' . $flow->id());
});
$router->map('GET', '/flows/[a:flow]', function($flow_id) use ($twig, $gc) {
$flow = $gc->client->redirect_flows()->get($flow_id);
echo $twig->render('flow.html', array('flow' => $flow));
});
$router->map('GET', '/billed/[a:pid]', function($pid) use ($gc) {
$payment = $gc->client->payments()->get($pid);
$creditor = $gc->client->creditors()->get($payment->links()->creditor());
echo $twig->render('billed.html', array('payment' => $payment, 'creditor' => $creditor));
});
$router->map('GET', '/mandate/[a:mid]/pdf', function($mid) use ($gc) {
try {
$pdfMandate = $gc->client->mandates()->get(
$mid,
array(),
array(
'Accept' => 'application/pdf',
'Accept-Language' => 'en'
)
);
header('Content-Type: application/pdf');
echo $pdfMandate->response()->raw_body();
} catch (\GoCardless\Core\Error\GoCardlessError $e) {
echo '<pre>' . print_r($e, true);
}
});
// match current request url
$match = $router->match();
// call closure or throw 404 status
if( $match && is_callable( $match['target'] ) ) {
call_user_func_array( $match['target'], $match['params'] );
} else {
// no route was matched
header( $_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found');
echo $twig->render('404.html');
}