Braintree交易,多个lineItem,PHP

时间:2019-07-03 02:39:35

标签: php braintree

我正在尝试针对购物篮中的每个产品在 Braintree 交易中添加多个 lineItems

下面的交易成功,并且可以正常工作,仅将一项添加为lineItem。

$result = Braintree_Transaction::sale([
    'orderId' => $hash,
    'amount' => $this->basket->subTotal() + $this->basket->delivery(),
    'paymentMethodNonce' => $request->getParam('payment_method_nonce'),
    'shippingAmount' => $this->basket->delivery(),
    'discountAmount' => '0',
    'shipsFromPostalCode' => '7008',
    'taxExempt' => true,
    'purchaseOrderNumber' => $hash1,
    'options' => [
        'submitForSettlement' => true,
    ],
    'customer' => [
        'firstName' => $request->getParam('name'),
        'email' => $request->getParam('email'),
    ],
    'shipping' => [
        'firstName' => $request->getParam('name'),
        'streetAddress' => $request->getParam('address1'),
        'locality' => $request->getParam('city'),
        'postalCode' => $request->getParam('postal_code'),
        'countryCodeAlpha3' => 'AUS',
    ],
    'lineItems' => [
        [
            'quantity' => $product->quantity, 
            'name' => $product->title, 
            'kind' => 'debit', 
            'unitAmount' => $product->price, 
            'totalAmount' => $product->price * $product->quantity
        ],
    ]
]);

但是,当我尝试将所有产品包括在购物篮中时,如下所示

$basketProducts = $this->basket->all();

if($basketProducts){
    $lineItems = '';
foreach ($basketProducts as $product){
    $lineItems .= <<<EOD
    [
        'quantity' => $product->quantity, 
        'name' => $product->title, 
        'kind' => 'debit', 
        'unitAmount' => $product->price, 
        'totalAmount' => $product->price * $product->quantity
    ],
EOD;
}
}

$result = Braintree_Transaction::sale([
    'orderId' => $hash,
    'amount' => $this->basket->subTotal() + $this->basket->delivery(),
    'paymentMethodNonce' => $request->getParam('payment_method_nonce'),
    'shippingAmount' => $this->basket->delivery(),
    'discountAmount' => '0',
    'shipsFromPostalCode' => '7008',
    'taxExempt' => true,
    'purchaseOrderNumber' => $hash1,
    'options' => [
        'submitForSettlement' => true,
    ],
    'customer' => [
        'firstName' => $request->getParam('name'),
        'email' => $request->getParam('email'),
    ],
    'shipping' => [
        'firstName' => $request->getParam('name'),
        'streetAddress' => $request->getParam('address1'),
        'locality' => $request->getParam('city'),
        'postalCode' => $request->getParam('postal_code'),
        'countryCodeAlpha3' => 'AUS',
    ],
    'lineItems' => [
        $lineItems
    ]
]);

我收到此错误消息:

  

类型:InvalidArgumentException
  消息:无效键:lineItems [['quantity'=> 1,'name'=>杏子鸡,'kind'=>'debit','unitAmount'=> 9.8,'totalAmount'=> 9.8 * 1],]
  文件:C:\ wamp64 \ www \ vendor \ braintree \ braintree_php \ lib \ Braintree \ Util.php
  行:396

因此,似乎从该变量中提取了正确的数据,只是Braintree将其视为无效数据。
我想念什么吗?任何帮助将非常有必要!

1 个答案:

答案 0 :(得分:0)

您无法在lineItems中为Braintree_Transaction::sale()传递字符串,因为它是一个数组。 我解决这个问题的方法是创建一个$lineItems数组,然后使用array_push将每个$product的新数组推入$lineItems

完整示例->

$basketProducts = $this->basket->all();

if($basketProducts){    
    $lineItems = array();   
foreach ($basketProducts as $product){
    array_push($lineItems, array(
        'quantity' => $product->quantity, 
        'name' => $product->title, 
        'kind' => 'debit', 
        'unitAmount' => $product->price, 
        'totalAmount' => $product->price * $product->quantity,
        'productCode' => $product->id)
    );
}
}

$sale = array(
    'orderId' => $hash,
    'amount' => $this->basket->subTotal() + $this->basket->delivery(),
    'paymentMethodNonce' => $request->getParam('payment_method_nonce'),
    'shippingAmount' => $this->basket->delivery(),
    'discountAmount' => '0',
    'shipsFromPostalCode' => '7008',
    'taxExempt' => true,
    'purchaseOrderNumber' => $hash1,
    'options' => array(
        'submitForSettlement' => true,
    ),
    'customer' => array(
        'firstName' => $request->getParam('name'),
        'email' => $request->getParam('email'),
    ),
    'shipping' => array(
        'firstName' => $request->getParam('name'),
        'streetAddress' => $request->getParam('address1'),
        'locality' => $request->getParam('city'),
        'postalCode' => $request->getParam('postal_code'),
        'countryCodeAlpha3' => 'AUS',
    ),
    'lineItems' => $lineItems
);

$result = Braintree_Transaction::sale($sale);