Created
May 20, 2020 13:20
-
-
Save fhdalikhan/e446bea87eaf882e103ddd266e5186d5 to your computer and use it in GitHub Desktop.
Stripe Subscription Controller
This file contains hidden or 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\Request; | |
use Auth; | |
use Carbon\Carbon; | |
use App\PaymentSripeSubscription; | |
use Stripe\Stripe; | |
use Stripe\Product; | |
use Stripe\Plan; | |
use Stripe\Subscription; | |
use Stripe\Customer; | |
class SubscriptionController extends Controller | |
{ | |
public function createStripeProduct(){ | |
Stripe::setApiKey('YOUR_API_KEY'); | |
$product = Product::create([ | |
'name' => 'Domain Hosting', | |
'type' => 'service', | |
]); | |
dd($product); | |
} | |
public function createStripePlan(){ | |
Stripe::setApiKey('YOUR_API_KEY'); | |
$plan = Plan::create([ | |
'currency' => 'eur', | |
'interval' => 'month', | |
'product' => 'ENTER PRODUCT ID CREATED USING Stripe\Product::create', | |
'nickname' => 'Pro Plan', | |
'amount' => 500, | |
]); | |
dd($plan); | |
} | |
public function createSubscription(Request $request){ | |
Stripe::setApiKey('YOUR_API_KEY'); | |
$token = $request->stripeToken; | |
$customer = Customer::create( | |
[ | |
'email' => Auth::user()->email, | |
'source' => $request->stripeToken | |
] | |
); | |
$result= Subscription::create( | |
[ | |
"customer" => $customer->id, | |
"items" => [ | |
[ | |
"plan" => "ENTER_PLAN_ID CREATED USING Stripe\Plan::create", | |
], | |
] | |
] | |
); | |
$stripeSubcription=new PaymentSripeSubscription(); | |
$stripeSubcription->user_id=Auth::user()->id; | |
$stripeSubcription->siteId=Auth::user()->idsite; | |
$stripeSubcription->subscription_id=$result->id; | |
$stripeSubcription->subscription_status=$result->status; | |
$stripeSubcription->subscription_plan='monthly'; | |
$stripeSubcription->start_date=$result->current_period_start; | |
$stripeSubcription->end_date=$result->current_period_end; | |
$stripeSubcription->save(); | |
return redirect('pa')->with('message','Stripe Subscription active sucessfully'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment