简体   繁体   中英

Braintree Payment Gateway : Marketplace Sub Merchant Creation and Proper Error Reporting

Here is the code I use to create Sub-merchant accounts in my Braintree account for testing purpose on my local machine using postman. The sub_merchant creation call will come from either from Android or iOS device using a custom form. In local this code is working fine with postman, can anyone suggest me how to capture the error messages if there any associated with any fields we are passing to Braintree from the $result object returned and display back to the client side screen in a neat way ?

    <?php
    include("../connection.php");

    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);

    require_once 'braintree_environment_settings.php';

    $dataReceived = json_decode(file_get_contents('php://input'), true);
    //var_dump($dataReceived);

    if((json_last_error() == JSON_ERROR_NONE))
    {
       // code to handle iOS call
       $firstName = $dataReceived['firstName'];
       $lastName = $dataReceived['lastName'];
       $email = $dataReceived['email'];
       $dateOfBirth = $dataReceived['dateOfBirth'];
       $streetAddress = $dataReceived['streetAddress'];
       $locality = $dataReceived['locality'];
       $region = $dataReceived['region'];
       $postalCode = $dataReceived['postalCode'];
       $accountNumber = $dataReceived['accountNumber'];
       $routingNumber = $dataReceived['routingNumber'];
    }
    else {
       // code to handle Android call
       $firstName = $_POST['firstName'];
       $lastName = $_POST['lastName'];
       $email = $_POST['email'];
       $dateOfBirth = $_POST['dateOfBirth'];
       $streetAddress = $_POST['streetAddress'];
       $locality = $_POST['locality'];
       $region = $_POST['region'];
       $postalCode = $_POST['postalCode'];
       $accountNumber = $_POST['accountNumber'];
       $routingNumber = $_POST['routingNumber'];
    }


    $merchantAccountParams = [
      'individual' => [
        'firstName' => $firstName,  
        'lastName' => $lastName,
        'email' => $email,
        'dateOfBirth' => $dateOfBirth,
        'address' => [
          'streetAddress' => $streetAddress,
          'locality' => $locality,
          'region' => $region,
          'postalCode' => $postalCode
        ]
      ],

      'funding' => [
        'destination' => Braintree_MerchantAccount::FUNDING_DESTINATION_BANK,
        'accountNumber' => $accountNumber,
        'routingNumber' => $routingNumber
      ],
      'tosAccepted' => true,
      'masterMerchantAccountId' => 'zeefasys'
      // 'id' => "Praveens_caffe_store"
    ];

    $result = Braintree_MerchantAccount::create($merchantAccountParams);
$responseData = array('Merchant_ID' => $result->merchantAccount->id, "message" => "Success");

header('Content-type: application/json');
echo json_encode($responseData);
?>

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

When an API call such as the MerchantAccount::create() fails, Braintree will send you details on what went wrong in the result object . In order to determine whether the response contains a merchant account object or information on errors, inspect the $result->success property. If it's true, you can extract the resulting merchant account from the result object. If it's false, you'll want to loop through the errors Braintree returned . Any errors related to the fields that you passed will be included there.

$result = Braintree_MerchantAccount::create($merchantAccountParams);
if ($result->success == true) {
    $responseData = array('Merchant_ID' => $result->merchantAccount->id, 'message' => "success");
elseif ($result->success == false) {
    foreach($result->errors->deepAll() as $error) {
        $message .= $error->attribute . ": " . $error->code . " " . $error->message . "\n";
    }
    $responseData = array('Merchant_ID' => NULL, 'message' => $message);
}

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