【发布时间】: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 将其检测为无效。
我错过了什么吗?任何帮助将不胜感激!
【问题讨论】: