简体   繁体   中英

Error while integrating Braintree php

I am using braintree to handle credit card payment in my codeigniter project and the Braintree_Transaction::sale throws invalid argument exception saying invalid keys message for billing address parameters . But i dont think there are any validation errors. What i am trying to do can be simplified as below:

$card_info = [
      'cardholderName' =>mysql_real_escape_string($_POST['full_name']),
      'number' =>mysql_real_escape_string($_POST['number']),
      'expirationMonth' =>mysql_real_escape_string($_POST['expiry_month']),
      'expirationYear' =>mysql_real_escape_string($_POST['expiry_year']),
      'cvv' =>mysql_real_escape_string($_POST['card_cvv']),
      'billingAddress' =[
              'firstName' =>mysql_real_escape_string($_POST['first_name']),
              'lastName'=>mysql_real_escape_string($_POST['last_name']),
              'streetAddress'=>mysql_real_escape_string($_POST['user_address']),
              'city'=>mysql_real_escape_string($_POST['user_city']),
              'state'=>mysql_real_escape_string($_POST['user_state']),
              'country' =>mysql_real_escape_string($_POST['user_country']),

                ]
       ]
       $result = Braintree_Transaction::sale(['amount'=>'4.99',
                                              'creditCard'=>$card_info,
                                              'options'=>['submitForSettlement' => true]
                                              ])

But when i run the following code i get following error :

 <br />
 <b>Fatal error</b>:  Uncaught exception 'InvalidArgumentException' with       message 'invalid keys: creditCard[billingAddress][city],   creditCard[billingAddress][country], creditCard[billingAddress][firstName], creditCard[billingAddress][lastName], creditCard[billingAddress][state], creditCard[billingAddress][streetAddress]' in   C:\xampp\htdocs\naya_admin\application\third_party\braintree\lib\Braintree\Util.php:343
 Stack trace:
     #0 C:\xampp\htdocs\naya_admin\application\third_party\braintree\lib\Braintree\TransactionGateway.php(48): Braintree\Util::verifyKeys(Array, Array)
      #1    C:\xampp\htdocs\naya_admin\application\third_party\braintree\lib\Braintree\TransactionGateway.php(251): Braintree\TransactionGateway-&gt;create(Array)
      #2 C:\xampp\htdocs\naya_admin\application\third_party\braintree\lib\Braintree\Transaction.php(480): Braintree\TransactionGateway-&gt;sale(Array)
      #3  C:\xampp\htdocs\naya_admin\application\modules\brain_tree\controllers\brain_tree .php(67): Braintree\Transaction::sale(Array)
      #4 [internal function]: Brain_tree-&gt;transaction()
      #5 in 
 <b>C:\xampp\htdocs\naya_admin\application\third_party\braintree\lib\Braintree\Ut il.php</b> on line 
     <b>343</b>
      <br />

You need to seperate the billing Address:

    $card_info = [
    'cardholderName' =>mysql_real_escape_string($_POST['full_name']),
    'number' =>mysql_real_escape_string($_POST['number']),
    'expirationMonth' =>mysql_real_escape_string($_POST['expiry_month']),
    'expirationYear' =>mysql_real_escape_string($_POST['expiry_year']),
    'cvv' =>mysql_real_escape_string($_POST['card_cvv']),
];

$billing =[
    'firstName' =>mysql_real_escape_string($_POST['first_name']),
    'lastName'=>mysql_real_escape_string($_POST['last_name']),
    'streetAddress'=>mysql_real_escape_string($_POST['user_address']),
    'city'=>mysql_real_escape_string($_POST['user_city']),
    'state'=>mysql_real_escape_string($_POST['user_state']),
    'country' =>mysql_real_escape_string($_POST['user_country']),

];

$result = Braintree_Transaction::sale(['amount'=>'4.99',
    'creditCard'=>$card_info,
    'billing' => $billing,
    'options'=>['submitForSettlement' => true]
]);

Refer to Braintree PHP API

Full disclosure: I work at Braintree. If you have any further questions, feel free to contact support .

Ilan Hasanov is correct that you need to pass the billing parameters separately from credit card details in the transaction call. In addition, you should replace "city" with "locality" , "state" with "region" , and "country" with whichever country field you are storing in your database

$billing =[
    'firstName' =>mysql_real_escape_string($_POST['first_name']),
    'lastName'=>mysql_real_escape_string($_POST['last_name']),
    'streetAddress'=>mysql_real_escape_string($_POST['user_address']),
    'locality'=>mysql_real_escape_string($_POST['user_city']),
    'region'=>mysql_real_escape_string($_POST['user_state']),
    'countryName' =>mysql_real_escape_string($_POST['user_country']),
];

In addition, to keep your PCI compliance burden low, I recommend passing a nonce to your server in place of the credit card details.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM