createSubscription

The createSubscription operation allows to make recurring payments (subscription).

It requires an existing and valid token.

The token can be created via:

  • the createToken operation,

    or

  • by form (see Implementation guideHosted Payment Page).

Three files are required for creating a token:

  • a "function.php" file for functions.

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

  • 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.payzen.eu/vads-ws/v5?wsdl";
    
    //Example of initialization of a SOAP client supporting SNI 
    		/*
    		$client = new soapClient($wsdl,	
    		$options = array('trace'=>1, 'exceptions'=> 0, 			
    					'encoding' => 'UTF-8','soapaction' => '',
    					'uri' => 'http://v5.ws.vads.lyra.com/',	            
    					'cache_wsdl' => WSDL_CACHE_NONE, 
    					//Proxy parameters
    					'proxy_host' => 'my.proxy.host',
    					'proxy_port' => 3128,
    					'stream_context' => stream_context_create (array('ssl' => array(                               
    								'SNI_enabled' => true,
    								'SNI_server_name' => 'secure.payzen.eu')))
    					));
    		*/	
    
    //Example of initialization of a SOAP client without a proxy		
    		$client = new soapClient($wsdl, $options = array(
    					'trace'=>1, 
    					'exceptions'=> 0, 			
    					'encoding' => 'UTF-8',
    					'soapaction' => '')
    				);		
    
    //Generation of 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);
    		
    //Generation of a body
    	
    	$commonRequest = new commonRequest;
    			$commonRequest->submissionDate = new DateTime('now',new DateTimeZone('UTC'));
    	
    	$orderRequest = new orderRequest;
    			$orderRequest->orderId = "myFirstSubscription";
    
    	$subscriptionRequest = new subscriptionRequest;
    			$subscriptionRequest->effectDate = "2016-07-10T18:00:00Z";	
    			$subscriptionRequest->amount = "30000";
    			$subscriptionRequest->currency = "978953756986840952356";
    			$subscriptionRequest->initialAmount = "1000";
    			$subscriptionRequest->initialAmountNumber = "1";
    			$subscriptionRequest->rrule = "RRULE:FREQ=MONTHLY;COUNT=12;BYMONTHDAY=10";
    			
    	$cardRequest = new cardRequest;
    			$cardRequest->paymentToken = "MyToken";
    			
    //Call to the createSubscription operation		
      try {		
    		$createSubscriptionRequest = new createSubscription;
    			$createSubscriptionRequest->commonRequest = $commonRequest;
    			$createSubscriptionRequest->orderRequest = $orderRequest;
    			$createSubscriptionRequest->cardRequest = $cardRequest; 
    			$createSubscriptionRequest->subscriptionRequest = $subscriptionRequest;
    	
    		$createSubscriptionRequest->commonRequest->submissionDate = $createSubscriptionRequest->commonRequest->submissionDate->format(dateTime::W3C);	
    		
    		$createSubscriptionResponse = $client->createSubscription($createSubscriptionRequest);
    	} catch (SoapFault $fault) {
    		
    //Handling 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 ($createPaymentResponse->createPaymentResult->commonResponse->responseCode != "0"){				
    				//process error				
    				}
    				else{
    				//Process successfully completed					
    					//Checking the presence of the transactionStatusLabel:
    					if (isset ($createSubscriptionResponse->createSubscriptionResult->commonResponse->transactionStatusLabel)){
    					//The card is not enrolled or 3DS disabled											
    						
    							// The payment is accepted	
    								// The code below must be modified to integrate database updates etc.
    								
    								switch ($createPaymentResponse->createPaymentResult->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;									
    								}						
    					}
    					
    				}
    			}
    ?>