【问题标题】:Getting the XML from a response using the Authorize.Net PHP SDK使用 Authorize.Net PHP SDK 从响应中获取 XML
【发布时间】:2018-03-04 06:01:59
【问题描述】:

我使用 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 之类的东西并手动发出请求?

【问题讨论】:

  • 为什么需要原始 XML?您希望如何处理 SDK 尚未为您做的事情?

标签: php xml authorize.net


【解决方案1】:

看着source code,我认为这很简单。

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

看到serializer也...

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

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

【讨论】:

  • 编辑第三方库通常被认为是一个坏主意,因为它会阻止您进行升级/更新,因为这些更改往往会在破坏调用代码时丢失。它假定进行更新的任何人都知道有修改、修改在哪里以及它们打算做什么,并且更新的源代码仍然以仍然可以完成相同黑客攻击的方式编写。
猜你喜欢
  • 2016-08-21
  • 2017-12-18
  • 1970-01-01
  • 1970-01-01
  • 2020-10-27
  • 1970-01-01
  • 2013-07-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多