Braintree支付网关:市场子商家创建和正确的错误报告

时间:2016-11-18 07:40:35

标签: php braintree braintree-sandbox

以下是我用于在我的Braintree帐户中创建子商户帐户的代码,用于使用邮递员在我的本地计算机上进行测试。 sub_merchant创建调用将来自Android或iOS设备使用自定义表单。在本地,这个代码与邮递员一起工作正常,任何人都可以建议我如何捕获错误消息,如果与我们从 $ result 传递给Braintree的任何字段有任何关联对象返回并以一种整洁的方式显示回客户端屏幕?

    <?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);
?>

1 个答案:

答案 0 :(得分:0)

完全披露:我在Braintree工作。如果您有任何其他问题,请随时联系support

MerchantAccount::create()等API调用失败时,Braintree会向您发送details on what went wrong in the result object。要确定响应是否包含商家帐户对象或有关错误的信息,请检查$result->success属性。如果确实如此,您可以从结果对象中提取生成的商家帐户。如果它是假的,您会想要loop through the errors Braintree returned。与您传递的字段相关的任何错误都将包含在那里。

$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);
}