Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/Controller/PaymentsCoinbaseTest.php
<?php | <?php | ||||
namespace Tests\Feature\Controller; | namespace Tests\Feature\Controller; | ||||
use App\Http\Controllers\API\V4\PaymentsController; | use App\Http\Controllers\API\V4\PaymentsController; | ||||
use App\Payment; | use App\Payment; | ||||
use App\Providers\PaymentProvider; | |||||
use App\Transaction; | use App\Transaction; | ||||
use App\Wallet; | use App\Wallet; | ||||
use App\WalletSetting; | use App\WalletSetting; | ||||
use App\Utils; | use App\Utils; | ||||
use GuzzleHttp\Psr7\Response; | use GuzzleHttp\Psr7\Response; | ||||
use Illuminate\Support\Facades\Bus; | use Illuminate\Support\Facades\Bus; | ||||
use Tests\TestCase; | use Tests\TestCase; | ||||
use Tests\BrowserAddonTrait; | use Tests\BrowserAddonTrait; | ||||
▲ Show 20 Lines • Show All 68 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' => 'bitcoin']; | $post = ['amount' => '12.34', 'currency' => 'FOO', 'methodId' => 'bitcoin']; | ||||
$response = $this->actingAs($user)->post("api/v4/payments", $post); | $response = $this->actingAs($user)->post("api/v4/payments", $post); | ||||
$response->assertStatus(500); | $response->assertStatus(500); | ||||
// Rate limit exceeded | // Rate limit exceeded | ||||
▲ Show 20 Lines • Show All 72 Lines • ▼ Show 20 Lines | public function testStoreAndWebhook(): void | ||||
'code' => $payment->id, | 'code' => $payment->id, | ||||
], | ], | ||||
'type' => 'charge:resolved', | 'type' => 'charge:resolved', | ||||
], | ], | ||||
]; | ]; | ||||
$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 Coinbase", | "Payment transaction {$payment->id} using Coinbase", | ||||
Show All 13 Lines | public function testStoreAndWebhook(): void | ||||
'code' => $payment->id, | 'code' => $payment->id, | ||||
], | ], | ||||
'type' => 'charge:created', | 'type' => 'charge:created', | ||||
], | ], | ||||
]; | ]; | ||||
$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); | ||||
$post = [ | $post = [ | ||||
'event' => | 'event' => | ||||
[ | [ | ||||
'api_version' => '2018-03-22', | 'api_version' => '2018-03-22', | ||||
'data' => [ | 'data' => [ | ||||
'code' => $payment->id, | 'code' => $payment->id, | ||||
], | ], | ||||
'type' => 'charge:resolved', | 'type' => 'charge:resolved', | ||||
], | ], | ||||
]; | ]; | ||||
$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 | // Test for payment failure | ||||
Bus::fake(); | Bus::fake(); | ||||
$payment->refresh(); | $payment->refresh(); | ||||
$payment->status = PaymentProvider::STATUS_OPEN; | $payment->status = Payment::STATUS_OPEN; | ||||
$payment->save(); | $payment->save(); | ||||
$post = [ | $post = [ | ||||
'event' => | 'event' => | ||||
[ | [ | ||||
'api_version' => '2018-03-22', | 'api_version' => '2018-03-22', | ||||
'data' => [ | 'data' => [ | ||||
'code' => $payment->id, | 'code' => $payment->id, | ||||
▲ Show 20 Lines • Show All 64 Lines • ▼ Show 20 Lines | public function testStoreAndWebhookForeignCurrency(): void | ||||
], | ], | ||||
'type' => 'charge:resolved', | 'type' => 'charge:resolved', | ||||
], | ], | ||||
]; | ]; | ||||
$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); | ||||
} | } | ||||
/** | /** | ||||
* Generate Coinbase-Signature header for a webhook payload | * Generate Coinbase-Signature header for a webhook payload | ||||
*/ | */ | ||||
protected function webhookRequest($post) | protected function webhookRequest($post) | ||||
▲ Show 20 Lines • Show All 59 Lines • ▼ Show 20 Lines | public function testListingPayments(): void | ||||
$response = $this->actingAs($user)->get("api/v4/payments/pending"); | $response = $this->actingAs($user)->get("api/v4/payments/pending"); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertSame('success', $json['status']); | $this->assertSame('success', $json['status']); | ||||
$this->assertSame(1, $json['count']); | $this->assertSame(1, $json['count']); | ||||
$this->assertSame(1, $json['page']); | $this->assertSame(1, $json['page']); | ||||
$this->assertSame(false, $json['hasMore']); | $this->assertSame(false, $json['hasMore']); | ||||
$this->assertCount(1, $json['list']); | $this->assertCount(1, $json['list']); | ||||
$this->assertSame(PaymentProvider::STATUS_OPEN, $json['list'][0]['status']); | $this->assertSame(Payment::STATUS_OPEN, $json['list'][0]['status']); | ||||
$this->assertSame('CHF', $json['list'][0]['currency']); | $this->assertSame('CHF', $json['list'][0]['currency']); | ||||
$this->assertSame(PaymentProvider::TYPE_ONEOFF, $json['list'][0]['type']); | $this->assertSame(Payment::TYPE_ONEOFF, $json['list'][0]['type']); | ||||
$this->assertSame(1234, $json['list'][0]['amount']); | $this->assertSame(1234, $json['list'][0]['amount']); | ||||
$response = $this->actingAs($user)->get("api/v4/payments/has-pending"); | $response = $this->actingAs($user)->get("api/v4/payments/has-pending"); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertSame(true, $json['hasPending']); | $this->assertSame(true, $json['hasPending']); | ||||
// Set the payment to paid | // Set the payment to paid | ||||
$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]; | ||||
$payment->status = PaymentProvider::STATUS_PAID; | $payment->status = Payment::STATUS_PAID; | ||||
$payment->save(); | $payment->save(); | ||||
// They payment should be gone from the pending list now | // They payment should be gone from the pending list now | ||||
$response = $this->actingAs($user)->get("api/v4/payments/pending"); | $response = $this->actingAs($user)->get("api/v4/payments/pending"); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertSame('success', $json['status']); | $this->assertSame('success', $json['status']); | ||||
$this->assertSame(0, $json['count']); | $this->assertSame(0, $json['count']); | ||||
$this->assertCount(0, $json['list']); | $this->assertCount(0, $json['list']); | ||||
Show All 9 Lines | class PaymentsCoinbaseTest extends TestCase | ||||
* @group coinbase | * @group coinbase | ||||
*/ | */ | ||||
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(); | ||||
$this->assertCount(4, $json); | $this->assertCount(4, $json); | ||||
$this->assertSame('bitcoin', $json[3]['id']); | $this->assertSame('bitcoin', $json[3]['id']); | ||||
$this->assertSame('BTC', $json[3]['currency']); | $this->assertSame('BTC', $json[3]['currency']); | ||||
$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); | ||||
} | } | ||||
} | } |