Skip to content
This repository has been archived by the owner on Feb 2, 2022. It is now read-only.

User mandate id not updated on first payment #5

Closed
DeBelserArne opened this issue May 29, 2019 · 9 comments · Fixed by #7
Closed

User mandate id not updated on first payment #5

DeBelserArne opened this issue May 29, 2019 · 9 comments · Fixed by #7

Comments

@DeBelserArne
Copy link

Hello there,

I'm testing out with the __invoke method provided in the docs:

class CreateSubscriptionController extends Controller
{
    /**
     * @param string $plan
     * @return \Illuminate\Http\RedirectResponse
     */
    public function __invoke(string $plan)
    {
        $user = Auth::user();

        if (!$user->subscribed($plan)) {

            $name = 'main';

            $result = $user->newSubscription($name, $plan)->create();

            if (is_a($result, RedirectResponse::class)) {
                return $result; // Redirect to Mollie checkout
            }

            return redirect()->view('feed.index')->with('success', 'Welcome to the ' . $plan . ' plan');
        }

        return redirect()->view('feed.index')->with('error', 'You are already on the ' . $plan . ' plan');
    }
}

When creating a new subscription it doesn't ever seem to hit the following line:
return redirect()->view('feed.index')->with('success', 'Welcome to the ' . $plan . ' plan');

Somewhere in the following lines:

if (is_a($result, RedirectResponse::class)) {
                return $result; // Redirect to Mollie checkout
            }

It seems to standard go to the 'redirect_url' => config(app.ur), provided in the config file. Which doesn't let me add a custom success message.

On another note, after doing a succesful subscription payment $user->subscribed($plan) keeps returning false. It seems a mollie_customer_id is created, but not a mandate, which I think is necessary for subscriptions?

https://i.gyazo.com/3d99c5b76ac4cfb054a1e7ae3bedbd26.png

@sandervanhooft
Copy link
Collaborator

The controller essentially picks the strategy for handling the request, and returns it:

  • already on this plan: redirect to previous page with a message
  • not yet on this plan, but a valid mandate: create the subscription and redirect to previous page with a success message
  • not yet on this plan and no valid mandate: redirect away from this page, to Mollie’s checkout. After checkout the subscription will be started (this logic is in a separate controller). Mollie redirects the customer to the redirectUrl defined on the plan. You can define your own route and pass it in, triggering your own controller.

@sandervanhooft
Copy link
Collaborator

Regarding not updating the mandate_id field:

  • make sure that Mollie can reach your app (so no localhost etc) for calling the cashier webhooks
  • generally Mollie calls the webhook before redirecting the customer. I recommend treating the webhook call and redirect as independent, async requests.

@DeBelserArne
Copy link
Author

Hello,

I understand why it's using the standard redirect. Something seems to go wrong when it tries to create a valid mandate.

For testing purposes I used one of my digital ocean droplets to have a website which is available from the outside. I tried to create a subscription and I can see on Mollie's end that everything went well.

Here's what I can see in my Mollie test dashboard:
https://i.gyazo.com/565677074d68f6e1e9667d4b3b7834e8.png

As you can see the webhook was called succesfully. Anyhow there was no mandate created for the user.
https://i.gyazo.com/8823c628a5af8ea5cb04b5891a8fa671.png

I'm going to scratch everything and go through the documentation from A-Z again to see if I missed something. In the meanwhile, do you think I could be forgetting something or so?

Thanks in advance!

@sandervanhooft
Copy link
Collaborator

sandervanhooft commented Jun 2, 2019

Hi @aFluxx ,

Thanks for clearing that up.

Can you check whether the FirstPaymentPaid event is dispatched, by listening for it? Can you dump the payment contained in it? I'm wondering if the mandateId is available there.

@DeBelserArne
Copy link
Author

DeBelserArne commented Jun 2, 2019

Hey Sander,

I tried to do as you asked (Bear with me since I'm a beginning developer learning all this stuff whilest I'm doing it💃 ).

I've created a FirstPaymentPaidListener.php listener class which is listening in the Laravel\Cashier\Events namespace.

<?php

// namespace App\Listeners;
namespace Laravel\Cashier\Events;

use Illuminate\Support\Facades\Log;
use Laravel\Cashier\Events\FirstPaymentPaid;

class FirstPaymentPaidListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  object  $event
     * @return void
     */
    public function handle(FirstPaymentPaid $event)
    {
        Log::debug($event);
    }
}

I've registered the FirstPaymentPaid event with the listener in the EventServiceProvider class.

<?php

namespace App\Providers;

use Laravel\Cashier\Events\FirstPaymentPaid;
use Laravel\Cashier\Events\FirstPaymentPaidListener;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        FirstPaymentPaid::class => [
            FirstPaymentPaidListener::class,
        ],
    ];

    /**
     * Register any events for your application.
     *
     * @return void
     */
    public function boot()
    {
        parent::boot();

        //
    }
}

Ive then used a service you recommended to test webhooks locally. mollie/laravel-mollie#55 (comment)

https://i.gyazo.com/4a578f11f0714163b8b3745059fca935.png

So I know the webhook is working since I can see in the Mollie dashboard that I'm actually creating testusers: https://i.gyazo.com/44569dbeb45b786acca6fa74bded17d3.png

But I don't see any $event data appear in my laravel.log file. So I'm either forgetting something, doing something wrong, or the event is not called. But from my judgement I think it's one of the prior two :(

@sandervanhooft
Copy link
Collaborator

Hi @aFluxx ,

That webhook service is great, but it does not forward the call to the app itself. So the app webhook does not get triggered, and so the event does not get dispatched.

For this to work you will need to make a request to the app webhook manually. You can use something like curl (Mac command line) or PostMan (free tool or chrome plugin) to do that.

The request:

  • method POST
  • include a value of id=<you_payment_id>
  • url: <your_app_url>/webhooks/mollie/first-payment

@sandervanhooft
Copy link
Collaborator

sandervanhooft commented Jun 3, 2019

Just found the culprit, preparing a PR to fix it.

Appears that using a EloquentModel::update(...) on a related model does not work in a DB transaction. Which makes kind of sense now I think of it.

@sandervanhooft sandervanhooft changed the title How to redirect via controller instead of config variable User mandate id not updated on first payment Jun 3, 2019
@DeBelserArne
Copy link
Author

DeBelserArne commented Jun 3, 2019

Hey Sander,

Just to let you know, I updated the package to the latest version, everything works fine. Also, the correct redirect is now triggered via the controller. Thanks man!

Have a nice day!

PS: First bug squashed ^^

@sandervanhooft
Copy link
Collaborator

Thanks @aFluxx , also for following up! :)

sandervanhooft added a commit to sandervanhooft/cashier-mollie that referenced this issue Dec 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants