22
33namespace Omnipay \NewebPay \Message ;
44
5+ use Omnipay \Common \Exception \InvalidRequestException ;
56use Omnipay \Common \Exception \InvalidResponseException ;
67use Omnipay \NewebPay \Traits \HasDefaults ;
78
@@ -60,9 +61,13 @@ public function getIndexType()
6061 return $ this ->getParameter ('IndexType ' ) ?: 1 ;
6162 }
6263
64+ /**
65+ * @throws InvalidRequestException
66+ * @throws InvalidResponseException
67+ */
6368 public function getData ()
6469 {
65- return array_filter ([
70+ $ postData = array_filter ([
6671 'RespondType ' => $ this ->getRespondType (),
6772 'Version ' => $ this ->getVersion () ?: '1.0 ' ,
6873 'Amt ' => (int ) $ this ->getAmount (),
@@ -73,37 +78,25 @@ public function getData()
7378 ], static function ($ value ) {
7479 return $ value !== null && $ value !== '' ;
7580 });
76- }
7781
78- /**
79- * @throws InvalidResponseException
80- */
81- public function sendData ($ data )
82- {
8382 $ response = $ this ->httpClient ->request ('POST ' , $ this ->getEndpoint (), [
8483 'Content-Type ' => 'application/x-www-form-urlencoded ' ,
8584 ], http_build_query ([
8685 'MerchantID_ ' => $ this ->getMerchantID (),
87- 'PostData_ ' => $ this ->encrypt ($ data ),
86+ 'PostData_ ' => $ this ->encrypt ($ postData ),
8887 ]));
8988
90- $ body = trim ((string ) $ response ->getBody ());
91-
92- $ result = json_decode ($ body , true );
89+ $ decode = $ this ->decodeResponse ($ response );
9390
94- if (json_last_error () === JSON_ERROR_NONE ) {
95- if (! hash_equals ($ result ['Result ' ]['CheckCode ' ], $ this ->checkCode ($ result ['Result ' ]))) {
96- throw new InvalidResponseException ('Incorrect CheckCode ' );
97- }
98- } else {
99- $ result = [];
100- parse_str (trim ((string ) $ response ->getBody ()), $ result );
101-
102- if (! hash_equals ($ result ['CheckCode ' ], $ this ->checkCode ($ result ))) {
103- throw new InvalidResponseException ('Incorrect CheckCode ' );
104- }
91+ if (! hash_equals ($ decode ['CheckCode ' ], $ this ->checkCode ($ decode ))) {
92+ throw new InvalidResponseException ('Incorrect CheckCode ' );
10593 }
10694
107- return $ this ->response = new VoidResponse ($ this , $ result );
95+ return $ decode ;
96+ }
97+
98+ public function sendData ($ data )
99+ {
100+ return $ this ->response = new VoidResponse ($ this , $ data );
108101 }
109102}
0 commit comments