0

这是我用来在我的 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);
?>
4

1 回答 1

0

全面披露:我在布伦特里工作。如果您还有其他问题,请随时联系支持人员

当 API 调用MerchantAccount::create()失败时,Braintree 将向您发送结果对象中出现问题的详细信息。为了确定响应是否包含商家帐户对象或错误信息,请检查$result->success属性。如果为真,您可以从结果对象中提取结果商家帐户。如果它是假的,你会想要遍历 Braintree 返回的错误。与您传递的字段相关的任何错误都将包含在此处。

$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);
}
于 2016-11-21T17:35:12.330 回答