Odyssey
CUDirect_API.i
1 <?php
2 /*
3  * 07-2019 Added from Mammoth, left most comments "as is." Made use of
4  * existing Odyssey functions where appropriate, eliminated duplicate
5  * functions, and moved shared ones to hcuConnect.i.
6  *
7  * SSO flow
8  * CuDir_config to set parameters
9  * populate mir
10  * $MIR = fetch_mir($Cu, $Cn);
11  * $MIR['status']['status'] = 101 is only usable value.
12  * 999 is connection err,
13  * anything else is data err
14  * Remember Mammoth fetch_mir returns camelcase keys
15  * Force to lowercase so this is Odyssey-ready
16  * Also Mammoth is $MIR['info'], Odyssey is $MIR['data']
17  *
18  * $reqMIR = array(
19  'accountnumber' => 1,
20  'firstname' => 1,
21  'lastname' => 1,
22  'address1' => 1,
23  'city' => 1,
24  'state' => 1,
25  'zip' => 1,
26  'phonenumbers' => 1
27  );
28  * $parsedMIR = populateMIR($MIR['data'], $Ml, $reqMIR, 'Y-m-d', 'named', true);
29  * throw error if $parsedMIR['status']['response'] is false
30  *
31  * build sso payload
32  * call CuDir_embcurl to get URI
33  * redirect to URI
34  */
35 
36 /**
37  * Set up config from Trusted Details. Note parms is by reference, is modified
38  * when this function is through with it.
39  * @param array $parms
40  * @throws Exception
41  * @return void
42  */
43 function CuDir_config(&$parms) {
44 
45  $parms['devMode'] = (!HCU_array_key_value('devMode', $parms)) ? 0 : HCU_array_key_value('devMode', $parms);
46  if ($parms['devMode']) {
47  // External Services / User Management
48  $parms['CuDirOrg'] = HCU_array_key_value('devOrg', $parms);
49  // HomeCU login
50  $parms['CuDirUser'] = HCU_array_key_value('devUser', $parms);
51  // HomeCU password
52  $parms['CuDirPass'] = HCU_array_key_value('devPass', $parms);
53  // Mobile Services / Deposits & History
54  $parms['CuDirURL'] = HCU_array_key_value('devURL', $parms);
55 
56  } else {
57  // External Services / User Management
58  $parms['CuDirOrg'] = HCU_array_key_value('prdOrg', $parms);
59  // HomeCU login
60  $parms['CuDirUser'] = HCU_array_key_value('prdUser', $parms);
61  // HomeCU password
62  $parms['CuDirPass'] = HCU_array_key_value('prdPass', $parms);
63  // Mobile Services / Deposits & History
64  $parms['CuDirURL'] = HCU_array_key_value('prdURL', $parms);
65  }
66 
67  if (
68  empty($parms['CuDirOrg']) ||
69  empty($parms['CuDirUser']) ||
70  empty($parms['CuDirPass']) ||
71  empty($parms['CuDirURL']
72  )) {
73  throw new Exception("Missing Parameters", 100);
74  }
75 }
76 
77 /**
78  * HomeCU implementation supports
79  * Primary Applicant only - no joint account owners
80  * Personal accounts only - no business accounts
81  *
82  * @param string $CuDirOrg
83  * @param string $CuDirUser
84  * @param string $CuDirPass
85  * @param string $mbrAccount
86  * @param array $mir
87  * @return array
88  */
89 function CuDir_buildSSOPayload($CuDirOrg, $CuDirUser, $CuDirPass, $mbrAccount, $mir) {
90 
91  try {
92  $return['status']['response'] = true;
93  $return['status']['message'] = 'Success';
94 
95  $req_string = '<?xml version="1.0" encoding="UTF-8"?>
96 <env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
97 xmlns:tns="http://www.cudl.com/CUDLConnect/" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
98 xmlns:ins0="http://www.cudl.com/CUDLConnect">
99 <env:Body>
100 <tns:ProcessSOAPMessage>
101 <CUDLConnect xmlns="http://www.cudl.com/CUDLConnect" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
102 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Version="1.0" Direction="Request" TargetAction="Other" OtherAction="GET SSO TOKEN">
103 <CUDLAuthentication Version="1.0">
104 <UserID>' . $CuDirUser . '</UserID>
105 <Password>' . $CuDirPass . '</Password>
106 <OrganizationCode>' . $CuDirOrg . '</OrganizationCode>
107 </CUDLAuthentication>
108 <CUDLAccountLink>
109 <ApplicationID>0</ApplicationID>
110 <Applicants>
111 <Applicant ApplicantType="PrimaryApplicant">
112 <EmailAddresses>
113 <EmailAddress>' . $mir['email'] . '</EmailAddress>
114 </EmailAddresses>
115 <Name Type="PersonName">
116 <First>' . $mir['firstname'] . '</First>
117 ';
118 
119  if (HCU_array_key_value('middlename', $mir)) {
120  $req_string .= '<Middle>' . $mir['middlename'] . '</Middle>
121 ';
122  }
123 
124  $req_string .= '<Last>' . $mir['lastname'] . '</Last>
125 </Name>
126 ';
127 
128  if (HCU_array_key_value('dob', $mir)) {
129  $req_string .= '<DateOfBirth>' . $mir['dob'] . '</DateOfBirth>
130 ';
131  }
132 
133  $req_string .= '<SSN>' . $mir['ssn'] . '</SSN>
134 <MemberAccountNumber>' . $mbrAccount . '</MemberAccountNumber>
135 <IsMember>true</IsMember>
136 <Addresses>
137 <Address AddressType="Home">
138 <Address1>' . $mir['address1'] . '</Address1>
139 ';
140 
141  if (HCU_array_key_value('address2', $mir)) {
142  $req_string .= '<Middle>' . $mir['address2'] . '</Middle>
143 ';
144  }
145 
146  $req_string .= '<City>' . $mir['city'] . '</City>
147 <State>' . $mir['state'] . '</State>
148 <Zip>' . $mir['zip'] . '</Zip>
149 </Address>
150 </Addresses>
151 <PhoneNumbers>
152 ';
153 
154  if (HCU_array_key_exists('phonenumbers', $mir)) {
155  foreach ($mir['phonenumbers'] as $ph => $phoneitm) {
156  $req_string .= '<PhoneNumber PhoneNumberType="' . $phoneitm['name'] . '">
157 <Number>' . $phoneitm['number'] . '</Number>
158 </PhoneNumber>
159 ';
160  }
161  }
162 
163  $req_string .= '</PhoneNumbers>
164 </Applicant>
165 </Applicants>
166 </CUDLAccountLink>
167 <OtherData>
168 <InternetApplication>
169 <SSOUserID>' . $CuDirOrg . ":" . $mbrAccount . '</SSOUserID>
170 </InternetApplication>
171 </OtherData>
172 </CUDLConnect>
173 </tns:ProcessSOAPMessage>
174 </env:Body>
175 </env:Envelope>';
176 
177  $return['data'] = $req_string;
178 
179  } catch (Exception $e) {
180  $return['status']['response'] = false;
181  $return['status']['message'] = $e->getMessage() . " (" . $e->getLine() . ")";
182  $return['data'] = [];
183  }
184 
185  return $return;
186 }
187 
188 /**
189  * Curl the URL set in trusted details (CuDirURL) Dev mode is different URL.
190  * @param array $parms
191  * @param string $reqURL
192  * @param string $reqMethod
193  * @param array $reqHeaders
194  * @param string $reqData
195  * @return array
196  */
197 function CuDir_embcurl($parms, $reqURL, $reqMethod, $reqHeaders, $reqData = '') {
198 
199  $curlopts = [
200  CURLOPT_RETURNTRANSFER => 1,
201  CURLOPT_SSL_VERIFYPEER => 0,
202  CURLOPT_SSL_VERIFYHOST => 0,
203  CURLOPT_HEADER => FALSE,
204  CURLOPT_URL => $reqURL
205  ];
206 
207  $ch = @curl_init();
208  curl_setopt_array($ch, $curlopts);
209 
210  if ($reqMethod != 'GET') {
211  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $reqMethod);
212  }
213 
214  if (strlen($reqData) > 0) {
215  curl_setopt($ch, CURLOPT_POSTFIELDS, $reqData);
216  }
217 
218  if (count($reqHeaders)) {
219  curl_setopt($ch, CURLOPT_HTTPHEADER, $reqHeaders);
220  }
221 
222  $response = curl_exec($ch);
223 
224  if (isset($parms["logging"]) && ($parms["logging"] == "enabled")) {
225 
226  // get the environment info passed in
227  $logParms = $parms["environment"];
228  // the id used across all communications in session
229  $logParms["token"] = '';
230  // the id for this transaction
231  $logParms["txnId"] = time();
232  // the request
233  $logParms["request"] = "curl ";
234 
235  if ($reqMethod != 'GET') {
236  $logParms["request"] .= "-X $reqMethod ";
237  }
238 
239  if (is_array($reqHeaders)) {
240  foreach ($reqHeaders as $hdr) {
241  $logParms["request"] .= "-H '$hdr' ";
242  }
243  }
244 
245  if (strlen($reqData) > 0) {
246  // the request
247  $logParms["request"] .= "-d '$reqData' ";
248  }
249 
250  // the request
251  $logParms['logPoint'] = 'CU Direct SSO';
252  $logParms["request"] .= "'$reqURL' ";
253  $logParms["reply"] = print_r(curl_getinfo($ch),true);
254  $logParms["reply"] .= "\n$response";
255  LogSSOActivity($logParms);
256  }
257 
258  $respERR = '';
259  $respHTTP = curl_getinfo($ch, CURLINFO_HTTP_CODE);
260  $respCURL = curl_errno($ch);
261 
262  if ($respCURL) {
263  // curl error
264  $respERR = "HCUERROR: Connection Failed cURL $respCURL";
265  } elseif ($respHTTP > 400 && $respHTTP < 600) {
266  // HTTP Response 4xx client error or 5xx server error
267  $respERR = "HCUERROR: Connection Failed HTTP $respHTTP";
268  } elseif (!isset($response) || $response == '') {
269  // no response
270  $respERR = "HCUERROR: Empty Response";
271  }
272 
273  if ($respERR == '') {
274  //set xml to catch errors, and remember the current setting
275  $xmlerr_setting = libxml_use_internal_errors(true);
276  $xml = simplexml_load_string($response);
277  // retrieve any errors encountered on the load (@todo this is not used anywhere)
278  $xmlerrors = libxml_get_errors();
279  // clear the internal buffer
280  libxml_clear_errors();
281  // put the setting back to previous state
282  libxml_use_internal_errors($xmlerr_setting);
283 
284  if (!is_object($xml)) {
285  $respERR = "HCUERROR INVALID XML: ";
286  // if we need to print ea. error this would do it
287  // but really just want to know the doc. didn't load
288  // foreach ($xmlerrors as $error) {
289  // $respERR .= $error->message;
290  // }
291  } else {
292 
293  $respToken = '';
294  $respBURL = '';
295 
296  // look for soap level error
297  $xml->registerXPathNamespace('s', "http://schemas.xmlsoap.org/soap/envelope/");
298  $errorresponse = $xml->xpath("//s:Fault");
299 
300  if (is_array($errorresponse) && count($errorresponse)) {
301  $respERR = "HCUERROR: " . $errorresponse[0]->faultcode . " " . $errorresponse[0]->faultstring;
302 
303  } else {
304  // got this far, guess good document first -
305  $xml->registerXPathNamespace('cudir', 'http://www.cudl.com/CUDLConnect');
306  // xml always returns an array. But there will only by 1, so use [0]
307  $respToken = (string) $xml->xpath("//cudir:SSOToken")[0];
308  $respBURL = (string) $xml->xpath("//cudir:BaseURL")[0];
309 
310  if (empty($respToken) || empty($respBURL)) {
311  // @todo not used anywhere
312  $respAPI = $xml->xpath("//cudir:CUDLConfirmation")[0];
313  $respERR = "HCUERROR: Empty Token or URL" ;
314  } else {
315  $respERR = "";
316  }
317  }
318  }
319  }
320 
321  curl_close($ch);
322  // return array('Error' => $respERR, 'Token' => (string) $respToken, 'BaseURL' => (string) $respBURL);
323  return [
324  'Error' => $respERR,
325  'Token' => $respToken,
326  'BaseURL' => $respBURL
327  ];
328 }
329