-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.php
184 lines (108 loc) · 4.73 KB
/
model.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
// API interfacing models
// considered doing in js but server does not seem to support CORS without too much hacking +
// more work to do securely
class ExpyInterface{
private $auth = array(
'partnerName' => 'PARTNERNAME',
'partnerPassword' => 'PASSWORD',
);
public $api_url = 'https://applicant.expensify.com/api' ;
public function expy_request($params){
isset($params['partnerUserID']) ? $params = array_merge($params, $this->auth) : null;
$r = new HttpRequest($this->api_url , HttpRequest::METH_POST); /// pecl_http
$r->addPostFields($params);
try {
return $r->send()->getBody();
} catch (HttpException $ex) {
return $ex;
}
}
}
// Almost zero error handling... would update for real use
class YodleeInterface{
private $auth = array(
'cobrandLogin' => 'COBLOGIN',
'cobrandPassword' => 'COBPASSWORD'
);
public function api_url($method) {
$url = 'https://rest.developer.yodlee.com/services/srest/restserver/v1.0/';
$method_url = array(
'cblogin' => 'authenticate/coblogin',
'login' => 'authenticate/login',
'getUserTransactions' => 'jsonsdk/TransactionSearchService/getUserTransactions',
'executeUserSearchRequest' => 'jsonsdk/TransactionSearchService/executeUserSearchRequest'
);
return $url . $method_url[$method];
}
public function set_cobtoken(){ // application token
session_status() == PHP_SESSION_NONE ? session_start() : null;
$response = $this->yodlee_request('cblogin', $this->auth);
$data = json_decode($response);
if(isset($data->cobrandConversationCredentials->sessionToken)){
$_SESSION['cobToken'] = $data->cobrandConversationCredentials->sessionToken;
return true;
}
return false;
}
public function set_usertoken($params){ // user token retrieval requires app token
session_status() == PHP_SESSION_NONE ? session_start() : null;
$params['cobSessionToken'] = $_SESSION['cobToken'];
$response = $this->yodlee_request('login', $params);
$data = json_decode($response);
if(isset($data->userContext->conversationCredentials->sessionToken)){
setcookie('yodlee_auth' , 1,time()+3600 , '/' );
$_SESSION['userToken'] = $data->userContext->conversationCredentials->sessionToken;
return true;
}
return $response;
}
// creates a search result object on yodlee but only returns search info and first page of results
// must use other calls referencing search id to use full search result
public function transaction_search(){
session_status() == PHP_SESSION_NONE ? session_start() : null;
$params = array(
'cobSessionToken' => $_SESSION['cobToken'],
'userSessionToken' => $_SESSION['userToken'],
'transactionSearchRequest.containerType' => 'All',
'transactionSearchRequest.higherFetchLimit' => 10000,
'transactionSearchRequest.lowerFetchLimit' => 0,
'transactionSearchRequest.resultRange.endNumber' => 20,
'transactionSearchRequest.resultRange.startNumber' => 1,
'transactionSearchRequest.searchClients.clientId' => 1,
'transactionSearchRequest.searchClients.clientName' => 'DataSearchService',
'transactionSearchRequest.searchFilter.currencyCode' => '',
'transactionSearchRequest.searchFilter.postDateRange.fromDate' => '01-01-1960', // should update to be dynamic by user input
'transactionSearchRequest.searchFilter.postDateRange.toDate' => '01-31-2015', // ^
'transactionSearchRequest.searchFilter.transactionSplitType.splitType' => 'A',
'transactionSearchRequest.ignoreUserInput' => 'True',
);
$response = $this->yodlee_request('executeUserSearchRequest', $params);
$data = json_decode($response);
$_SESSION['ysearch'] = $data->searchIdentifier->identifier; // search identifier required to pull results in other calls
return $data->numberOfHits; // return number of transactions found
}
public function search_results(){ // Pulls search results
session_status() == PHP_SESSION_NONE ? session_start() : null;
$params = array(
'cobSessionToken' => $_SESSION['cobToken'],
'userSessionToken' => $_SESSION['userToken'],
'searchFetchRequest.searchIdentifier.identifier' => $_SESSION['ysearch'],
'searchFetchRequest.searchResultRange.startNumber' => 1,
'searchFetchRequest.searchResultRange.endNumber' => 50000
);
$response = $this->yodlee_request('getUserTransactions', $params);
$data = json_decode($response);
return $data->transactions;
}
public function yodlee_request($method, $params = null){ // api call
$r = new HttpRequest($this->api_url($method) , HttpRequest::METH_POST);
$r->addPostFields($params);
try {
return $r->send()->getBody();
} catch (HttpException $ex) {
return $ex;
}
}
}
?>