Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/Controller/PaymentsMollieTest.php
Show First 20 Lines • Show All 137 Lines • ▼ Show 20 Lines | public function testMandates(): void | ||||
$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']); | ||||
// Assert the proper payment amount has been used | // Assert the proper payment amount has been used | ||||
$payment = Payment::where('id', $json['id'])->first(); | $payment = Payment::where('id', $json['id'])->first(); | ||||
$this->assertSame(2010, $payment->amount); | $this->assertSame(2010, $payment->amount); | ||||
$this->assertSame($wallet->id, $payment->wallet_id); | $this->assertSame($wallet->id, $payment->wallet_id); | ||||
$this->assertSame("Kolab Now Auto-Payment Setup", $payment->description); | $this->assertSame(\config('app.name') . " Auto-Payment Setup", $payment->description); | ||||
$this->assertSame(PaymentProvider::TYPE_MANDATE, $payment->type); | $this->assertSame(PaymentProvider::TYPE_MANDATE, $payment->type); | ||||
// Test fetching the mandate information | // Test fetching the mandate information | ||||
$response = $this->actingAs($user)->get("api/v4/payments/mandate"); | $response = $this->actingAs($user)->get("api/v4/payments/mandate"); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
▲ Show 20 Lines • Show All 179 Lines • ▼ Show 20 Lines | class PaymentsMollieTest extends TestCase | ||||
public function testStoreAndWebhook(): void | public function testStoreAndWebhook(): void | ||||
{ | { | ||||
Bus::fake(); | Bus::fake(); | ||||
// Unauth access not allowed | // Unauth access not allowed | ||||
$response = $this->post("api/v4/payments", []); | $response = $this->post("api/v4/payments", []); | ||||
$response->assertStatus(401); | $response->assertStatus(401); | ||||
// Invalid amount | |||||
$user = $this->getTestUser('john@kolab.org'); | $user = $this->getTestUser('john@kolab.org'); | ||||
$post = ['amount' => -1]; | $post = ['amount' => -1]; | ||||
$response = $this->actingAs($user)->post("api/v4/payments", $post); | $response = $this->actingAs($user)->post("api/v4/payments", $post); | ||||
$response->assertStatus(422); | $response->assertStatus(422); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertSame('error', $json['status']); | $this->assertSame('error', $json['status']); | ||||
$this->assertCount(1, $json['errors']); | $this->assertCount(1, $json['errors']); | ||||
$min = intval(PaymentProvider::MIN_AMOUNT / 100) . ' CHF'; | $min = intval(PaymentProvider::MIN_AMOUNT / 100) . ' CHF'; | ||||
$this->assertSame("Minimum amount for a single payment is {$min}.", $json['errors']['amount']); | $this->assertSame("Minimum amount for a single payment is {$min}.", $json['errors']['amount']); | ||||
$post = ['amount' => '12.34']; | // Invalid currency | ||||
$post = ['amount' => '12.34', 'currency' => 'FOO', 'methodId' => 'creditcard']; | |||||
$response = $this->actingAs($user)->post("api/v4/payments", $post); | |||||
$response->assertStatus(500); | |||||
// Successful payment | |||||
$post = ['amount' => '12.34', 'currency' => 'CHF', 'methodId' => 'creditcard']; | |||||
$response = $this->actingAs($user)->post("api/v4/payments", $post); | $response = $this->actingAs($user)->post("api/v4/payments", $post); | ||||
$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']); | ||||
▲ Show 20 Lines • Show All 288 Lines • ▼ Show 20 Lines | public function testRefundAndChargeback(): void | ||||
$mollie = PaymentProvider::factory('mollie'); | $mollie = PaymentProvider::factory('mollie'); | ||||
// Create a paid payment | // Create a paid payment | ||||
$payment = Payment::create([ | $payment = Payment::create([ | ||||
'id' => 'tr_123456', | 'id' => 'tr_123456', | ||||
'status' => PaymentProvider::STATUS_PAID, | 'status' => PaymentProvider::STATUS_PAID, | ||||
'amount' => 123, | 'amount' => 123, | ||||
'currency_amount' => 123, | |||||
'currency' => 'CHF', | |||||
'type' => PaymentProvider::TYPE_ONEOFF, | 'type' => PaymentProvider::TYPE_ONEOFF, | ||||
'wallet_id' => $wallet->id, | 'wallet_id' => $wallet->id, | ||||
'provider' => 'mollie', | 'provider' => 'mollie', | ||||
'description' => 'test', | 'description' => 'test', | ||||
]); | ]); | ||||
// Test handling a refund by the webhook | // Test handling a refund by the webhook | ||||
▲ Show 20 Lines • Show All 139 Lines • ▼ Show 20 Lines | protected function createMandate(Wallet $wallet, array $params) | ||||
// and do actions with use of Dusk browser. | // and do actions with use of Dusk browser. | ||||
$this->startBrowser() | $this->startBrowser() | ||||
->visit($json['redirectUrl']) | ->visit($json['redirectUrl']) | ||||
->click('input[value="paid"]') | ->click('input[value="paid"]') | ||||
->click('button.form__button'); | ->click('button.form__button'); | ||||
$this->stopBrowser(); | $this->stopBrowser(); | ||||
} | } | ||||
/** | |||||
* Test listing a pending payment | |||||
* | |||||
* @group mollie | |||||
*/ | |||||
public function testListingPayments(): void | |||||
{ | |||||
Bus::fake(); | |||||
$user = $this->getTestUser('john@kolab.org'); | |||||
//Empty response | |||||
$response = $this->actingAs($user)->get("api/v4/payments/pending"); | |||||
$json = $response->json(); | |||||
$this->assertSame('success', $json['status']); | |||||
$this->assertSame(0, $json['count']); | |||||
$this->assertSame(1, $json['page']); | |||||
$this->assertSame(false, $json['hasMore']); | |||||
$this->assertCount(0, $json['list']); | |||||
$response = $this->actingAs($user)->get("api/v4/payments/has-pending"); | |||||
$json = $response->json(); | |||||
$this->assertSame(false, $json['hasPending']); | |||||
$wallet = $user->wallets()->first(); | |||||
// Successful payment | |||||
$post = ['amount' => '12.34', 'currency' => 'CHF', 'methodId' => 'creditcard']; | |||||
$response = $this->actingAs($user)->post("api/v4/payments", $post); | |||||
$response->assertStatus(200); | |||||
//A response | |||||
$response = $this->actingAs($user)->get("api/v4/payments/pending"); | |||||
$json = $response->json(); | |||||
$this->assertSame('success', $json['status']); | |||||
$this->assertSame(1, $json['count']); | |||||
$this->assertSame(1, $json['page']); | |||||
$this->assertSame(false, $json['hasMore']); | |||||
$this->assertCount(1, $json['list']); | |||||
$this->assertSame(PaymentProvider::STATUS_OPEN, $json['list'][0]['status']); | |||||
$response = $this->actingAs($user)->get("api/v4/payments/has-pending"); | |||||
$json = $response->json(); | |||||
$this->assertSame(true, $json['hasPending']); | |||||
// Set the payment to paid | |||||
$payments = Payment::where('wallet_id', $wallet->id)->get(); | |||||
$this->assertCount(1, $payments); | |||||
$payment = $payments[0]; | |||||
$payment->status = PaymentProvider::STATUS_PAID; | |||||
$payment->save(); | |||||
// They payment should be gone from the pending list now | |||||
$response = $this->actingAs($user)->get("api/v4/payments/pending"); | |||||
$json = $response->json(); | |||||
$this->assertSame('success', $json['status']); | |||||
machniak: We test the webhook in other places, so I guess you could just change the payment status in DB… | |||||
$this->assertSame(0, $json['count']); | |||||
$this->assertCount(0, $json['list']); | |||||
$response = $this->actingAs($user)->get("api/v4/payments/has-pending"); | |||||
$json = $response->json(); | |||||
$this->assertSame(false, $json['hasPending']); | |||||
} | |||||
/** | |||||
* Test listing payment methods | |||||
* | |||||
* @group mollie | |||||
*/ | |||||
public function testListingPaymentMethods(): void | |||||
{ | |||||
Bus::fake(); | |||||
$user = $this->getTestUser('john@kolab.org'); | |||||
Done Inline ActionsWe should probably have this test also in PaymentsStripeTest.php machniak: We should probably have this test also in PaymentsStripeTest.php | |||||
$response = $this->actingAs($user)->get('api/v4/payments/methods?type=' . PaymentProvider::TYPE_ONEOFF); | |||||
$response->assertStatus(200); | |||||
$json = $response->json(); | |||||
$this->assertCount(2, $json); | |||||
$this->assertSame('creditcard', $json[0]['id']); | |||||
Done Inline ActionsYou can do get() and put the param in the url. get('api/v4/payments/methods?type=' . PaymentProvider::TYPE_ONEOFF) machniak: You can do get() and put the param in the url. `get('api/v4/payments/methods?type=' . | |||||
$this->assertSame('paypal', $json[1]['id']); | |||||
$response = $this->actingAs($user)->get('api/v4/payments/methods?type=' . PaymentProvider::TYPE_RECURRING); | |||||
$response->assertStatus(200); | |||||
$json = $response->json(); | |||||
$this->assertCount(1, $json); | |||||
$this->assertSame('creditcard', $json[0]['id']); | |||||
} | |||||
} | } |
We test the webhook in other places, so I guess you could just change the payment status in DB to make it simpler/faster. All we test here is that the paid payment is not included in the list.