简体   繁体   English

Symfony 4-设置Braintree Payments集成

[英]Symfony 4 - Setting up Braintree Payments integration

I am trying to set up Braintree integration in my Symfony 4 PHP app. 我试图在我的Symfony 4 PHP应用程序中设置Braintree集成。

I have used composer to require and install the latest version of Braintree SDK, and have added testing credentials to env file. 我已经使用composer要求并安装了最新版本的Braintree SDK,并已将测试凭据添加到env文件中。

Set Up Client 设置客户端

https://developers.braintreepayments.com/start/hello-client/javascript/v3 https://developers.braintreepayments.com/start/hello-client/javascript/v3

I then added the client code to my twig template for Drop-in UI. 然后,我将客户端代码添加到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>

And included this script in the js block 并将此脚本包含在js块中

<script src="https://js.braintreegateway.com/web/dropin/1.14.1/js/dropin.min.js"></script>

Set Up Server 设置服务器

https://developers.braintreepayments.com/start/hello-server/php https://developers.braintreepayments.com/start/hello-server/php

Next step is to generate a client token 下一步是生成客户令牌

$clientToken = $gateway->clientToken()->generate([
    "customerId" => $aCustomerId
]);

and then send token to client etc. 然后将令牌发送给客户端等

Question

My question is where do I put the server side code in my Symfony 4 app? 我的问题是我将服务器端代码放在Symfony 4应用程序的哪里?

Do you create a Braintree.php service in src/Services and put all the Braintree PHP code in there or in the controller, or some in both? 您是否在src / Services中创建了Braintree.php服务,并将所有Braintree PHP代码放入其中或放置在控制器中,或者放置在两者中?

Best practice is to keep Controllers as thin as possible. 最佳实践是使控制器尽可能薄。 Controller method should: 控制器方法应:

  • accept the request 接受请求
  • call the appropriate service 致电适当的服务
  • handle response 处理回应

This is all a controller should be responsible of. 这是控制器应负责的全部。 In your case, the appropriate service would be your BraintreeService , a class responsible for whatever you need to do with Braintree SDK or anything related to Braintree. 在您的情况下,适当的服务是您的BraintreeService ,该类负责您对Braintree SDK或与Braintree相关的任何操作。

Symfony follows the philosophy of "thin controllers and fat models". Symfony遵循“瘦控制器和胖模型”的哲学。 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 https://symfony.com/doc/current/best_practices/controllers.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM