Authorize.Net-XML - 添加行项


Authorize.Net-XML - Adding line items

首先感谢您编写此类。它使我在构建应用程序时的生活变得更加轻松。

我设置了 CIM,添加用户、处理付款等没有问题。但是,我坚持添加行项目。github 上的示例使用用于创建 XML 请求 EX 的数组的静态填充:

'lineItems' => array(
    'itemId' => 'ITEM00001',
    'name' => 'name of item sold',
    'description' => 'Description of item sold',
    'quantity' => '1',
    'unitPrice' => '6.95',
    'taxable' => 'true'
 ),
 'lineItems' => array(
     'itemId' => 'ITEM00002',
     'name' => 'other name of item sold',
     'description' => 'Description of other item sold',
     'quantity' => '1',
     'unitPrice' => '1.00',
     'taxable' => 'true'
 ),

如果您手动创建内容,这非常有用,但我根据用户输入动态创建这些行项目。不幸的是,由于键("lineItems")被覆盖并且我最终得到一个行项目,我无法向数组添加多个行项目。

我尝试创建一个行项目数组,然后将其合并,但没有运气。希望我只是缺少一个简单的解决方法。

感谢您回复约翰!再一次,这门课的出色工作让我的生活变得更加轻松。

这是我最终为简单起见所做的。我相信如有必要,可以对此进行阐述,但对我来说,这很完美。我没有在数组的同一级别上传递多个行项,而是创建了行项作为它们自己的数组,然后修改了 setParamaters() 以遍历该数组。

private function setParameters($xml, $array)
{
    if (is_array($array))
    {
        foreach ($array as $key => $value)
        {
            if (is_array($value))
            {
                if($key == 'lineItems'){
                    foreach($value as $lineitems){
                        $line_item = $xml->addChild('lineItems');
                        foreach($lineitems as $itemkey => $itemvalue) {
                            $line_item->addChild($itemkey, $itemvalue);
                        }
                    }
                }
                else
                {
                    $xml->addChild($key);
                    $this->setParameters($xml->$key, $value);
                }
            }
            else
            {
                $xml->$key = $value;
            }
        }
    }
}

这完全符合我的需求,因此除了嵌套 lineItems 数组之外,我不必在前端更改任何内容。 所以我发送的数组看起来更像这样:

["lineItems"]=>
  array(2) {
    [0]=>
    array(6) {
      ["itemId"]=>
      string(9) "ITEM00010"
      ["name"]=>
      string(21) "Blah Blah"
      ["description"]=>
      string(21) "Blah Blah Description"
      ["quantity"]=>
      string(1) "1"
      ["unitPrice"]=>
      string(4) "100"
      ["taxable"]=>
      string(5) "false"
    }
    [1]=>
    array(6) {
      ["itemId"]=>
      string(9) "ITEM00011"
      ["name"]=>
      string(25) "Thing Thing"
      ["description"]=>
      string(25) "Thing Thing Description"
      ["quantity"]=>
      string(1) "2"
      ["unitPrice"]=>
      string(3) "50"
      ["taxable"]=>
      string(5) "false"
    }
  }

另外,对于任何希望为行项目构建数组的人,我这样做了:

foreach ($services as $key => $service){
    $line_items["lineItems"][] = array(
        'itemId'        => 'ITEM000'.$key,
        'name'          => $service->name,
        'description'   => $service->name,
        'quantity'      => $service_count[$key],
        'unitPrice'     => $service->price,
        'taxable'       => 'false'
    );
}

然后只是将其添加到我传递给 AuthnetXML 实例的transaction_array中。

再次感谢!

乔尔

我是该类的作者。AuthnetXML 类当前有一个错误,导致您看到的结果。您需要对核心类进行更改才能解决此问题。

我收到了一封来自用户的电子邮件,该用户提供了我尚未有机会查看的解决方案。我会给你他们给我的相同信息,希望它对你有帮助:

The problem is that you can't have duplicate keys at the same level in an array. If you do the last one entered wins and the rest are overwritten.
So you need a way to represent repeating items from XML in and array. I decide to use the JSON methods to keep it simple. A quick wat to convert Simple XML to and array is to pass it through JSON.
$array = json_decode( json_encode( $simpleXML), true);
That will convert XML like this:
<transactionSettings>
  <setting>
    <settingName>allowPartialAuth</settingName>
    <settingValue>false</settingValue>
  </setting>
  <setting>
    <settingName>duplicateWindow</settingName>
    <settingValue>0</settingValue>
  </setting>
  <setting>
    <settingName>emailCustomer</settingName>
    <settingValue>false</settingValue>
  </setting>
  <setting>
    <settingName>recurringBilling</settingName>
    <settingValue>false</settingValue>
  </setting>
  <setting>
    <settingName>testRequest</settingName>
    <settingValue>false</settingValue>
  </setting>
</transactionSettings>

To an array like this:

array(
'transactionSettings' => array(  
  'setting' => array(
    0 => array('settingName' =>'allowPartialAuth' ,  'settingValue' => 'false',),
    1 => array('settingName' => 'duplicateWindow', 'settingValue' => '0', ),
    2 => array('settingName' => 'emailCustomer', 'settingValue' => 'false', ),
    3 => array('settingName' => 'recurringBilling', 'settingValue' => 'false',),
    4 => array( 'settingName' => 'testRequest', false, ),
  )
);

So you need to modify AuthNetXML.class to recognize this format. Just replace your setParameters() method with:

  private function setParameters($xml, $array)
  {
    if (is_array($array))
    {
      $first = true;
      foreach ($array as $key => $value)
      {
        if (is_array($value)) {
          if( is_numeric($key) )  {
            if($first){
              $xmlx = $xml;
              $first = false;
            } else {
              $parent = $xml->xpath('parent::*');
              $xmlx = $parent[0]->addChild($xml->getName());
            }
          } else {
            $xmlx = $xml->addChild($key);
          }
          $this->setParameters($xmlx, $value);
        }
        else
        {
          $xml->$key = $value;
        }
      }
    }
  }

更新 2012-08-21

此错误已得到修复。示例代码已更新。

编辑:我已经解决了这个问题。lineItems密钥必须像这样传入:

'lineItems' => array(
    'itemId'      => '13',
    'name'        => 'hello',
    'description' => 'hello description',
    'quantity'    => '1',
    'unitPrice'   => '55.00'
),

请注意,这与在 Authorize.net-XML 存储库中提供的示例不同。我现在要去那里并提交修复程序。

原始问题:我遇到了涉及 Authorize.net-XML 类的类似问题;执行createCustomerProfileTransactionRequest()方法时,我收到以下错误:

The element 'lineItems' in namespace 'AnetApi/xml/v1/schema/AnetApiSchema.xsd' has 
invalid child element 'lineItem' in namespace 
'AnetApi/xml/v1/schema/AnetApiSchema.xsd'. 
List of possible elements expected: 'itemId' in namespace 
'AnetApi/xml/v1/schema/AnetApiSchema.xsd'.' (length=272)

我什至粘贴了此处提供的示例输入,但收到相同的错误?该类在其他方面工作正常;我使用createTransactionRequest()createCustomerProfileRequest()方法来处理AIM交易并创建CIM配置文件,没有问题。

重申一下,我什至尝试使用与 GitHub 上相同的示例输入。

有什么想法吗?

非常感谢!杰森