使用Authorize.Net PHP SDK从响应中获取XML

时间:2017-09-22 15:39:33

标签: php xml authorize.net

我使用Authorize.Net的PHP SDK编写了一些函数,如下所示:

public function getCustomerProfiles() {
    $customerProfiles = array();

    // Before we can get customer profiles, we need to get a list of all customer id's.
    $customerIdListRequest = new AnetAPI\GetCustomerProfileIdsRequest();
    $customerIdListRequest->setMerchantAuthentication(self::getMerchantAuth(Config::LOGIN_ID, Config::TRANSACTION_KEY));

    $customerIdListController = new AnetController\GetCustomerProfileIdsController($customerIdListRequest);
    $customerIdListResponse = $customerIdListController->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::SANDBOX);

    if(($customerIdListResponse != null) && ($customerIdListResponse->getMessages()->getResultCode() == "Ok")) {
        // TODO: Investigate warning about no method named getIds().
        foreach( $customerIdListResponse->getIds() as $id ) {
            // Now we can get each customer profile.
            $request = new AnetAPI\GetCustomerProfileRequest();
            $request->setMerchantAuthentication(self::getMerchantAuth(Config::LOGIN_ID, Config::TRANSACTION_KEY));
            $request->setCustomerProfileId($id);

            $controller = new AnetController\GetCustomerProfileController($request);
            $response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::SANDBOX);

            if(($response != null) && ($response->getMessages()->getResultCode() == "Ok")) {
                // TODO: Investigate warning about no method named getProfile()

                // Add it to the array.
                array_push($customerProfiles, $response->getProfile()->xml);
            } else {
                throw new \Exception($response->getMessages()->getMessage());
            }

        }
    } else {
        throw new \Exception($customerIdListResponse->getMessages()->getMessage());
    }

    return $customerProfiles;
}

目前,我只是返回一个对象数组。我更喜欢获得原始的XML响应。这个功能是否可以通过Authorize.Net的PHP SDK获得?或者我最好使用像Guzzle这样的东西并手动发出请求?

1 个答案:

答案 0 :(得分:1)

source code我认为这很简单。

查看那里execute调用的executeWithApiResponse方法。见xmlResponse?只需将其存储为类属性(并添加公共getter),或者调整函数以获取额外的参数,告诉它返回原始响应。可以破解它,或者更好的是,扩展ApiOperationBase类(注意界面IApiOperation为你提供了一个大纲)。

看到serializer也......

$this->apiResponse = $this->serializer->deserialize( $xmlResponse, $this->apiResponseType , 'xml');

也许可以做一些更优雅的事情。但不像我最初描述的路径那么清晰。