Last active
May 20, 2022 13:02
-
-
Save sandervanhooft/2e336b02debaf712dd16364c6d6275a5 to your computer and use it in GitHub Desktop.
Cashier Mollie basic example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Create a new subscription | |
$user = User::find(1); | |
$user->newSubscription('main', 'monthly') | |
->trialDays(10) | |
->withCoupon('MADRID2019') | |
->create(); | |
// Check subscription status | |
$user->subscribed('main'); | |
$user->subscription('main')->onTrial(); | |
// Switch to another plan | |
$user->subscription('main')->swap('plan-b'); | |
// Cancel the plan | |
$user->subscription('main')->cancel(); | |
// Access invoices | |
$user->invoices(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Laravel\Cashier\Coupon\CouponOrderItemPreprocessor as ProcessCoupons; | |
use Laravel\Cashier\Order\PersistOrderItemsPreprocessor as PersistOrderItems; | |
return [ | |
/** | |
* Plan defaults. Can be overridden per Plan. | |
*/ | |
'defaults' => [ | |
/** | |
* A first payment requires a customer to go through the Mollie checkout screen in order to create a Mandate for | |
* future recurring payments. | |
* @link https://docs.mollie.com/payments/recurring#payments-recurring-first-payment | |
*/ | |
'first_payment' => config('cashier.first_payment'), | |
/** | |
* The default chain of subscription OrderItem preprocessors. These are called right before the Subscription's | |
* OrderItem is processed into an OrderItem. You can use this for calculating variable costs a.k.a. metered billing. | |
* Can be overridden per subscription plan. Make sure the preprocessors extend BaseOrderItemPreprocessor. | |
*/ | |
'order_item_preprocessors' => [ | |
ProcessCoupons::class, | |
PersistOrderItems::class, | |
], | |
], | |
/** | |
* The subscription plans. | |
*/ | |
'plans' => [ | |
/** | |
* The plan reference. | |
*/ | |
'basic' => [ | |
/** | |
* The amount to be billed each billing cycle. | |
*/ | |
'amount' => [ | |
/** | |
* A string containing the exact amount you want to charge each billing cycle, in the given currency. | |
* Make sure to set the right amount of decimals. Non-string values are not accepted by Mollie. | |
*/ | |
'value' => '10.00', | |
/** | |
* An ISO 4217 currency code. The currencies supported depend on the payment methods that are enabled on | |
* your Mollie account. | |
*/ | |
'currency' => 'EUR', | |
], | |
/** | |
* The length of the billing cycle. | |
*/ | |
'interval' => '1 month', | |
/** | |
* The text to appear on the invoice. | |
*/ | |
'description' => 'Basic membership - ' . config('app.name'), | |
/** | |
* The chain of subscription OrderItem preprocessors. These are called right before the Subscription's | |
* OrderItem is processed into an OrderItem. You can use this for calculating variable costs a.k.a. metered | |
* billing. Make sure the preprocessors implement the PreprocessesOrderItems interface. | |
*/ | |
//'order_item_preprocessors' => [ | |
// ProcessCoupons::class, | |
// PersistOrderItems::class, | |
//], | |
], | |
/** | |
* The plan reference. | |
*/ | |
'premium' => [ | |
/** | |
* The amount to be billed each billing cycle. | |
*/ | |
'amount' => [ | |
/** | |
* A string containing the exact amount you want to charge each billing cycle, in the given currency. | |
* Make sure to set the right amount of decimals. Non-string values are not accepted by Mollie. | |
*/ | |
'value' => '25.00', | |
/** | |
* An ISO 4217 currency code. The currencies supported depend on the payment methods that are enabled on | |
* your Mollie account. | |
*/ | |
'currency' => 'EUR', | |
], | |
/** | |
* The length of the billing cycle. | |
*/ | |
'interval' => '1 month', | |
/** | |
* The text to appear on the invoice. | |
*/ | |
'description' => 'Premium membership - ' . config('app.name'), | |
/** | |
* The chain of subscription OrderItem preprocessors. These are called right before the Subscription's | |
* OrderItem is processed into an OrderItem. You can use this for calculating variable costs a.k.a. metered | |
* billing. Make sure the preprocessors implement the PreprocessesOrderItems interface. | |
*/ | |
//'order_item_preprocessors' => [ | |
// ProcessCoupons::class, | |
// PersistOrderItems::class, | |
//], | |
], | |
], | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use Illuminate\Http\RedirectResponse; | |
use Illuminate\Support\Facades\Auth; | |
class CreateSubscriptionController extends Controller | |
{ | |
/** | |
* @param string $plan | |
* @return \Illuminate\Http\RedirectResponse | |
*/ | |
public function __invoke(string $plan) | |
{ | |
$user = Auth::user(); | |
if(!$user->subscribed($plan)) { | |
$name = ucfirst($plan) . ' membership'; | |
$result = $user->newSubscription($name, $plan)->create(); | |
if(is_a($result, RedirectResponse::class)) { | |
return $result; // Redirect to Mollie checkout | |
} | |
// $result is a \Laravel\Cashier\Subscription model | |
return back()->with('status', 'Welcome to the ' . $plan . ' plan'); | |
} | |
return back()->with('status', 'You are already on the ' . $plan . ' plan'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
Route::middleware('auth')->group(function () { | |
Route::name('subscriptions.store')->post('/subscriptions/store/{plan}', 'CreateSubscriptionController'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment