<?php
/**
*
*
* @author Michael J. Burgess
* @package SnPayPal
* @copyright Michael J. Burgess, 2008
* @version 1.0
* @licence MIT
*/
/**
* Handles soap envelope generation and sending (via SnPayPalStream)
*
*/
class SnPayPalApiInterface extends SnPayPalStream
{
/**
* Will be set to read the body of the SOAP envelope (i.e. not the HEAD etc.)
*
* @var boolean
*/
public $rtnSoapBody = true;
/**
* Contains SimpleXMLElement object
* resulting from the parsing of a paypal soap response
*
* @var SimpleXMLElement
*/
private $xml;
/**
* SOAP XML Sent to PayPal
*
* @var string
*/
private $soap;
/**
* Return SimpleXML form of the SOAP response
* You can check if it is a valid response various ways
* e.g. if($response instanceof PayPalSoapResponse)
*
* You can use $property to return a subsection of the response
* (to get the bits that contain the main info, e.g. 'GetBalanceResponse')
*
* @param string $property
* @return PayPalSoapResponse
*/
public function getResponse($property = null)
{
return (empty($property)) ? $this->xml : $this->xml->$property;
}
/**
* Returns the SOAP XML packet sent (for e.g. debugging)
*
* @return string
*/
public function getSoapSent()
{
return $this->soap;
}
/**
* Converts array of SnPayPalParameters into SOAP xml
*
* @param array $array
* @return string
*/
public static function arrayToXml($array = null)
{
if(empty($array)) return '';
$parameters = '';
foreach ($array as $parameter)
{
$attr = (empty($parameter->attribute)) ? '' : " $parameter->attribute";
$parameters .= "
<$parameter->name xsi:type=\"$parameter->type\"$attr>$parameter->value</$parameter->name>";
}
return $parameters;
}
/**
* Send soap envelope
*
* Access response via ->getXml() or ->getResponse()
*
* @param string $request SOAP Env.
* @return boolean
*/
public function putToApi($request)
{
$this->open();
$this->putString($request);
$this->close();
return (isset($this->response) && !is_bool($this->response));
}
/**
* Call PayPal API
*
* @param string $api
* @param mixed $parameters
*/
public function callAPI($api, $parameters = null)
{
$xmlMainSection = $this->arrayToXml((array) $parameters);
$request = $api.'Request';
$req = $api.'Req';
$soap = "
<$req xmlns=\"urn:ebay:api:PayPalAPI\">
<$request xsi:type=\"ns:{$request}Type\">";
$soap .= '
<Version xmlns="urn:ebay:apis:eBLBaseComponents" xsi:type="xsd:string">1.0</Version>';
$soap .= "$xmlMainSection
</$request>
</$req>";
$this->soap = $this->newSoapEnvelope($soap);
if($this->putToApi($this->soap))
{
$this->xmlLoadString();
return $this;
}
else return false;
}
/**
* Get an object representation of the SOAP response,
* if $this->rtnSoapBody is set to true (default) then the body of the response is returned
* (as oppose to including the head which contains meta)
*
*/
private function xmlLoadString()
{
$xml = simplexml_load_string($this->response, 'PayPalSoapResponse');
if($this->rtnSoapBody)
{
$xml = $xml->xpath('/SOAP-ENV:Envelope/SOAP-ENV:Body');
$this->xml = $xml[0];
}
else
{
$this->xml = $xml;
}
}
/**
* Generate the XML required for a new SOAP envelope
*
* @param string $body
* @return string
*/
public function newSoapEnvelope($body)
{
$signature = SnPayPalConfig::SIGNATURE;
$signature = (empty($signature)) ? '' : "<Signature>$signature</Signature>";
return '<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/1999/XMLSchema"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Header>
<RequesterCredentials xmlns="urn:ebay:api:PayPalAPI" SOAP-ENV:mustUnderstand="1">
<Credentials xmlns="urn:ebay:apis:eBLBaseComponents">
<Username>'. SnPayPalConfig::USERNAME .'</Username>
<Password>'. SnPayPalConfig::PASSWORD .'</Password>
'.$signature.'
</Credentials>
</RequesterCredentials>
</SOAP-ENV:Header>
<SOAP-ENV:Body>'. $body .'
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>';
}
}
?>