文档章节

braintree api调用记录

o
 osc_pn11u1x9
发布于 2018/08/06 14:21
字数 1204
阅读 30
收藏 0

精选30+云产品,助力企业轻松上云!>>>

国外的支付集成接入。

只使用基础的卡支付,跟PayPal支付。

braintree 有沙盒环境可以申请测试,有php sdk包直接下载调用,非常简单。

1,声明配置信息

private $_debug         = false;

    private $_pay_method    = 'braintree';
    private $_config        = null;
    private $_gateway       = null;
    private $_merchantAccountId     = [ 'usd' => 'xxxUSD' ]; //货币对应的merchantAccountId,建议自定义该值,
    private $_customerPre   = ''; //用户名前缀,用户生成顾客id,
    private $_customerId    = null;
    public  $_err           = '';
  
  
public function __construct() { if( $this->_debug ) { $this->_config['environment'] = 'sandbox'; $this->_config['merchantId'] = '6xqytmznhcczwwph'; $this->_config['publicKey'] = 'ks2j7rg234tq7fm7'; $this->_config['privateKey'] = '13012ef64ae8b95443f08226ac25c890'; $this->_merchantAccountId['usd'] = ''; //测试使用默认,也可以自定义, $this->_customerPre = 'test1'; }else { $this->_config['environment'] = 'production'; $this->_config['merchantId'] = ''; $this->_config['publicKey'] = ''; $this->_config['privateKey'] = ''; $this->_customerPre = 'Pro'; }
   //实例化gate类,为基础类 import(
"Pay.braintree_macaroon_app.lib.Braintree", VENDOR_PATH, '.php' ); $this->_gateway = new \Braintree_Gateway( [ 'merchantId' => $this->_config['merchantId'], 'publicKey' => $this->_config['publicKey'], 'privateKey' => $this->_config['privateKey'], 'environment' => $this->_config['environment'], 'timeout' => 2, ] ); }

2,生成并获取顾客id,顾客id可以保留顾客的支付方式,方便其下次直接已购买,而不用重复输入卡或者paypal信息

/**
    create table uro_braintree_uid(
    id int unsigned not null auto_increment,
    uid int unsigned not null default 0 comment '本站id',
    bid varchar(256) not null default '' comment 'braintree customerid',
    primary key (`id`),
    unique key (`uid`)
    ) engine innodb charset utf8 comment 'braintree 用户对应关系表,creditcard,paypal等信息以后可加字段';
     * 获取、创建braintree 用户,
     * author liuxiaodong
     * date 2018/7/31 15:38
     * @param $uid
     * @return string
     */
    private function getCreateCustom( $uid )
    {
        $model      = M('braintreeUid');
        $bid        = $model->where( ['uid' => $uid] )->getField( 'bid' );
        if( $bid )
            return $bid;

        try{
            $res        = $this->_gateway->customer()->create( [
                'id'    => $this->_customerPre . $uid
            ] );
            if( $res->success ) {
                $bid    = $res->customer->id;
                if( !$model->add( ['uid' => $uid, 'bid' => $bid] ) )
                    $this->_warnNotice( 'send -- into db error', '数据入库失败,入库数据为 = '.  json_encode( ['uid' => $uid, 'bid' => $bid] ) . ',err =' . $model->getLastSql() .'|'. $model->getDbError() );
            }else
                $this->_warnNotice( 'send -- res error', '请求braintree服务器,生成用户信息失败 = ' . json_encode( $res ) );

            return $bid;
        }catch ( \Exception $e ) {
            $this->_warnNotice( 'send -- createCustom', '请求braintree服务器,抛出异常' . json_encode( $e ) );
            return '';
        }
    }

3,获取客户端token,同时可以追加自己的信息,比如这边追加返回了订单的信息,让客户端回传,

