Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/Controller/PaymentsStripeTest.php
Show First 20 Lines • Show All 108 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 = $wallet->money(PaymentProvider::MIN_AMOUNT); | $min = $wallet->money(Payment::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' => Payment::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); | ||||
$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']); | ||||
$this->assertSame("The specified amount does not cover the balance on the account.", $json['errors']['amount']); | $this->assertSame("The specified amount does not cover the balance on the account.", $json['errors']['amount']); | ||||
// Test creating a mandate (valid input) | // Test creating a mandate (valid input) | ||||
$post = ['amount' => 20.10, 'balance' => 0, 'methodId' => PaymentProvider::METHOD_CREDITCARD]; | $post = ['amount' => 20.10, 'balance' => 0, 'methodId' => PaymentProvider::METHOD_CREDITCARD]; | ||||
$response = $this->actingAs($user)->post("api/v4/payments/mandate", $post); | $response = $this->actingAs($user)->post("api/v4/payments/mandate", $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']); | ||||
// 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($user->tenant->title . " Auto-Payment Setup", $payment->description); | $this->assertSame($user->tenant->title . " Auto-Payment Setup", $payment->description); | ||||
$this->assertSame(PaymentProvider::TYPE_MANDATE, $payment->type); | $this->assertSame(Payment::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(); | ||||
$this->assertEquals(20.10, $json['amount']); | $this->assertEquals(20.10, $json['amount']); | ||||
▲ Show 20 Lines • Show All 151 Lines • ▼ Show 20 Lines | public function testStoreAndWebhook(): void | ||||
$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 = $wallet->money(PaymentProvider::MIN_AMOUNT); | $min = $wallet->money(Payment::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 | ||||
▲ Show 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | public function testStoreAndWebhook(): void | ||||
], | ], | ||||
'type' => "payment_intent.succeeded" | 'type' => "payment_intent.succeeded" | ||||
]; | ]; | ||||
// Test payment succeeded event | // Test payment succeeded event | ||||
$response = $this->webhookRequest($post); | $response = $this->webhookRequest($post); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$this->assertSame(PaymentProvider::STATUS_PAID, $payment->fresh()->status); | $this->assertSame(Payment::STATUS_PAID, $payment->fresh()->status); | ||||
$this->assertEquals(1234, $wallet->fresh()->balance); | $this->assertEquals(1234, $wallet->fresh()->balance); | ||||
$transaction = $wallet->transactions() | $transaction = $wallet->transactions() | ||||
->where('type', Transaction::WALLET_CREDIT)->get()->last(); | ->where('type', Transaction::WALLET_CREDIT)->get()->last(); | ||||
$this->assertSame(1234, $transaction->amount); | $this->assertSame(1234, $transaction->amount); | ||||
$this->assertSame( | $this->assertSame( | ||||
"Payment transaction {$payment->id} using Stripe", | "Payment transaction {$payment->id} using Stripe", | ||||
$transaction->description | $transaction->description | ||||
); | ); | ||||
// Assert that email notification job wasn't dispatched, | // Assert that email notification job wasn't dispatched, | ||||
// it is expected only for recurring payments | // it is expected only for recurring payments | ||||
Bus::assertDispatchedTimes(\App\Jobs\PaymentEmail::class, 0); | Bus::assertDispatchedTimes(\App\Jobs\PaymentEmail::class, 0); | ||||
// Test that balance didn't change if the same event is posted | // Test that balance didn't change if the same event is posted | ||||
$response = $this->webhookRequest($post); | $response = $this->webhookRequest($post); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$this->assertSame(PaymentProvider::STATUS_PAID, $payment->fresh()->status); | $this->assertSame(Payment::STATUS_PAID, $payment->fresh()->status); | ||||
$this->assertEquals(1234, $wallet->fresh()->balance); | $this->assertEquals(1234, $wallet->fresh()->balance); | ||||
// Test for payment failure ('failed' status) | // Test for payment failure ('failed' status) | ||||
$payment->refresh(); | $payment->refresh(); | ||||
$payment->status = PaymentProvider::STATUS_OPEN; | $payment->status = Payment::STATUS_OPEN; | ||||
$payment->save(); | $payment->save(); | ||||
$post['type'] = "payment_intent.payment_failed"; | $post['type'] = "payment_intent.payment_failed"; | ||||
$post['data']['object']['status'] = 'failed'; | $post['data']['object']['status'] = 'failed'; | ||||
$response = $this->webhookRequest($post); | $response = $this->webhookRequest($post); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$this->assertSame(PaymentProvider::STATUS_FAILED, $payment->fresh()->status); | $this->assertSame(Payment::STATUS_FAILED, $payment->fresh()->status); | ||||
$this->assertEquals(1234, $wallet->fresh()->balance); | $this->assertEquals(1234, $wallet->fresh()->balance); | ||||
// Assert that email notification job wasn't dispatched, | // Assert that email notification job wasn't dispatched, | ||||
// it is expected only for recurring payments | // it is expected only for recurring payments | ||||
Bus::assertDispatchedTimes(\App\Jobs\PaymentEmail::class, 0); | Bus::assertDispatchedTimes(\App\Jobs\PaymentEmail::class, 0); | ||||
// Test for payment failure ('canceled' status) | // Test for payment failure ('canceled' status) | ||||
$payment->refresh(); | $payment->refresh(); | ||||
$payment->status = PaymentProvider::STATUS_OPEN; | $payment->status = Payment::STATUS_OPEN; | ||||
$payment->save(); | $payment->save(); | ||||
$post['type'] = "payment_intent.canceled"; | $post['type'] = "payment_intent.canceled"; | ||||
$post['data']['object']['status'] = 'canceled'; | $post['data']['object']['status'] = 'canceled'; | ||||
$response = $this->webhookRequest($post); | $response = $this->webhookRequest($post); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$this->assertSame(PaymentProvider::STATUS_CANCELED, $payment->fresh()->status); | $this->assertSame(Payment::STATUS_CANCELED, $payment->fresh()->status); | ||||
$this->assertEquals(1234, $wallet->fresh()->balance); | $this->assertEquals(1234, $wallet->fresh()->balance); | ||||
// Assert that email notification job wasn't dispatched, | // Assert that email notification job wasn't dispatched, | ||||
// it is expected only for recurring payments | // it is expected only for recurring payments | ||||
Bus::assertDispatchedTimes(\App\Jobs\PaymentEmail::class, 0); | Bus::assertDispatchedTimes(\App\Jobs\PaymentEmail::class, 0); | ||||
} | } | ||||
/** | /** | ||||
Show All 9 Lines | public function testCreateMandateAndWebhook(): void | ||||
// Test creating a mandate (valid input) | // Test creating a mandate (valid input) | ||||
$post = ['amount' => 20.10, 'balance' => 0]; | $post = ['amount' => 20.10, 'balance' => 0]; | ||||
$response = $this->actingAs($user)->post("api/v4/payments/mandate", $post); | $response = $this->actingAs($user)->post("api/v4/payments/mandate", $post); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$payment = $wallet->payments()->first(); | $payment = $wallet->payments()->first(); | ||||
$this->assertSame(PaymentProvider::STATUS_OPEN, $payment->status); | $this->assertSame(Payment::STATUS_OPEN, $payment->status); | ||||
$this->assertSame(PaymentProvider::TYPE_MANDATE, $payment->type); | $this->assertSame(Payment::TYPE_MANDATE, $payment->type); | ||||
$this->assertSame(0, $payment->amount); | $this->assertSame(0, $payment->amount); | ||||
$post = [ | $post = [ | ||||
'id' => "evt_1GlZ814fj3SIEU8wtxMZ4Nsa", | 'id' => "evt_1GlZ814fj3SIEU8wtxMZ4Nsa", | ||||
'object' => "event", | 'object' => "event", | ||||
'api_version' => "2020-03-02", | 'api_version' => "2020-03-02", | ||||
'created' => 1590147209, | 'created' => 1590147209, | ||||
'data' => [ | 'data' => [ | ||||
Show All 14 Lines | public function testCreateMandateAndWebhook(): void | ||||
Bus::fake(); | Bus::fake(); | ||||
// Test payment succeeded event | // Test payment succeeded event | ||||
$response = $this->webhookRequest($post); | $response = $this->webhookRequest($post); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$payment->refresh(); | $payment->refresh(); | ||||
$this->assertSame(PaymentProvider::STATUS_PAID, $payment->status); | $this->assertSame(Payment::STATUS_PAID, $payment->status); | ||||
$this->assertSame($payment->id, $wallet->fresh()->getSetting('stripe_mandate_id')); | $this->assertSame($payment->id, $wallet->fresh()->getSetting('stripe_mandate_id')); | ||||
// Expect a WalletCharge job if the balance is negative | // Expect a WalletCharge job if the balance is negative | ||||
Bus::assertDispatchedTimes(\App\Jobs\WalletCharge::class, 1); | Bus::assertDispatchedTimes(\App\Jobs\WalletCharge::class, 1); | ||||
Bus::assertDispatched(\App\Jobs\WalletCharge::class, function ($job) use ($wallet) { | Bus::assertDispatched(\App\Jobs\WalletCharge::class, function ($job) use ($wallet) { | ||||
$job_wallet = TestCase::getObjectProperty($job, 'wallet'); | $job_wallet = TestCase::getObjectProperty($job, 'wallet'); | ||||
return $job_wallet->id === $wallet->id; | return $job_wallet->id === $wallet->id; | ||||
}); | }); | ||||
▲ Show 20 Lines • Show All 139 Lines • ▼ Show 20 Lines | public function testTopUpAndWebhook(): void | ||||
], | ], | ||||
'type' => "payment_intent.succeeded" | 'type' => "payment_intent.succeeded" | ||||
]; | ]; | ||||
// Test payment succeeded event | // Test payment succeeded event | ||||
$response = $this->webhookRequest($post); | $response = $this->webhookRequest($post); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$this->assertSame(PaymentProvider::STATUS_PAID, $payment->fresh()->status); | $this->assertSame(Payment::STATUS_PAID, $payment->fresh()->status); | ||||
$this->assertEquals(2010, $wallet->fresh()->balance); | $this->assertEquals(2010, $wallet->fresh()->balance); | ||||
$transaction = $wallet->transactions() | $transaction = $wallet->transactions() | ||||
->where('type', Transaction::WALLET_CREDIT)->get()->last(); | ->where('type', Transaction::WALLET_CREDIT)->get()->last(); | ||||
$this->assertSame(2010, $transaction->amount); | $this->assertSame(2010, $transaction->amount); | ||||
$this->assertSame( | $this->assertSame( | ||||
"Auto-payment transaction {$payment->id} using Stripe", | "Auto-payment transaction {$payment->id} using Stripe", | ||||
$transaction->description | $transaction->description | ||||
); | ); | ||||
// Assert that email notification job has been dispatched | // Assert that email notification job has been dispatched | ||||
Bus::assertDispatchedTimes(\App\Jobs\PaymentEmail::class, 1); | Bus::assertDispatchedTimes(\App\Jobs\PaymentEmail::class, 1); | ||||
Bus::assertDispatched(\App\Jobs\PaymentEmail::class, function ($job) use ($payment) { | Bus::assertDispatched(\App\Jobs\PaymentEmail::class, function ($job) use ($payment) { | ||||
$job_payment = $this->getObjectProperty($job, 'payment'); | $job_payment = $this->getObjectProperty($job, 'payment'); | ||||
return $job_payment->id === $payment->id; | return $job_payment->id === $payment->id; | ||||
}); | }); | ||||
Bus::fake(); | Bus::fake(); | ||||
// Test for payment failure ('failed' status) | // Test for payment failure ('failed' status) | ||||
$payment->refresh(); | $payment->refresh(); | ||||
$payment->status = PaymentProvider::STATUS_OPEN; | $payment->status = Payment::STATUS_OPEN; | ||||
$payment->save(); | $payment->save(); | ||||
$wallet->setSetting('mandate_disabled', null); | $wallet->setSetting('mandate_disabled', null); | ||||
$post['type'] = "payment_intent.payment_failed"; | $post['type'] = "payment_intent.payment_failed"; | ||||
$post['data']['object']['status'] = 'failed'; | $post['data']['object']['status'] = 'failed'; | ||||
$response = $this->webhookRequest($post); | $response = $this->webhookRequest($post); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$wallet->refresh(); | $wallet->refresh(); | ||||
$this->assertSame(PaymentProvider::STATUS_FAILED, $payment->fresh()->status); | $this->assertSame(Payment::STATUS_FAILED, $payment->fresh()->status); | ||||
$this->assertEquals(2010, $wallet->balance); | $this->assertEquals(2010, $wallet->balance); | ||||
$this->assertTrue(!empty($wallet->getSetting('mandate_disabled'))); | $this->assertTrue(!empty($wallet->getSetting('mandate_disabled'))); | ||||
// Assert that email notification job has been dispatched | // Assert that email notification job has been dispatched | ||||
Bus::assertDispatchedTimes(\App\Jobs\PaymentEmail::class, 1); | Bus::assertDispatchedTimes(\App\Jobs\PaymentEmail::class, 1); | ||||
Bus::assertDispatched(\App\Jobs\PaymentEmail::class, function ($job) use ($payment) { | Bus::assertDispatched(\App\Jobs\PaymentEmail::class, function ($job) use ($payment) { | ||||
$job_payment = $this->getObjectProperty($job, 'payment'); | $job_payment = $this->getObjectProperty($job, 'payment'); | ||||
return $job_payment->id === $payment->id; | return $job_payment->id === $payment->id; | ||||
}); | }); | ||||
Bus::fake(); | Bus::fake(); | ||||
// Test for payment failure ('canceled' status) | // Test for payment failure ('canceled' status) | ||||
$payment->refresh(); | $payment->refresh(); | ||||
$payment->status = PaymentProvider::STATUS_OPEN; | $payment->status = Payment::STATUS_OPEN; | ||||
$payment->save(); | $payment->save(); | ||||
$post['type'] = "payment_intent.canceled"; | $post['type'] = "payment_intent.canceled"; | ||||
$post['data']['object']['status'] = 'canceled'; | $post['data']['object']['status'] = 'canceled'; | ||||
$response = $this->webhookRequest($post); | $response = $this->webhookRequest($post); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$this->assertSame(PaymentProvider::STATUS_CANCELED, $payment->fresh()->status); | $this->assertSame(Payment::STATUS_CANCELED, $payment->fresh()->status); | ||||
$this->assertEquals(2010, $wallet->fresh()->balance); | $this->assertEquals(2010, $wallet->fresh()->balance); | ||||
// Assert that email notification job wasn't dispatched, | // Assert that email notification job wasn't dispatched, | ||||
// it is expected only for recurring payments | // it is expected only for recurring payments | ||||
Bus::assertDispatchedTimes(\App\Jobs\PaymentEmail::class, 0); | Bus::assertDispatchedTimes(\App\Jobs\PaymentEmail::class, 0); | ||||
} | } | ||||
/** | /** | ||||
▲ Show 20 Lines • Show All 108 Lines • ▼ Show 20 Lines | class PaymentsStripeTest extends TestCase | ||||
* @group stripe | * @group stripe | ||||
*/ | */ | ||||
public function testListingPaymentMethods(): void | public function testListingPaymentMethods(): void | ||||
{ | { | ||||
Bus::fake(); | Bus::fake(); | ||||
$user = $this->getTestUser('john@kolab.org'); | $user = $this->getTestUser('john@kolab.org'); | ||||
$response = $this->actingAs($user)->get('api/v4/payments/methods?type=' . PaymentProvider::TYPE_ONEOFF); | $response = $this->actingAs($user)->get('api/v4/payments/methods?type=' . Payment::TYPE_ONEOFF); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$hasCoinbase = !empty(\config('services.coinbase.key')); | $hasCoinbase = !empty(\config('services.coinbase.key')); | ||||
$this->assertCount(2 + intval($hasCoinbase), $json); | $this->assertCount(2 + intval($hasCoinbase), $json); | ||||
$this->assertSame('creditcard', $json[0]['id']); | $this->assertSame('creditcard', $json[0]['id']); | ||||
$this->assertSame('paypal', $json[1]['id']); | $this->assertSame('paypal', $json[1]['id']); | ||||
$this->assertSame('bitcoin', $json[2]['id']); | $this->assertSame('bitcoin', $json[2]['id']); | ||||
$response = $this->actingAs($user)->get('api/v4/payments/methods?type=' . PaymentProvider::TYPE_RECURRING); | $response = $this->actingAs($user)->get('api/v4/payments/methods?type=' . Payment::TYPE_RECURRING); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertCount(1, $json); | $this->assertCount(1, $json); | ||||
$this->assertSame('creditcard', $json[0]['id']); | $this->assertSame('creditcard', $json[0]['id']); | ||||
} | } | ||||
/** | /** | ||||
Show All 14 Lines |