@@ -15,7 +15,7 @@ discard block  | 
                                                    ||
| 15 | 15 | * Package Config Path  | 
                                                        
| 16 | 16 | * @var string  | 
                                                        
| 17 | 17 | */  | 
                                                        
| 18 | - private $packageConfigPath = __DIR__.'/config/yourmembership.php';  | 
                                                        |
| 18 | + private $packageConfigPath = __DIR__ . '/config/yourmembership.php';  | 
                                                        |
| 19 | 19 | /**  | 
                                                        
| 20 | 20 | * Indicates if loading of the provider is deferred.  | 
                                                        
| 21 | 21 | *  | 
                                                        
@@ -34,7 +34,7 @@ discard block  | 
                                                    ||
| 34 | 34 | $this->packageConfigPath, $this->packageName  | 
                                                        
| 35 | 35 | );  | 
                                                        
| 36 | 36 | |
| 37 | -		$this->app->bind(YourMembershipClient::class, function ($app, $parameters) { | 
                                                        |
| 37 | +		$this->app->bind(YourMembershipClient::class, function($app, $parameters) { | 
                                                        |
| 38 | 38 | $guzzleClient = new \GuzzleHttp\Client($app['config']['yourmembership']['guzzle-client']);  | 
                                                        
| 39 | 39 | return new YourMembershipClient($guzzleClient, $parameters[0], $parameters[1]);  | 
                                                        
| 40 | 40 | });  | 
                                                        
@@ -60,168 +60,168 @@  | 
                                                    ||
| 60 | 60 | <SaPasscode>************</SaPasscode>  | 
                                                        
| 61 | 61 | </YourMembership>  | 
                                                        
| 62 | 62 | */  | 
                                                        
| 63 | -        $xml = new \SimpleXMLElement('<YourMembership></YourMembership>'); | 
                                                        |
| 64 | -        $xml->addChild('Version', self::API_VERSION); | 
                                                        |
| 65 | -        $xml->addChild('ApiKey', $this->apiKey); | 
                                                        |
| 66 | -        $xml->addChild('CallID', self::$callId); | 
                                                        |
| 67 | -        $xml->addChild('SaPasscode', $this->saPasscode); | 
                                                        |
| 68 | -  | 
                                                        |
| 69 | - return $xml;  | 
                                                        |
| 70 | - }  | 
                                                        |
| 71 | -  | 
                                                        |
| 72 | - /**  | 
                                                        |
| 73 | - * Generates the XML for a API method call within  | 
                                                        |
| 74 | - * @method createCallPayload  | 
                                                        |
| 75 | - * @author PA  | 
                                                        |
| 76 | - * @throws YourMembershipRequestException  | 
                                                        |
| 77 | - * @date 2017-01-09  | 
                                                        |
| 78 | - * @param string $method YourMembership API Function Name  | 
                                                        |
| 79 | - * @param array $arguments Array of Arguments to be passed as part of the YourMembership "Call"  | 
                                                        |
| 80 | - * @return \SimpleXMLElement  | 
                                                        |
| 81 | - */  | 
                                                        |
| 82 | - public function createCallPayload(string $method, array $arguments) : \SimpleXMLElement  | 
                                                        |
| 83 | -    { | 
                                                        |
| 84 | - //Create Call Node  | 
                                                        |
| 85 | -        $call = new \SimpleXMLElement('<Call> </Call>'); | 
                                                        |
| 86 | -        $call->addAttribute('Method', $method); | 
                                                        |
| 87 | -  | 
                                                        |
| 88 | - //Add Arguments to the Call Node  | 
                                                        |
| 89 | -        try { | 
                                                        |
| 90 | - $call = $this->sxmlAddChildrenRecursive($call, $arguments);  | 
                                                        |
| 91 | -        } catch (\Exception $e) { | 
                                                        |
| 92 | - throw new YourMembershipRequestException($e->getMessage(), $e->getCode(), $method, $arguments, $e);  | 
                                                        |
| 93 | - }  | 
                                                        |
| 94 | - return $call;  | 
                                                        |
| 95 | - }  | 
                                                        |
| 96 | - /**  | 
                                                        |
| 97 | - * Recursively builds the array into a XML Tree  | 
                                                        |
| 98 | - * //NOTE Child arrays must be associative  | 
                                                        |
| 99 | - * @method sxmlAddChildrenRecursive  | 
                                                        |
| 100 | - * @author PA  | 
                                                        |
| 101 | - * @date 2017-01-12  | 
                                                        |
| 102 | - * @param \SimpleXMLElement $root Root XML Node  | 
                                                        |
| 103 | - * @param array $arguments Array of Arguments to be added to XML Node  | 
                                                        |
| 104 | - * @return \SimpleXMLElement Resulting XML Tree  | 
                                                        |
| 105 | - */  | 
                                                        |
| 106 | - private function sxmlAddChildrenRecursive(\SimpleXMLElement $root, array $arguments) : \SimpleXMLElement  | 
                                                        |
| 107 | -    { | 
                                                        |
| 108 | -        foreach ($arguments as $key => $value) { | 
                                                        |
| 109 | -            if (is_array($value)) { | 
                                                        |
| 110 | -                $child = new \SimpleXMLElement(sprintf('<%s></%s>', $key, $key)); | 
                                                        |
| 111 | - $this->sxmlAddChildrenRecursive($child, $value);  | 
                                                        |
| 112 | - $this->sxmlAppend($root, $child);  | 
                                                        |
| 113 | -            } else { | 
                                                        |
| 114 | - $root->addChild($key, $value);  | 
                                                        |
| 115 | - }  | 
                                                        |
| 116 | -  | 
                                                        |
| 117 | - }  | 
                                                        |
| 118 | - return $root;  | 
                                                        |
| 119 | - }  | 
                                                        |
| 120 | - /**  | 
                                                        |
| 121 | - * Builds The XML Request Body for the Your Membership API Call  | 
                                                        |
| 122 | - * @method buildXMLBody  | 
                                                        |
| 123 | - * @author PA  | 
                                                        |
| 124 | - * @date 2017-01-10  | 
                                                        |
| 125 | - * @param string $method Your Membership API Function Name  | 
                                                        |
| 126 | - * @param array $arguments Your Membership Arguments  | 
                                                        |
| 127 | - * @return \SimpleXMLElement  | 
                                                        |
| 128 | - */  | 
                                                        |
| 129 | - public function buildXMLBody(string $method, array $arguments) : \SimpleXMLElement  | 
                                                        |
| 130 | -    { | 
                                                        |
| 131 | - $xml = $this->buildBasePayload(); // Common Envelope  | 
                                                        |
| 132 | -  | 
                                                        |
| 133 | -        if ($this->isSessionRequiredForMethod($method)) { | 
                                                        |
| 134 | - $xml = $this->addSessionIdToRequest($xml);  | 
                                                        |
| 135 | - }  | 
                                                        |
| 136 | -  | 
                                                        |
| 137 | - $callPayload = $this->createCallPayload($method, $arguments); // Specific API Call Envelope  | 
                                                        |
| 138 | -  | 
                                                        |
| 139 | - // Put Api call into common envelope  | 
                                                        |
| 140 | - $this->sxmlAppend($xml, $callPayload);  | 
                                                        |
| 141 | -  | 
                                                        |
| 142 | - return $xml;  | 
                                                        |
| 143 | - }  | 
                                                        |
| 144 | - /**  | 
                                                        |
| 145 | - * Builds a Guzzle Request Object  | 
                                                        |
| 146 | - * @method buildRequest  | 
                                                        |
| 147 | - * @author PA  | 
                                                        |
| 148 | - * @date 2017-01-11  | 
                                                        |
| 149 | - * @param string $method YourMembership API Method  | 
                                                        |
| 150 | - * @param array $arguments YourMembership API Method Call Arguments  | 
                                                        |
| 151 | - * @return \GuzzleHttp\Psr7\Request Guzzle Request Object  | 
                                                        |
| 152 | - */  | 
                                                        |
