createToken
Creating a token
Three files are required for creating a token:
- a file for the functions "function.php".
- a file for defining objects "v5.php".
- createToken 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.co.in/vads-ws/v5?wsdl"; //Example of Initialisation of a SOAP client with SNI management /* $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.co.in'))) )); */ //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; $commonRequest->submissionDate = new DateTime('now',new DateTimeZone('UTC')); $cardRequest = new cardRequest; $cardRequest->number = "4970100000000003"; $cardRequest->scheme = "VISA"; $cardRequest->expiryMonth = "12"; $cardRequest->expiryYear = "2023"; $cardRequest->cardSecurityCode = "123"; $customerRequest = new customerRequest; $customerRequest->billingDetails = new billingDetailsRequest; $customerRequest->billingDetails->email="nom.prenom@exemple.com"; $customerRequest->extraDetails = new extraDetailsRequest; $customerRequest->extraDetails->sendMail ="1"; $customerRequest->extraDetails->ipAddress ="127.0.0.1"; //Call to the createToken operation try { $createTokenRequest = new createToken; $createTokenRequest->commonRequest = $commonRequest; $createTokenRequest->cardRequest = $cardRequest; $createTokenRequest->customerRequest = $customerRequest; $createTokenRequest->commonRequest->submissionDate = $createTokenRequest->commonRequest->submissionDate->format(dateTime::W3C); $createTokenResponse = $client->createToken($createTokenRequest); } 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 ($createPaymentResponse->createPaymentResult->commonResponse->responseCode != "0"){ //process error } else{ //Process successfully completed //Checking the presence of the transactionStatusLabel: if (isset ($createPaymentResponse->createPaymentResult->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 ($createTokenResponse->createTokenResult->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; } } } } ?>