mollie / laravel-mollie

Mollie API client wrapper for Laravel & Mollie Connect provider for Laravel Socialite
https://www.mollie.com/
MIT License
322 stars 62 forks source link

Webhook example for a subscription #235

Closed Dennise89 closed 4 months ago

Dennise89 commented 8 months ago

Would be nice if there was an example of a webhook for a subscription as well. Because it's different than a normal payment or first payment webhook.

Jeffrey-Lang commented 6 months ago

In the read.me there is a line: Looking for a complete recurring billing solution? Take a look at laravel/cashier-mollie instead.

Is that what you are looking for? because Subscription is recurring billing.

Dennise89 commented 5 months ago

In the read.me there is a line: Looking for a complete recurring billing solution? Take a look at laravel/cashier-mollie instead.

Is that what you are looking for? because Subscription is recurring billing.

Yes I know that package, but that's not useable with Mollie Connect. It only works with a single mollie key. I am using Mollie Connect to connect to multiple profiles/accounts. Thanks for the link though.

Naoray commented 5 months ago

Hi @Dennise89,

sorry for taking so long!

The subscription endpoint uses the same webhook mechanism as for payments. The only difference with subscriptions is that when you receive the payment from the API, it will contain subscription specific information, such as the subscriptionId (s. https://docs.mollie.com/reference/v2/payments-api/get-payment#response-parameters-for-recurring-payments).

$mollieSubscription = Mollie::api()
    ->subscriptions
    ->create(...);

// save to your database
$subscription = new Subscription;
$subscription->mollie_id = $mollieSubscription->id;
$subscription->save();

Route::post('subscriptions/webhooks', function ($request) {
    $payment = Mollie::api()
        ->payments
        ->get($request->id);

    $subscription = Subscription::where('mollie_id', $payment->subscriptionId)->first();

    // handle the payment
});

Let me know if you need further help!