| 153 | - public function buildRequest(string $method, array $arguments) : \GuzzleHttp\Psr7\Request  | 
                                                        |
| 154 | -    { | 
                                                        |
| 155 | - $requestBody = $this->buildXMLBody($method, $arguments)->asXML();  | 
                                                        |
| 156 | -        return new GuzzleRequest('POST', self::BASE_URL, ['Content-Type' => 'application/x-www-form-urlencoded; charset=UTF8'], $requestBody); | 
                                                        |
| 157 | - }  | 
                                                        |
| 158 | -  | 
                                                        |
| 159 | - /**  | 
                                                        |
| 160 | - * Checks if Request Requires Session ID  | 
                                                        |
| 161 | - * @method isSessionRequiredForMethod  | 
                                                        |
| 162 | - * @author PA  | 
                                                        |
| 163 | - * @date 2017-01-10  | 
                                                        |
| 164 | - * @param string $method YourMembership API Method  | 
                                                        |
| 165 | - * @return bool  | 
                                                        |
| 166 | - */  | 
                                                        |
| 167 | - public function isSessionRequiredForMethod(string $method) : bool  | 
                                                        |
| 168 | -    { | 
                                                        |
| 169 | - //TODO Add config Logic for what API Methods require Session ID  | 
                                                        |
| 170 | - return ($method != 'Session.Create');  | 
                                                        |
| 171 | - }  | 
                                                        |
| 172 | -  | 
                                                        |
| 173 | - /**  | 
                                                        |
| 174 | - * Helper for Deep Copy for of $from element into $to element for SimpleXML  | 
                                                        |
| 175 | - * @method sxmlAppend  | 
                                                        |
| 176 | - * @author PA  | 
                                                        |
| 177 | - * @date 2017-01-09  | 
                                                        |
| 178 | - * @param \SimpleXMLElement $to  | 
                                                        |
| 179 | - * @param \SimpleXMLElement $from  | 
                                                        |
| 180 | - * @return void  | 
                                                        |
| 181 | - */  | 
                                                        |
| 182 | - private function sxmlAppend(\SimpleXMLElement $to, \SimpleXMLElement $from)  | 
                                                        |
| 183 | -    { | 
                                                        |
| 184 | - $toDom = dom_import_simplexml($to);  | 
                                                        |
| 185 | - $fromDom = dom_import_simplexml($from);  | 
                                                        |
| 186 | - $toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true));  | 
                                                        |
| 187 | - }  | 
                                                        |
| 188 | -  | 
                                                        |
| 189 | - /**  | 
                                                        |
| 190 | - * Adds the Session Variable to the given XML Request Payload  | 
                                                        |
| 191 | - * @method addSessionIdToRequest  | 
                                                        |
| 192 | - * @author PA  | 
                                                        |
| 193 | - * @date 2017-01-10  | 
                                                        |
| 194 | - * @param \SimpleXMLElement $requestXML Base Request XML Payload  | 
                                                        |
| 195 | - */  | 
                                                        |
| 196 | - private function addSessionIdToRequest(\SimpleXMLElement $requestXML) : \SimpleXMLElement  | 
                                                        |
| 197 | -    { | 
                                                        |
| 198 | -        $requestXML->addChild('SessionID', self::$sessionId); | 
                                                        |
| 199 | - return $requestXML;  | 
                                                        |
| 200 | - }  | 
                                                        |
| 201 | -  | 
                                                        |
| 202 | -  | 
                                                        |
| 203 | - /**  | 
                                                        |
| 204 | - * Setter Method for SessionID  | 
                                                        |
| 205 | - * @method setSessionId  | 
                                                        |
| 206 | - * @author PA  | 
                                                        |
| 207 | - * @date 2017-01-10  | 
                                                        |
| 208 | - * @param string $sessionId YourMembership Session ID  | 
                                                        |
| 209 | - */  | 
                                                        |
| 210 | - public static function setSessionId(string $sessionId)  | 
                                                        |
