| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- <?php
- require_once 'Broker.class.php';
- /**
- *
- */
- class BraintreeBroker extends Broker {
- //
- private $Sandbox = false;
- private $Gateway = null;
- /**
- *
- */
- public function __construct($name, $sandbox) {
- parent::__construct($name);
- $this->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
- );
- }
- }
-
- }
|