diff --git a/src/app/Providers/Payment/Mollie.php b/src/app/Providers/Payment/Mollie.php --- a/src/app/Providers/Payment/Mollie.php +++ b/src/app/Providers/Payment/Mollie.php @@ -6,6 +6,7 @@ use App\Utils; use App\Wallet; use Illuminate\Support\Facades\DB; +use Mollie\Api\Exceptions\ApiException; class Mollie extends \App\Providers\PaymentProvider { @@ -417,21 +418,21 @@ // Get the manadate reference we already have if ($customer_id && $mandate_id) { - $mandate = mollie()->mandates()->getForId($customer_id, $mandate_id); - if ($mandate) {// && ($mandate->isValid() || $mandate->isPending())) { - return $mandate; - } - } + try { + return mollie()->mandates()->getForId($customer_id, $mandate_id); + } catch (ApiException $e) { + // FIXME: What about 404? + if ($e->getCode() == 410) { + // The mandate is gone, remove the reference + $wallet->setSetting('mollie_mandate_id', null); + return null; + } - // Get all mandates from Mollie and find the active one - /* - foreach ($customer->mandates() as $mandate) { - if ($mandate->isValid() || $mandate->isPending()) { - $wallet->setSetting('mollie_mandate_id', $mandate->id); - return $mandate; + // TODO: Maybe we shouldn't always throw? It make sense in the job + // but for example when we're just fetching wallet info... + throw $e; } } - */ } /** diff --git a/src/tests/Feature/Controller/PaymentsMollieTest.php b/src/tests/Feature/Controller/PaymentsMollieTest.php --- a/src/tests/Feature/Controller/PaymentsMollieTest.php +++ b/src/tests/Feature/Controller/PaymentsMollieTest.php @@ -213,7 +213,7 @@ $this->assertSame($mandate_id, $json['id']); $this->assertFalse($json['isDisabled']); - $wallet = $user->wallets()->first(); + $wallet->refresh(); $this->assertEquals(30.10, $wallet->getSetting('mandate_amount')); $this->assertEquals(1, $wallet->getSetting('mandate_balance')); @@ -272,6 +272,34 @@ $mandate = mollie()->mandates()->getForId($customer_id, $mandate_id); $this->assertNull($wallet->fresh()->getSetting('mollie_mandate_id')); + + // Test Mollie's "410 Gone" response handling when fetching the mandate info + // It is expected to remove the mandate reference + $mollie_response = [ + 'status' => 410, + 'title' => "Gone", + 'detail' => "You are trying to access an object, which has previously been deleted", + '_links' => [ + 'documentation' => [ + 'href' => "https://docs.mollie.com/errors", + 'type' => "text/html" + ] + ] + ]; + + $responseStack = $this->mockMollie(); + $responseStack->append(new Response(410, [], json_encode($mollie_response))); + + $wallet->fresh()->setSetting('mollie_mandate_id', '123'); + + $response = $this->actingAs($user)->get("api/v4/payments/mandate"); + $response->assertStatus(200); + + $json = $response->json(); + + $this->assertFalse(array_key_exists('id', $json)); + $this->assertFalse(array_key_exists('method', $json)); + $this->assertNull($wallet->fresh()->getSetting('mollie_mandate_id')); } /**