在 iOS + php 中使用 braintree 生成客户端令牌

Generate client token with braintree in iOS + php

我第一次在 Brain tree 上工作,第一步就遇到了问题。我无法使用 iOS + php

访问插件功能

我需要创建客户端令牌,但它不能使用这些代码,我真的不明白有什么问题。请告诉我如何生成客户端令牌。

$clientToken = Braintree_ClientToken::generate(array(
"customerId" => $aCustomerId
));

我刚刚在 iOS 中遇到了同样的错误 - xCode 告诉我 client_token 格式错误,它需要一个关联数组,但上面的代码只是 returns 一个对象。试试这个 - 对我有用:

$aCustomerId = '';

$clientToken["client_token"] = Braintree_ClientToken::generate(array("customerId" => $aCustomerId));
return ($clientToken);

请注意,我没有在此处设置 customerId - 您可以输入一个,也可以将其从代码中完全删除,即完全删除 'array("customerId" => $aCustomerId)'。它应该以任何方式工作。

注意:这是在服务器端 PHP 脚本中,不是 XCode 顺便说一下

编辑: 当您使用 braintree 函数(::create 或 ::sale)创建客户时,您可以在 'customer' 数组下分配您选择的 'id',例如:

$result = Braintree_Transaction::sale(array(
        'amount' => $value,
        'customer' => array(
            'id' => $anIdOfYourChoosing,
)
));

然后下次创建 ::sale 时,您可以在 sale 数组中调用 'customerId',它将使用该客户之前设置的付款详细信息,例如

$result = Braintree_Transaction::sale(array(
            'amount' => $value,
            'customerId' => $anIdOfYourChoosing,
    )
    ));

因此在 clientToken 的情况下,如果您将一个值传递给 $aCustomerId,它将在 Braintree 保险库中搜索该 customerId 并为您提供该客户的令牌(前提是该客户之前已创建)。目前在 Braintree 指南中没有很好地解释。希望有帮助