Reputation: 203
I want to integrate Braintree paypal in my codeigniter website.i load braintree files and credentials from server in my controllers constructor and put other code in a method but it always giving the error Unknown or expired payment_method_nonce.Below is method
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');
}
}
and here is my constructor
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');
}
i tried google but no luck.please help and thanks in advance.
Upvotes: 2
Views: 921
Reputation: 696
Full disclosure: I work at Braintree. If you have any further questions, feel free to contact support.
From the documentation on Transaction:sale()
:
To create a transaction, you must include an amount and either a paymentMethodNonce or a paymentMethodToken.
The parameter that you're passing in your paymentMethodNonce
parameter is not a payment method nonce. Instead, you're passing it a client token, which is causing it to fail. These items are similarly-named, but have very different purposes.
To create a transaction properly in your code, you'll have to either reference a payment method that you've saved in your vault (by using a payment method token), or reference a set of newly-tokenized payment method information that a customer submitted on your website (by using a payment method nonce). For example, if you were to save the customers' payment method tokens in your database, you might run something like this:
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
]
]);
If you need some additional resources to create a payment method nonce and pass it back to your server, you can reference our full PHP integration example.
Upvotes: 2