使用 Paypal 的 Braintree 付款无法连接到我的 Paypal 帐户
Braintree payment with Paypal doesn't connect with my Paypal account
我一直在研究 Braintree 支付网关的实现,现在我被它的 Paypal 方法困住了。问题描述如下。
I have integrated the necessary code for for this and when selection the Paypal method and trying to login to Paypal, it always shows the error like below screenshot.
[![登录paypal时出现错误屏幕][1]][1]
我认为发生这种情况是因为客户端令牌错误。但是我已经使用 php 方法创建了令牌:$clientToken = Braintree_ClientToken::generate().
(当我提供 Braintree 演示代码提供的演示客户端令牌时,它当前会登录)。
下面我提到了我的代码。请检查并希望任何人都可以触发此问题。
<?php
require_once '_environment.php';
function braintree_text_field($label, $name, $result) {
echo('<div>' . $label . '</div>');
$fieldValue = isset($result) ? $result->valueForHtmlField($name) : '';
echo('<div><input type="text" name="' . $name .'" value="' . $fieldValue . '" /></div>');
$errors = isset($result) ? $result->errors->onHtmlField($name) : array();
foreach($errors as $error) {
echo('<div style="color: red;">' . $error->message . '</div>');
}
echo("\n");
}
// CLIENT TOKEN
$clientToken = Braintree_ClientToken::generate();
if(isset($_POST['payment_method_nonce'])){
$nonce = $_POST['payment_method_nonce'];
$result = Braintree_Transaction::sale(array(
'amount' => '91.00',
'paymentMethodNonce' => $nonce
));
if ($result->success) {
echo($result->customer->id);
echo($result->customer->creditCards[0]->token);
echo 'success';
} else {
foreach($result->errors->deepAll() AS $error) {
echo($error->code . ": " . $error->message . "\n");
}
}
}
?>
<form id="checkout" method="post" action="">
<div id="payment-form"></div>
<input type="submit" value="Pay ">
</form>
<script src="https://js.braintreegateway.com/v2/braintree.js"></script>
<script>
var clientToken = '<?php echo trim($clientToken);?>';
braintree.setup(clientToken, "dropin", {
container: "payment-form"
});
</script>
要创建 PayPal 交易,您需要一个客户 ID,以下示例适用于我。
index.php
<?php
require("config.php"); //config.php contains Braintree_Configuration object.
$aCustomerId = '39538986'; // can be generated from braintreegateway.com vault->New Customer
$clientToken = Braintree_ClientToken::generate(array(
"customerId" => $aCustomerId
));
;
?>
<script src="https://js.braintreegateway.com/v2/braintree.js"></script>
<script>
var clientToken = "<?php echo $clientToken ?>";
braintree.setup(clientToken, "dropin", {
container: "payment-form"
});
</script>
<form>
<div id="paypal-container"></div>
</form>
<script type="text/javascript">
braintree.setup(clientToken, "paypal", {
container: "paypal-container",
onPaymentMethodReceived: function (obj) {
window.location.href = 'http://localhost/braintree/checkout_paypal.php?nounce='+obj.nonce;
}
});
</script>
checkout_paypal.php
<?php
require("config.php");
$nonce = $_GET["nounce"];
$result = Braintree_Transaction::sale(array(
'amount' => '100.00',
'paymentMethodNonce' => $nonce,
'options' => array(
'submitForSettlement' => True
)
));
?>
我一直在研究 Braintree 支付网关的实现,现在我被它的 Paypal 方法困住了。问题描述如下。
I have integrated the necessary code for for this and when selection the Paypal method and trying to login to Paypal, it always shows the error like below screenshot. [![登录paypal时出现错误屏幕][1]][1]
我认为发生这种情况是因为客户端令牌错误。但是我已经使用 php 方法创建了令牌:$clientToken = Braintree_ClientToken::generate().
(当我提供 Braintree 演示代码提供的演示客户端令牌时,它当前会登录)。
下面我提到了我的代码。请检查并希望任何人都可以触发此问题。
<?php
require_once '_environment.php';
function braintree_text_field($label, $name, $result) {
echo('<div>' . $label . '</div>');
$fieldValue = isset($result) ? $result->valueForHtmlField($name) : '';
echo('<div><input type="text" name="' . $name .'" value="' . $fieldValue . '" /></div>');
$errors = isset($result) ? $result->errors->onHtmlField($name) : array();
foreach($errors as $error) {
echo('<div style="color: red;">' . $error->message . '</div>');
}
echo("\n");
}
// CLIENT TOKEN
$clientToken = Braintree_ClientToken::generate();
if(isset($_POST['payment_method_nonce'])){
$nonce = $_POST['payment_method_nonce'];
$result = Braintree_Transaction::sale(array(
'amount' => '91.00',
'paymentMethodNonce' => $nonce
));
if ($result->success) {
echo($result->customer->id);
echo($result->customer->creditCards[0]->token);
echo 'success';
} else {
foreach($result->errors->deepAll() AS $error) {
echo($error->code . ": " . $error->message . "\n");
}
}
}
?>
<form id="checkout" method="post" action="">
<div id="payment-form"></div>
<input type="submit" value="Pay ">
</form>
<script src="https://js.braintreegateway.com/v2/braintree.js"></script>
<script>
var clientToken = '<?php echo trim($clientToken);?>';
braintree.setup(clientToken, "dropin", {
container: "payment-form"
});
</script>
要创建 PayPal 交易,您需要一个客户 ID,以下示例适用于我。
index.php
<?php
require("config.php"); //config.php contains Braintree_Configuration object.
$aCustomerId = '39538986'; // can be generated from braintreegateway.com vault->New Customer
$clientToken = Braintree_ClientToken::generate(array(
"customerId" => $aCustomerId
));
;
?>
<script src="https://js.braintreegateway.com/v2/braintree.js"></script>
<script>
var clientToken = "<?php echo $clientToken ?>";
braintree.setup(clientToken, "dropin", {
container: "payment-form"
});
</script>
<form>
<div id="paypal-container"></div>
</form>
<script type="text/javascript">
braintree.setup(clientToken, "paypal", {
container: "paypal-container",
onPaymentMethodReceived: function (obj) {
window.location.href = 'http://localhost/braintree/checkout_paypal.php?nounce='+obj.nonce;
}
});
</script>
checkout_paypal.php
<?php
require("config.php");
$nonce = $_GET["nounce"];
$result = Braintree_Transaction::sale(array(
'amount' => '100.00',
'paymentMethodNonce' => $nonce,
'options' => array(
'submitForSettlement' => True
)
));
?>