Symfony 4 - 设置 Braintree 支付集成
Symfony 4 - Setting up Braintree Payments integration
我正在尝试在我的 Symfony 4 PHP 应用程序中设置 Braintree 集成。
我已经使用 composer 要求并安装了最新版本的 Braintree SDK,并将测试凭据添加到 env 文件中。
设置客户端
https://developers.braintreepayments.com/start/hello-client/javascript/v3
然后我将客户端代码添加到我的 Drop-in 模板中 UI。
<div id="dropin-container"></div>
<button id="submit-button">Request payment method</button>
<script>
var button = document.querySelector('#submit-button');
braintree.dropin.create({
authorization: 'CLIENT_TOKEN_FROM_SERVER',
container: '#dropin-container'
}, function (createErr, instance) {
button.addEventListener('click', function () {
instance.requestPaymentMethod(function (err, payload) {
// Submit payload.nonce to your server
});
});
});
</script>
并将此脚本包含在 js 块中
<script src="https://js.braintreegateway.com/web/dropin/1.14.1/js/dropin.min.js"></script>
设置服务器
https://developers.braintreepayments.com/start/hello-server/php
下一步是生成客户端令牌
$clientToken = $gateway->clientToken()->generate([
"customerId" => $aCustomerId
]);
然后将令牌发送给客户端等
问题
我的问题是我应该将服务器端代码放在我的 Symfony 4 应用程序的什么位置?
您是否在 src/Services 中创建了一个 Braintree.php 服务并将所有 Braintree PHP 代码放在其中或控制器中,或两者中?
最佳做法是使控制器尽可能薄。控制器方法应该:
- 接受请求
- 调用适当的服务
- 处理响应
这是一个控制器应该负责的全部。在您的情况下,适当的服务将是您的 BraintreeService
,class 负责您需要使用 Braintree SDK 或与 Braintree 相关的任何事情。
Symfony follows the philosophy of "thin controllers and fat models". This means that controllers should hold just the thin layer of glue-code needed to coordinate the different parts of the application.
https://symfony.com/doc/current/best_practices/controllers.html
我正在尝试在我的 Symfony 4 PHP 应用程序中设置 Braintree 集成。
我已经使用 composer 要求并安装了最新版本的 Braintree SDK,并将测试凭据添加到 env 文件中。
设置客户端
https://developers.braintreepayments.com/start/hello-client/javascript/v3
然后我将客户端代码添加到我的 Drop-in 模板中 UI。
<div id="dropin-container"></div>
<button id="submit-button">Request payment method</button>
<script>
var button = document.querySelector('#submit-button');
braintree.dropin.create({
authorization: 'CLIENT_TOKEN_FROM_SERVER',
container: '#dropin-container'
}, function (createErr, instance) {
button.addEventListener('click', function () {
instance.requestPaymentMethod(function (err, payload) {
// Submit payload.nonce to your server
});
});
});
</script>
并将此脚本包含在 js 块中
<script src="https://js.braintreegateway.com/web/dropin/1.14.1/js/dropin.min.js"></script>
设置服务器
https://developers.braintreepayments.com/start/hello-server/php
下一步是生成客户端令牌
$clientToken = $gateway->clientToken()->generate([
"customerId" => $aCustomerId
]);
然后将令牌发送给客户端等
问题
我的问题是我应该将服务器端代码放在我的 Symfony 4 应用程序的什么位置?
您是否在 src/Services 中创建了一个 Braintree.php 服务并将所有 Braintree PHP 代码放在其中或控制器中,或两者中?
最佳做法是使控制器尽可能薄。控制器方法应该:
- 接受请求
- 调用适当的服务
- 处理响应
这是一个控制器应该负责的全部。在您的情况下,适当的服务将是您的 BraintreeService
,class 负责您需要使用 Braintree SDK 或与 Braintree 相关的任何事情。
Symfony follows the philosophy of "thin controllers and fat models". This means that controllers should hold just the thin layer of glue-code needed to coordinate the different parts of the application.
https://symfony.com/doc/current/best_practices/controllers.html