/**
     * 初始化订单信息,返回 clientoken,orderinfo 信息
     * author liuxiaodong
     * date 2018/7/27 17:56
     * @param array $params
     * @return array
     */
    public function send( $order )
    {
        $this->_customerId  = $this->getCreateCustom( $order['braintree']['uid'] );

        $this->_warnNotice( 'send -- getClientToken', 'get request', 'debug' );

        $res            = ['clientoken' => '', 'transaction' => []];
        try{
            $res['clientoken']      = $this->_gateway->clientToken()->generate( [
                'customerId'        => $this->_customerId
            ] );
        }catch ( \Exception $e ) {
            $this->_warnNotice( 'send -- getClientToken', '错误信息:格式化exception ==' . json_encode( $e ) . ' , errmsg = ' . $e->getMessage()  );
            return [];
        }

        $res['transaction']     = $this->_encrypt( $order['braintree'] ); 
        $this->_warnNotice( 'send -- getClientToken', 'send request' . json_encode( $res ), 'debug' );

        return $res;
    }

4,支付, 是客户端请求后,请求braintree直接获取支付结果,做逻辑操作。

//此接口做支付
    public function notify( $params )
    {
        $this->_warnNotice( 'notify', '参数' . json_encode( $params ), 'debug' );

        if( empty( $params['nonce'] )
            || empty( $params['transaction'] )
        ) {
            $this->_warnNotice( 'notify', '请求参数异常 无nonce、transaction, 参数为 == ' . json_encode( $params ) );
            $this->_err         = 'invalid params';
            return false;
        }

        $transaction        = $this->_decrypt( $params['transaction'] );
        if( empty( $transaction ) || !is_array( $transaction ) ) {
            $this->_warnNotice( 'notify', '参数异常, 无transaction == ' . json_encode( $params ) );
            $this->_err         = 'invalid params';
            return false;
        }

        //读取订单信息
        $model          = D('Common/OrderRetail');
        $order          = $model->find( $transaction['oid'] );
        if( !$order ) {
            $this->_err         = 'invalid order info';
            $this->_warnNotice( 'notify', '读取订单信息异常 == ' . json_encode( $transaction ) . ' from ' . $params['client'] . ' order == ' . json_encode( $order ) . ' sql ==' . $model->getLastSql() );
            return false;
        }

        if( $order['amount'] != $transaction['amount'] ) {
            $this->_err         = 'check amount error';
            $this->_warnNotice( 'notify', '核对订单金额失败 == ' . json_encode( $transaction ) . ' from ' . $params['client'] . ' order amount = ' .$order['amount'] );
            return false;
        }

        $amount     = $transaction['amount'];
        if( $this->_debug )
            $amount = 0.01; //debug下请保证前端使用的金额也是0.01 

        //商品信息
        foreach ( $transaction['goods'] as $v ) {
            $lineItems[] = [
                'description'   => $transaction['oid'], // Maximum 127 characters
                'kind'          => 'debit',
                'name'          => mb_substr( $v['product_info']['name'], 0, 30, 'utf8' ) . '...',
                'productCode'   => $v['product_id'], //gid
                'quantity'      => $v['num'],
                'totalAmount'   => $v['product_info']['price'] * $v['product_info']['num'],
                'unitAmount'    => $v['product_info']['price'],
                'url'           => ''
            ];
        }

        $this->_customerId      = $this->getCreateCustom( $transaction['uid'] );
        $trans      = [
            'amount'                => $amount, //总金额
            'merchantAccountId'     => $this->_merchantAccountId['usd'], //merchantAccountId
            'paymentMethodNonce'    => $params['nonce'],
            'lineItems'             => $lineItems,
            'orderId'               => $transaction['oid'], //自定义的值
            'customerId'            => $this->_customerId,
            'options'               => [
                'submitForSettlement'     => true, //申请结算,
            ]
        ];
        $this->_warnNotice( 'notify', 'sale 发送请求' . json_encode( $trans ) . 'from ' . $params['client'], 'debug' );
        $res        = $this->_gateway->transaction()->sale( $trans );
        $this->_warnNotice( 'notify', 'sale 结果' . json_encode( $res->success ) . 'from ' . $params['client'], 'debug' );

        if( $res->success ) {
            $trance         = $res->transaction;
           
            
       //个人逻辑
return true; }else { $this->_err = $res->message . '('.$res->transaction->processorResponseCode.')'; $this->_warnNotice( 'notify', '请求 sale 发送交易抛出异常, 简讯 '.$this->_err.' 详情 == ' . json_encode( $res ), 'error' ); return false; } }

5, 其他附加方法,报警;加解密

