forked from silverstripe-archive/silverstripe-payment
/
DPSPaymentTest.php
369 lines (314 loc) · 11.8 KB
/
DPSPaymentTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<?php
/**
* The unit testing for DPS payment
* The testings cannot be run through without defining four Constants:
* DPSPAY_USERID_TEST, DPSPAY_KEY_TEST, DPSPAY_USERID_TEST, DPSPAY_KEY_TEST
* The testings cannot be run through without setting the above four constants to DPSAdapter correctly.
*/
class DPSPaymentTest extends SapphireTest implements TestOnly{
static $right_cc_data = array(
'CardHolderName' => 'SilverStripe Tester',
'CardNumber' => array(
'4111','1111','1111','1111'
),
'Cvc2' => '1111',
);
static $wrong_cc_data = array(
'CardHolderName' => 'SilverStripe Tester',
'CardNumber' => array(
'1234','5678','9012','3456'
),
'Cvc2' => '1111',
);
static function get_right_cc_data(){
$data = self::$right_cc_data;
$data['DateExpiry'] = date('my', strtotime('+1 year'));
return $data;
}
static function get_wrong_cc_data(){
$data = self::$wrong_cc_data;
$data['DateExpiry'] = date('my', strtotime('+1 year'));
return $data;
}
static function get_expired_cc_data(){
$data = self::$right_cc_data;
$data['DateExpiry'] = date('my', strtotime('-1 year'));
return $data;
}
function setUp(){
parent::setUp();
$runnable = self::get_runnable();
if(!isset($runnable)){
$canrun = true;
if(defined('DPSPOST_USERNAME_TEST') && defined('DPSPOST_PASSWORD_TEST') && defined('DPSPAY_USERID_TEST') && defined('DPSPAY_KEY_TEST')){
$canrun &= DPSAdapter::get_pxpost_username() === DPSPOST_USERNAME_TEST;
$canrun &= DPSAdapter::get_pxpost_password() === DPSPOST_PASSWORD_TEST;
$canrun &= DPSAdapter::get_pxpay_userid() === DPSPAY_USERID_TEST;
$canrun &= DPSAdapter::get_pxpay_key() === DPSPAY_KEY_TEST;
}else{
if(!defined('DPSPOST_USERNAME_TEST')){
$this->markTestSkipped("Can't run test without setting up DPSPOST_USERNAME_TEST the Unit Testing");
}else if(!defined('DPSPOST_PASSWORD_TEST')){
$this->markTestSkipped("Can't run test without setting up DPSPOST_PASSWORD_TEST the Unit Testing");
}else if(!defined('DPSPAY_USERID_TEST')){
$this->markTestSkipped("Can't run test without setting up DPSPAY_USERID_TEST the Unit Testing");
}else if(!defined('DPSPAY_KEY_TEST')){
$this->markTestSkipped("Can't run test without setting up DPSPAY_KEY_TEST the Unit Testing");
}
$canrun = false;
}
if(!$canrun){
self::set_runnable(false);
}else{
$canrun &= DPSAdapter::postConnect();
if(!$canrun){
self::set_runnable(false);
$this->markTestSkipped("Can't run test without successfully test the connection to dps getway");
}else{
self::set_runnable(true);
}
}
}
}
static $runnable;
static function set_runnable($runnable){
self::$runnable = $runnable;
}
static function get_runnable(){
return self::$runnable;
}
function createAPayment($currency, $amount){
$payment = new DPSPayment();
$payment->Amount->Currency = $currency;
$payment->Amount->Amount = $amount;
$payment->write();
return $payment;
}
function createARecurringPayment($currency, $amount){
$payment = new DPSRecurringPayment();
$payment->Amount->Currency = $currency;
$payment->Amount->Amount = $amount;
$payment->write();
return $payment;
}
function testAuthSuccess(){
if(self::get_runnable()){
$payment = $this->createAPayment('NZD', '1.00');
$payment->auth(self::get_right_cc_data());
$this->assertEquals($payment->TxnType, 'Auth');
$this->assertEquals($payment->Status, 'Success');
}
}
function testAuthFailure(){
if(self::get_runnable()){
$payment = $this->createAPayment('NZD', '1.00');
$payment->auth(self::get_wrong_cc_data());
$this->assertEquals($payment->Status, 'Failure');
$this->assertContains('Invalid Card', $payment->Message);
$payment->auth(self::get_expired_cc_data());
$this->assertEquals($payment->Status, 'Failure');
$this->assertContains('Card Expired', $payment->Message);
}
}
function testCompleteSuccess(){
if(self::get_runnable()){
$auth = $this->createAPayment('NZD', '1.00');
$auth->auth(self::get_right_cc_data());
$complete = $this->createAPayment('USD', '100.00');
$complete->AuthPaymentID = $auth->ID;
$complete->write();
$complete->complete();
$this->assertEquals($complete->TxnType, 'Complete');
$this->assertEquals($complete->Status, 'Success');
}
}
function testCompleteFailure(){
if(self::get_runnable()){
$auth = $this->createAPayment('NZD', '1.00');
$auth->auth(self::get_wrong_cc_data());
$complete = $this->createAPayment('USD', '100.00');
$complete->AuthPaymentID = $auth->ID;
$complete->write();
$complete->complete();
$this->assertEquals($complete->Status, 'Failure');
$this->assertContains('The transaction was Declined', $complete->Message);
}
}
function testCompleteOnlyOnce(){
if(self::get_runnable()){
$auth = $this->createAPayment('NZD', '1.00');
$auth->auth(self::get_right_cc_data());
$complete1 = $this->createAPayment('USD', '100.00');
$complete1->AuthPaymentID = $auth->ID;
$complete1->write();
$complete1->complete();
$this->assertEquals($complete1->Status, 'Success');
$complete2 = $this->createAPayment('USD', '100.00');
$complete2->AuthPaymentID = $auth->ID;
$complete2->write();
$complete2->complete();
$this->assertEquals($complete2->Status, 'Failure');
$this->assertContains('This transaction has been previously completed and cannot be completed again.', $complete2->Message);
}
}
function testPurchaseSuccess(){
if(self::get_runnable()){
$purchase = $this->createAPayment('NZD', '100.00');
$purchase->purchase(self::get_right_cc_data());
$this->assertEquals($purchase->TxnType, 'Purchase');
$this->assertEquals($purchase->Status, 'Success');
}
}
function testPurchaseFailure(){
if(self::get_runnable()){
$purchase = $this->createAPayment('NZD', '1.00');
$purchase->purchase(self::get_wrong_cc_data());
$this->assertEquals($purchase->Status, 'Failure');
$this->assertContains('Invalid Card', $purchase->Message);
$purchase->purchase(self::get_expired_cc_data());
$this->assertEquals($purchase->Status, 'Failure');
$this->assertContains('Card Expired', $purchase->Message);
DPSAdapter::set_pxpost_account(DPSPOST_USERNAME_TEST, 'wrongpass');
$purchase->purchase(self::get_right_cc_data());
$this->assertEquals($purchase->Status, 'Failure');
$this->assertContains('The transaction was Declined', $purchase->Message);
DPSAdapter::set_pxpost_account(DPSPOST_USERNAME_TEST, DPSPOST_PASSWORD_TEST);
}
}
function testRefundSeccess(){
if(self::get_runnable()){
$auth = $this->createAPayment('NZD', '1.00');
$auth->auth(self::get_right_cc_data());
$complete = $this->createAPayment('USD', '100.00');
$complete->AuthPaymentID = $auth->ID;
$complete->write();
$complete->complete();
$refund1 = $this->createAPayment('USD', '100.00');
$refund1->RefundedForID = $complete->ID;
$refund1->write();
$refund1->refund();
$this->assertEquals($refund1->TxnType, 'Refund');
$this->assertEquals($refund1->Status, 'Success');
$purchase = $this->createAPayment('NZD', '100.00');
$purchase->purchase(self::get_right_cc_data());
$refund2 = $this->createAPayment('USD', '100.00');
$refund2->RefundedForID = $purchase->ID;
$refund2->write();
$refund2->refund();
$this->assertEquals($refund2->TxnType, 'Refund');
$this->assertEquals($refund2->Status, 'Success');
$this->assertEquals($refund2->Amount->Amount, 100.00);
$this->assertEquals($refund2->Amount->Currency, 'NZD');
}
}
function testRefundFailure(){
if(self::get_runnable()){
$purchase1 = $this->createAPayment('NZD', '100.00');
$purchase1->purchase(self::get_expired_cc_data());
$refund1 = $this->createAPayment('NZD', '100.00');
$refund1->RefundedForID = $purchase1->ID;
$refund1->write();
$refund1->refund();
$this->assertEquals($refund1->Status, 'Failure');
$this->assertContains('The transaction was Declined', $refund1->Message);
$purchase2 = $this->createAPayment('NZD', '100.00');
$purchase2->purchase(self::get_right_cc_data());
$refund2 = $this->createAPayment('NZD', '1000.00');
$refund2->RefundedForID = $purchase2->ID;
$refund2->write();
$refund2->refund();
$this->assertEquals($refund2->Status, 'Failure');
$this->assertContains('The transaction was Declined', $refund2->Message);
}
}
function testMultipleRefund(){
if(self::get_runnable()){
$purchase = $this->createAPayment('NZD', '2.00');
$purchase->purchase(self::get_right_cc_data());
for($i=0;$i<2;$i++){
$refund = $this->createAPayment('NZD', '1.00');
$refund->RefundedForID = $purchase->ID;
$refund->write();
$refund->refund();
$this->assertEquals($refund->Status, 'Success');
}
$refund = $this->createAPayment('NZD', '1.00');
$refund->RefundedForID = $purchase->ID;
$refund->write();
$refund->refund();
$this->assertEquals($refund->Status, 'Failure');
$this->assertContains('The transaction has already been fully refunded', $refund->Message);
}
}
function testRecurringDPSPayment(){
if(self::get_runnable()){
$payment = $this->createARecurringPayment('NZD', '100.00');
$payment->Frequency = 'Monthly';
$payment->StartingDate = date('Y-m-d');
$payment->Times = 100;
$payment->write();
$payment->merchantRecurringAuth(self::get_right_cc_data());
$this->assertEquals($payment->Status, 'Success');
$this->assertContains('Transaction Approved', $payment->Message);
$date = $payment->StartingDate;
for($i=0;$i<3;$i++){
$payment->payNext();
$next = $payment->getLatestPayment();
$this->assertEquals($next->Status, 'Success');
$this->assertContains('Transaction Approved', $next->Message);
$this->assertEquals($next->PaymentDate, $date);
$date = date('Y-m-d', strtotime("1 month", strtotime($date)));
}
}
}
function testDPShostedPurchase(){
if(self::get_runnable()){
$purchase = $this->createAPayment('NZD', '100.00');
DPSAdapter::set_mode('Unit_Test_Only');
$url = $purchase->dpshostedPurchase(array());
DPSAdapter::set_mode('Normal');
$this->assertContains("https://www.paymentexpress.com/pxpay/pxpay.aspx?userid=", $url);
}
}
function testErrorHandling(){
if(self::get_runnable()){
$purchase = $this->createAPayment('NZD', '100.00');
DPSAdapter::set_mode('Error_Handling_Mode');
$purchase->purchase(self::get_right_cc_data());
DPSAdapter::set_mode('Normal');
$purchase = DataObject::get_by_id('DPSPayment', $purchase->ID);
$this->assertEquals('Incomplete', $purchase->Status);
$this->assertContains("'tsixe_dluoc_reven_taht_noitcnuf_a_function_that_never_could_exist' does not exist", $purchase->ExceptionError);
}
}
function testDBTransaction(){
if(self::get_runnable()){
$payment = $this->createARecurringPayment('NZD', '100.00');
$payment->Frequency = 'Monthly';
$payment->StartingDate = date('Y-m-d');
$payment->Times = 100;
$payment->write();
$payment->merchantRecurringAuth(self::get_right_cc_data());
if(defined('SS_DATABASE_CLASS') && SS_DATABASE_CLASS == 'PostgreSQLDatabase'){
DPSAdapter::set_mode('Rolling_Back_Mode');
$payment->payNext();
DPSAdapter::set_mode('Normal');
}
DPSAdapter::set_mode('Error_Handling_Mode');
$payment->payNext();
DPSAdapter::set_mode('Normal');
for($i=0;$i<2;$i++){
$payment->payNext();
}
$this->assertType('DataObjectSet', $payment->Payments());
$this->assertType('DataObjectSet', $payment->SuccessPayments());
$this->assertEquals($payment->Payments()->count(), 3);
$this->assertEquals($payment->SuccessPayments()->count(), 2);
}
$this->assertType('DataObjectSet', $payment->Payments());
$this->assertEquals($payment->Payments()->count(), 3);
$this->assertType('DataObjectSet', $payment->SuccessPayments());
$this->assertEquals($payment->SuccessPayments()->count(), 2);
}
}
?>