I am trying to include multiple lineItems in a Braintree transaction for each product in the basket.
The transaction below is successful and it works as expected, only one item is added as a 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
],
]
]);
However, when I try to include all products in the basket like the following
$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
]
]);
I get this error message:
Type: InvalidArgumentException
Message: invalid keys: lineItems[ [ 'quantity' => 1, 'name' => Apricot Chicken, 'kind' => 'debit', 'unitAmount' => 9.8, 'totalAmount' => 9.8 * 1 ],]
File: C:\\wamp64\\www\\vendor\\braintree\\braintree_php\\lib\\Braintree\\Util.php
Line: 396
So it seems like it's pulling the right data from that variable, it's just Braintree detecting it as invalid.
Am I missing something? Any help would be much obliged!
You cannot pass a string in lineItems
for Braintree_Transaction::sale()
, as it is an array. The way I got around this was to make $lineItems
an array and then use array_push
to push a new array for each $product
into $lineItems
Full example ->
$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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.