【问题标题】:Braintree Transaction, multiple lineItems, PHPBraintree 交易,多个 lineItems,PHP
【发布时间】:2019-07-03 02:39:35
【问题描述】:

我正在尝试在 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' => Apricot Chicken, 'kind' => 'debit', 'unitAmount' => 9.8, 'totalAmount' => 9.8 * 1 ],]
文件:C:\wamp64\www\vendor\braintree\braintree_php\lib\Braintree\Util.php
线路:396

因此,它似乎从该变量中提取了正确的数据,只是 Braintree 将其检测为无效。
我错过了什么吗?任何帮助将不胜感激!

【问题讨论】:

    标签: php braintree


    【解决方案1】:

    您不能在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);
    

    【讨论】:

      猜你喜欢
      • 2017-03-11
      • 2012-06-06
      • 1970-01-01
      • 1970-01-01
      • 2018-09-21
      • 2016-03-09
      • 1970-01-01
      • 2015-03-08
      • 2022-01-14
      相关资源
      最近更新 更多