/**
     * 获取客户端token
     * author liuxiaodong
     * date 2018/7/26 15:57
     * @return array
     */
    public function getClientToken()
    {
        try{
            $token      = $this->_gateway->clientToken()->generate();
            return [true, $token];
        }catch ( \Exception $e ) {
            $this->_warnNotice( 'getClientToken', '获取clienttoken 失败。。'  . json_encode( $e ), 'error' );
            return [false, $e->getMessage()];
        }
    }

    private function _warnNotice( $action, $msg, $level = 'error' )
    {
        if( $level == 'debug' && !$this->_debug )
            return;

        $msg        .= PHP_EOL;
        Log::write( $action . ' -- ' . $msg, $level, '', C('LOG_PATH') . 'braintreeErr/' . date( 'Y-m-d' ) . '.log'  );

        if( !$this->_debug ) {
            $email      = new AliyunEail();
            $email->sendEmail( 'email...', 'braintree pay error',  date( 'Y-m-d H:i:s' ) . '<br />' . $msg );
        }
    }

    /**
     * Encrypts the input text using the cipher key
     *
     * @param $input
     * @return string
     */
    private function _encrypt( Array $input)
    {
        $input      = json_encode( $input );
        // Create a random IV. Not using mcrypt to generate one, as to not have a dependency on it.
        $iv = substr(uniqid("", true), 0, self::IV_SIZE);
        // Encrypt the data
        $encrypted = openssl_encrypt($input, "AES-256-CBC", 'key', 0, $iv);
        // Encode the data with IV as prefix
        return base64_encode($iv . $encrypted);
    }

    /**
     * Decrypts the input text from the cipher key
     *
     * @param $input
     * @return string
     */
    private function _decrypt($input)
    {
        // Decode the IV + data
        $input  = base64_decode($input);
        // Remove the IV
        $iv = substr($input, 0, self::IV_SIZE);
        // Return Decrypted Data
        $output     =  openssl_decrypt(substr($input, self::IV_SIZE), "AES-256-CBC", 'key', 0, $iv);
        return json_decode( $output, true );
    }

    public function getError()
    {
        $msg    = 'err: ';
        $msg    .= $this->_err ? $this->_err : 'server fail';
        return $msg;
    }

  

截图:

报警: (该错误是前段生成的金额跟后端的不一致导致,多是debug的时候)

 

o
粉丝 0
博文 500
码字总数 0
作品 0
私信 提问
加载中
请先登录后再评论。

暂无文章

以太坊智能合约开发常见的10个安全问题

本文介绍CheckMarx安全研究小组通过扫描公开的以太坊智能合约所发现的Solidity智能合约开发中常见的十大安全问题,其中__未检查的外部调用__ 和 高成本循环 分列排行榜前两名。该安全问题排行...

区块链教程
52分钟前
17
0
Android Studio写flutter快捷键

1.stl :代表StatelessWidget 2.stf :StatefulWidget 3.cmd + shift + 减号 :折叠所有代码 4.cmd + 减号 :折叠当前代码块 5.ctrl + r :编译运行 6.cmd + s :hot reload 7.cmd + { :回到...

一代码农码一代
今天
21
0
远程桌面如何修改登录密码

打开运行, C:\Windows\explorer.exe shell:::{2559a1f2-21d7-11d4-bdaf-00c04f60b9f0} 即可 https://www.itexperience.net/10-ways-to-change-password-in-remote-desktop-session/......

ethanleellj
今天
17
0
easyui的menu接收后台集合,并且根据集合利用appendItem动态生成菜单项,判断菜单项的字数大于指定长度,则多余字符以。。。显示,并且悬浮提示

JSP: <a id="bb" href="javascript:void(0);" class="easyui-menubutton" data-options="menu:'#layout_north_stMenu222',iconCls:'icon-cologne-sign-out'" >导出</a><div id="aaa" style......

文文1
今天
13
0
Mysql主从同步

1主从同步 1.1Master 1.1.1配置--编辑 my.cnf #编辑 mysql 的 /etc/my.cnf 配置文件vi /etc/my.cnf#添加如下配置server-id=1 #设置服务 IDlog_bin=mysql-bin #启动 binlog...

风雪满弓刀
今天
14
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部