gpt4 book ai didi

javascript - Symfony 4 - 设置 Braintree 表单

转载 作者:行者123 更新时间:2023-11-30 19:55:28 25 4
gpt4 key购买 nike

尝试在我的 Symfony 4 应用程序的 UI 中设置 Braintree drop。( https://developers.braintreepayments.com/start/hello-client/javascript/v3 , https://developers.braintreepayments.com/start/hello-server/php )

我创建了一个服务:

namespace App\Services;

use Braintree\ClientToken;
use Braintree\Configuration;

class Braintree

{
// environment variables:
const ENVIRONMENT = 'BRAINTREE_ENVIRONMENT';
const MERCHANT_ID = 'BRAINTREE_MERCHANT_ID';
const PUBLIC_KEY = 'BRAINTREE_PUBLIC_KEY';
const PRIVATE_KEY = 'BRAINTREE_PRIVATE_KEY';
function __construct() {
Configuration::environment(getenv(self::ENVIRONMENT));
Configuration::merchantId(getenv(self::MERCHANT_ID));
Configuration::publicKey(getenv(self::PUBLIC_KEY));
Configuration::privateKey(getenv(self::PRIVATE_KEY));
}
//
public function generateNonce() {
return ClientToken::generate();
}
}

并且我在我的 Twig 模板中添加了一个表单和一些 javascript:

{% block body %}
{{ parent() }}

<div class="container">
<div class="card">
<div class="row">
<div class="col-12">
<h3>Booking New</h3>

<div id="datepicker"></div>

{{ form_start(bookingForm) }}
{{ form_widget(bookingForm) }}

<button type="submit" class="btn btn-primary">Create</button>
{{ form_end(bookingForm) }}

</div>
</div>
<div class="row">
<div class="col-12">
<form method="post" id="payment-form">
<section>
<label for="amount">
<span class="input-label">Amount</span>
<div class="input-wrapper amount-wrapper">
<input id="amount" name="amount" type="tel" min="1" placeholder="Amount" value="10">
</div>
</label>

<div class="bt-drop-in-wrapper">
<div id="bt-dropin"></div>
</div>
</section>

<input id="nonce" name="payment_method_nonce" type="hidden" />
<button class="button" type="submit"><span>Test Transaction</span></button>
</form>
<button id="submit-button">Request payment method</button>
</div>
</div>
</div>
</div>
{% endblock %}

{% block javascripts %}
{{ parent() }}
<script src="https://js.braintreegateway.com/web/dropin/1.14.1/js/dropin.min.js"></script>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
var form = document.querySelector('#payment-form');
var client_token = "<?php echo($gateway->ClientToken()->generate()); ?>";
braintree.dropin.create({
authorization: client_token,
selector: '#bt-dropin',
paypal: {
flow: 'vault'
}
}, function (createErr, instance) {
if (createErr) {
console.log('Create Error', createErr);
return;
}
form.addEventListener('submit', function (event) {
event.preventDefault();
instance.requestPaymentMethod(function (err, payload) {
if (err) {
console.log('Request Payment Method Error', err);
return;
}
// Add the nonce to the form and submit
document.querySelector('#nonce').value = payload.nonce;
form.submit();
});
});
});
</script>
<script>
$( function() {
$( "#datepicker" ).datepicker();
} );
</script>
{% endblock %}

当我加载页面时,它不会像我期望的那样呈现 Braintree 表单 braintree.dropin.create 。当我按下提交时也没有任何反应。

如何正确设置此代码?

编辑:

检查控制台:

Create Error 
r
message: "There was an error creating Drop-in."
name: "DropinError"
_braintreeWebError: n {name: "BraintreeError", code: "CLIENT_INVALID_AUTHORIZATION", message: "Authorization is invalid. Make sure your client token or tokenization key is valid.", type: "MERCHANT", details: undefined}
__proto__: Error

最佳答案

从你的控制台错误,我可以推断出这一行是错误的:

var client_token = "<?php echo($gateway->ClientToken()->generate()); ?>";

与其在 javascript 中使用 php block ,不如向后端发出 AJAX 请求,该请求将返回客户端 token ,然后您可以在表单中使用该 token 。

考虑这个例子:

// Set up our HTTP request
var xhr = new XMLHttpRequest();

// Setup our listener to process completed requests
xhr.onload = function () {

// Process our return data
if (xhr.status >= 200 && xhr.status < 300) {
var client_token = xhr.response.client_token; // Set your client token and use it later
} else {
// What do when the request fails
console.log('The request failed!');
}

// Code that should run regardless of the request status
console.log('This always runs...');
};

// Create and send a GET request
// The first argument is the post type (GET, POST, PUT, DELETE, etc.)
// The second argument is the endpoint URL
xhr.open('GET', '/route/to/braintree/controller'); // Create a Symfony route which will use `BraintreeService` and return generated client token.
xhr.send();

这更像是一个伪代码,但它应该让您大致了解应该做什么。从您的代码看来,您应该首先获取该 client_token,然后呈现表单。

如果这不是问题,请继续查看控制台错误,这绝对是您无法呈现表单的原因。也许再次访问 Braintree 文档,他们有出色的框架和与框架无关的示例。

关于javascript - Symfony 4 - 设置 Braintree 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54047674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com