Извлечение значений из CIM - authorize.net

У меня есть сценарий для сбора информации из CIM:

$content = 
  "<?xml version=\"1.0\" encoding=\"utf-8\"?>" .
  "<getCustomerProfileRequest xmlns=\"AnetApi/xml/v1/schema/AnetApiSchema.xsd\">" .
  merchantAuthenticationBlock().
  "<customerProfileId>$cid</customerProfileId>" .
  "</getCustomerProfileRequest>";

$response = send_xml_request($content);
$parsedresponse = parse_api_response($response);

Так как же теперь записать возвращаемое значение в переменную?

Я пробовал:

$customerPaymentProfileId = $parsedresponse->customerPaymentProfileId;
$customerShippingAddressId = $parsedresponse->customerShippingAddressId;

Но это возвращает переменные пустыми. Я чувствую, что упускаю что-то простое.

1 ответ

Решение

Чтобы увидеть структуру $parsedresponse делать либо print_r($parsedresponse) или же var_dump($parsedresponse), Оттуда вы можете увидеть, как массив структурирован и получить ваши значения оттуда.

К вашему сведению, платежные профили находятся в массиве, поэтому вам нужно будет просмотреть их, чтобы получить их значения. Если предположить, parsedresponse является корневым узлом XML, вы можете получить их так:

foreach ($parsedresponse->profile->paymentProfiles as $profile)
{
    echo $profile->customerPaymentProfileId;
}

К вашему сведению, это пример структуры массива для этого ответа (из образца кода AuthnetXML. Я являюсь автором этой библиотеки):

<?xml version="1.0" encoding="utf-8"?>
<getCustomerProfileResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">
  <messages>
    <resultCode>Ok</resultCode>
    <message>
      <code>I00001</code>
      <text>Successful.</text>
    </message>
  </messages>
  <profile>
    <merchantCustomerId>12345</merchantCustomerId>
    <email>user@example.com</email>
    <customerProfileId>5427896</customerProfileId>
    <paymentProfiles>
      <billTo>
        <firstName>John</firstName>
        <lastName>Smith</lastName>
        <address>123 Main Street</address>
        <city>Townsville</city>
        <state>NJ</state>
        <zip>12345</zip>
        <phoneNumber>800-555-1234</phoneNumber>
      </billTo>
      <customerPaymentProfileId>4796541</customerPaymentProfileId>
      <payment>
        <creditCard>
          <cardNumber>XXXX1111</cardNumber>
          <expirationDate>XXXX</expirationDate>
        </creditCard>
      </payment>
    </paymentProfiles>
    <paymentProfiles>
      <billTo>
        <firstName>John</firstName>
        <lastName>Doe</lastName>
        <company/>
        <address>123 Main St.</address>
        <city>Bellevue</city>
        <state>WA</state>
        <zip>98004</zip>
        <country>USA</country>
        <phoneNumber>800-555-1234</phoneNumber>
        <faxNumber>800-555-1234</faxNumber>
      </billTo>
      <customerPaymentProfileId>4796585</customerPaymentProfileId>
      <payment>
        <creditCard>
          <cardNumber>XXXX1111</cardNumber>
          <expirationDate>XXXX</expirationDate>
        </creditCard>
      </payment>
    </paymentProfiles>
    <shipToList>
      <firstName>John</firstName>
      <lastName>Smith</lastName>
      <address>123 Main Street</address>
      <city>Townsville</city>
      <state>NJ</state>
      <zip>12345</zip>
      <phoneNumber>800-555-1234</phoneNumber>
      <customerAddressId>4907537</customerAddressId>
    </shipToList>
    <shipToList>
      <firstName>John</firstName>
      <lastName>Doe</lastName>
      <company/>
      <address>123 Main St.</address>
      <city>Bellevue</city>
      <state>WA</state>
      <zip>98004</zip>
      <country>USA</country>
      <phoneNumber>800-555-1234</phoneNumber>
      <faxNumber>800-555-1234</faxNumber>
      <customerAddressId>4907591</customerAddressId>
    </shipToList>
  </profile>
</getCustomerProfileResponse>
Другие вопросы по тегам