@@ -22,46 +22,53 @@ class Webhook implements WebhookInterface
2222 private const XML_UTF8_CONTENT_TYPE = "text/xml; charset=UTF-8 " ;
2323 private const STATUSCODE_BAD_REQUEST = 400 ;
2424
25- public string $ service ;
26- public ?SimpleXMLElement $ xmlObject ;
25+ public string $ service = '' ;
2726
28- private string $ xmlInterface ;
29- private string $ xmlPayloadKey ;
27+ public ?SimpleXMLElement $ xmlObject = null ;
28+
29+ private string $ xmlInterface = '' ;
30+
31+ private string $ xmlPayloadKey = '' ;
3032
3133 public function __construct (
3234 private $ senderID ,
33- private $ environment = BLUEM_ENVIRONMENT_TESTING
35+ private $ environment = BLUEM_ENVIRONMENT_TESTING ,
36+ private $ webhookData = ''
3437 ) {
35- $ this ->parse ();
38+ $ this ->parse ($ this -> webhookData );
3639 }
3740
38- private function parse (): void
41+ private function parse ($ xmlData = '' ): void
3942 {
40- if (!$ this ->isHttpsRequest ()) {
41- $ this ->exitWithError ('Not HTTPS ' );
42- return ;
43- }
43+ if (empty ($ xmlData ))
44+ {
45+ if (!$ this ->isHttpsRequest ()) {
46+ $ this ->exitWithError ('Not HTTPS ' );
47+ return ;
48+ }
4449
45- if ($ _SERVER ['REQUEST_METHOD ' ] !== 'POST ' ) {
46- $ this ->exitWithError ('Not POST ' );
47- return ;
48- }
50+ if ($ _SERVER ['REQUEST_METHOD ' ] !== 'POST ' ) {
51+ $ this ->exitWithError ('Not POST ' );
52+ return ;
53+ }
4954
50- // Check: An empty POST to the URL (normal HTTP request) always has to respond with HTTP 200 OK.
51- $ postData = file_get_contents ('php://input ' );
55+ // Check: content type: XML with utf-8 encoding
56+ if ($ _SERVER ["CONTENT_TYPE " ] !== self ::XML_UTF8_CONTENT_TYPE ) {
57+ $ this ->exitWithError ('Wrong Content-Type given: should be XML with UTF-8 encoding ' );
58+ return ;
59+ }
5260
53- if (empty ($ postData )) {
54- $ this ->exitWithError ('No data body given ' );
55- return ;
56- }
61+ // Check: An empty POST to the URL (normal HTTP request) always has to respond with HTTP 200 OK.
62+ $ xmlData = file_get_contents ('php://input ' );
5763
58- // Check: content type: XML with utf-8 encoding
59- if ( $ _SERVER [ " CONTENT_TYPE " ] !== self :: XML_UTF8_CONTENT_TYPE ) {
60- $ this -> exitWithError ( ' Wrong Content-Type given: should be XML with UTF-8 encoding ' ) ;
61- return ;
64+ if ( empty ( $ xmlData )) {
65+ $ this -> exitWithError ( ' No data body given ' );
66+ return ;
67+ }
6268 }
6369
64- $ xmlObject = $ this ->parseRawXML ($ postData );
70+ $ xmlObject = $ this ->parseRawXML ($ xmlData );
71+
6572 if (! $ xmlObject instanceof \SimpleXMLElement) {
6673 $ this ->exitWithError ('Could not parse XML ' );
6774 return ;
@@ -73,7 +80,7 @@ private function parse(): void
7380 return ;
7481 }
7582
76- $ signatureValidation = (new WebhookSignatureValidation ($ this ->environment ))->validate ($ postData );
83+ $ signatureValidation = (new WebhookSignatureValidation ($ this ->environment ))->validate ($ xmlData );
7784 if (! $ signatureValidation ::$ isValid ) {
7885 $ this ->exitWithError ($ signatureValidation ->errorMessage ());
7986 return ;
@@ -137,9 +144,9 @@ private function getPayloadValue(string $key): SimpleXMLElement|string|null
137144 if ((is_countable ($ payload ->children ()) ? count ($ payload ->children ()) : 0 ) > 0 ) {
138145 return $ payload ;
139146 }
140-
141- return $ payload ->$ key . '' ?? '' ;
147+ return $ payload ->$ key ?? '' ;
142148 }
149+
143150 private function getPayload (): SimpleXMLElement
144151 {
145152 if ($ this ->isEmandates ()) {
@@ -197,7 +204,11 @@ public function getPurchaseID(): ?string
197204 public function getStatus (): ?string
198205 {
199206 if ($ this ->isPayments ()) {
200- return $ this ->getPayloadValue ('Status ' );
207+ $ value = $ this ->getPayloadValue ('Status ' );
208+
209+ if (!empty ($ value )) {
210+ return $ value ;
211+ }
201212 }
202213 return $ this ->getPayload ()->Status . "" ;
203214 }
0 commit comments