| 211 | -    { | 
                                                        |
| 212 | - self::$sessionId = $sessionId;  | 
                                                        |
| 213 | - }  | 
                                                        |
| 214 | -  | 
                                                        |
| 215 | - /**  | 
                                                        |
| 216 | - * Checks if we have an active session available  | 
                                                        |
| 217 | - * @method hasSession  | 
                                                        |
| 218 | - * @author PA  | 
                                                        |
| 219 | - * @date 2017-01-11  | 
                                                        |
| 220 | - * @return boolean  | 
                                                        |
| 221 | - */  | 
                                                        |
| 222 | - public function hasSession()  | 
                                                        |
| 223 | -    { | 
                                                        |
| 224 | - return !is_null(self::$sessionId);  | 
                                                        |
| 225 | - }  | 
                                                        |
| 63 | +		$xml = new \SimpleXMLElement('<YourMembership></YourMembership>'); | 
                                                        |
| 64 | +		$xml->addChild('Version', self::API_VERSION); | 
                                                        |
| 65 | +		$xml->addChild('ApiKey', $this->apiKey); | 
                                                        |
| 66 | +		$xml->addChild('CallID', self::$callId); | 
                                                        |
| 67 | +		$xml->addChild('SaPasscode', $this->saPasscode); | 
                                                        |
| 68 | +  | 
                                                        |
| 69 | + return $xml;  | 
                                                        |
| 70 | + }  | 
                                                        |
| 71 | +  | 
                                                        |
| 72 | + /**  | 
                                                        |
| 73 | + * Generates the XML for a API method call within  | 
                                                        |
| 74 | + * @method createCallPayload  | 
                                                        |
| 75 | + * @author PA  | 
                                                        |
| 76 | + * @throws YourMembershipRequestException  | 
                                                        |
| 77 | + * @date 2017-01-09  | 
                                                        |
| 78 | + * @param string $method YourMembership API Function Name  | 
                                                        |
| 79 | + * @param array $arguments Array of Arguments to be passed as part of the YourMembership "Call"  | 
                                                        |
| 80 | + * @return \SimpleXMLElement  | 
                                                        |
| 81 | + */  | 
                                                        |
| 82 | + public function createCallPayload(string $method, array $arguments) : \SimpleXMLElement  | 
                                                        |
| 83 | +	{ | 
                                                        |
| 84 | + //Create Call Node  | 
                                                        |
| 85 | +		$call = new \SimpleXMLElement('<Call> </Call>'); | 
                                                        |
| 86 | +		$call->addAttribute('Method', $method); | 
                                                        |
| 87 | +  | 
                                                        |
| 88 | + //Add Arguments to the Call Node  | 
                                                        |
| 89 | +		try { | 
                                                        |
| 90 | + $call = $this->sxmlAddChildrenRecursive($call, $arguments);  | 
                                                        |
| 91 | +		} catch (\Exception $e) { | 
                                                        |
| 92 | + throw new YourMembershipRequestException($e->getMessage(), $e->getCode(), $method, $arguments, $e);  | 
                                                        |
| 93 | + }  | 
                                                        |
| 94 | + return $call;  | 
                                                        |
| 95 | + }  | 
                                                        |
| 96 | + /**  | 
                                                        |
| 97 | + * Recursively builds the array into a XML Tree  | 
                                                        |
| 98 | + * //NOTE Child arrays must be associative  | 
                                                        |
| 99 | + * @method sxmlAddChildrenRecursive  | 
                                                        |
| 100 | + * @author PA  | 
                                                        |
| 101 | + * @date 2017-01-12  | 
                                                        |
| 102 | + * @param \SimpleXMLElement $root Root XML Node  | 
                                                        |
| 103 | + * @param array $arguments Array of Arguments to be added to XML Node  | 
                                                        |
| 104 | + * @return \SimpleXMLElement Resulting XML Tree  | 
                                                        |
| 105 | + */  | 
                                                        |
