1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Anton Tuyakhov <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace tuyakhov\braintree; |
8
|
|
|
|
9
|
|
|
use Braintree\Customer; |
10
|
|
|
use Braintree\Result\Error; |
11
|
|
|
use Yii; |
12
|
|
|
use yii\helpers\ArrayHelper; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class BraintreeFormTest. |
16
|
|
|
*/ |
17
|
|
|
class BraintreeFormTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
public static $customer; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param string $ccNumber |
23
|
|
|
* @param string $cvv |
24
|
|
|
* @param string $exp |
25
|
|
|
* @param string $cardholderName |
26
|
|
|
* @dataProvider validCreditCardProvider |
27
|
|
|
*/ |
28
|
|
|
public function testSale(string $ccNumber, string $cvv, string $exp, string $cardholderName) |
29
|
|
|
{ |
30
|
|
|
$model = new BraintreeForm(); |
31
|
|
|
$model->setScenario('sale'); |
32
|
|
|
static::assertTrue( |
33
|
|
|
$model->load( |
34
|
|
|
[ |
35
|
|
|
'creditCard_number' => $ccNumber, |
36
|
|
|
'creditCard_cvv' => $cvv, |
37
|
|
|
'creditCard_expirationDate' => $exp, |
38
|
|
|
'creditCard_cardholderName' => $cardholderName, |
39
|
|
|
], |
40
|
|
|
'' |
41
|
|
|
) |
42
|
|
|
); |
43
|
|
|
$model->amount = rand(1, 200); |
44
|
|
|
static::assertNotFalse($model->send()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $ccNumber |
49
|
|
|
* @param string $cvv |
50
|
|
|
* @param string $exp |
51
|
|
|
* @dataProvider invalidCreditCardProvider |
52
|
|
|
*/ |
53
|
|
|
public function testSaleFail(string $ccNumber, string $cvv, string $exp) |
54
|
|
|
{ |
55
|
|
|
$model = new BraintreeForm(); |
56
|
|
|
$model->setScenario('sale'); |
57
|
|
|
static::assertTrue( |
58
|
|
|
$model->load( |
59
|
|
|
[ |
60
|
|
|
'creditCard_number' => $ccNumber, |
61
|
|
|
'creditCard_cvv' => $cvv, |
62
|
|
|
'creditCard_expirationDate' => $exp, |
63
|
|
|
], |
64
|
|
|
'' |
65
|
|
|
) |
66
|
|
|
); |
67
|
|
|
$model->amount = 1; |
68
|
|
|
static::assertFalse($model->send()); |
69
|
|
|
static::assertInstanceOf(Error::class, $model->lastError); |
70
|
|
|
static::assertIsString($model->lastError->message); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $firstName |
75
|
|
|
* @param string $lastName |
76
|
|
|
* @dataProvider customerProvider |
77
|
|
|
*/ |
78
|
|
|
public function testCustomerCreate(string $firstName, string $lastName) |
79
|
|
|
{ |
80
|
|
|
$model = new BraintreeForm(); |
81
|
|
|
$model->setScenario('customer'); |
82
|
|
|
static::assertTrue( |
83
|
|
|
$model->load( |
84
|
|
|
[ |
85
|
|
|
'customer_firstName' => $firstName, |
86
|
|
|
'customer_lastName' => $lastName, |
87
|
|
|
], |
88
|
|
|
'' |
89
|
|
|
) |
90
|
|
|
); |
91
|
|
|
$result = $model->send(); |
92
|
|
|
static::assertNotFalse($result); |
93
|
|
|
static::assertArrayHasKey('result', $result); |
94
|
|
|
static::assertObjectHasAttribute('customer', $result['result']); |
95
|
|
|
static::$customer = $result['result']->customer; |
96
|
|
|
static::assertInstanceOf(Customer::class, static::$customer); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $ccNumber |
101
|
|
|
* @param string $cvv |
102
|
|
|
* @param string $exp |
103
|
|
|
* @param string $cardholderName |
104
|
|
|
* @depends testCustomerCreate |
105
|
|
|
* @dataProvider validCreditCardProvider |
106
|
|
|
*/ |
107
|
|
|
public function testCreditCardCreate(string $ccNumber, string $cvv, string $exp, string $cardholderName) |
108
|
|
|
{ |
109
|
|
|
$model = new BraintreeForm(); |
110
|
|
|
$model->setScenario('creditCard'); |
111
|
|
|
static::assertTrue( |
112
|
|
|
$model->load( |
113
|
|
|
[ |
114
|
|
|
'creditCard_number' => $ccNumber, |
115
|
|
|
'creditCard_cvv' => $cvv, |
116
|
|
|
'creditCard_expirationDate' => $exp, |
117
|
|
|
'creditCard_cardholderName' => $cardholderName, |
118
|
|
|
], |
119
|
|
|
'' |
120
|
|
|
) |
121
|
|
|
); |
122
|
|
|
$model->customerId = static::$customer->id; |
123
|
|
|
static::assertNotFalse($model->send()); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @depends testCustomerCreate |
128
|
|
|
*/ |
129
|
|
|
public function testTokenPayment() |
130
|
|
|
{ |
131
|
|
|
$customer = Customer::find(static::$customer->id); |
132
|
|
|
static::assertInstanceOf(Customer::class, $customer); |
133
|
|
|
static::assertArrayHasKey(0, $customer->paymentMethods); |
134
|
|
|
$model = new BraintreeForm(); |
135
|
|
|
$model->setScenario('saleFromVault'); |
136
|
|
|
static::assertTrue( |
137
|
|
|
$model->load( |
138
|
|
|
[ |
139
|
|
|
'amount' => rand(1, 200), |
140
|
|
|
'paymentMethodToken' => $customer->paymentMethods[0]->token, |
141
|
|
|
], |
142
|
|
|
'' |
143
|
|
|
) |
144
|
|
|
); |
145
|
|
|
static::assertNotFalse($model->send()); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function testGetAllPlans() |
149
|
|
|
{ |
150
|
|
|
$plans = BraintreeForm::getAllPlans(); |
151
|
|
|
static::assertIsArray($plans); |
152
|
|
|
foreach ($plans as $plan) { |
153
|
|
|
static::assertInstanceOf('Braintree\Plan', $plan); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function testFindMerchant() |
158
|
|
|
{ |
159
|
|
|
$merchantId = ArrayHelper::getValue(Yii::$app->params, 'merchant_account_id'); |
160
|
|
|
if (!isset($merchantId)) { |
161
|
|
|
static::markTestSkipped( |
162
|
|
|
'Valid "merchant_account_id" should be specified in tests/config/params-local.php.' |
163
|
|
|
); |
164
|
|
|
} |
165
|
|
|
$model = new BraintreeForm(); |
166
|
|
|
static::assertInstanceOf('Braintree\MerchantAccount', $model->findMerchant($merchantId)); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function testSearchSubscription() |
170
|
|
|
{ |
171
|
|
|
$model = new BraintreeForm(); |
172
|
|
|
static::assertInstanceOf('Braintree\ResourceCollection', $model->searchSubscription()); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function validCreditCardProvider(): array |
176
|
|
|
{ |
177
|
|
|
return [ |
178
|
|
|
[ |
179
|
|
|
'5555555555554444', |
180
|
|
|
'123', |
181
|
|
|
'12/2020', |
182
|
|
|
'BRAD PITT' |
183
|
|
|
], |
184
|
|
|
]; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function customerProvider(): array |
188
|
|
|
{ |
189
|
|
|
return [ |
190
|
|
|
[ |
191
|
|
|
'Brad', |
192
|
|
|
'Pitt', |
193
|
|
|
], |
194
|
|
|
]; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function invalidCreditCardProvider(): array |
198
|
|
|
{ |
199
|
|
|
return [ |
200
|
|
|
'invalid card number' => ['0', '123', '12/2020'], |
201
|
|
|
]; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|