PHP Payment Gateway交易错误

时间:2018-08-15 18:39:35

标签: php braintree

我不断收到以下2个我不知道如何解决的错误

Fatal error: Uncaught exception 'InvalidArgumentException' with message 'invalid keys: lineItems[ in C:\wamp64\www\pay\braintree\lib\Braintree\Util.php on line 351

InvalidArgumentException: invalid keys: lineItems[ in C:\wamp64\www\pay\braintree\lib\Braintree\Util.php on line 351

这是下面代码中的第351行throw new InvalidArgumentException('invalid keys: ' . $sortedList);

public static function verifyKeys($signature, $attributes)
{
    $validKeys = self::_flattenArray($signature);
    $userKeys = self::_flattenUserKeys($attributes);
    $invalidKeys = array_diff($userKeys, $validKeys);
    $invalidKeys = self::_removeWildcardKeys($validKeys, $invalidKeys);

    if(!empty($invalidKeys)) {
        asort($invalidKeys);
        $sortedList = join(', ', $invalidKeys);
        throw new InvalidArgumentException('invalid keys: ' . $sortedList);
    }
}

这是我的两个似乎引起错误的函数中的第一个

function payment_data($gateway, $order_id){
    try{
        $account_number = account_number();
        $total_amount = total_amount();

        $credit_cards = $gateway->customer()->find($account_number);
        $members_first_name = $credit_cards->firstName;
        $members_last_name = $credit_cards->lastName;
        $members_email = $credit_cards->email;

        if(!empty($credit_cards)){
            foreach($credit_cards->creditCards as $c){
                if($c->default === true){
                    $first_name = $c->billingAddress->firstName;
                    $last_name = $c->billingAddress->lastName;
                    $street_address = $c->billingAddress->streetAddress;
                    $extended_address = $c->billingAddress->extendedAddress;
                    $city = $c->billingAddress->locality;
                    $region = $c->billingAddress->region;
                    $postal_code = $c->billingAddress->postalCode;
                    $country_name = $c->billingAddress->countryName;
                    $card_token = $c->token;
                    $customer_id = $c->customerId;

                    if(empty($extended_address)){
                        $extended_address = null;
                    }                   

                    if(empty($region)){
                        $region = null;
                    }

                    $result = $gateway->transaction()->sale([
                      'amount' => $total_amount,
                      'orderId' => $order_id,
                      'customerId' => $customer_id,
                      'customer' => [
                        'firstName' => $members_first_name,
                        'lastName' => $members_last_name,
                        'email' => $members_email
                      ],
                      'billing' => [
                        'firstName' => $first_name,
                        'lastName' => $last_name,
                        'streetAddress' => $street_address ,
                        'extendedAddress' => $extended_address,
                        'countryName' => $country_name,
                        'locality' => $city,
                        'region' => $region,
                        'postalCode' => $postal_code
                      ],
                      'shipping' => [
                        'firstName' => $first_name,
                        'lastName' => $last_name,
                        'streetAddress' => $street_address ,
                        'extendedAddress' => $extended_address,
                        'countryName' => $country_name,
                        'locality' => $city,
                        'region' => $region,
                        'postalCode' => $postal_code
                      ],
                      'lineItems' => [
                        products($order_id)
                      ],
                      'options' => [
                        'submitForSettlement' => true
                      ]
                    ]);
                }
            }           
        }
    } catch (Braintree_Exception_NotFound $e) {
        return false;
    }
}

这是我的第二个功能

function products($order_id){
    if(products('Now') !== false){
        $total = count(products('Now'));
        $x = 1;
        $line_items = '';

        foreach(products('Now') as $product){
            $title = $product['title'];
            $quantity = $product['quantity'];
            $sales_tax = $product['sales_tax'];
            $regular_price = $product['regular_price'];
            $total_sales_tax = $product['total_sales_tax'];
            $total_amount = $product['total_amount'];
            $savings_price = $product['savings_price'];
            $product_id = $product['product_id'];

            $line_items .= "[";
            $line_items .= "'name' => '" . $title . "',";
            $line_items .= "'description' => 'Product',";
            $line_items .= "'quantity' => '" . $quantity . "',";
            $line_items .= "'unitTaxAmount' => '" . $sales_tax . "',";
            $line_items .= "'unitAmount' => '" . $regular_price . "',";
            $line_items .= "'taxAmount' => '" . $total_sales_tax . "',";
            $line_items .= "'totalAmount' => '" . $total_amount . "',";
            $line_items .= "'discountAmount' => '" . $savings_price . "',";
            $line_items .= "'productCode' => '" . $product_id . "',";
            $line_items .= "'kind' => 'debit',";
            $line_items .= "'url' => 'http://localhost/product.php?id=" . $product_id . "'";

            if($x !== $total){
                $line_items .= "],";
            } else {
                $line_items .= "]";
            }

            $x++;
        }

        return $line_items;
    }       
}

1 个答案:

答案 0 :(得分:0)

我猜您需要更多类似的东西,我已经使用了Braintree一次,但从未将类似的大量数据传递给它。

function products($order_id){
    if(products('Now') !== false){

        $total = count(products('Now'));

        $line_items = Array();
        foreach(products('Now') as $product){

            $title = $product['title'];
            $quantity = $product['quantity'];
            $sales_tax = $product['sales_tax'];
            $regular_price = $product['regular_price'];
            $total_sales_tax = $product['total_sales_tax'];
            $total_amount = $product['total_amount'];
            $savings_price = $product['savings_price'];
            $product_id = $product['product_id'];

            $line_item = Array();
            $line_item['name'] = $title;
            $line_item['description'] = 'Product';
            $line_item['quantity'] = $quantity;
            $line_item['unitTaxAmount'] = $sales_tax;
            $line_item['unitAmount'] = $regular_price;
            $line_item['taxAmount'] = $total_sales_tax;
            $line_item['totalAmount'] = $total_amount;
            $line_item['discountAmount'] = $savings_price;
            $line_item['productCode'] = $product_id;
            $line_item['kind'] = 'debit';
            $line_item['url'] = 'http://localhost/product.php?id=' . $product_id;
            $line_items[] = $line_item;
        }

        return $line_items;
    }       
}