| 106 | + private function sxmlAddChildrenRecursive(\SimpleXMLElement $root, array $arguments) : \SimpleXMLElement  | 
                                                        |
| 107 | +	{ | 
                                                        |
| 108 | +		foreach ($arguments as $key => $value) { | 
                                                        |
| 109 | +			if (is_array($value)) { | 
                                                        |
| 110 | +				$child = new \SimpleXMLElement(sprintf('<%s></%s>', $key, $key)); | 
                                                        |
| 111 | + $this->sxmlAddChildrenRecursive($child, $value);  | 
                                                        |
| 112 | + $this->sxmlAppend($root, $child);  | 
                                                        |
| 113 | +			} else { | 
                                                        |
| 114 | + $root->addChild($key, $value);  | 
                                                        |
| 115 | + }  | 
                                                        |
| 116 | +  | 
                                                        |
| 117 | + }  | 
                                                        |
| 118 | + return $root;  | 
                                                        |
| 119 | + }  | 
                                                        |
| 120 | + /**  | 
                                                        |
| 121 | + * Builds The XML Request Body for the Your Membership API Call  | 
                                                        |
| 122 | + * @method buildXMLBody  | 
                                                        |
| 123 | + * @author PA  | 
                                                        |
| 124 | + * @date 2017-01-10  | 
                                                        |
| 125 | + * @param string $method Your Membership API Function Name  | 
                                                        |
| 126 | + * @param array $arguments Your Membership Arguments  | 
                                                        |
| 127 | + * @return \SimpleXMLElement  | 
                                                        |
| 128 | + */  | 
                                                        |
| 129 | + public function buildXMLBody(string $method, array $arguments) : \SimpleXMLElement  | 
                                                        |
| 130 | +	{ | 
                                                        |
| 131 | + $xml = $this->buildBasePayload(); // Common Envelope  | 
                                                        |
| 132 | +  | 
                                                        |
| 133 | +		if ($this->isSessionRequiredForMethod($method)) { | 
                                                        |
| 134 | + $xml = $this->addSessionIdToRequest($xml);  | 
                                                        |
| 135 | + }  | 
                                                        |
| 136 | +  | 
                                                        |
| 137 | + $callPayload = $this->createCallPayload($method, $arguments); // Specific API Call Envelope  | 
                                                        |
| 138 | +  | 
                                                        |
| 139 | + // Put Api call into common envelope  | 
                                                        |
| 140 | + $this->sxmlAppend($xml, $callPayload);  | 
                                                        |
| 141 | +  | 
                                                        |
| 142 | + return $xml;  | 
                                                        |
| 143 | + }  | 
                                                        |
| 144 | + /**  | 
                                                        |
| 145 | + * Builds a Guzzle Request Object  | 
                                                        |
| 146 | + * @method buildRequest  | 
                                                        |
| 147 | + * @author PA  | 
                                                        |
| 148 | + * @date 2017-01-11  | 
                                                        |
| 149 | + * @param string $method YourMembership API Method  | 
                                                        |
| 150 | + * @param array $arguments YourMembership API Method Call Arguments  | 
                                                        |
| 151 | + * @return \GuzzleHttp\Psr7\Request Guzzle Request Object  | 
                                                        |
| 152 | + */  | 
                                                        |
| 153 | + public function buildRequest(string $method, array $arguments) : \GuzzleHttp\Psr7\Request  | 
                                                        |
| 154 | +	{ | 
                                                        |
| 155 | + $requestBody = $this->buildXMLBody($method, $arguments)->asXML();  | 
                                                        |
| 156 | +		return new GuzzleRequest('POST', self::BASE_URL, ['Content-Type' => 'application/x-www-form-urlencoded; charset=UTF8'], $requestBody); | 
                                                        |
| 157 | + }  | 
                                                        |
| 158 | +  | 
                                                        |
| 159 | + /**  | 
                                                        |
| 160 | + * Checks if Request Requires Session ID  | 
                                                        |
| 161 | + * @method isSessionRequiredForMethod  | 
                                                        |
| 162 | + * @author PA  | 
                                                        |
| 163 | + * @date 2017-01-10  | 
                                                        |
| 164 | + * @param string $method YourMembership API Method  | 
                                                        |
| 165 | + * @return bool  | 
                                                        |
| 166 | + */  | 
                                                        |
