你可能現在已經想通了。但是我發佈了我所有看到這個問題的人的解決方案。 我創建了一個數組來分解$ tresponse對象。
if ($response != null){
$tresponse = $response->getTransactionResponse();
$result = array(
'response' => ($response != null)? true: false,
'getResultCode' => $response->getMessages()->getResultCode(),
'getMessages' => ($response->getTransactionResponse()->getMessages() != null)? array(
'msjCode' => $response->getTransactionResponse()->getMessages()[0]->getCode(),
'msjDesc' => $response->getTransactionResponse()->getMessages()[0]->getDescription()
): null,
'getResponseCode' => ($tresponse->getResponseCode() != null)? $tresponse->getResponseCode(): null,
'getAuthCode' => ($tresponse->getAuthCode() != null)? $tresponse->getAuthCode(): null,
'getTransId' => ($tresponse->getTransId() != null)? $tresponse->getTransId(): null,
'messageCode' => ($tresponse->getMessages() != null)? $tresponse->getMessages()[0]->getCode(): null,
'msgDescription' => ($tresponse->getMessages() != null)? $tresponse->getMessages()[0]->getDescription(): null,
'transactionResponseErrors' => ($tresponse->getErrors() != null)? array(
'errorText' => $tresponse->getErrors()[0]->getErrorText(),
'errorCode' => $tresponse->getErrors()[0]->getErrorCode()
): null,
'getErrorCode' => ($tresponse->getErrors() != null)? $tresponse->getErrors()[0]->getErrorCode(): null,
'getErrorText' => ($tresponse->getErrors() != null)? $tresponse->getErrors()[0]->getErrorText(): null,
'userCardNumber' => $userCardNumber,
'userCardExpMonth' => $userCardExpMonth,
'userCardExpYear' => $userCardExpYear
);
}else{
$tresponse = $response->getTransactionResponse();
$result = array(
'response' => ($response != null)? true: false,
'transactionResponseErrors' => ($tresponse->getErrors() != null)? array(
'errorText' => $tresponse->getErrors()[0]->getErrorText(),
'errorCode' => $tresponse->getErrors()[0]->getErrorCode()
): null,
'getErrorCode' => ($tresponse->getErrors() != null)? $tresponse->getErrors()[0]->getErrorCode(): null,
'getErrorText' => ($tresponse->getErrors() != null)? $tresponse->getErrors()[0]->getErrorText(): null,
'messageCode' => ($response->getMessages()->getMessage() != null)? $response->getMessages()->getMessage()[0]->getCode(): null,
'messageText' => ($response->getMessages()->getMessage() != null)? $response->getMessages()->getMessage()[0]->getText(): null
);
}
echo json_encode($result);
生成的JSON對象是這樣的:
{
"response": true,
"getResultCode": "Error",
"getMessages": null,
"getResponseCode": "3",
"getAuthCode": null,
"getTransId": "0",
"messageCode": null,
"msgDescription": null,
"transactionResponseErrors": {
"errorText": "The credit card number is invalid.",
"errorCode": "6"
},
"getErrorCode": "6",
"getErrorText": "The credit card number is invalid.",
"userCardNumber": "\t44444",
"userCardExpMonth": "12",
"userCardExpYear": "2018"
}