Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/Controller/PaymentsTest.php
<?php | <?php | ||||
namespace Tests\Feature\Controller; | namespace Tests\Feature\Controller; | ||||
use App\Payment; | use App\Transaction; | ||||
use App\Wallet; | use App\Wallet; | ||||
use GuzzleHttp\Psr7\Response; | use GuzzleHttp\Psr7\Response; | ||||
use Tests\TestCase; | use Tests\TestCase; | ||||
class PaymentsTest extends TestCase | class PaymentsTest extends TestCase | ||||
{ | { | ||||
use \Tests\MollieMocksTrait; | use \Tests\MollieMocksTrait; | ||||
/** | /** | ||||
* {@inheritDoc} | * {@inheritDoc} | ||||
*/ | */ | ||||
public function setUp(): void | public function setUp(): void | ||||
{ | { | ||||
parent::setUp(); | parent::setUp(); | ||||
$john = $this->getTestUser('john@kolab.org'); | $john = $this->getTestUser('john@kolab.org'); | ||||
$wallet = $john->wallets()->first(); | $wallet = $john->wallets()->first(); | ||||
$john->setSetting('mollie_id', null); | $john->setSetting('mollie_id', null); | ||||
Payment::where('wallet_id', $wallet->id)->delete(); | Transaction::where('wallet_id', $wallet->id)->delete(); | ||||
Wallet::where('id', $wallet->id)->update(['balance' => 0]); | Wallet::where('id', $wallet->id)->update(['balance' => 0]); | ||||
} | } | ||||
/** | /** | ||||
* {@inheritDoc} | * {@inheritDoc} | ||||
*/ | */ | ||||
public function tearDown(): void | public function tearDown(): void | ||||
{ | { | ||||
$john = $this->getTestUser('john@kolab.org'); | $john = $this->getTestUser('john@kolab.org'); | ||||
$wallet = $john->wallets()->first(); | $wallet = $john->wallets()->first(); | ||||
$john->setSetting('mollie_id', null); | $john->setSetting('mollie_id', null); | ||||
Payment::where('wallet_id', $wallet->id)->delete(); | Transaction::where('wallet_id', $wallet->id)->delete(); | ||||
Wallet::where('id', $wallet->id)->update(['balance' => 0]); | Wallet::where('id', $wallet->id)->update(['balance' => 0]); | ||||
parent::tearDown(); | parent::tearDown(); | ||||
} | } | ||||
/** | /** | ||||
* Test creating a payment and receiving a status via webhook) | * Test creating a payment and receiving a status via webhook) | ||||
* | * | ||||
Show All 22 Lines | public function testStoreAndWebhook(): void | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertSame('success', $json['status']); | $this->assertSame('success', $json['status']); | ||||
$this->assertRegExp('|^https://www.mollie.com|', $json['redirectUrl']); | $this->assertRegExp('|^https://www.mollie.com|', $json['redirectUrl']); | ||||
$wallet = $user->wallets()->first(); | $wallet = $user->wallets()->first(); | ||||
$payments = Payment::where('wallet_id', $wallet->id)->get(); | $transactions = Transaction::where('wallet_id', $wallet->id)->get(); | ||||
$this->assertCount(1, $payments); | $this->assertCount(1, $transactions); | ||||
$payment = $payments[0]; | $transaction = $transactions[0]; | ||||
$this->assertSame(1234, $payment->amount); | $this->assertSame(1234, $transaction->amount); | ||||
$this->assertSame('Kolab Now Payment', $payment->description); | $this->assertSame('Kolab Now Payment', $transaction->description); | ||||
$this->assertSame('open', $payment->status); | $this->assertSame('open', $transaction->mollie_status); | ||||
$this->assertEquals(0, $wallet->balance); | $this->assertEquals(0, $wallet->balance); | ||||
// Test the webhook | // Test the webhook | ||||
// Note: Webhook end-point does not require authentication | // Note: Webhook end-point does not require authentication | ||||
$mollie_response = [ | $mollie_response = [ | ||||
"resource" => "payment", | "resource" => "payment", | ||||
"id" => $payment->id, | "id" => $transaction->mollie_id, | ||||
"status" => "paid", | "status" => "paid", | ||||
// Status is not enough, paidAt is used to distinguish the state | // Status is not enough, paidAt is used to distinguish the state | ||||
"paidAt" => date('c'), | "paidAt" => date('c'), | ||||
"mode" => "test", | "mode" => "test", | ||||
/* | /* | ||||
"createdAt" => "2018-03-20T13:13:37+00:00", | "createdAt" => "2018-03-20T13:13:37+00:00", | ||||
"amount" => { | "amount" => { | ||||
"value" => "10.00", | "value" => "10.00", | ||||
Show All 17 Lines | */ | ||||
]; | ]; | ||||
// We'll trigger the webhook with payment id and use mocking for | // We'll trigger the webhook with payment id and use mocking for | ||||
// a request to the Mollie payments API. We cannot force Mollie | // a request to the Mollie payments API. We cannot force Mollie | ||||
// to make the payment status change. | // to make the payment status change. | ||||
$responseStack = $this->mockMollie(); | $responseStack = $this->mockMollie(); | ||||
$responseStack->append(new Response(200, [], json_encode($mollie_response))); | $responseStack->append(new Response(200, [], json_encode($mollie_response))); | ||||
$post = ['id' => $payment->id]; | $post = ['id' => $transaction->mollie_id]; | ||||
$response = $this->post("api/webhooks/payment/mollie", $post); | $response = $this->post("api/webhooks/payment/mollie", $post); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$this->assertSame('paid', $payment->fresh()->status); | $this->assertSame('paid', $transaction->fresh()->mollie_status); | ||||
$this->assertEquals(1234, $wallet->fresh()->balance); | $this->assertEquals(1234, $wallet->fresh()->balance); | ||||
// Verify "paid -> open -> paid" scenario, assert that balance didn't change | // Verify "paid -> open -> paid" scenario, assert that balance didn't change | ||||
$mollie_response['status'] = 'open'; | $mollie_response['status'] = 'open'; | ||||
unset($mollie_response['paidAt']); | unset($mollie_response['paidAt']); | ||||
$responseStack->append(new Response(200, [], json_encode($mollie_response))); | $responseStack->append(new Response(200, [], json_encode($mollie_response))); | ||||
$response = $this->post("api/webhooks/payment/mollie", $post); | $response = $this->post("api/webhooks/payment/mollie", $post); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$this->assertSame('paid', $payment->fresh()->status); | $this->assertSame('paid', $transaction->fresh()->mollie_status); | ||||
$this->assertEquals(1234, $wallet->fresh()->balance); | $this->assertEquals(1234, $wallet->fresh()->balance); | ||||
$mollie_response['status'] = 'paid'; | $mollie_response['status'] = 'paid'; | ||||
$mollie_response['paidAt'] = date('c'); | $mollie_response['paidAt'] = date('c'); | ||||
$responseStack->append(new Response(200, [], json_encode($mollie_response))); | $responseStack->append(new Response(200, [], json_encode($mollie_response))); | ||||
$response = $this->post("api/webhooks/payment/mollie", $post); | $response = $this->post("api/webhooks/payment/mollie", $post); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$this->assertSame('paid', $payment->fresh()->status); | $this->assertSame('paid', $transaction->fresh()->mollie_status); | ||||
$this->assertEquals(1234, $wallet->fresh()->balance); | $this->assertEquals(1234, $wallet->fresh()->balance); | ||||
} | } | ||||
public function testDirectCharge(): void | public function testDirectCharge(): void | ||||
{ | { | ||||
$this->markTestIncomplete(); | $this->markTestIncomplete(); | ||||
} | } | ||||
} | } |