我正在尝试使用 Braintree 的支付 DropinUI,配置它需要发送生成的 token 到我的页面中的 javascript 函数。我的 Razor 页面中有以下标记:
<div id="dropin-container"></div>
<button id="submit-button">Request payment method</button>
<script>
function configureBraintreeClient(clientToken) {
var button = document.querySelector('#submit-button');
braintree.dropin.create({
authorization: clientToken,
container: '#dropin-container'
}, function (createErr, instance) {
button.addEventListener('click', function () {
instance.requestPaymentMethod(function (requestPaymentMethodErr, payload) {
// Submit payload.nonce to your server
});
});
});
}
</script>
div #dropin-container 由函数configureBraintreeClient 的结果填充。因此,我需要在页面加载时传入 clientToken。
我的页面模型生成客户端 token :
public class IndexModel : PageModel
{
private readonly IJSRuntime _jsRuntime;
public IndexModel(IJSRuntime jsRuntime)
{
_jsRuntime = jsRuntime;
}
public IActionResult OnGet()
{
// Create gateway
var gateway = new BraintreeGateway
{
Environment = Environment.SANDBOX,
MerchantId = "xxxxxxx",
PublicKey = "xxxxxxx",
PrivateKey = "xxxxxxx"
};
var clientToken = gateway.ClientToken.Generate();
JSRuntimeExtensions.InvokeVoidAsync(_jsRuntime, "configureBraintreeClient", clientToken);
// Not sure if this is needed, doesn't work with return type of
// void and this line removed either.
Page();
}
}
这不起作用。该 div 从未被填充。
我可以将客户端 token 直接插入到标记中并且它运行完美,因此它与传递客户端 token 有关 从页面模型到页面。我不应该使用 JSRuntime 扩展吗?
我尝试将客户端 token 设置为模型属性,并使用 razor 语法将其插入到函数中,例如 authorization: @Model.ClientToken
但它不起作用。
我试图深入研究以更好地理解页面生命周期,但我发现没有任何东西可以帮助我解决这个问题。
我在浏览器的开发者控制台或 Visual Studio 中没有看到任何错误,但我对在 ASP .NET Core 中调试 javascript 不太了解。
将参数传递到像这样更新 div 的 javascript 函数中的正确方法是什么?
最佳答案
由于我们没有商户ID
、公钥
和私钥
,请确保您生成了正确的 token ,然后您可以试试这个:
1.IndexModel():
private readonly IJSRuntime _jsRuntime;
public IndexModel(IJSRuntime jsRuntime)
{
_jsRuntime = jsRuntime;
}
[BindProperty]
public string clientToken { get; set; }
public void OnGet()
{
// Create gateway
var gateway = new BraintreeGateway
{
Environment = Braintree.Environment.SANDBOX,
MerchantId = "xxxxxxx",
PublicKey = "xxxxxxx",
PrivateKey = "xxxxxxx"
};
clientToken = gateway.ClientToken.Generate();
JSRuntimeExtensions.InvokeVoidAsync(_jsRuntime, "configureBraintreeClient", clientToken);
}
2.Razor页面(无需使用功能):
<div id="dropin-container"></div>
<button id="submit-button">Request payment method</button>
<script src="https://js.braintreegateway.com/web/dropin/1.20.4/js/dropin.min.js">
</script>
<script>
var button = document.querySelector('#submit-button');
braintree.dropin.create({
authorization: '@Model.clientToken',
container: '#dropin-container'
}, function (createErr, instance) {
button.addEventListener('click', function () {
instance.requestPaymentMethod(function (requestPaymentMethodErr, payload) {
// Submit payload.nonce to your server
});
});
});
</script>
https://developers.braintreepayments.com/start/hello-client/javascript/v3
I can insert a client token directly into the markup and it runs perfectly, so it has something to do with passing the client token to the page from the page model. Should I not be using the JSRuntime extension?
对此,如果您想使用客户端 token ,我建议您可以引用: https://developers.braintreepayments.com/start/tutorial-drop-in-node
授权也可以使用 tokenization key .
关于javascript - 如何将参数传递给将 div 从页面模型填充到其 Razor 页面的 javascript 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58634543/