前面,我们已经在服务器部署好braintree的组件和创建了braintree的沙箱环境,下面我们进入正式的开发
创建模块
- 创建对应的模块文件目录和文件
app\etc\modules\Braintree_PaymentMethod.xml
app\code\local\Braintree\PaymentMethod\Block\Payment\Braintree.php
app\code\local\Braintree\PaymentMethod\controllers\ApiController.php
app\code\local\Braintree\PaymentMethod\etc\config.xml
app\code\local\Braintree\PaymentMethod\etc\system.xml
app\code\local\Braintree\PaymentMethod\Helper\Data.php
app\code\local\Braintree\PaymentMethod\Model\Creditcard.php
app\code\local\Braintree\PaymentMethod\Model\Source\Environment.php
2. 配置模块的基础信息,在app\etc\modules\Braintree_PaymentMethod.xml
<?xml version="1.0"?>
<config>
<modules>
<Braintree_PaymentMethod>
<active>true</active>
<codePool>local</codePool>
<depends>
<Mage_Payment/>
</depends>
</Braintree_PaymentMethod>
</modules>
</config>
上面配置了模块的开关和模块的位置在local文件目录下
3. 配置braintree的模块的信息,在app\code\local\Braintree\PaymentMethod\etc\config.xml
<?xml version="1.0"?>
<config>
<modules>
<Braintree_PaymentMethod>
<version>0.1.0</version>
</Braintree_PaymentMethod>
</modules>
<global>
<blocks>
<braintree_paymentmethod>
<class>Braintree_PaymentMethod_Block</class>
</braintree_paymentmethod>
</blocks>
<models>
<braintree_paymentmethod>
<class>Braintree_PaymentMethod_Model</class>
</braintree_paymentmethod>
</models>
<helpers>
<braintree_paymentmethod>
<class>Braintree_PaymentMethod_Helper</class>
</braintree_paymentmethod>
</helpers>
</global>
<frontend>
<routers>
<braintree_paymentmethod>
<use>standard</use>
<args>
<module>Braintree_PaymentMethod</module>
<frontName>braintree_pay</frontName>
</args>
</braintree_paymentmethod>
</routers>
</frontend>
<default>
<payment>
<braintree_creditcard>
<model>braintree_paymentmethod/creditcard</model>
<active>1</active>
<order_status>pending</order_status>
<title>Braintree Credit Card</title>
<environment>Y</environment>
<payment_action>sale</payment_action>
<sort_order>100</sort_order>
</braintree_creditcard>
</payment>
</default>
</config>
4. 在model中,配置环境和API凭据,在app\code\local\Braintree\PaymentMethod\Model\Creditcard.php
<?php
use Braintree\Gateway;
class Braintree_PaymentMethod_Model_Creditcard extends Mage_Payment_Model_Method_Abstract
{
// This is the identifier of our payment method
protected $_code = 'braintree_creditcard';
protected $_isInitializeNeeded = true;
protected $_canUseCheckout = true; //是否在前端checkout页显示
protected $_canUseInternal = false;
protected $_canUseForMultishipping = false;
/**
* API凭证
* environment 该环境指定应将通过API的请求定向到哪里-沙箱或生产
* merchantId 商家ID是整个网关帐户(包括网关中可能存在的多个商家帐户)的唯一标识符
* publicKey 用户特定的公共标识符
* privateKey 用户的专用标识符
* link https://articles.braintreepayments.com/control-panel/important-gateway-credentials?_ga=1.78189399.251773956.1611709303#api-credentials
*/
public function getGateway()
{
$gateway = new Gateway([
'environment' => 'sandbox',
'merchantId' => 'use_your_merchant_id',
'publicKey' => 'use_your_public_key',
'privateKey' => 'use_your_private_key'
]);
return $gateway;
}
/**
* 生成客户令牌
* 令牌包含客户端初始化客户端SDK与Braintree进行通信所需的所有授权和配置信息
* https://developers.braintreepayments.com/reference/request/client-token/generate/php
*/
public function getClientToken()
{
$clientToken = $this->getGateway()->clientToken()->generate();
return $clientToken;
}
我们开始测试一下,看magento能否和braintree进行通讯了,在app\code\local\Braintree\PaymentMethod\controllers\ApiController.php
<?php
class Braintree_PaymentMethod_ApiController extends Mage_Core_Controller_Front_Action
{
public function testAction(){
$result = Mage::getModel('braintree_paymentmethod/creditcard')->getClientToken();
var_dump($result);
}
}
在页面上打开链接查看一下,控制器能否打印出token
http://你的域名/braintree_pay/api/test
能正常输出token,说明magento与braintree通讯成功,下一步就是尝试展示付款页面