Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/Controller/PaymentsMollieEuroTest.php
- This file was copied from src/tests/Feature/Controller/PaymentsMollieTest.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\Providers\PaymentProvider; | ||||
use App\Transaction; | use App\Transaction; | ||||
use App\Wallet; | use App\Wallet; | ||||
use App\WalletSetting; | use App\WalletSetting; | ||||
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; | ||||
use Tests\MollieMocksTrait; | use Tests\MollieMocksTrait; | ||||
class PaymentsMollieTest extends TestCase | class PaymentsMollieEuroTest extends TestCase | ||||
{ | { | ||||
use MollieMocksTrait; | use MollieMocksTrait; | ||||
use BrowserAddonTrait; | use BrowserAddonTrait; | ||||
/** | /** | ||||
* {@inheritDoc} | * {@inheritDoc} | ||||
*/ | */ | ||||
public function setUp(): void | public function setUp(): void | ||||
{ | { | ||||
parent::setUp(); | parent::setUp(); | ||||
// All tests in this file use Mollie | // All tests in this file use Mollie | ||||
\config(['services.payment_provider' => 'mollie']); | \config(['services.payment_provider' => 'mollie']); | ||||
$john = $this->getTestUser('john@kolab.org'); | |||||
$wallet = $john->wallets()->first(); | |||||
Payment::where('wallet_id', $wallet->id)->delete(); | |||||
Wallet::where('id', $wallet->id)->update(['balance' => 0]); | |||||
WalletSetting::where('wallet_id', $wallet->id)->delete(); | |||||
$types = [ | |||||
Transaction::WALLET_CREDIT, | |||||
Transaction::WALLET_REFUND, | |||||
Transaction::WALLET_CHARGEBACK, | |||||
]; | |||||
Transaction::where('object_id', $wallet->id)->whereIn('type', $types)->delete(); | |||||
} | } | ||||
/** | /** | ||||
* {@inheritDoc} | * {@inheritDoc} | ||||
*/ | */ | ||||
public function tearDown(): void | public function tearDown(): void | ||||
{ | { | ||||
$john = $this->getTestUser('john@kolab.org'); | $this->deleteTestUser('euro@' . \config('app.domain')); | ||||
$wallet = $john->wallets()->first(); | |||||
Payment::where('wallet_id', $wallet->id)->delete(); | |||||
Wallet::where('id', $wallet->id)->update(['balance' => 0]); | |||||
WalletSetting::where('wallet_id', $wallet->id)->delete(); | |||||
$types = [ | |||||
Transaction::WALLET_CREDIT, | |||||
Transaction::WALLET_REFUND, | |||||
Transaction::WALLET_CHARGEBACK, | |||||
]; | |||||
Transaction::where('object_id', $wallet->id)->whereIn('type', $types)->delete(); | |||||
parent::tearDown(); | parent::tearDown(); | ||||
} | } | ||||
/** | /** | ||||
* Test creating/updating/deleting an outo-payment mandate | * Test creating/updating/deleting an outo-payment mandate | ||||
* | * | ||||
* @group mollie | * @group mollie | ||||
*/ | */ | ||||
public function testMandates(): void | public function testMandates(): void | ||||
{ | { | ||||
// Unauth access not allowed | // Unauth access not allowed | ||||
$response = $this->get("api/v4/payments/mandate"); | $response = $this->get("api/v4/payments/mandate"); | ||||
$response->assertStatus(401); | $response->assertStatus(401); | ||||
$response = $this->post("api/v4/payments/mandate", []); | $response = $this->post("api/v4/payments/mandate", []); | ||||
$response->assertStatus(401); | $response->assertStatus(401); | ||||
$response = $this->put("api/v4/payments/mandate", []); | $response = $this->put("api/v4/payments/mandate", []); | ||||
$response->assertStatus(401); | $response->assertStatus(401); | ||||
$response = $this->delete("api/v4/payments/mandate"); | $response = $this->delete("api/v4/payments/mandate"); | ||||
$response->assertStatus(401); | $response->assertStatus(401); | ||||
$user = $this->getTestUser('john@kolab.org'); | $user = $this->getTestUser('euro@' . \config('app.domain')); | ||||
$wallet = $user->wallets()->first(); | $wallet = $user->wallets()->first(); | ||||
$wallet->currency = 'EUR'; | |||||
$wallet->save(); | |||||
// Test creating a mandate (invalid input) | // Test creating a mandate (invalid input) | ||||
$post = []; | $post = []; | ||||
$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(); | ||||
Show All 17 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']); | ||||
$this->assertMatchesRegularExpression("/[0-9.,]+ €\.$/", $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); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
Show All 11 Lines | public function testMandates(): void | ||||
$this->assertSame('success', $json['status']); | $this->assertSame('success', $json['status']); | ||||
$this->assertMatchesRegularExpression('|^https://www.mollie.com|', $json['redirectUrl']); | $this->assertMatchesRegularExpression('|^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(\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 60 Lines • ▼ Show 20 Lines | public function testMandates(): void | ||||
$response = $this->actingAs($user)->put("api/v4/payments/mandate", $post); | $response = $this->actingAs($user)->put("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("Minimum amount for a single payment is {$min}.", $json['errors']['amount']); | $this->assertSame("Minimum amount for a single payment is {$min}.", $json['errors']['amount']); | ||||
$this->assertMatchesRegularExpression("/[0-9.,]+ €\.$/", $json['errors']['amount']); | |||||
// Test updating a mandate (valid input) | // Test updating a mandate (valid input) | ||||
$responseStack->append(new Response(200, [], json_encode($mollie_response))); | $responseStack->append(new Response(200, [], json_encode($mollie_response))); | ||||
$post = ['amount' => 30.10, 'balance' => 10]; | $post = ['amount' => 30.10, 'balance' => 10]; | ||||
$response = $this->actingAs($user)->put("api/v4/payments/mandate", $post); | $response = $this->actingAs($user)->put("api/v4/payments/mandate", $post); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
▲ Show 20 Lines • Show All 103 Lines • ▼ Show 20 Lines | class PaymentsMollieEuroTest 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('euro@' . \config('app.domain')); | ||||
$user = $this->getTestUser('john@kolab.org'); | $wallet = $user->wallets()->first(); | ||||
$wallet->currency = 'EUR'; | |||||
$wallet->save(); | |||||
// Invalid amount | |||||
$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']); | ||||
$this->assertMatchesRegularExpression("/[0-9.,]+ €\.$/", $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' => 'EUR', '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('|^https://www.mollie.com|', $json['redirectUrl']); | $this->assertMatchesRegularExpression('|^https://www.mollie.com|', $json['redirectUrl']); | ||||
$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(1234, $payment->currency_amount); | $this->assertSame(1234, $payment->currency_amount); | ||||
$this->assertSame('CHF', $payment->currency); | $this->assertSame('EUR', $payment->currency); | ||||
$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 | ||||
// Note: Webhook end-point does not require authentication | // Note: Webhook end-point does not require authentication | ||||
$mollie_response = [ | $mollie_response = [ | ||||
"resource" => "payment", | "resource" => "payment", | ||||
▲ Show 20 Lines • Show All 78 Lines • ▼ Show 20 Lines | public function testStoreAndWebhook(): void | ||||
$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 creating a payment and receiving a status via webhook using a foreign currency | |||||
* | |||||
* @group mollie | |||||
*/ | |||||
public function testStoreAndWebhookForeignCurrency(): void | |||||
{ | |||||
Bus::fake(); | |||||
$user = $this->getTestUser('john@kolab.org'); | |||||
$wallet = $user->wallets()->first(); | |||||
// Successful payment in EUR | |||||
$post = ['amount' => '12.34', 'currency' => 'EUR', 'methodId' => 'banktransfer']; | |||||
$response = $this->actingAs($user)->post("api/v4/payments", $post); | |||||
$response->assertStatus(200); | |||||
$payment = $wallet->payments() | |||||
->where('currency', 'EUR')->get()->last(); | |||||
$this->assertSame(1234, $payment->amount); | |||||
$this->assertSame(1117, $payment->currency_amount); | |||||
$this->assertSame('EUR', $payment->currency); | |||||
$this->assertEquals(0, $wallet->balance); | |||||
$mollie_response = [ | |||||
"resource" => "payment", | |||||
"id" => $payment->id, | |||||
"status" => "paid", | |||||
// Status is not enough, paidAt is used to distinguish the state | |||||
"paidAt" => date('c'), | |||||
"mode" => "test", | |||||
]; | |||||
$responseStack = $this->mockMollie(); | |||||
$responseStack->append(new Response(200, [], json_encode($mollie_response))); | |||||
$post = ['id' => $payment->id]; | |||||
$response = $this->post("api/webhooks/payment/mollie", $post); | |||||
$response->assertStatus(200); | |||||
$this->assertSame(PaymentProvider::STATUS_PAID, $payment->fresh()->status); | |||||
$this->assertEquals(1234, $wallet->fresh()->balance); | |||||
} | |||||
/** | |||||
* Test automatic payment charges | * Test automatic payment charges | ||||
* | * | ||||
* @group mollie | * @group mollie | ||||
*/ | */ | ||||
public function testTopUp(): void | public function testTopUp(): void | ||||
{ | { | ||||
Bus::fake(); | Bus::fake(); | ||||
$user = $this->getTestUser('john@kolab.org'); | $user = $this->getTestUser('euro@' . \config('app.domain')); | ||||
$wallet = $user->wallets()->first(); | $wallet = $user->wallets()->first(); | ||||
$wallet->currency = 'EUR'; | |||||
$wallet->save(); | |||||
// Create a valid mandate first (balance=0, so there's no extra payment yet) | // Create a valid mandate first (balance=0, so there's no extra payment yet) | ||||
$this->createMandate($wallet, ['amount' => 20.10, 'balance' => 0]); | $this->createMandate($wallet, ['amount' => 20.10, 'balance' => 0]); | ||||
$wallet->setSetting('mandate_balance', 10); | $wallet->setSetting('mandate_balance', 10); | ||||
// 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 | ||||
// and the balance is below the threshold | // and the balance is below the threshold | ||||
▲ Show 20 Lines • Show All 160 Lines • ▼ Show 20 Lines | class PaymentsMollieEuroTest extends TestCase | ||||
* Test refund/chargeback handling by the webhook | * Test refund/chargeback handling by the webhook | ||||
* | * | ||||
* @group mollie | * @group mollie | ||||
*/ | */ | ||||
public function testRefundAndChargeback(): void | public function testRefundAndChargeback(): void | ||||
{ | { | ||||
Bus::fake(); | Bus::fake(); | ||||
$user = $this->getTestUser('john@kolab.org'); | $user = $this->getTestUser('euro@' . \config('app.domain')); | ||||
$wallet = $user->wallets()->first(); | $wallet = $user->wallets()->first(); | ||||
$wallet->currency = 'EUR'; | |||||
$wallet->save(); | |||||
$wallet->transactions()->delete(); | $wallet->transactions()->delete(); | ||||
$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_amount' => 123, | ||||
'currency' => 'CHF', | 'currency' => 'EUR', | ||||
'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 All 19 Lines | public function testRefundAndChargeback(): void | ||||
"refunds" => [ | "refunds" => [ | ||||
[ | [ | ||||
"resource" => "refund", | "resource" => "refund", | ||||
"id" => "re_123456", | "id" => "re_123456", | ||||
"status" => \Mollie\Api\Types\RefundStatus::STATUS_REFUNDED, | "status" => \Mollie\Api\Types\RefundStatus::STATUS_REFUNDED, | ||||
"paymentId" => $payment->id, | "paymentId" => $payment->id, | ||||
"description" => "refund desc", | "description" => "refund desc", | ||||
"amount" => [ | "amount" => [ | ||||
"currency" => "CHF", | "currency" => "EUR", | ||||
"value" => "1.01", | "value" => "1.01", | ||||
], | ], | ||||
] | ] | ||||
] | ] | ||||
] | ] | ||||
]; | ]; | ||||
// We'll trigger the webhook with payment id and use mocking for | // We'll trigger the webhook with payment id and use mocking for | ||||
▲ Show 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | public function testRefundAndChargeback(): void | ||||
"_links" => [], | "_links" => [], | ||||
"_embedded" => [ | "_embedded" => [ | ||||
"chargebacks" => [ | "chargebacks" => [ | ||||
[ | [ | ||||
"resource" => "chargeback", | "resource" => "chargeback", | ||||
"id" => "chb_123456", | "id" => "chb_123456", | ||||
"paymentId" => $payment->id, | "paymentId" => $payment->id, | ||||
"amount" => [ | "amount" => [ | ||||
"currency" => "CHF", | "currency" => "EUR", | ||||
"value" => "0.15", | "value" => "0.15", | ||||
], | ], | ||||
] | ] | ||||
] | ] | ||||
] | ] | ||||
]; | ]; | ||||
// We'll trigger the webhook with payment id and use mocking for | // We'll trigger the webhook with payment id and use mocking for | ||||
Show All 27 Lines | public function testRefundAndChargeback(): void | ||||
$this->assertSame('', $payments[0]->description); | $this->assertSame('', $payments[0]->description); | ||||
Bus::assertNotDispatched(\App\Jobs\PaymentEmail::class); | Bus::assertNotDispatched(\App\Jobs\PaymentEmail::class); | ||||
$this->unmockMollie(); | $this->unmockMollie(); | ||||
} | } | ||||
/** | /** | ||||
* Test refund/chargeback handling by the webhook in a foreign currency | |||||
* | |||||
* @group mollie | |||||
*/ | |||||
public function testRefundAndChargebackForeignCurrency(): void | |||||
{ | |||||
Bus::fake(); | |||||
$user = $this->getTestUser('john@kolab.org'); | |||||
$wallet = $user->wallets()->first(); | |||||
$wallet->transactions()->delete(); | |||||
$mollie = PaymentProvider::factory('mollie'); | |||||
// Create a paid payment | |||||
$payment = Payment::create([ | |||||
'id' => 'tr_123456', | |||||
'status' => PaymentProvider::STATUS_PAID, | |||||
'amount' => 1234, | |||||
'currency_amount' => 1117, | |||||
'currency' => 'EUR', | |||||
'type' => PaymentProvider::TYPE_ONEOFF, | |||||
'wallet_id' => $wallet->id, | |||||
'provider' => 'mollie', | |||||
'description' => 'test', | |||||
]); | |||||
// Test handling a refund by the webhook | |||||
$mollie_response1 = [ | |||||
"resource" => "payment", | |||||
"id" => $payment->id, | |||||
"status" => "paid", | |||||
// Status is not enough, paidAt is used to distinguish the state | |||||
"paidAt" => date('c'), | |||||
"mode" => "test", | |||||
"_links" => [ | |||||
"refunds" => [ | |||||
"href" => "https://api.mollie.com/v2/payments/{$payment->id}/refunds", | |||||
"type" => "application/hal+json" | |||||
] | |||||
] | |||||
]; | |||||
$mollie_response2 = [ | |||||
"count" => 1, | |||||
"_links" => [], | |||||
"_embedded" => [ | |||||
"refunds" => [ | |||||
[ | |||||
"resource" => "refund", | |||||
"id" => "re_123456", | |||||
"status" => \Mollie\Api\Types\RefundStatus::STATUS_REFUNDED, | |||||
"paymentId" => $payment->id, | |||||
"description" => "refund desc", | |||||
"amount" => [ | |||||
"currency" => "EUR", | |||||
"value" => "1.01", | |||||
], | |||||
] | |||||
] | |||||
] | |||||
]; | |||||
// We'll trigger the webhook with payment id and use mocking for | |||||
// requests to the Mollie payments API. | |||||
$responseStack = $this->mockMollie(); | |||||
$responseStack->append(new Response(200, [], json_encode($mollie_response1))); | |||||
$responseStack->append(new Response(200, [], json_encode($mollie_response2))); | |||||
$post = ['id' => $payment->id]; | |||||
$response = $this->post("api/webhooks/payment/mollie", $post); | |||||
$response->assertStatus(200); | |||||
$wallet->refresh(); | |||||
$this->assertTrue($wallet->balance <= -108); | |||||
$this->assertTrue($wallet->balance >= -114); | |||||
$payments = $wallet->payments()->where('id', 're_123456')->get(); | |||||
$this->assertCount(1, $payments); | |||||
$this->assertTrue($payments[0]->amount <= -108); | |||||
$this->assertTrue($payments[0]->amount >= -114); | |||||
$this->assertSame(-101, $payments[0]->currency_amount); | |||||
$this->assertSame('EUR', $payments[0]->currency); | |||||
$this->unmockMollie(); | |||||
} | |||||
/** | |||||
* Create Mollie's auto-payment mandate using our API and Chrome browser | * Create Mollie's auto-payment mandate using our API and Chrome browser | ||||
*/ | */ | ||||
protected function createMandate(Wallet $wallet, array $params) | protected function createMandate(Wallet $wallet, array $params) | ||||
{ | { | ||||
// Use the API to create a first payment with a mandate | // Use the API to create a first payment with a mandate | ||||
$response = $this->actingAs($wallet->owner)->post("api/v4/payments/mandate", $params); | $response = $this->actingAs($wallet->owner)->post("api/v4/payments/mandate", $params); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
// There's no easy way to confirm a created mandate. | // There's no easy way to confirm a created mandate. | ||||
// The only way seems to be to fire up Chrome on checkout page | // The only way seems to be to fire up Chrome on checkout page | ||||
// 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 | * Test listing a pending payment | ||||
* | * | ||||
* @group mollie | * @group mollie | ||||
*/ | */ | ||||
public function testListingPayments(): void | public function testListingPayments(): void | ||||
{ | { | ||||
Bus::fake(); | Bus::fake(); | ||||
$user = $this->getTestUser('john@kolab.org'); | $user = $this->getTestUser('euro@' . \config('app.domain')); | ||||
$wallet = $user->wallets()->first(); | |||||
$wallet->currency = 'EUR'; | |||||
$wallet->save(); | |||||
//Empty response | //Empty response | ||||
$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->assertSame(1, $json['page']); | $this->assertSame(1, $json['page']); | ||||
$this->assertSame(false, $json['hasMore']); | $this->assertSame(false, $json['hasMore']); | ||||
$this->assertCount(0, $json['list']); | $this->assertCount(0, $json['list']); | ||||
$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(false, $json['hasPending']); | $this->assertSame(false, $json['hasPending']); | ||||
$wallet = $user->wallets()->first(); | |||||
// Successful payment | // Successful payment | ||||
$post = ['amount' => '12.34', 'currency' => 'CHF', 'methodId' => 'creditcard']; | $post = ['amount' => '12.34', 'currency' => 'EUR', '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); | ||||
//A response | //A response | ||||
$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(PaymentProvider::STATUS_OPEN, $json['list'][0]['status']); | ||||
$this->assertSame('EUR', $json['list'][0]['currency']); | |||||
$this->assertSame(PaymentProvider::TYPE_ONEOFF, $json['list'][0]['type']); | |||||
$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(); | ||||
Show All 19 Lines | class PaymentsMollieEuroTest extends TestCase | ||||
* Test listing payment methods | * Test listing payment methods | ||||
* | * | ||||
* @group mollie | * @group mollie | ||||
*/ | */ | ||||
public function testListingPaymentMethods(): void | public function testListingPaymentMethods(): void | ||||
{ | { | ||||
Bus::fake(); | Bus::fake(); | ||||
$user = $this->getTestUser('john@kolab.org'); | $user = $this->getTestUser('euro@' . \config('app.domain')); | ||||
$wallet = $user->wallets()->first(); | |||||
$wallet->currency = 'EUR'; | |||||
$wallet->save(); | |||||
$response = $this->actingAs($user)->get('api/v4/payments/methods?type=' . PaymentProvider::TYPE_ONEOFF); | $response = $this->actingAs($user)->get('api/v4/payments/methods?type=' . PaymentProvider::TYPE_ONEOFF); | ||||
$response->assertStatus(200); | $response->assertStatus(200); | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertCount(3, $json); | $this->assertCount(3, $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('banktransfer', $json[2]['id']); | $this->assertSame('banktransfer', $json[2]['id']); | ||||
$this->assertSame('EUR', $json[0]['currency']); | |||||
$this->assertSame('EUR', $json[1]['currency']); | |||||
$this->assertSame('EUR', $json[2]['currency']); | |||||
$this->assertSame(1, $json[2]['exchangeRate']); | |||||
$this->assertSame(1, $json[2]['exchangeRate']); | |||||
$this->assertSame(1, $json[2]['exchangeRate']); | |||||
$response = $this->actingAs($user)->get('api/v4/payments/methods?type=' . PaymentProvider::TYPE_RECURRING); | $response = $this->actingAs($user)->get('api/v4/payments/methods?type=' . PaymentProvider::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']); | ||||
$this->assertSame('EUR', $json[0]['currency']); | |||||
} | } | ||||
} | } |