Sandbox = $sandbox; } /** * */ protected function init() { $this->Gateway = new Braintree_Gateway([ //'environment' => ($this->Sandbox?'sandbox':'production'), 'environment' => 'sandbox', 'merchantId' => $this->Config->MerchantID, 'publicKey' => $this->Config->PublicKey, 'privateKey' => $this->Config->PrivateKey ]); } /** * */ public function getToken($User) { $customerId = 0; $collection = $this->Gateway->customer()->search([ Braintree_CustomerSearch::id()->is($User->ID) ]); // New customer if(iterator_count($collection) == 0) { $result = $this->Gateway->customer()->create([ 'id' => $User->ID, 'firstName' => $User->firstname, 'lastName' => $User->lastname, 'email' => $User->email ]); $customerId = $result->customer->id; } // Got it else if(iterator_count($collection) == 1) { $collection->rewind(); $customerId = $collection->current()->id; } // Should never happen else { throw new \Exception('braintree/database internal error'); } // Generate token try { return $this->Gateway->clientToken()->generate([ "merchantAccountId" => $this->Config->MerchantID, "customerId" => $customerId ]); } catch(Braintree_Exception_Authentication $e) { throw $e; } catch(Braintree_Exception_Authorization $e) { throw $e; } } /** * */ public function submit($User, $token, $amount) { $result = $this->Gateway->transaction()->sale([ 'amount' => $amount, 'paymentMethodNonce' => $token, 'billing' => [ 'firstName' => $User->lastname, 'lastName' => $User->firstname ], 'options' => [ 'submitForSettlement' => True ] ]); if ($result->success) { // See $result->transaction for details return array( 'result' => 'OK', 'transactionID' => $result->transaction->id ); } else { /*print_r($result->message); print_r($result->transaction->id); print_r($result->transaction->status);*/ return array( 'result' => 'ERROR', 'transactionID' => $result->transaction->id, 'status' => $result->transaction->status, 'message' => $result->message ); } } }