1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tzsk\Payu\Tests\Feature; |
4
|
|
|
|
5
|
|
|
use Illuminate\Foundation\Testing\RefreshDatabase; |
6
|
|
|
use Illuminate\Support\Facades\Event; |
7
|
|
|
use Illuminate\Support\Facades\Session; |
8
|
|
|
use Illuminate\Support\Facades\URL; |
9
|
|
|
use Tzsk\Payu\Events\TransactionFailed; |
10
|
|
|
use Tzsk\Payu\Events\TransactionInvalidated; |
11
|
|
|
use Tzsk\Payu\Events\TransactionSuccessful; |
12
|
|
|
use Tzsk\Payu\Facades\Payu; |
13
|
|
|
use Tzsk\Payu\Models\PayuTransaction; |
14
|
|
|
use Tzsk\Payu\Tests\GatewayResponse; |
15
|
|
|
use Tzsk\Payu\Tests\TestCase; |
16
|
|
|
|
17
|
|
|
class ReceiveCallbackTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
use RefreshDatabase; |
20
|
|
|
|
21
|
|
|
/** @test */ |
22
|
|
|
public function it_can_return_captured_transaction() |
23
|
|
|
{ |
24
|
|
|
/** @var PayuTransaction $transaction */ |
25
|
|
|
$transaction = factory(PayuTransaction::class)->create(); |
26
|
|
|
Session::put('payuTransactionId', $transaction->transaction_id); |
27
|
|
|
|
28
|
|
|
$this->assertInstanceOf(PayuTransaction::class, $captured = Payu::capture()); |
29
|
|
|
$this->assertSame($transaction->transaction_id, $captured->transaction_id); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** @test */ |
33
|
|
|
public function it_can_receive_success_post_back_from_payu() |
34
|
|
|
{ |
35
|
|
|
Event::fake(); |
36
|
|
|
/** @var PayuTransaction $transaction */ |
37
|
|
|
$transaction = factory(PayuTransaction::class)->create(); |
38
|
|
|
$body = new GatewayResponse($transaction); |
39
|
|
|
|
40
|
|
|
$url = $this->getUrl($transaction, PayuTransaction::STATUS_SUCCESSFUL); |
41
|
|
|
$payload = $body->payment('success'); |
42
|
|
|
|
43
|
|
|
$this->assertTrue($transaction->pending()); |
44
|
|
|
$this->assertFalse($transaction->successful()); |
45
|
|
|
|
46
|
|
|
$response = $this->postJson($url, $payload); |
47
|
|
|
$response->assertRedirect($transaction->destination); |
48
|
|
|
|
49
|
|
|
$fresh = $transaction->fresh(); |
50
|
|
|
$this->assertFalse($fresh->pending()); |
51
|
|
|
$this->assertTrue($fresh->successful()); |
52
|
|
|
Event::assertDispatched(TransactionSuccessful::class, fn ($event) => $event->transaction instanceof PayuTransaction); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** @test */ |
56
|
|
|
public function it_can_receive_failure_post_back_from_payu() |
57
|
|
|
{ |
58
|
|
|
Event::fake(); |
59
|
|
|
/** @var PayuTransaction $transaction */ |
60
|
|
|
$transaction = factory(PayuTransaction::class)->create(); |
61
|
|
|
$body = new GatewayResponse($transaction); |
62
|
|
|
|
63
|
|
|
$url = $this->getUrl($transaction, PayuTransaction::STATUS_FAILED); |
64
|
|
|
$payload = $body->payment('failure'); |
65
|
|
|
|
66
|
|
|
$this->assertTrue($transaction->pending()); |
67
|
|
|
$this->assertFalse($transaction->failed()); |
68
|
|
|
|
69
|
|
|
$response = $this->postJson($url, $payload); |
70
|
|
|
$response->assertRedirect($transaction->destination); |
71
|
|
|
|
72
|
|
|
$fresh = $transaction->fresh(); |
73
|
|
|
$this->assertFalse($fresh->pending()); |
74
|
|
|
$this->assertTrue($fresh->failed()); |
75
|
|
|
Event::assertDispatched(TransactionFailed::class, fn ($event) => $event->transaction instanceof PayuTransaction); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** @test */ |
79
|
|
|
public function it_can_receive_invalid_post_back_from_payu() |
80
|
|
|
{ |
81
|
|
|
Event::fake(); |
82
|
|
|
/** @var PayuTransaction $transaction */ |
83
|
|
|
$transaction = factory(PayuTransaction::class)->create(); |
84
|
|
|
$body = new GatewayResponse($transaction); |
85
|
|
|
|
86
|
|
|
$url = $this->getUrl($transaction, PayuTransaction::STATUS_SUCCESSFUL); |
87
|
|
|
$payload = $body->payment('failure'); |
88
|
|
|
|
89
|
|
|
$this->assertTrue($transaction->pending()); |
90
|
|
|
$this->assertFalse($transaction->invalid()); |
91
|
|
|
|
92
|
|
|
$response = $this->postJson($url, array_merge($payload, ['status' => 'success'])); |
93
|
|
|
$response->assertRedirect($transaction->destination); |
94
|
|
|
|
95
|
|
|
$fresh = $transaction->fresh(); |
96
|
|
|
$this->assertFalse($fresh->pending()); |
97
|
|
|
$this->assertTrue($fresh->invalid()); |
98
|
|
|
Event::assertDispatched(TransactionInvalidated::class, fn ($event) => $event->transaction instanceof PayuTransaction); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
protected function getUrl(PayuTransaction $transaction, string $status) |
102
|
|
|
{ |
103
|
|
|
return $path = URL::temporarySignedRoute( |
104
|
|
|
'payu::redirect', |
105
|
|
|
now()->addMinutes(30), |
106
|
|
|
['transaction' => $transaction->transaction_id, 'urlType' => $status], |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|