braintree paypal在codeigniter中不起作用

时间:2017-03-24 05:53:04

标签: php codeigniter paypal braintree braintree-sandbox

我想在我的codeigniter网站中整合Braintree paypal。我在我的控制器构造函数中加载braintree文件和服务器的凭据,并将其他代码放在一个方法中,但它总是给出错误Unknown或过期payment_method_nonce.Below是方法

public function braintree(){
    if($this->session->userdata('user_id') != '' && $this->session->userdata('user_id') != 'user'){
        $user_id        = $this->input->post('user_id');
        $amount         = $this->input->post('amount');
        $inscription_id = $this->input->post('inscription_id');

        $user_details = $this->db->get_where('spm_users',array('id'=>$user_id))->row_array();
        if(!empty($user_details->braintree_customer_id)){
            $CustomerId =  (string)$user_details->braintree_customer_id;
        }else{
            $result = Braintree_Customer::create([
                        'firstName' => $user_details->name,
                        'lastName' => $user_details->surname,
                        'email' => $user_details->email,
                        'phone' => $user_details->telephone,
                    ]);
                $CustomerId = (string)$result->customer->id;
                $this->db->where("id",$user_id);
                $this->db->update("spm_users", array('braintree_customer_id'=>$CustomerId));
        }
        $clientToken = Braintree_ClientToken::generate([
                        "customerId" => $CustomerId
                    ]);
        $card_id ='';            
        $clientToken_new = Braintree_ClientToken::generate();
        $result1 = Braintree_Transaction::sale([
                      'amount' => $amount,
                      'paymentMethodNonce' => $clientToken_new,
                      'options' => [
                        'submitForSettlement' => True
                      ]
                    ]);
        if($result1->success == true){ 
            $updateArr = array(
                'amount'=>$result1->transaction->amount,
                'balance_transaction'=>$result1->transaction->id,
                'inscription_status'=>2,
                'status'=>1,
                'data'=>json_encode($result1),
                'payment_method'        => 'Braintree',
                'payment_date'=>date('Y-m-d H:i:s')
                );
                $this->db->where("id",$inscription_id);
                $this->_db->update("spm_inscription", $updateArr);
                $this->session->set_flashdata('msg','Inscription Payment Success');
                redirect('frontend/paypalpayment/'.$inscription_id);
        }else{
            $this->session->set_flashdata('msg','Payment failed');
            redirect('frontend/paypalpayment/'.$inscription_id);
        }    
    }else{
        redirect('frontend');
    } 
}

这是我的构造函数

public function __construct() {
    parent::__construct();
    require_once '/home/public_html/mysite/braintree/Braintree.php';
    Braintree_Configuration::environment('sandbox');
    Braintree_Configuration::merchantId('zxxxxxxxxxxxxxxd');
    Braintree_Configuration::publicKey('7xxxxxxxxxxxxxxx7');
    Braintree_Configuration::privateKey('1xxxxxxxxxxxxxxxxxxxxxx8');
}

我试过谷歌但没有运气。请帮助并提前感谢。

1 个答案:

答案 0 :(得分:1)

完全披露:我在Braintree工作。如果您有任何其他问题,请随时联系support

来自documentation on Transaction:sale()

  

要创建交易,您必须包含金额以及paymentMethodNonce或paymentMethodToken。

您在paymentMethodNonce参数中传递的参数不是payment method nonce。相反,你传递的是client token,这会导致它失败。这些项目的名称相似,但目的却截然不同。

  • 客户端令牌:包含有关您的网关帐户的配置信息。 client-side SDKs使用它们来正确设置自己的配置并与您的服务器无缝协作,不应将它们传递给交易销售电话。
  • 付款方式随机数:对一组标记化付款方式信息的引用,例如信用卡号和到期日。这些通常是在用户输入信用卡信息后,通过客户端SDK中的tokenize调用生成的。
  • 付款方式令牌:对您在Braintree帐户中保存的付款方式的引用。

要在代码中正确创建交易,您必须引用保存在保管库中的付款方式(使用付款方式令牌),或引用一组新标记的付款方式信息客户在您的网站上提交的内容(使用付款方式nonce)。例如,如果您要在数据库中保存客户的付款方式令牌,则可能会运行以下内容:

    if (!empty($user_details->braintree_customer_id)) {
        $CustomerId = (string) $user_details->braintree_customer_id;
        $CustomerSavedPaymentMethod = (string) $user_details->payment_method_token;
    } else {
        ...
    }

    $result1 = Braintree_Transaction::sale([
        'amount' => $amount,
        'paymentMethodToken' => $CustomerSavedPaymentMethod,
        'options' => [
            'submitForSettlement' => True
        ]
    ]);

如果您需要一些额外资源来创建付款方式nonce并将其传递回服务器,则可以参考our full PHP integration example