Authorize.Net-XML - 添加行项目 [英] Authorize.Net-XML - Adding line items

查看:22
本文介绍了Authorize.Net-XML - 添加行项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先感谢您编写本课程.它让我在构建应用程序时变得更加轻松.

First off thanks for writing this class. It has made life much easier for me in building applications.

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

I have CIM set up and I have no problem adding users, processing payments, etc. However I am stuck on adding line items. The examples on github use static population of the array used to create the XML request 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') 被覆盖,我最终只有一个行项目,因此我无法将多个行项目添加到数组中.

This works great if you are manually creating things but I am dynamically creating these line items based on user input. Unfortunately, I am unable do add multiple line items to the array due to the fact that the key ('lineItems') gets overwritten and I end up with one line item.

我尝试创建一个 lineItem 数组,然后将其合并,但没有成功.希望我只是错过了一个简单的修复.

I have tried creating an array of lineItems and then merging it with no luck. Hopefully I am just missing a simple fix for this.

推荐答案

感谢 John 的回复!再一次,这门课的出色工作让我的生活变得更轻松.

Thanks for responding John! Once again, great work on this class it has made my life much easier.

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

Here is what I ended up doing for simplicity. I am sure this can be expounded upon if necessary, but for me this worked perfect. Instead of passing multiple line items on the same level of the array I created line items as their own array and then modified setParamaters() to iterate through that array.

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 数组之外,我无需更改前端的任何内容.所以我发送的数组看起来更像这样:

This suited my needs perfectly and made it so I did not have to change anything on the front end except nesting the lineItems array. So the array I am sending looks more like this:

["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"
    }
  }

此外,对于那些希望为订单项构建数组的人,我这样做了:

Also, for anyone out there looking to build the arrays for the line items I did this:

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 中.

And then just added it to the transaction_array that I passed to the AuthnetXML instance.

再次感谢!

乔尔

这篇关于Authorize.Net-XML - 添加行项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