| 1 | #!perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Mojolicious::Lite; | ||
| 7 | use Mojo::JSON; | ||
| 8 | |||
| 9 | $ENV{MOJO_LOG_LEVEL} = 'debug'; | ||
| 10 | |||
| 11 | plugin 'OAuth2::Server' => { | ||
| 12 | access_token_ttl => 21600, | ||
| 13 | authorize_route => '/', | ||
| 14 | access_token_route => '/oauth2/token', | ||
| 15 | jwt_secret => "ThisIsMyMonzoJWTSecret", | ||
| 16 | clients => { | ||
| 17 | test_client => { | ||
| 18 | client_secret => 'test_client_secret', | ||
| 19 | scopes => {}, | ||
| 20 | }, | ||
| 21 | }, | ||
| 22 | users => { | ||
| 23 | leejo => "Weeeee", | ||
| 24 | } | ||
| 25 | }; | ||
| 26 | |||
| 27 | group { | ||
| 28 | |||
| 29 | # all routes must have an access token | ||
| 30 | under '/' => sub { | ||
| 31 | my ( $c ) = @_; | ||
| 32 | return 1 if $c->oauth; | ||
| 33 | $c->render( status => 401, text => 'Unauthorized' ); | ||
| 34 | return undef; | ||
| 35 | }; | ||
| 36 | |||
| 37 | post '/ping/whoami' => sub { | ||
| 38 | my ( $c ) = @_; | ||
| 39 | |||
| 40 | $c->render( json => { | ||
| 41 | authenticated => Mojo::JSON::true, | ||
| 42 | client_id => $c->oauth->{client}, | ||
| 43 | user_id => $c->oauth->{user_id}, | ||
| 44 | } ); | ||
| 45 | |||
| 46 | }; | ||
| 47 | |||
| 48 | get '/accounts' => sub { | ||
| 49 | my ( $c ) = @_; | ||
| 50 | |||
| 51 | $c->render( json => { | ||
| 52 | accounts => [ | ||
| 53 | { | ||
| 54 | id => "acc_00009237aqC8c5umZmrRdh", | ||
| 55 | description => "Peter Pan's Account", | ||
| 56 | created => "2015-11-13T12:17:42Z", | ||
| 57 | }, | ||
| 58 | ], | ||
| 59 | } ); | ||
| 60 | }; | ||
| 61 | |||
| 62 | get '/pots/listV1' => sub { | ||
| 63 | my ( $c ) = @_; | ||
| 64 | |||
| 65 | $c->render( json => { | ||
| 66 | pots => [ | ||
| 67 | { | ||
| 68 | id => "pot_0000778xxfgh4iu8z83nWb", | ||
| 69 | name => "Peter Pan's Savings", | ||
| 70 | style => "piggy_bank", | ||
| 71 | balance => 133700, | ||
| 72 | currency => "GBP", | ||
| 73 | created => "2017-11-09T12:30:53.695Z", | ||
| 74 | updated => "2017-11-09T12:30:53.695Z", | ||
| 75 | deleted => Mojo::JSON::false | ||
| 76 | }, | ||
| 77 | { | ||
| 78 | id => "pot_0000779h4iuxxnWfg8z83b", | ||
| 79 | name => "Tinker Bell's Break", | ||
| 80 | style => "beach_ball", | ||
| 81 | balance => 33700, | ||
| 82 | currency => "GBP", | ||
| 83 | created => "2017-11-09T12:30:53.695Z", | ||
| 84 | updated => "2017-11-09T12:30:53.695Z", | ||
| 85 | deleted => Mojo::JSON::false | ||
| 86 | }, | ||
| 87 | ], | ||
| 88 | } ); | ||
| 89 | }; | ||
| 90 | |||
| 91 | get '/balance' => sub { | ||
| 92 | my ( $c ) = @_; | ||
| 93 | |||
| 94 | my $account_id = $c->param( 'account_id' ) | ||
| 95 | || return $c->render( status => 400, text => "account_id required" ); | ||
| 96 | |||
| 97 | $c->render( json => { | ||
| 98 | balance => 5000, | ||
| 99 | currency => "GBP", | ||
| 100 | spend_today => 0, | ||
| 101 | } ); | ||
| 102 | }; | ||
| 103 | |||
| 104 | get '/transactions/:transaction_id' => sub { | ||
| 105 | my ( $c ) = @_; | ||
| 106 | |||
| 107 | my $tid = $c->param( 'transaction_id' ); | ||
| 108 | |||
| 109 | $c->render( json => { | ||
| 110 | "transaction" => _transactions( $c->param( 'expand[]' ) )->[1], | ||
| 111 | } ); | ||
| 112 | }; | ||
| 113 | |||
| 114 | patch '/transactions/:transaction_id' => sub { | ||
| 115 | my ( $c ) = @_; | ||
| 116 | |||
| 117 | my $tid = $c->param( 'transaction_id' ); | ||
| 118 | |||
| 119 | my $metadata = _convert_map_params_to_hash( $c,'metadata' ); | ||
| 120 | |||
| 121 | $c->render( json => { | ||
| 122 | "transaction" => _transactions( undef,$metadata )->[$tid - 1], | ||
| 123 | } ); | ||
| 124 | }; | ||
| 125 | |||
| 126 | get '/transactions' => sub { | ||
| 127 | my ( $c ) = @_; | ||
| 128 | |||
| 129 | my $account_id = $c->param( 'account_id' ) | ||
| 130 | || return $c->render( status => 400, text => "account_id required" ); | ||
| 131 | |||
| 132 | $c->render( json => { | ||
| 133 | "transactions" => _transactions(), | ||
| 134 | } ); | ||
| 135 | }; | ||
| 136 | |||
| 137 | post '/feed' => sub { | ||
| 138 | my ( $c ) = @_; | ||
| 139 | |||
| 140 | my $account_id = $c->param( 'account_id' ) | ||
| 141 | || return $c->render( status => 400, text => "account_id required" ); | ||
| 142 | |||
| 143 | my $type = $c->param( 'type' ) | ||
| 144 | || return $c->render( status => 400, text => "type required" ); | ||
| 145 | |||
| 146 | my $params = _convert_map_params_to_hash( $c,'params' ) | ||
| 147 | || return $c->render( status => 400, text => "params required" ); | ||
| 148 | |||
| 149 | foreach my $required_param ( qw/ title image_url / ) { | ||
| 150 | defined $params->{$required_param} | ||
| 151 | || return $c->render( status => 400, text => "params[$required_param] required" ); | ||
| 152 | } | ||
| 153 | |||
| 154 | # no-op at present | ||
| 155 | $c->render( json => {} ); | ||
| 156 | }; | ||
| 157 | |||
| 158 | post '/webhooks' => sub { | ||
| 159 | my ( $c ) = @_; | ||
| 160 | |||
| 161 | my $account_id = $c->param( 'account_id' ) | ||
| 162 | || return $c->render( status => 400, text => "account_id required" ); | ||
| 163 | |||
| 164 | my $url = $c->param( 'url' ) | ||
| 165 | || return $c->render( status => 400, text => "url required" ); | ||
| 166 | |||
| 167 | $c->render( json => { | ||
| 168 | webhook => { | ||
| 169 | account_id => $account_id, | ||
| 170 | url => $url, | ||
| 171 | id => time, | ||
| 172 | } | ||
| 173 | } ); | ||
| 174 | }; | ||
| 175 | |||
| 176 | get '/webhooks' => sub { | ||
| 177 | my ( $c ) = @_; | ||
| 178 | |||
| 179 | my $account_id = $c->param( 'account_id' ) | ||
| 180 | || return $c->render( status => 400, text => "account_id required" ); | ||
| 181 | |||
| 182 | $c->render( json => { | ||
| 183 | webhooks => _webhooks( $account_id ), | ||
| 184 | } ); | ||
| 185 | }; | ||
| 186 | |||
| 187 | del '/webhooks/:webhook_id' => sub { | ||
| 188 | shift->render( json => {} ); | ||
| 189 | }; | ||
| 190 | |||
| 191 | post '/attachment/upload' => sub { | ||
| 192 | my ( $c ) = @_; | ||
| 193 | |||
| 194 | my $file_name = $c->param( 'file_name' ) | ||
| 195 | || return $c->render( status => 400, text => "file_name required" ); | ||
| 196 | |||
| 197 | my $file_type = $c->param( 'file_type' ) | ||
| 198 | || return $c->render( status => 400, text => "file_type required" ); | ||
| 199 | |||
| 200 | my $url = $c->req->url; | ||
| 201 | |||
| 202 | my $entity_id = "user_00009237hliZellUicKuG1"; | ||
| 203 | $c->render( json => { | ||
| 204 | "file_url" => $url->base . "/file/$entity_id/LcCu4ogv1xW28OCcvOTL-foo.png", | ||
| 205 | "upload_url" => $url->base . "/upload/$entity_id/LcCu4ogv1xW28OCcvOTL-foo.png?AWSAccessKeyId=AKIAIR3IFH6UCTCXB5PQ\u0026Expires=1447353431\u0026Signature=k2QeDCCQQHaZeynzYKckejqXRGU%!D(MISSING)" | ||
| 206 | } ); | ||
| 207 | }; | ||
| 208 | |||
| 209 | post '/attachment/register' => sub { | ||
| 210 | my ( $c ) = @_; | ||
| 211 | |||
| 212 | my $external_id = $c->param( 'external_id' ) | ||
| 213 | || return $c->render( status => 400, text => "external_id required" ); | ||
| 214 | |||
| 215 | my $file_url = $c->param( 'file_url' ) | ||
| 216 | || return $c->render( status => 400, text => "file_url required" ); | ||
| 217 | |||
| 218 | my $file_type = $c->param( 'file_type' ) | ||
| 219 | || return $c->render( status => 400, text => "file_type required" ); | ||
| 220 | |||
| 221 | $c->render( json => { | ||
| 222 | "attachment" => { | ||
| 223 | "id" => "attach_00009238aOAIvVqfb9LrZh", | ||
| 224 | "user_id" => "user_00009238aMBIIrS5Rdncq9", | ||
| 225 | "external_id" => $external_id, | ||
| 226 | "file_url" => $file_url, | ||
| 227 | "file_type" => $file_type, | ||
| 228 | "created" => "2015-11-12T18:37:02Z" | ||
| 229 | } | ||
| 230 | } ); | ||
| 231 | }; | ||
| 232 | |||
| 233 | post '/attachment/deregister' => sub { | ||
| 234 | my ( $c ) = @_; | ||
| 235 | |||
| 236 | my $id = $c->param( 'id' ) | ||
| 237 | || return $c->render( status => 400, text => "id required" ); | ||
| 238 | |||
| 239 | $c->render( json => {} ); | ||
| 240 | } | ||
| 241 | }; | ||
| 242 | |||
| 243 | # convenience methods for file upload emulation, these endpoints | ||
| 244 | # do not exist in the Monzo API, they are here to fake uploads | ||
| 245 | get '/file/:entity_id/:file_name' => sub { | ||
| 246 | my ( $c ) = @_; | ||
| 247 | |||
| 248 | $c->render( text => "OK" ); | ||
| 249 | }; | ||
| 250 | |||
| 251 | post '/upload/:entity_id/:file_name' => sub { | ||
| 252 | my ( $c ) = @_; | ||
| 253 | |||
| 254 | $c->render( text => "OK" ); | ||
| 255 | }; | ||
| 256 | |||
| 257 | sub _convert_map_params_to_hash { | ||
| 258 | my ( $c,$prefix ) = @_; | ||
| 259 | |||
| 260 | # converts { params[foo] => bar } to { foo => bar } | ||
| 261 | # (this is horrible! why not just send JSON in the request body?) | ||
| 262 | |||
| 263 | my $params = $c->req->params->to_hash; | ||
| 264 | |||
| 265 | my %extracted_params = | ||
| 266 | map { my $v = $params->{$_}; s/^$prefix\[//g; chop; $_ => $v } | ||
| 267 | grep { /^$prefix\[[^\[]+\]$/ } keys %{ $params // {} }; | ||
| 268 | |||
| 269 | return \%extracted_params; | ||
| 270 | } | ||
| 271 | |||
| 272 | sub _transactions { | ||
| 273 | my ( $expand,$metadata ) = @_; | ||
| 274 | |||
| 275 | $expand //= 'none'; | ||
| 276 | $metadata //= { | ||
| 277 | stuff => 'yes', | ||
| 278 | more_stuff => 'yep', | ||
| 279 | }; | ||
| 280 | |||
| 281 | my $attachment = { | ||
| 282 | "created" => "2016-04-23T12:46:41Z", | ||
| 283 | "external_id" => "tx_0000...", | ||
| 284 | "file_type" => "image/jpeg", | ||
| 285 | "file_url" => "https://...", | ||
| 286 | "id" => "attach_0000...", | ||
| 287 | "type" => "image/jpeg", | ||
| 288 | "url" => "https://...", | ||
| 289 | "user_id" => "user_0000..." | ||
| 290 | }; | ||
| 291 | |||
| 292 | return [ | ||
| 293 | { | ||
| 294 | "currency" => "GBP", | ||
| 295 | "merchant" => undef, | ||
| 296 | "counterparty" => {}, | ||
| 297 | "local_amount" => 10000, | ||
| 298 | "id" => "1", | ||
| 299 | "created" => "2016-04-22T12:35:55.563Z", | ||
| 300 | "is_load" => Mojo::JSON::true, | ||
| 301 | "updated" => "2016-04-28T20:15:35.043Z", | ||
| 302 | "notes" => "", | ||
| 303 | "dedupe_id" => "5529515640563", | ||
| 304 | "description" => "Initial top up", | ||
| 305 | "attachments" => [], | ||
| 306 | "metadata" => $metadata, | ||
| 307 | "account_balance" => 10000, | ||
| 308 | "originator" => Mojo::JSON::false, | ||
| 309 | "scheme" => "gps_mastercard", | ||
| 310 | "amount" => 10000, | ||
| 311 | "settled" => "2016-04-22T12:35:55.563Z", | ||
| 312 | "account_id" => "acc_0000000000000000000001", | ||
| 313 | "local_currency" => "GBP", | ||
| 314 | "category" => "monzo" | ||
| 315 | }, | ||
| 316 | { | ||
| 317 | "amount" => -1433, | ||
| 318 | "settled" => "2016-04-24T23:00:00.5Z", | ||
| 319 | "originator" => Mojo::JSON::false, | ||
| 320 | "account_balance" => 8565, | ||
| 321 | "scheme" => "gps_mastercard", | ||
| 322 | "category" => "cash", | ||
| 323 | "account_id" => "acc_0000000000000000000001", | ||
| 324 | "local_currency" => "CHF", | ||
| 325 | "id" => "2", | ||
| 326 | "created" => "2016-04-23T09:22:44.12Z", | ||
| 327 | "currency" => "GBP", | ||
| 328 | "counterparty" => {}, | ||
| 329 | "merchant" => $expand eq 'merchant' | ||
| 330 | ? _merchant() | ||
| 331 | : "merch_0000000000000000000001", | ||
| 332 | "local_amount" => -2000, | ||
| 333 | "description" => "BCV VILLARS/OLL. 2 Villars-sur-O CHE", | ||
| 334 | "dedupe_id" => "565726953219015109", | ||
| 335 | "attachments" => [ $attachment,$attachment ], | ||
| 336 | "metadata" => $metadata, | ||
| 337 | "is_load" => Mojo::JSON::false, | ||
| 338 | "updated" => "2016-04-25T09:50:56.605Z", | ||
| 339 | "notes" => "" | ||
| 340 | }, | ||
| 341 | ]; | ||
| 342 | } | ||
| 343 | |||
| 344 | sub _merchant { | ||
| 345 | |||
| 346 | return { | ||
| 347 | "merchant" => { | ||
| 348 | "emoji" => "💵", | ||
| 349 | "updated" => "2016-04-23T09:22:45.005Z", | ||
| 350 | "online" => Mojo::JSON::false, | ||
| 351 | "category" => "cash", | ||
| 352 | "metadata" => { | ||
| 353 | "suggested_tags" => "#money #ATM #cashpoint #cash ", | ||
| 354 | "google_places_id" => "ChIJzXdG2omVjkcRqXc-9o1QxZI", | ||
| 355 | "foursquare_category" => "ATM", | ||
| 356 | "foursquare_id" => "", | ||
| 357 | "foursquare_website" => "", | ||
| 358 | "suggested_name" => "Caixa 24 Horas", | ||
| 359 | "foursquare_category_icon" => "https://ss3.4sqi.net/img/categories_v2/shops/financial_88.png", | ||
| 360 | "google_places_icon" => "https://maps.gstatic.com/mapfiles/place_api/icons/bank_dollar-71.png", | ||
| 361 | "google_places_name" => "UBS", | ||
| 362 | "created_for_merchant" => "merch_0000000000000000000001", | ||
| 363 | "created_for_transaction" => "1", | ||
| 364 | "twitter_id" => "", | ||
| 365 | "website" => "" | ||
| 366 | }, | ||
| 367 | "disable_feedback" => Mojo::JSON::false, | ||
| 368 | "atm" => Mojo::JSON::true, | ||
| 369 | "logo" => "", | ||
| 370 | "group_id" => "grp_0000000000000000000001", | ||
| 371 | "id" => "merch_0000000000000000000001", | ||
| 372 | "name" => "ATM", | ||
| 373 | "created" => "2016-04-23T09:22:45.005Z", | ||
| 374 | "address" => { | ||
| 375 | "country" => "CHE", | ||
| 376 | "city" => "Villars-sur-o", | ||
| 377 | "longitude" => 7.076864, | ||
| 378 | "address" => "", | ||
| 379 | "region" => "", | ||
| 380 | "formatted" => "Villars-sur-o, 1884, Switzerland", | ||
| 381 | "latitude" => 46.3118929, | ||
| 382 | "approximate" => Mojo::JSON::false, | ||
| 383 | "zoom_level" => 17, | ||
| 384 | "short_formatted" => "Villars-sur-o, 1884, Switzerland", | ||
| 385 | "postcode" => "1884" | ||
| 386 | } | ||
| 387 | }, | ||
| 388 | }; | ||
| 389 | } | ||
| 390 | |||
| 391 | sub _webhooks { | ||
| 392 | my ( $account_id ) = @_; | ||
| 393 | |||
| 394 | $account_id //= "acc_000091yf79yMwNaZHhHGzp"; | ||
| 395 | |||
| 396 | return [ | ||
| 397 | { | ||
| 398 | "account_id" => $account_id, | ||
| 399 | "id" => "webhook_000091yhhOmrXQaVZ1Irsv", | ||
| 400 | "url" => "http://example.com/callback" | ||
| 401 | }, | ||
| 402 | { | ||
| 403 | "account_id" => $account_id, | ||
| 404 | "id" => "webhook_000091yhhzvJSxLYGAceC9", | ||
| 405 | "url" => "http://example2.com/anothercallback" | ||
| 406 | }, | ||
| 407 | ]; | ||
| 408 | } | ||
| 409 | |||
| 410 | app->start; | ||
| 411 | |||
| 412 | # vim: ts=4:sw=4:et |