Authorize.Net-XML-添加订单项 [英] Authorize.Net-XML - Adding line items

查看:119
本文介绍了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.

我尝试过创建一个lineItems数组,然后将其合并而没有运气.希望我只是为此错过了一个简单的解决方法.

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天全站免登陆