Created
September 6, 2016 16:27
-
-
Save dnetix/a92eead1f3b46f53853a247c725a1cdd to your computer and use it in GitHub Desktop.
Quick spaghetti code for a SOAP connection example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This is a really quick SOAP connection example, by no means it should be the | |
* definitive way to connect with PHP | |
*/ | |
header('Content-Type: text/plain'); | |
$client = new SoapClient('http://redirection.dnetix.co/soap/redirect?wsdl'); | |
$login = ''; | |
$tranKey = ''; | |
// Generating a random Nonce | |
if (function_exists('random_bytes')) { | |
$Nonce = bin2hex(random_bytes(16)); | |
} elseif (function_exists('openssl_random_pseudo_bytes')) { | |
$Nonce = bin2hex(openssl_random_pseudo_bytes(16)); | |
} else { | |
$Nonce = mt_rand(); | |
} | |
$Created = date('c'); | |
$PasswordDigest = base64_encode(sha1($Nonce . $Created . $tranKey, true)); | |
// Example for a basic request | |
$RedirectRequest = [ | |
'locale' => 'es_CO', | |
'buyer' => [ | |
'documentType' => 'CC', | |
'document' => '123456789', | |
'name' => 'Pedro', | |
'surname' => 'Perez', | |
'email' => '[email protected]', | |
'mobile' => '3218450961' | |
], | |
'payment' => [ | |
'reference' => 'e2622059', | |
'description' => 'Pago de prueba', | |
'amount' => [ | |
'currency' => 'COP', | |
'total' => '120000', | |
], | |
'allowPartial' => 'false' | |
], | |
'expiration' => date('c', strtotime('+1 day')), | |
'returnUrl' => 'http://www.yoursite.com/needed/return', | |
'ipAddress' => '127.0.0.1', | |
'userAgent' => isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'CLIENT_USER_AGENT' | |
]; | |
// Authentication Soap Header Generation | |
$UsernameToken = new stdClass(); | |
$UsernameToken->Username = new SoapVar($login, XSD_STRING, NULL, 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd', NULL, 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'); | |
$UsernameToken->Password = new SoapVar($PasswordDigest, XSD_STRING, 'PasswordDigest', NULL, 'Password', 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'); | |
$UsernameToken->Nonce = new SoapVar(base64_encode($Nonce), XSD_STRING, null, 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd', null, 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'); | |
$UsernameToken->Created = new SoapVar($Created, XSD_STRING, NULL, 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd', null, 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd'); | |
$security = new stdClass(); | |
$security->UsernameToken = new SoapVar($UsernameToken, SOAP_ENC_OBJECT, NULL, 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd', 'UsernameToken', 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'); | |
$header = new SoapHeader('http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd', 'Security', $security, true); | |
// Creating the payment request | |
$requestId = null; | |
try { | |
$client->__setSoapHeaders($header); | |
$response = $client->createRequest([ | |
'payload' => $RedirectRequest | |
]); | |
if ($response) { | |
$response = $response->createRequestResult; | |
if ($response->status->status == 'OK') { | |
// Correct response, store the requestId and redirect the client to the processUrl | |
echo "\n-- createRequest Response --\n"; | |
$requestId = $response->requestId; | |
var_dump($response); | |
} else { | |
// Something went wrong, check the status->message to verify | |
var_dump($response); | |
} | |
} else { | |
// Error al conectar | |
} | |
} catch (Exception $e) { | |
echo "Error on createRequest\n"; | |
var_dump($e->getMessage()); | |
} | |
// Only if the createRequest worked this is called, but you can call this method with all of | |
// that requestId generated by your site | |
if($requestId) { | |
try { | |
$client->__setSoapHeaders($header); | |
$response = $client->getRequestInformation([ | |
'requestId' => $requestId | |
]); | |
if ($response) { | |
$response = $response->getRequestInformationResult; | |
if ($response->status->status == 'OK') { | |
// Correct response | |
echo "\n-- getRequestInformation Response --\n"; | |
var_dump($response); | |
} else { | |
// Something went wrong, check the status->message to verify | |
var_dump($response); | |
} | |
} else { | |
// Error al conectar | |
} | |
} catch (Exception $e) { | |
echo "Error on getRequestInformation\n"; | |
var_dump($e->getMessage()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment