Braintree Transaction, несколько строк, PHP - PullRequest
0 голосов
/ 03 июля 2019

Я пытаюсь включить несколько lineItems в транзакцию Braintree для каждого продукта в корзине.

Приведенная ниже транзакция прошла успешно и работает, как и ожидалось, в качестве 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 [['amount' => 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 голосов
/ 05 июля 2019

Вы не можете передать строку в 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);
...