Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/Controller/PaymentsStripeTest.php
Show First 20 Lines • Show All 129 Lines • ▼ Show 20 Lines | public function testMandates(): void | ||||
$this->assertSame('success', $json['status']); | $this->assertSame('success', $json['status']); | ||||
$this->assertRegExp('|^cs_test_|', $json['id']); | $this->assertRegExp('|^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("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 154 Lines • ▼ Show 20 Lines | public function testStoreAndWebhook(): void | ||||
$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('|^cs_test_|', $json['id']); | $this->assertRegExp('|^cs_test_|', $json['id']); | ||||
▲ Show 20 Lines • Show All 383 Lines • ▼ Show 20 Lines | protected function webhookRequest($post) | ||||
$ts = time(); | $ts = time(); | ||||
$payload = "$ts." . json_encode($post); | $payload = "$ts." . json_encode($post); | ||||
$sig = sprintf('t=%d,v1=%s', $ts, \hash_hmac('sha256', $payload, $secret)); | $sig = sprintf('t=%d,v1=%s', $ts, \hash_hmac('sha256', $payload, $secret)); | ||||
return $this->withHeaders(['Stripe-Signature' => $sig]) | return $this->withHeaders(['Stripe-Signature' => $sig]) | ||||
->json('POST', "api/webhooks/payment/stripe", $post); | ->json('POST', "api/webhooks/payment/stripe", $post); | ||||
} | } | ||||
/** | |||||
* Test listing payment methods | |||||
* | |||||
* @group stripe | |||||
*/ | |||||
public function testListingPaymentMethods(): void | |||||
{ | |||||
Bus::fake(); | |||||
$user = $this->getTestUser('john@kolab.org'); | |||||
$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']); | |||||
$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']); | |||||
} | |||||
} | } |