1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Anton Tuyakhov <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
namespace tuyakhov\braintree; |
6
|
|
|
|
7
|
|
|
use yii\base\Component; |
8
|
|
|
use yii\base\InvalidConfigException; |
9
|
|
|
|
10
|
|
|
class Braintree extends Component |
11
|
|
|
{ |
12
|
|
|
public $environment = 'sandbox'; |
|
|
|
|
13
|
|
|
public $merchantId; |
|
|
|
|
14
|
|
|
public $publicKey; |
|
|
|
|
15
|
|
|
public $privateKey; |
|
|
|
|
16
|
|
|
public $clientSideKey; |
|
|
|
|
17
|
|
|
|
18
|
|
|
public $options; |
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
|
|
|
|
21
|
|
|
* Sets up Braintree configuration from config file |
|
|
|
|
22
|
|
|
* @throws \yii\base\InvalidConfigException |
|
|
|
|
23
|
|
|
*/ |
|
|
|
|
24
|
|
|
public function init() |
|
|
|
|
25
|
|
|
{ |
|
|
|
|
26
|
|
|
foreach (['merchantId', 'publicKey', 'privateKey', 'environment'] as $attribute) { |
|
|
|
|
27
|
|
|
if ($this->$attribute === null) { |
|
|
|
|
28
|
|
|
throw new InvalidConfigException(strtr('"{class}::{attribute}" cannot be empty.', [ |
|
|
|
|
29
|
|
|
'{class}' => static::className(), |
|
|
|
|
30
|
|
|
'{attribute}' => '$' . $attribute |
|
|
|
|
31
|
|
|
])); |
|
|
|
|
32
|
|
|
} |
|
|
|
|
33
|
|
|
\Braintree_Configuration::$attribute($this->$attribute); |
|
|
|
|
34
|
|
|
} |
|
|
|
|
35
|
|
|
$this->clientSideKey = \Braintree_ClientToken::generate(); |
|
|
|
|
36
|
|
|
parent::init(); |
|
|
|
|
37
|
|
|
} |
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
|
|
|
|
40
|
|
|
* Braintree sale function |
|
|
|
|
41
|
|
|
* @param bool|true $submitForSettlement |
|
|
|
|
42
|
|
|
* @param bool|true $storeInVaultOnSuccess |
|
|
|
|
43
|
|
|
* @return array |
|
|
|
|
44
|
|
|
*/ |
|
|
|
|
45
|
|
|
public function singleCharge($submitForSettlement = true, $storeInVaultOnSuccess = true) |
|
|
|
|
46
|
|
|
{ |
|
|
|
|
47
|
|
|
$this->options['options']['submitForSettlement'] = $submitForSettlement; |
|
|
|
|
48
|
|
|
$this->options['options']['storeInVaultOnSuccess'] = $storeInVaultOnSuccess; |
|
|
|
|
49
|
|
|
$result = \Braintree_Transaction::sale($this->options); |
|
|
|
|
50
|
|
|
|
51
|
|
|
if ($result->success) { |
|
|
|
|
52
|
|
|
return ['status' => true, 'result' => $result]; |
|
|
|
|
53
|
|
View Code Duplication |
} else if ($result->transaction) { |
|
|
|
|
54
|
|
|
return ['status' => false, 'result' => $result]; |
|
|
|
|
55
|
|
|
} else { |
|
|
|
|
56
|
|
|
return ['status' => false, 'result' => $result]; |
|
|
|
|
57
|
|
|
} |
|
|
|
|
58
|
|
|
} |
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
|
|
|
|
61
|
|
|
* Finds transaction by id |
|
|
|
|
62
|
|
|
*/ |
|
|
|
|
63
|
|
|
public function findTransaction($id) |
|
|
|
|
64
|
|
|
{ |
|
|
|
|
65
|
|
|
return \Braintree_Transaction::find($id); |
|
|
|
|
66
|
|
|
} |
|
|
|
|
67
|
|
|
|
68
|
|
|
/** |
|
|
|
|
69
|
|
|
* This save customer to braintree and returns result array |
|
|
|
|
70
|
|
|
* @return array |
|
|
|
|
71
|
|
|
*/ |
|
|
|
|
72
|
|
|
public function saveCustomer() |
|
|
|
|
73
|
|
|
{ |
|
|
|
|
74
|
|
|
if (isset($this->options['customerId'])) { |
|
|
|
|
75
|
|
|
$this->options['customer']['id'] = $this->options['customerId']; |
|
|
|
|
76
|
|
|
} |
|
|
|
|
77
|
|
|
$result = \Braintree_Customer::create($this->options['customer']); |
|
|
|
|
78
|
|
|
|
79
|
|
View Code Duplication |
if ($result->success) { |
|
|
|
|
80
|
|
|
return ['status' => true, 'result' => $result]; |
|
|
|
|
81
|
|
|
} else { |
|
|
|
|
82
|
|
|
return ['status' => false, 'result' => $result]; |
|
|
|
|
83
|
|
|
} |
|
|
|
|
84
|
|
|
} |
|
|
|
|
85
|
|
|
|
86
|
|
|
/** |
|
|
|
|
87
|
|
|
* This save credit cart to braintree |
|
|
|
|
88
|
|
|
* @return array |
|
|
|
|
89
|
|
|
*/ |
|
|
|
|
90
|
|
|
public function saveCreditCard() |
|
|
|
|
91
|
|
|
{ |
|
|
|
|
92
|
|
|
$send_array = $this->options['creditCard']; |
|
|
|
|
93
|
|
|
if (isset($this->options['billing'])) { |
|
|
|
|
94
|
|
|
$send_array['billingAddress'] = $this->options['billing']; |
|
|
|
|
95
|
|
|
} |
|
|
|
|
96
|
|
|
if (isset($this->options['customerId'])) { |
|
|
|
|
97
|
|
|
$send_array['customerId'] = $this->options['customerId']; |
|
|
|
|
98
|
|
|
} |
|
|
|
|
99
|
|
|
$result = \Braintree_CreditCard::create($send_array); |
|
|
|
|
100
|
|
|
|
101
|
|
View Code Duplication |
if ($result->success) { |
|
|
|
|
102
|
|
|
return ['status' => true, 'result' => $result]; |
|
|
|
|
103
|
|
|
} else { |
|
|
|
|
104
|
|
|
return ['status' => false, 'result' => $result]; |
|
|
|
|
105
|
|
|
} |
|
|
|
|
106
|
|
|
} |
|
|
|
|
107
|
|
|
|
108
|
|
|
public function saveAddress() |
|
|
|
|
109
|
|
|
{ |
|
|
|
|
110
|
|
|
$send_array = $this->options['billing']; |
|
|
|
|
111
|
|
|
if (isset($this->options['customerId'])) { |
|
|
|
|
112
|
|
|
$send_array['customerId'] = $this->options['customerId']; |
|
|
|
|
113
|
|
|
} |
|
|
|
|
114
|
|
|
$result = \Braintree_Address::create($send_array); |
|
|
|
|
115
|
|
|
|
116
|
|
View Code Duplication |
if ($result->success) { |
|
|
|
|
117
|
|
|
return ['status' => true, 'result' => $result]; |
|
|
|
|
118
|
|
|
} else { |
|
|
|
|
119
|
|
|
return ['status' => false, 'result' => $result]; |
|
|
|
|
120
|
|
|
} |
|
|
|
|
121
|
|
|
} |
|
|
|
|
122
|
|
|
|
123
|
|
|
/** |
|
|
|
|
124
|
|
|
* Constructs the Credit Card array for payment |
|
|
|
|
125
|
|
|
* @param integer $number Credit Card Number |
|
|
|
|
126
|
|
|
* @param integer $cvv (optional)Credit Card Security code |
|
|
|
|
127
|
|
|
* @param integer $expirationMonth format: MM (use expirationMonth and expirationYear or expirationDate not both) |
|
|
|
|
128
|
|
|
* @param integer $expirationYear format: YYYY (use expirationMonth and expirationYear or expirationDate not both) |
|
|
|
|
129
|
|
|
* @param string $expirationDate format: MM/YYYY (use expirationMonth and expirationYear or expirationDate not both) |
|
|
|
|
130
|
|
|
*/ |
|
|
|
|
131
|
|
|
public function setCreditCard($number, $cvv = null, $expirationMonth = null, $expirationYear = null, $expirationDate = null) |
|
|
|
|
132
|
|
|
{ |
|
|
|
|
133
|
|
|
$this->options['creditCard'] = []; |
|
|
|
|
134
|
|
|
$this->options['creditCard']['number'] = $number; |
|
|
|
|
135
|
|
|
if (isset($cvv)) $this->options['creditCard']['cvv'] = $cvv; |
|
|
|
|
136
|
|
|
if (isset($expirationMonth)) $this->options['creditCard']['expirationMonth'] = $expirationMonth; |
|
|
|
|
137
|
|
|
if (isset($expirationYear)) $this->options['creditCard']['expirationYear'] = $expirationYear; |
|
|
|
|
138
|
|
|
if (isset($expirationDate)) $this->options['creditCard']['expirationDate'] = $expirationDate; |
|
|
|
|
139
|
|
|
} |
|
|
|
|
140
|
|
|
|
141
|
|
|
public function getCreditCard($input_values) |
|
|
|
|
142
|
|
|
{ |
|
|
|
|
143
|
|
|
$default = [ |
|
|
|
|
144
|
|
|
'cvv' => null, |
|
|
|
|
145
|
|
|
'expirationMonth' => null, |
|
|
|
|
146
|
|
|
'expirationYear' => null, |
|
|
|
|
147
|
|
|
'expirationDate' => null, |
|
|
|
|
148
|
|
|
'name' => null, |
|
|
|
|
149
|
|
|
]; |
|
|
|
|
150
|
|
|
$values = array_merge($default, $input_values); |
|
|
|
|
151
|
|
|
$this->setCreditCard($values['number'], $values['cvv'], $values['expirationMonth'], $values['expirationYear'], $values['expirationDate'], $values['name']); |
|
|
|
|
152
|
|
|
} |
|
|
|
|
153
|
|
|
|
154
|
|
|
public function getOptions($values) |
|
|
|
|
155
|
|
|
{ |
|
|
|
|
156
|
|
|
if (!empty($values)) { |
|
|
|
|
157
|
|
|
foreach ($values as $key => $value) { |
|
|
|
|
158
|
|
|
if ($key == 'amount') |
|
|
|
|
159
|
|
|
$this->setAmount($values['amount']); |
|
|
|
|
160
|
|
|
elseif ($key == 'creditCard') |
|
|
|
|
161
|
|
|
$this->getCreditCard($values['creditCard']); |
|
|
|
|
162
|
|
|
else |
|
|
|
|
163
|
|
|
$this->options[$key] = $value; |
|
|
|
|
164
|
|
|
} |
|
|
|
|
165
|
|
|
} |
|
|
|
|
166
|
|
|
} |
|
|
|
|
167
|
|
|
|
168
|
|
|
/** |
|
|
|
|
169
|
|
|
* Set the amount to charge |
|
|
|
|
170
|
|
|
* @param float $amount No dollar sign needed |
|
|
|
|
171
|
|
|
*/ |
|
|
|
|
172
|
|
|
public function setAmount($amount) |
|
|
|
|
173
|
|
|
{ |
|
|
|
|
174
|
|
|
$this->options['amount'] = round($amount, 2); |
|
|
|
|
175
|
|
|
} |
|
|
|
|
176
|
|
|
} |