ResponseParser.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace AmazonPay;
  3. /* ResponseParser
  4. * Methods provided to convert the Response from the POST to XML, Array or JSON
  5. */
  6. require_once 'ResponseInterface.php';
  7. class ResponseParser implements ResponseInterface
  8. {
  9. public $response = null;
  10. public function __construct($response=null)
  11. {
  12. $this->response = $response;
  13. }
  14. /* Returns the XML portion of the response */
  15. public function toXml()
  16. {
  17. return $this->response['ResponseBody'];
  18. }
  19. /* toJson - converts XML into Json
  20. * @param $response [XML]
  21. */
  22. public function toJson()
  23. {
  24. $response = $this->simpleXmlObject();
  25. return (json_encode($response));
  26. }
  27. /* toArray - converts XML into associative array
  28. * @param $this->response [XML]
  29. */
  30. public function toArray()
  31. {
  32. $response = $this->simpleXmlObject();
  33. // Converting the SimpleXMLElement Object to array()
  34. $response = json_encode($response);
  35. return (json_decode($response, true));
  36. }
  37. private function simpleXmlObject()
  38. {
  39. $response = $this->response;
  40. // Getting the HttpResponse Status code to the output as a string
  41. $status = strval($response['Status']);
  42. // Getting the Simple XML element object of the XML Response Body
  43. $response = simplexml_load_string((string) $response['ResponseBody']);
  44. // Adding the HttpResponse Status code to the output as a string
  45. $response->addChild('ResponseStatus', $status);
  46. return $response;
  47. }
  48. /* Get the status of the Order Reference ID */
  49. public function getOrderReferenceDetailsStatus($response)
  50. {
  51. $oroStatus = $this->getStatus('GetORO', '//GetORO:OrderReferenceStatus', $response);
  52. return $oroStatus;
  53. }
  54. /* Get the status of the BillingAgreement */
  55. public function getBillingAgreementDetailsStatus($response)
  56. {
  57. $baStatus = $this->getStatus('GetBA', '//GetBA:BillingAgreementStatus', $response);
  58. return $baStatus;
  59. }
  60. private function getStatus($type, $path, $response)
  61. {
  62. $data= new \SimpleXMLElement($response);
  63. $namespaces = $data->getNamespaces(true);
  64. foreach($namespaces as $key=>$value){
  65. $namespace = $value;
  66. }
  67. $data->registerXPathNamespace($type, $namespace);
  68. foreach ($data->xpath($path) as $value) {
  69. $status = json_decode(json_encode((array)$value), TRUE);
  70. }
  71. return $status;
  72. }
  73. }