Skip to content

Commit

Permalink
ResponseType JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
recca0120 committed Nov 12, 2023
1 parent dfdedeb commit 76006b5
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 10 deletions.
19 changes: 14 additions & 5 deletions src/Message/VoidRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,22 @@ public function sendData($data)
'MerchantID_' => $this->getMerchantID(),
'PostData_' => $this->encrypt($data),
]));
var_dump((string) $response->getBody());

$result = [];
parse_str(trim((string) $response->getBody()), $result);
$body = trim((string) $response->getBody());

if (! hash_equals($result['CheckCode'], $this->checkCode($result))) {
throw new InvalidResponseException('Incorrect CheckCode');
$result = json_decode($body, true);

if (json_last_error() === JSON_ERROR_NONE) {
if (! hash_equals($result['Result']['CheckCode'], $this->checkCode($result['Result']))) {
throw new InvalidResponseException('Incorrect CheckCode');
}
} else {
$result = [];
parse_str(trim((string) $response->getBody()), $result);

if (! hash_equals($result['CheckCode'], $this->checkCode($result))) {
throw new InvalidResponseException('Incorrect CheckCode');
}
}

return $this->response = new VoidResponse($this, $result);
Expand Down
12 changes: 8 additions & 4 deletions src/Message/VoidResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ public function getMessage()
return $this->data['Message'];
}

public function getTransactionId()
public function getTransactionReference()
{
return $this->data["MerchantOrderNo"];
return array_key_exists('Result', $this->data)
? $this->data['Result']['TradeNo']
: $this->data['TradeNo'];
}

public function getTransactionReference()
public function getTransactionId()
{
return $this->data["TradeNo"];
return array_key_exists('Result', $this->data)
? $this->data['Result']['MerchantOrderNo']
: $this->data['MerchantOrderNo'];
}
}
34 changes: 33 additions & 1 deletion tests/Message/VoidRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class VoidRequestTest extends TestCase
{
public function testGetData(): void
public function testSendData(): void
{
$timestamp = 1641348593;
$request = new VoidRequest($this->getHttpClient(), $this->getHttpRequest());
Expand All @@ -30,6 +30,7 @@ public function testGetData(): void
]));

$response = $request->send();

self::assertTrue($response->isSuccessful());
self::assertEquals('SUCCESS', $response->getCode());
self::assertEquals('放棄授權成功', $response->getMessage());
Expand All @@ -42,4 +43,35 @@ public function testGetData(): void
$postData['PostData_']
);
}

public function testSendDataForJSON(): void
{
$timestamp = 1641348593;
$request = new VoidRequest($this->getHttpClient(), $this->getHttpRequest());

$this->setMockHttpResponse('VoidSuccessJSON.txt');

$request->initialize(array_merge([
'HashKey' => 'Fs5cX1TGqYM2PpdbE14a9H83YQSQF5jn',
'HashIV' => 'C6AcmfqJILwgnhIP',
'MerchantID' => 'MS127874575',
'testMode' => true,
], [
'RespondType' => 'JSON',
'Version' => '1.0',
'TimeStamp' => $timestamp,
'Amt' => '30',
'MerchantOrderNo' => "Vanespl_ec_".$timestamp,
'IndexType' => '1',
'PayerEmail' => 'tek.chen@ezpay.com.tw',
]));

$response = $request->send();

self::assertTrue($response->isSuccessful());
self::assertEquals('SUCCESS', $response->getCode());
self::assertEquals('放棄授權成功', $response->getMessage());
self::assertEquals('Vanespl_ec_1641348593', $response->getTransactionId());
self::assertEquals('23111221191660146', $response->getTransactionReference());
}
}
13 changes: 13 additions & 0 deletions tests/Mock/VoidSuccessJSON.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Strict-Transport-Security: max-age=63072000
X-Akamai-Transformed: 9 234 0 pmb=mRUM,1
Expires: Sun, 12 Nov 2023 13:33:03 GMT
Cache-Control: max-age=0, no-cache, no-store
Pragma: no-cache
Date: Sun, 12 Nov 2023 13:33:03 GMT
Content-Length: 234
Connection: keep-alive
Server-Timing: cdn-cache; desc=MISS, edge; dur=55, origin; dur=51, ak_p; desc="1699795982548_1752377015_139633467_10571_2669_5_828_-";dur=1

{"Status":"SUCCESS","Message":"\u653e\u68c4\u6388\u6b0a\u6210\u529f","Result":{"MerchantID":"MS127874575","Amt":30,"MerchantOrderNo":"Vanespl_ec_1641348593","TradeNo":"23111221191660146","CheckCode":"A71CAB190914DD9432C49B49AA71A5A291EFFFBFFE149417818C0E04D3BEB40B"}}

0 comments on commit 76006b5

Please sign in to comment.