Last active
September 16, 2017 09:35
-
-
Save bayareawebpro/a83d503c0e25cf4964c924df2e1f3087 to your computer and use it in GitHub Desktop.
Sync Stripe Plan with Local Model
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 | |
//Your Stripe Plan Model | |
protected static function boot() { | |
parent::boot(); | |
static::creating(function($model) { // before create() method call this | |
$model->stripe_id = str_slug($model->name); | |
Stripe\Stripe::setApiKey(env('STRIPE_SECRET')); | |
try{ | |
$newStripePlan = Stripe\Plan::create(array( | |
'currency' => "usd", | |
'id' => $model->stripe_id, | |
'name' => $model->name, | |
'amount' => $model->amount, | |
'interval' => $model->interval,//day, week, month or year. | |
'interval_count'=>$model->interval_count, | |
'statement_descriptor' => $model->statement_descriptor | |
)); | |
} catch (Stripe\Error\ApiConnection $e) { | |
// Network problem, perhaps try again. | |
self::throwStripeError($e); | |
} catch (Stripe\Error\InvalidRequest $e) { | |
// You screwed up in your programming. Shouldn't happen! | |
self::throwStripeError($e); | |
} catch (Stripe\Error\Api $e) { | |
// Stripe's servers are down! | |
self::throwStripeError($e); | |
} | |
}); | |
static::saving(function($model) { // before save() method call this | |
Stripe\Stripe::setApiKey(env('STRIPE_SECRET')); | |
try{ | |
$stripePlan = Stripe\Plan::retrieve($model->stripe_id); | |
$stripePlan->name = $model->name; | |
$stripePlan->statement_descriptor = $model->statement_descriptor; | |
$stripePlan->save(); | |
} catch (Stripe\Error\ApiConnection $e) { | |
// Network problem, perhaps try again. | |
self::throwStripeError($e); | |
} catch (Stripe\Error\InvalidRequest $e) { | |
// You screwed up in your programming. Shouldn't happen! | |
self::throwStripeError($e); | |
} catch (Stripe\Error\Api $e) { | |
// Stripe's servers are down! | |
self::throwStripeError($e); | |
} | |
}); | |
static::deleting(function($model) { // before delete() method call this | |
Stripe\Stripe::setApiKey(env('STRIPE_SECRET')); | |
try{ | |
$stripePlan = Stripe\Plan::retrieve($model->stripe_id); | |
$stripePlan->delete(); | |
} catch (Stripe\Error\ApiConnection $e) { | |
// Network problem, perhaps try again. | |
self::throwStripeError($e); | |
} catch (Stripe\Error\InvalidRequest $e) { | |
// You screwed up in your programming. Shouldn't happen! | |
self::throwStripeError($e); | |
} catch (Stripe\Error\Api $e) { | |
// Stripe's servers are down! | |
self::throwStripeError($e); | |
} | |
}); | |
} | |
public static function throwStripeError($e){ | |
request()->session()->flash('alert', array( | |
'type' => 'warning', | |
'title' => 'Whoops!', | |
'message' => $e, | |
'btn_txt' => null, | |
'btn_url' => null, | |
)); | |
return redirect()->back(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment