Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/Controller/PaymentsStripeTest.php
Show First 20 Lines • Show All 100 Lines • ▼ Show 20 Lines | public function testMandates(): void | ||||
$post = ['amount' => -100, 'balance' => 0]; | $post = ['amount' => -100, 'balance' => 0]; | ||||
$response = $this->actingAs($user)->post("api/v4/payments/mandate", $post); | $response = $this->actingAs($user)->post("api/v4/payments/mandate", $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 = $wallet->money(PaymentProvider::MIN_AMOUNT); | ||||
$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']); | ||||
// Test creating a mandate (negative balance, amount too small) | // Test creating a mandate (negative balance, amount too small) | ||||
Wallet::where('id', $wallet->id)->update(['balance' => -2000]); | Wallet::where('id', $wallet->id)->update(['balance' => -2000]); | ||||
$post = ['amount' => PaymentProvider::MIN_AMOUNT / 100, 'balance' => 0]; | $post = ['amount' => PaymentProvider::MIN_AMOUNT / 100, 'balance' => 0]; | ||||
$response = $this->actingAs($user)->post("api/v4/payments/mandate", $post); | $response = $this->actingAs($user)->post("api/v4/payments/mandate", $post); | ||||
$response->assertStatus(422); | $response->assertStatus(422); | ||||
Show All 12 Lines | public function testMandates(): void | ||||
$this->assertSame('success', $json['status']); | $this->assertSame('success', $json['status']); | ||||
$this->assertMatchesRegularExpression('|^cs_test_|', $json['id']); | $this->assertMatchesRegularExpression('|^cs_test_|', $json['id']); | ||||
// Assert the proper payment amount has been used | // Assert the proper payment amount has been used | ||||
// Stripe in 'setup' mode does not allow to set the amount | // Stripe in 'setup' mode does not allow to set the amount | ||||
$payment = Payment::where('wallet_id', $wallet->id)->first(); | $payment = Payment::where('wallet_id', $wallet->id)->first(); | ||||
$this->assertSame(0, $payment->amount); | $this->assertSame(0, $payment->amount); | ||||
$this->assertSame(\config('app.name') . " Auto-Payment Setup", $payment->description); | $this->assertSame($user->tenant->title . " 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 142 Lines • ▼ Show 20 Lines | class PaymentsStripeTest extends TestCase | ||||
{ | { | ||||
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); | ||||
$user = $this->getTestUser('john@kolab.org'); | $user = $this->getTestUser('john@kolab.org'); | ||||
$wallet = $user->wallets()->first(); | |||||
$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 = $wallet->money(PaymentProvider::MIN_AMOUNT); | ||||
$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']); | ||||
// Invalid currency | // Invalid currency | ||||
$post = ['amount' => '12.34', 'currency' => 'FOO', 'methodId' => 'creditcard']; | $post = ['amount' => '12.34', 'currency' => 'FOO', 'methodId' => 'creditcard']; | ||||
$response = $this->actingAs($user)->post("api/v4/payments", $post); | $response = $this->actingAs($user)->post("api/v4/payments", $post); | ||||
$response->assertStatus(500); | $response->assertStatus(500); | ||||
// Successful payment | // Successful payment | ||||
$post = ['amount' => '12.34', 'currency' => 'CHF', 'methodId' => 'creditcard']; | $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->assertMatchesRegularExpression('|^cs_test_|', $json['id']); | $this->assertMatchesRegularExpression('|^cs_test_|', $json['id']); | ||||
$wallet = $user->wallets()->first(); | |||||
$payments = Payment::where('wallet_id', $wallet->id)->get(); | $payments = Payment::where('wallet_id', $wallet->id)->get(); | ||||
$this->assertCount(1, $payments); | $this->assertCount(1, $payments); | ||||
$payment = $payments[0]; | $payment = $payments[0]; | ||||
$this->assertSame(1234, $payment->amount); | $this->assertSame(1234, $payment->amount); | ||||
$this->assertSame(\config('app.name') . ' Payment', $payment->description); | $this->assertSame($user->tenant->title . ' Payment', $payment->description); | ||||
$this->assertSame('open', $payment->status); | $this->assertSame('open', $payment->status); | ||||
$this->assertEquals(0, $wallet->balance); | $this->assertEquals(0, $wallet->balance); | ||||
// Test the webhook | // Test the webhook | ||||
$post = [ | $post = [ | ||||
'id' => "evt_1GlZ814fj3SIEU8wtxMZ4Nsa", | 'id' => "evt_1GlZ814fj3SIEU8wtxMZ4Nsa", | ||||
'object' => "event", | 'object' => "event", | ||||
▲ Show 20 Lines • Show All 193 Lines • ▼ Show 20 Lines | public function testTopUpAndWebhook(): void | ||||
]); | ]); | ||||
$paymentIntent = json_encode([ | $paymentIntent = json_encode([ | ||||
"id" => "pi_XX", | "id" => "pi_XX", | ||||
"object" => "payment_intent", | "object" => "payment_intent", | ||||
"created" => 123456789, | "created" => 123456789, | ||||
"amount" => 2010, | "amount" => 2010, | ||||
"currency" => "chf", | "currency" => "chf", | ||||
"description" => "Kolab Recurring Payment" | "description" => $user->tenant->title . " Recurring Payment" | ||||
]); | ]); | ||||
$client = $this->mockStripe(); | $client = $this->mockStripe(); | ||||
$client->addResponse($setupIntent); | $client->addResponse($setupIntent); | ||||
$client->addResponse($paymentMethod); | $client->addResponse($paymentMethod); | ||||
$client->addResponse($setupIntent); | $client->addResponse($setupIntent); | ||||
$client->addResponse($paymentIntent); | $client->addResponse($paymentIntent); | ||||
$client->addResponse($setupIntent); | $client->addResponse($setupIntent); | ||||
$client->addResponse($paymentMethod); | $client->addResponse($paymentMethod); | ||||
// Expect a recurring payment as we have a valid mandate at this point | // Expect a recurring payment as we have a valid mandate at this point | ||||
$result = PaymentsController::topUpWallet($wallet); | $result = PaymentsController::topUpWallet($wallet); | ||||
$this->assertTrue($result); | $this->assertTrue($result); | ||||
// Check that the payments table contains a new record with proper amount | // Check that the payments table contains a new record with proper amount | ||||
// There should be two records, one for the first payment and another for | // There should be two records, one for the first payment and another for | ||||
// the recurring payment | // the recurring payment | ||||
$this->assertCount(1, $wallet->payments()->get()); | $this->assertCount(1, $wallet->payments()->get()); | ||||
$payment = $wallet->payments()->first(); | $payment = $wallet->payments()->first(); | ||||
$this->assertSame(2010, $payment->amount); | $this->assertSame(2010, $payment->amount); | ||||
$this->assertSame(\config('app.name') . " Recurring Payment", $payment->description); | $this->assertSame($user->tenant->title . " Recurring Payment", $payment->description); | ||||
$this->assertSame("pi_XX", $payment->id); | $this->assertSame("pi_XX", $payment->id); | ||||
// Expect no payment if the mandate is disabled | // Expect no payment if the mandate is disabled | ||||
$wallet->setSetting('mandate_disabled', 1); | $wallet->setSetting('mandate_disabled', 1); | ||||
$result = PaymentsController::topUpWallet($wallet); | $result = PaymentsController::topUpWallet($wallet); | ||||
$this->assertFalse($result); | $this->assertFalse($result); | ||||
$this->assertCount(1, $wallet->payments()->get()); | $this->assertCount(1, $wallet->payments()->get()); | ||||
▲ Show 20 Lines • Show All 173 Lines • Show Last 20 Lines |