| 167 | + public function isSessionRequiredForMethod(string $method) : bool  | 
                                                        |
| 168 | +	{ | 
                                                        |
| 169 | + //TODO Add config Logic for what API Methods require Session ID  | 
                                                        |
| 170 | + return ($method != 'Session.Create');  | 
                                                        |
| 171 | + }  | 
                                                        |
| 172 | +  | 
                                                        |
| 173 | + /**  | 
                                                        |
| 174 | + * Helper for Deep Copy for of $from element into $to element for SimpleXML  | 
                                                        |
| 175 | + * @method sxmlAppend  | 
                                                        |
| 176 | + * @author PA  | 
                                                        |
| 177 | + * @date 2017-01-09  | 
                                                        |
| 178 | + * @param \SimpleXMLElement $to  | 
                                                        |
| 179 | + * @param \SimpleXMLElement $from  | 
                                                        |
| 180 | + * @return void  | 
                                                        |
| 181 | + */  | 
                                                        |
| 182 | + private function sxmlAppend(\SimpleXMLElement $to, \SimpleXMLElement $from)  | 
                                                        |
| 183 | +	{ | 
                                                        |
| 184 | + $toDom = dom_import_simplexml($to);  | 
                                                        |
| 185 | + $fromDom = dom_import_simplexml($from);  | 
                                                        |
| 186 | + $toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true));  | 
                                                        |
| 187 | + }  | 
                                                        |
| 188 | +  | 
                                                        |
| 189 | + /**  | 
                                                        |
| 190 | + * Adds the Session Variable to the given XML Request Payload  | 
                                                        |
| 191 | + * @method addSessionIdToRequest  | 
                                                        |
| 192 | + * @author PA  | 
                                                        |
| 193 | + * @date 2017-01-10  | 
                                                        |
| 194 | + * @param \SimpleXMLElement $requestXML Base Request XML Payload  | 
                                                        |
| 195 | + */  | 
                                                        |
| 196 | + private function addSessionIdToRequest(\SimpleXMLElement $requestXML) : \SimpleXMLElement  | 
                                                        |
| 197 | +	{ | 
                                                        |
| 198 | +		$requestXML->addChild('SessionID', self::$sessionId); | 
                                                        |
| 199 | + return $requestXML;  | 
                                                        |
| 200 | + }  | 
                                                        |
| 201 | +  | 
                                                        |
| 202 | +  | 
                                                        |
| 203 | + /**  | 
                                                        |
| 204 | + * Setter Method for SessionID  | 
                                                        |
| 205 | + * @method setSessionId  | 
                                                        |
| 206 | + * @author PA  | 
                                                        |
| 207 | + * @date 2017-01-10  | 
                                                        |
| 208 | + * @param string $sessionId YourMembership Session ID  | 
                                                        |
| 209 | + */  | 
                                                        |
| 210 | + public static function setSessionId(string $sessionId)  | 
                                                        |
| 211 | +	{ | 
                                                        |
| 212 | + self::$sessionId = $sessionId;  | 
                                                        |
| 213 | + }  | 
                                                        |
| 214 | +  | 
                                                        |
| 215 | + /**  | 
                                                        |
| 216 | + * Checks if we have an active session available  | 
                                                        |
| 217 | + * @method hasSession  | 
                                                        |
| 218 | + * @author PA  | 
                                                        |
| 219 | + * @date 2017-01-11  | 
                                                        |
| 220 | + * @return boolean  | 
                                                        |
| 221 | + */  | 
                                                        |
| 222 | + public function hasSession()  | 
                                                        |
| 223 | +	{ | 
                                                        |
| 224 | + return !is_null(self::$sessionId);  | 
                                                        |
| 225 | + }  | 
                                                        |
| 226 | 226 | |
| 227 | 227 | }  |