cancelSubscription

Three files are required for creating a subscription:

  • a file for the functions "function.php".

  • a file for defining objects "v5.php".

  • createSubscription file (operation):
    <?php
    include_once 'v5.php'; 	// File containing the definition of different objects 
    include_once 'function.php';// File containing all the useful functions (generation of the uuid, etc.)
    
    //Intialization of variables
    $shopId = "12345678";
    $key = "1234567891234567";	
    $mode = "TEST";
    $wsdl =  "https://secure.lyra.com/vads-ws/v5?wsdl";
    
    //Example of Initialization of a SOAP client without proxy	
    		$client = new soapClient($wsdl, $options = array(
    					'trace'=>1, 
    					'exceptions'=> 0, 			
    					'encoding' => 'UTF-8',
    					'soapaction' => '')
    				);		
    
    //Generating a header
    			$requestId = gen_uuid ();
    			$timestamp = gmdate ( "Y-m-d\TH:i:s\Z" );			
    			$authToken = base64_encode(hash_hmac('sha256',$requestId.$timestamp, $key, true));			
    			setHeaders ($shopId, $requestId, $timestamp, $mode, $authToken, $key, $client);
    			
    //Generating a body
    	
    	$commonRequest = new commonRequest;
    	
    	$queryRequest = new queryRequest;
    			$queryRequest->paymentToken = "c975d6af1d5e478da3570d43494d86d2";
    			$queryRequest->submissionDate = "2015-08-28T09:21:34+00:00";
    			$queryRequest->subscriptionId = "20150828i6VFSA";
    			
    //	Call to the cancelSubscription operation	
    	try {		
    		$cancelSubscriptionRequest = new cancelSubscription;
    			$cancelSubscriptionRequest->commonRequest = $commonRequest;
    			$cancelSubscriptionRequest->queryRequest = $queryRequest;
    	
    		$cancelSubscriptionResponse = $client->cancelSubscription($cancelSubscriptionRequest);
    	} catch (SoapFault $fault) {
    		
    //Managing exceptions
    	
    	trigger_error("SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})", E_USER_ERROR);
    				 }
    	
    		/* Displaying XML logs that must be replaced in a log file.	
    		 *
    		 * WARNING DO NOT REGISTER CARD NUMBERS IN YOUR LOGS
    		 */
    			echo "<hr> [Request Header] <br/>", htmlspecialchars($client->__getLastRequestHeaders()), "<br/>";
    			echo "<hr> [Request] <br/>", htmlspecialchars($client->__getLastRequest()), "<br/>";	
    			echo "<hr> [Response Header]<br/>", htmlspecialchars($client->__getLastResponseHeaders()), "<br/>";
    			echo "<hr> [Response]<br/>", htmlspecialchars($client->__getLastResponse()), "<br/>";
    			echo '<hr>';			
    			echo "<hr> [Response SOAP Headers]<br/>";
    
    ///Response analysis
    	//Retrieving the SOAP Header of the response to store the headers in a table (here $responseHeader)
    		$dom = new DOMDocument;
            $dom->loadXML($client->__getLastResponse(), LIBXML_NOWARNING);
            $path = new DOMXPath($dom);
            $headers = $path->query('//*[local-name()="Header"]/*');			
    		$responseHeader = array();				
    		foreach($headers as $headerItem) {			
    			$responseHeader[$headerItem->nodeName] = $headerItem->nodeValue;				
    		}	
    	
    	//Computation of the authentication token of the response				
    		$authTokenResponse = base64_encode(hash_hmac('sha256',$responseHeader['timestamp'].$responseHeader['requestId'], $key, true));			
    		if ($authTokenResponse !== $responseHeader['authToken']){			
    			//Computation error or attempted fraud echo 'Internal error';
    		}
    		else{
    			
    			//Response analysis
    			if ($cancelSubscriptionResponse->cancelSubscriptionResult->commonResponse->responseCode != "0"){
    				//process error				
    				}
    				else{
    				
    				//Process successfully completed					
    					//Checking the presence of the transactionStatusLabel:
    					if (isset ($cancelSubscriptionResponse->cancelSubscriptionResult->commonResponse->transactionStatusLabel)){
    					//The card is not enrolled or 3DS deactivated											
    						
    							// The payment is accepted	
    								// The code below must be modified to integrate database updates etc.
    								
    								switch ($cancelSubscriptionResponse->cancelSubscriptionResult->commonResponse->transactionStatusLabel){							
    								case "AUTHORISED":
    									echo "payment accepted";
    								break;
    								case "WAITING_AUTHORISATION":
    									echo "payment accepted";
    									break;
    									case "AUTHORISED_TO_VALIDATE":
    									echo "payment accepted";
    									break;
    									case "WAITING_AUTHORISATION_TO_VALIDATE":
    									echo "payment accepted";
    									break;							
    								// The payment is declined							
    									default:
    										echo "payment declined";
    									break;									
    								}						
    					}
    					
    				}
    			}
    ?>