Braintree.class.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. require_once 'Broker.class.php';
  3. /**
  4. *
  5. */
  6. class BraintreeBroker extends Broker {
  7. //
  8. private $Sandbox = false;
  9. private $Gateway = null;
  10. /**
  11. *
  12. */
  13. public function __construct($name, $sandbox) {
  14. parent::__construct($name);
  15. $this->Sandbox = $sandbox;
  16. }
  17. /**
  18. *
  19. */
  20. protected function init() {
  21. $this->Gateway = new Braintree_Gateway([
  22. //'environment' => ($this->Sandbox?'sandbox':'production'),
  23. 'environment' => 'sandbox',
  24. 'merchantId' => $this->Config->MerchantID,
  25. 'publicKey' => $this->Config->PublicKey,
  26. 'privateKey' => $this->Config->PrivateKey
  27. ]);
  28. }
  29. /**
  30. *
  31. */
  32. public function getToken($User) {
  33. $customerId = 0;
  34. $collection = $this->Gateway->customer()->search([
  35. Braintree_CustomerSearch::id()->is($User->ID)
  36. ]);
  37. // New customer
  38. if(iterator_count($collection) == 0) {
  39. $result = $this->Gateway->customer()->create([
  40. 'id' => $User->ID,
  41. 'firstName' => $User->firstname,
  42. 'lastName' => $User->lastname,
  43. 'email' => $User->email
  44. ]);
  45. $customerId = $result->customer->id;
  46. }
  47. // Got it
  48. else if(iterator_count($collection) == 1) {
  49. $collection->rewind();
  50. $customerId = $collection->current()->id;
  51. }
  52. // Should never happen
  53. else {
  54. throw new \Exception('braintree/database internal error');
  55. }
  56. // Generate token
  57. try {
  58. return $this->Gateway->clientToken()->generate([
  59. "merchantAccountId" => $this->Config->MerchantID,
  60. "customerId" => $customerId
  61. ]);
  62. }
  63. catch(Braintree_Exception_Authentication $e) {
  64. throw $e;
  65. }
  66. catch(Braintree_Exception_Authorization $e) {
  67. throw $e;
  68. }
  69. }
  70. /**
  71. *
  72. */
  73. public function submit($User, $token, $amount) {
  74. $result = $this->Gateway->transaction()->sale([
  75. 'amount' => $amount,
  76. 'paymentMethodNonce' => $token,
  77. 'billing' => [
  78. 'firstName' => $User->lastname,
  79. 'lastName' => $User->firstname
  80. ],
  81. 'options' => [
  82. 'submitForSettlement' => True
  83. ]
  84. ]);
  85. if ($result->success) {
  86. // See $result->transaction for details
  87. return array(
  88. 'result' => 'OK',
  89. 'transactionID' => $result->transaction->id
  90. );
  91. }
  92. else {
  93. /*print_r($result->message);
  94. print_r($result->transaction->id);
  95. print_r($result->transaction->status);*/
  96. return array(
  97. 'result' => 'ERROR',
  98. 'transactionID' => $result->transaction->id,
  99. 'status' => $result->transaction->status,
  100. 'message' => $result->message
  101. );
  102. }
  103. }
  104. }