2016-08-15 44 views

回答

0

你可能现在已经想通了。但是我发布了我所有看到这个问题的人的解决方案。 我创建了一个数组来分解$ 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" 
}