php - Braintree Paypal 无法在 Codeigniter 中工作

标签 php codeigniter paypal braintree braintree-sandbox

我想将 Braintree paypal 集成到我的 codeigniter 网站中。我在 Controller 构造函数中从服务器加载 Braintree 文件和凭据,并将其他代码放入方法中,但它总是给出错误未知或过期的 payment_method_nonce。下面是方法

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');
}

我尝试了谷歌,但没有成功。请帮助并提前致谢。

最佳答案

全面披露:我在 Braintree 工作。如果您还有任何疑问,请随时联系support .

来自 documentation on Transaction:sale() :

To create a transaction, you must include an amount and either a paymentMethodNonce or a paymentMethodToken.

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

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

要在代码中正确创建交易,您必须引用您在保险库中保存的付款方式(通过使用付款方式 token ),或者引用一组新标记化的付款方式信息客户在您的网站上提交的信息(通过使用付款方式随机数)。例如,如果您要将客户的付款方式 token 保存在数据库中,您可能会运行如下命令:

    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
        ]
    ]);

如果您需要一些额外的资源来创建支付方式随机数并将其传回您的服务器,您可以引用our full PHP integration example .

关于php - Braintree Paypal 无法在 Codeigniter 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42992337/

相关文章:

paypal - Braintree Drop-In UI PayPal 按钮弹出窗口是否可以在 Adob​​e AIR 桌面应用程序内显示的网页上工作?

PHP - 想要选择特定数量的行并从 SQL 中对它们进行排序

mysql - 如何在 CodeIgniter 中有条件地使用不同的数据库

java - 学习使用 OOP 文档

php - 在 CodeIgniter 中按日期范围获取记录

PHP 和 Paypal IPN 订阅和定期付款

api - Paypal 商品打折免费,运费仍需支付

php - if 语句检查字符串是否为问号

php - MySQLi CURDATE() 今天和明天 2 小时后?

php - 为什么点击浏览器的 'back' 按钮后 MySQL 值会被删除?