-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathapp.php
165 lines (130 loc) · 5.29 KB
/
app.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
159
160
161
162
163
164
165
<?php
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Slim\Factory\AppFactory;
// This halts execution if a static file is found, allowing it to render instead
if (PHP_SAPI == 'cli-server') {
$url = parse_url($_SERVER['REQUEST_URI']);
$path = $url['path'] == '/' ? 'index.html' : $url['path'];
$public_dir = getenv('PUBLIC_DIR_PATH') ? getenv('PUBLIC_DIR_PATH') : '/../../public';
$file = __DIR__ . "$public_dir/$path";
if (is_file($file)) return false;
}
require 'vendor/autoload.php';
// Configure the client with your API Key. We're using ENV vars here,
// but you may wish to store them elsewhere
$recurly_client = new \Recurly\Client(getenv('RECURLY_API_KEY'));
$app = AppFactory::create();
// Create a new account, subscription, and billing information
// For this we will use the Recurly Create Purchase endpoint
// See: https://developers.recurly.com/api/latest/index.html#operation/create_purchase
$app->post('/api/subscriptions/new', function (Request $request, Response $response, array $args) {
global $recurly_client;
// Retrieve the parsed body from the request as params
$params = (array)$request->getParsedBody();
// Retrieve the token created by Recurly.js and submitted in our form
$token_id = $params['recurly-token'];
// If our form specifies an account code, we can use that; otherwise,
// create an account code with a uniqid
$account_code = $params['recurly-account-code'];
if (is_null($account_code)) {
$account_code = uniqid();
}
// Specify the minimum purchase attributes for a subscription: plan_code, account, and currency
$purchase_create = [
'currency' => 'USD',
'account' => [
'code' => $account_code,
'first_name' => $params['first-name'],
'last_name' => $params['last-name'],
'billing_info' => [
'token_id' => $token_id
],
],
'subscriptions' => [
[
'plan_code' => 'basic'
]
]
];
// Optionally add a 3D Secure token if one is present
$three_d_secure_token = $params['three-d-secure-token'];
if ($three_d_secure_token) {
$purchase_create['account']['billing_info']['three_d_secure_action_result_token_id'] = $three_d_secure_token;
}
// We wrap this is a try-catch to handle any errors
try {
// Create the purchase
$recurly_client->createPurchase($purchase_create);
} catch (\Recurly\Errors\Transaction $e) {
// Here we handle a 3D Secure required error by redirecting to an authentication page
$transaction_error = $e->getApiError()->getTransactionError();
if ($transaction_error && $transaction_error->getCode() == 'three_d_secure_action_required') {
$action_token_id = $transaction_error->getThreeDSecureActionTokenId();
$location = "/3d-secure/authenticate.html#token_id=$token_id&action_token_id=$action_token_id&account_code=$account_code";
return $response->withHeader('Location', $location)->withStatus(302);
}
// Assign the error message and use it to handle any customer messages or logging
$error = $e->getMessage();
} catch (\Recurly\Errors\Validation $e) {
// If the request was not valid, you may want to tell your user why.
$error = $e->getMessage();
}
// Now we may wish to redirect to a confirmation or back to the form to fix any errors.
$location = $_ENV['SUCCESS_URL'];
if (isset($error)) {
$location = "$_ENV[ERROR_URL]?error=$error";
}
return $response->withHeader('Location', $location)->withStatus(302);
});
// Create a new account and billing information
$app->post('/api/accounts/new', function (Request $request, Response $response, array $args) {
global $recurly_client;
$params = (array)$request->getParsedBody();
$account_create = [
'code' => $account_code,
'first_name' => $params['first-name'],
'last_name' => $params['last-name'],
'billing_info' => [
'token_id' => $params['recurly-token']
]
];
try {
$recurly_client->createAccount($account_create);
} catch (\Recurly\Errors\Validation $e) {
$error = $e->getMessage();
}
$location = $_ENV['SUCCESS_URL'];
if (isset($error)) {
$location = "$_ENV[ERROR_URL]?error=$error";
}
return $response->withHeader('Location', $location)->withStatus(302);
});
$app->put('/api/accounts/{account_code}', function (Request $request, Response $response, array $args) {
global $recurly_client;
$params = (array)$request->getParsedBody();
$account_update = [
'first_name' => $params['first-name'],
'last_name' => $params['last-name'],
'billing_info' => [
'token_id' => $params['recurly-token']
]
];
try {
$recurly_client->updateAccount("code-$args[account_code]", $account_update);
} catch (\Recurly\Errors\Validation $e) {
$error = $e->getMessage();
}
$location = $_ENV['SUCCESS_URL'];
if (isset($error)) {
$location = "$_ENV[ERROR_URL]?error=$error";
}
return $response->withHeader('Location', $location)->withStatus(302);
});
// This endpoint provides configuration to recurly.js
$app->get('/config', function (Request $request, Response $response, array $args) {
$PUBLIC_KEY = getenv('RECURLY_PUBLIC_KEY');
$response->getBody()->write("window.recurlyConfig = { publicKey: '$PUBLIC_KEY' }");
return $response->withHeader('Content-Type', 'application/javascript');
});
$app->run();