1
|
|
|
<?php |
2
|
|
|
namespace tuyakhov\braintree; |
3
|
|
|
|
4
|
|
|
use yii\base\InvalidCallException; |
5
|
|
|
use yii\base\Model; |
6
|
|
|
use yii\base\ModelEvent; |
7
|
|
|
|
8
|
|
|
class BraintreeForm extends Model |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
public $amount; |
|
|
|
|
12
|
|
|
public $orderId; |
|
|
|
|
13
|
|
|
public $paymentMethodToken; |
|
|
|
|
14
|
|
|
|
15
|
|
|
public $creditCard_number; |
|
|
|
|
16
|
|
|
public $creditCard_cvv; |
|
|
|
|
17
|
|
|
public $creditCard_expirationMonth; |
|
|
|
|
18
|
|
|
public $creditCard_expirationYear; |
|
|
|
|
19
|
|
|
public $creditCard_expirationDate; |
|
|
|
|
20
|
|
|
public $creditCard_name; |
|
|
|
|
21
|
|
|
|
22
|
|
|
public $customer_firstName; |
|
|
|
|
23
|
|
|
public $customer_lastName; |
|
|
|
|
24
|
|
|
public $customer_company; |
|
|
|
|
25
|
|
|
public $customer_phone; |
|
|
|
|
26
|
|
|
public $customer_fax; |
|
|
|
|
27
|
|
|
public $customer_website; |
|
|
|
|
28
|
|
|
public $customer_email; |
|
|
|
|
29
|
|
|
|
30
|
|
|
public $billing_firstName; |
|
|
|
|
31
|
|
|
public $billing_lastName; |
|
|
|
|
32
|
|
|
public $billing_company; |
|
|
|
|
33
|
|
|
public $billing_streetAddress; |
|
|
|
|
34
|
|
|
public $billing_extendedAddress; |
|
|
|
|
35
|
|
|
public $billing_locality; |
|
|
|
|
36
|
|
|
public $billing_region; |
|
|
|
|
37
|
|
|
public $billing_postalCode; |
|
|
|
|
38
|
|
|
public $billing_countryCodeAlpha2; |
|
|
|
|
39
|
|
|
|
40
|
|
|
public $shipping_firstName; |
|
|
|
|
41
|
|
|
public $shipping_lastName; |
|
|
|
|
42
|
|
|
public $shipping_company; |
|
|
|
|
43
|
|
|
public $shipping_streetAddress; |
|
|
|
|
44
|
|
|
public $shipping_extendedAddress; |
|
|
|
|
45
|
|
|
public $shipping_locality; |
|
|
|
|
46
|
|
|
public $shipping_region; |
|
|
|
|
47
|
|
|
public $shipping_postalCode; |
|
|
|
|
48
|
|
|
public $shipping_countryCodeAlpha2; |
|
|
|
|
49
|
|
|
|
50
|
|
|
public $customerId; |
|
|
|
|
51
|
|
|
|
52
|
|
|
public function rules() |
|
|
|
|
53
|
|
|
{ |
|
|
|
|
54
|
|
|
return [ |
|
|
|
|
55
|
|
|
[['customerId', 'creditCard_number', 'creditCard_cvv', 'creditCard_expirationDate'], 'required', 'on' => 'creditCard'], |
|
|
|
|
56
|
|
|
[['customerId'], 'required', 'on' => 'address'], |
|
|
|
|
57
|
|
|
[['customer_firstName', 'customer_lastName'], 'required', 'on' => 'customer'], |
|
|
|
|
58
|
|
|
[['amount', 'creditCard_number', 'creditCard_cvv', 'creditCard_expirationDate'], 'required', 'on' => 'sale'], |
|
|
|
|
59
|
|
|
[['amount', 'paymentMethodToken'], 'required', 'on' => 'saleFromVault'], |
|
|
|
|
60
|
|
|
[['amount'], 'double'], |
|
|
|
|
61
|
|
|
[['customer_email'], 'email'], |
|
|
|
|
62
|
|
|
[['creditCard_expirationMonth', |
|
|
|
|
63
|
|
|
'creditCard_expirationYear', |
|
|
|
|
64
|
|
|
'creditCard_expirationDate', |
|
|
|
|
65
|
|
|
'customer_firstName', |
|
|
|
|
66
|
|
|
'customer_lastName', |
|
|
|
|
67
|
|
|
'customer_company', |
|
|
|
|
68
|
|
|
'customer_phone', |
|
|
|
|
69
|
|
|
'customer_fax', |
|
|
|
|
70
|
|
|
'customer_website', |
|
|
|
|
71
|
|
|
'billing_firstName', |
|
|
|
|
72
|
|
|
'billing_lastName', |
|
|
|
|
73
|
|
|
'billing_company', |
|
|
|
|
74
|
|
|
'billing_streetAddress', |
|
|
|
|
75
|
|
|
'billing_extendedAddress', |
|
|
|
|
76
|
|
|
'billing_locality', |
|
|
|
|
77
|
|
|
'billing_region', |
|
|
|
|
78
|
|
|
'billing_postalCode', |
|
|
|
|
79
|
|
|
'billing_countryCodeAlpha2', |
|
|
|
|
80
|
|
|
'shipping_firstName', |
|
|
|
|
81
|
|
|
'shipping_lastName', |
|
|
|
|
82
|
|
|
'shipping_company', |
|
|
|
|
83
|
|
|
'shipping_streetAddress', |
|
|
|
|
84
|
|
|
'shipping_extendedAddress', |
|
|
|
|
85
|
|
|
'shipping_locality', |
|
|
|
|
86
|
|
|
'shipping_region', |
|
|
|
|
87
|
|
|
'shipping_postalCode', |
|
|
|
|
88
|
|
|
'shipping_countryCodeAlpha2'], 'safe'], |
|
|
|
|
89
|
|
|
]; |
|
|
|
|
90
|
|
|
} |
|
|
|
|
91
|
|
|
|
92
|
|
|
public function attributeLabels() |
|
|
|
|
93
|
|
|
{ |
|
|
|
|
94
|
|
|
return [ |
|
|
|
|
95
|
|
|
'amount' => 'Amount($)', |
|
|
|
|
96
|
|
|
'orderId' => 'Order ID', |
|
|
|
|
97
|
|
|
'creditCard_number' => 'Credit Card Number', |
|
|
|
|
98
|
|
|
'creditCard_cvv' => 'Security Code', |
|
|
|
|
99
|
|
|
'creditCard_expirationMonth' => 'Expiration Month (MM)', |
|
|
|
|
100
|
|
|
'creditCard_expirationYear' => 'Expiration Year (YYYY)', |
|
|
|
|
101
|
|
|
'creditCard_expirationDate' => 'Expiration Date (MM/YYYY)', |
|
|
|
|
102
|
|
|
'creditCard_name' => 'Name on Card', |
|
|
|
|
103
|
|
|
'customer_firstName' => 'First Name', |
|
|
|
|
104
|
|
|
'customer_lastName' => 'Last Name', |
|
|
|
|
105
|
|
|
'customer_company' => 'Company Name', |
|
|
|
|
106
|
|
|
'customer_phone' => 'Phone Number', |
|
|
|
|
107
|
|
|
'customer_fax' => 'Fax Number', |
|
|
|
|
108
|
|
|
'customer_website' => 'Website', |
|
|
|
|
109
|
|
|
'customer_email' => 'Email', |
|
|
|
|
110
|
|
|
'billing_firstName' => 'First Name', |
|
|
|
|
111
|
|
|
'billing_lastName' => 'Last Name', |
|
|
|
|
112
|
|
|
'billing_company' => 'Company Name', |
|
|
|
|
113
|
|
|
'billing_streetAddress' => 'Address', |
|
|
|
|
114
|
|
|
'billing_extendedAddress' => 'Address', |
|
|
|
|
115
|
|
|
'billing_locality' => 'City/Locality', |
|
|
|
|
116
|
|
|
'billing_region' => 'State/Region', |
|
|
|
|
117
|
|
|
'billing_postalCode' => 'Zip/Postal Code', |
|
|
|
|
118
|
|
|
'billing_countryCodeAlpha2' => 'Country', |
|
|
|
|
119
|
|
|
'shipping_firstName' => 'First Name', |
|
|
|
|
120
|
|
|
'shipping_lastName' => 'Last Name', |
|
|
|
|
121
|
|
|
'shipping_company' => 'Company Name', |
|
|
|
|
122
|
|
|
'shipping_streetAddress' => 'Address', |
|
|
|
|
123
|
|
|
'shipping_extendedAddress' => 'Address', |
|
|
|
|
124
|
|
|
'shipping_locality' => 'City/Locality', |
|
|
|
|
125
|
|
|
'shipping_region' => 'State/Region', |
|
|
|
|
126
|
|
|
'shipping_postalCode' => 'Zip/Postal Code', |
|
|
|
|
127
|
|
|
'shipping_countryCodeAlpha2' => 'Country', |
|
|
|
|
128
|
|
|
]; |
|
|
|
|
129
|
|
|
} |
|
|
|
|
130
|
|
|
|
131
|
|
|
public function getValuesFromAttributes() |
|
|
|
|
132
|
|
|
{ |
|
|
|
|
133
|
|
|
$values = array(); |
|
|
|
|
134
|
|
|
foreach ($this->attributes as $key => $val) { |
|
|
|
|
135
|
|
|
if (!is_object($val) && !is_null($val) && strlen($val) > 0) { |
|
|
|
|
136
|
|
|
if (strpos($key, '_') === false) { |
|
|
|
|
137
|
|
|
$values[$key] = $val; |
|
|
|
|
138
|
|
|
} else { |
|
|
|
|
139
|
|
|
$pieces = explode('_', $key); |
|
|
|
|
140
|
|
|
$values[$pieces[0]][$pieces[1]] = $val; |
|
|
|
|
141
|
|
|
} |
|
|
|
|
142
|
|
|
} |
|
|
|
|
143
|
|
|
} |
|
|
|
|
144
|
|
|
return $values; |
|
|
|
|
145
|
|
|
} |
|
|
|
|
146
|
|
|
|
147
|
|
|
public function send() |
|
|
|
|
148
|
|
|
{ |
|
|
|
|
149
|
|
|
\Yii::$app->get('braintree')->getOptions($this->getValuesFromAttributes()); |
|
|
|
|
150
|
|
|
$scenario = $this->getScenario(); |
|
|
|
|
151
|
|
|
return $this->$scenario(); |
|
|
|
|
152
|
|
|
|
153
|
|
|
} |
|
|
|
|
154
|
|
|
|
155
|
|
View Code Duplication |
public function sale() |
|
|
|
|
156
|
|
|
{ |
|
|
|
|
157
|
|
|
$return = \Yii::$app->get('braintree')->singleCharge(); |
|
|
|
|
158
|
|
|
if ($return['status'] === false) { |
|
|
|
|
159
|
|
|
$this->addErrorFromResponse($return['result']); |
|
|
|
|
160
|
|
|
return false; |
|
|
|
|
161
|
|
|
} else { |
|
|
|
|
162
|
|
|
return $return; |
|
|
|
|
163
|
|
|
} |
|
|
|
|
164
|
|
|
} |
|
|
|
|
165
|
|
|
|
166
|
|
View Code Duplication |
public function saleFromVault() |
|
|
|
|
167
|
|
|
{ |
|
|
|
|
168
|
|
|
$return = \Yii::$app->get('braintree')->singleCharge(); |
|
|
|
|
169
|
|
|
if ($return['status'] === false) { |
|
|
|
|
170
|
|
|
$this->addErrorFromResponse($return['result']); |
|
|
|
|
171
|
|
|
return false; |
|
|
|
|
172
|
|
|
} else { |
|
|
|
|
173
|
|
|
return $return; |
|
|
|
|
174
|
|
|
} |
|
|
|
|
175
|
|
|
} |
|
|
|
|
176
|
|
|
|
177
|
|
View Code Duplication |
public function customer() |
|
|
|
|
178
|
|
|
{ |
|
|
|
|
179
|
|
|
$return = \Yii::$app->get('braintree')->saveCustomer(); |
|
|
|
|
180
|
|
|
if ($return['status'] === false) { |
|
|
|
|
181
|
|
|
$this->addErrorFromResponse($return['result']); |
|
|
|
|
182
|
|
|
return false; |
|
|
|
|
183
|
|
|
} else { |
|
|
|
|
184
|
|
|
return $return; |
|
|
|
|
185
|
|
|
} |
|
|
|
|
186
|
|
|
} |
|
|
|
|
187
|
|
|
|
188
|
|
View Code Duplication |
public function creditCard() |
|
|
|
|
189
|
|
|
{ |
|
|
|
|
190
|
|
|
$return = \Yii::$app->get('braintree')->saveCreditCard(); |
|
|
|
|
191
|
|
|
if ($return['status'] === false) { |
|
|
|
|
192
|
|
|
$this->addErrorFromResponse($return['result']); |
|
|
|
|
193
|
|
|
return false; |
|
|
|
|
194
|
|
|
} else { |
|
|
|
|
195
|
|
|
return $return; |
|
|
|
|
196
|
|
|
} |
|
|
|
|
197
|
|
|
} |
|
|
|
|
198
|
|
|
|
199
|
|
View Code Duplication |
public function address() |
|
|
|
|
200
|
|
|
{ |
|
|
|
|
201
|
|
|
$return = \Yii::$app->get('braintree')->saveAddress(); |
|
|
|
|
202
|
|
|
if ($return['status'] === false) { |
|
|
|
|
203
|
|
|
$this->addErrorFromResponse($return['result']); |
|
|
|
|
204
|
|
|
return false; |
|
|
|
|
205
|
|
|
} else { |
|
|
|
|
206
|
|
|
return $return; |
|
|
|
|
207
|
|
|
} |
|
|
|
|
208
|
|
|
} |
|
|
|
|
209
|
|
|
|
210
|
|
|
/** |
|
|
|
|
211
|
|
|
* This add error from braintree response. |
|
|
|
|
212
|
|
|
* @param $result \Braintree_Result_Error |
|
|
|
|
213
|
|
|
*/ |
|
|
|
|
214
|
|
|
public function addErrorFromResponse($result) |
|
|
|
|
215
|
|
|
{ |
|
|
|
|
216
|
|
|
$values = $this->getValuesFromAttributes(); |
|
|
|
|
217
|
|
|
if (!empty($result->errors)) { |
|
|
|
|
218
|
|
|
foreach (array_keys($values) as $key) { |
|
|
|
|
219
|
|
|
$keyErrors = $result->errors->forKey($key); |
|
|
|
|
220
|
|
|
if (isset($keyErrors)) { |
|
|
|
|
221
|
|
|
foreach ($keyErrors->shallowAll() as $error) { |
|
|
|
|
222
|
|
|
$this->addError(($key . '_' . $error->attribute), $error->message); |
|
|
|
|
223
|
|
|
} |
|
|
|
|
224
|
|
|
} |
|
|
|
|
225
|
|
|
} |
|
|
|
|
226
|
|
|
} |
|
|
|
|
227
|
|
|
} |
|
|
|
|
228
|
|
|
|
229
|
|
|
} |