mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-05-24 13:44:42 +00:00
frontend & backend update
This commit is contained in:
@@ -0,0 +1,199 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
|
||||
use App\PaymentGateway;
|
||||
use App\User;
|
||||
use App\UserCard;
|
||||
use App\UserSettings;
|
||||
use Cartalyst\Stripe\Exception\CardErrorException;
|
||||
use Stripe;
|
||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
|
||||
class PaymentService
|
||||
{
|
||||
/**
|
||||
* PaymentService constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->stripe = Stripe::make(config('services.stripe.secret'), '2020-03-02');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get and charge customer
|
||||
*
|
||||
* @param $request
|
||||
* @param $plan
|
||||
* @param $user
|
||||
*/
|
||||
public function getAndChargeCustomer($request, $plan, $user)
|
||||
{
|
||||
// Get Stripe Customer
|
||||
$customer = $this->findOrCreateStripeCustomer($user);
|
||||
|
||||
$paymentIntent = $this->stripe->paymentIntents()->create([
|
||||
'amount' => $plan->price,
|
||||
'currency' => 'USD',
|
||||
'payment_method_types' => [
|
||||
'card',
|
||||
],
|
||||
]);
|
||||
|
||||
dd($paymentIntent);
|
||||
|
||||
// Try charge customer
|
||||
try {
|
||||
if ($request->has('payment.meta.card_token')) {
|
||||
|
||||
// Register customer credit card
|
||||
$created_card = $this->registerStripeCreditCard($customer, $request->input('payment.meta.card_token'));
|
||||
|
||||
// Charge with created credit card
|
||||
/*$this->stripe->charges()->create([
|
||||
'description' => 'Subscription ' . $plan->name,
|
||||
'customer' => $customer['id'],
|
||||
'amount' => $plan->price,
|
||||
'currency' => 'USD',
|
||||
'source' => $created_card->card_id
|
||||
]);*/
|
||||
|
||||
} else {
|
||||
|
||||
// Charge with customer default creadit card
|
||||
$this->stripe->charges()->create([
|
||||
'description' => 'Subscription ' . $plan->name,
|
||||
'customer' => $customer['id'],
|
||||
'amount' => $plan->price,
|
||||
'currency' => 'USD',
|
||||
]);
|
||||
}
|
||||
|
||||
} catch (CardErrorException $error) {
|
||||
|
||||
//dd($error);
|
||||
|
||||
// Remove previously registered card
|
||||
if (in_array($error->getErrorCode(), ['rejected_card', 'card_declined'])
|
||||
&& $request->has('payment.meta.card_token')
|
||||
&& isset($error->getRawOutput()['error']['charge'])) {
|
||||
|
||||
// Get charge
|
||||
$charge = $this->stripe->charges()->find($error->getRawOutput()['error']['charge']);
|
||||
|
||||
// Remove card from stripe
|
||||
$this->deleteStripeCard($user->settings->stripe_customer_id, $charge['payment_method']);
|
||||
|
||||
// Get card
|
||||
$error_card = UserCard::where('card_id', $charge['payment_method'])->first();
|
||||
|
||||
// Delete card
|
||||
$error_card->delete();
|
||||
}
|
||||
|
||||
throw new HttpException($error->getCode(), $error->getMessage());
|
||||
}
|
||||
|
||||
// Increase payment processed column
|
||||
PaymentGateway::where('slug', 'stripe')->first()->increment('payment_processed');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Find or create stripe customer
|
||||
*
|
||||
* @param $user
|
||||
* @return array
|
||||
*/
|
||||
private function findOrCreateStripeCustomer($user)
|
||||
{
|
||||
// Get existing stripe customer
|
||||
if ($user->settings->stripe_customer_id) {
|
||||
|
||||
return $this->stripe->customers()->find($user->settings->stripe_customer_id);
|
||||
}
|
||||
|
||||
// Create new stripe costumer
|
||||
if (!$user->settings->stripe_customer_id) {
|
||||
|
||||
$customer = $this->stripe->customers()->create([
|
||||
'email' => $user->email,
|
||||
'name' => $user->name,
|
||||
'address' => [
|
||||
'city' => $user->settings->billing_city,
|
||||
'country' => $user->settings->billing_country,
|
||||
'line1' => $user->settings->billing_address,
|
||||
'state' => $user->settings->billing_state,
|
||||
'postal_code' => $user->settings->billing_postal_code,
|
||||
]
|
||||
]);
|
||||
|
||||
// Store user stripe_customer_id
|
||||
$user->settings()->update([
|
||||
'stripe_customer_id' => $customer['id']
|
||||
]);
|
||||
|
||||
return $customer;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register stripe credit card
|
||||
*
|
||||
* @param $customer
|
||||
* @param $card_id
|
||||
* @return object
|
||||
*/
|
||||
private function registerStripeCreditCard($customer, $card_id)
|
||||
{
|
||||
// Register user card
|
||||
$card = $this->stripe->cards()->create($customer['id'], $card_id);
|
||||
|
||||
// Get user settings
|
||||
$user_settings = UserSettings::where('stripe_customer_id', $customer['id'])->first();
|
||||
|
||||
// Set default status
|
||||
$default_card = UserCard::where('user_id', $user_settings->user_id)->get()->count() > 0 ? 0 : 1;
|
||||
|
||||
// Store user card
|
||||
$card = UserCard::create([
|
||||
'user_id' => $user_settings->user_id,
|
||||
'status' => 'active',
|
||||
'default' => $default_card,
|
||||
'provider' => 'stripe',
|
||||
'card_id' => $card['id'],
|
||||
'brand' => $card['brand'],
|
||||
'last4' => $card['last4'],
|
||||
'exp_month' => $card['exp_month'],
|
||||
'exp_year' => $card['exp_year'],
|
||||
]);
|
||||
|
||||
return $card;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set new default source from existing credit card
|
||||
*
|
||||
* @param $stripe_customer_id
|
||||
* @param $card_id
|
||||
*/
|
||||
public function setDefaultStripeCard($stripe_customer_id, $card_id)
|
||||
{
|
||||
$this->stripe->customers()->update($stripe_customer_id, [
|
||||
'default_source' => $card_id,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete customer stripe credit card
|
||||
*
|
||||
* @param $stripe_customer_id
|
||||
* @param $card_id
|
||||
*/
|
||||
public function deleteStripeCard($stripe_customer_id, $card_id)
|
||||
{
|
||||
$this->stripe->cards()->delete($stripe_customer_id, $card_id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,229 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
use Laravel\Cashier\Exceptions\IncompletePayment;
|
||||
use Stripe;
|
||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
|
||||
class StripeService
|
||||
{
|
||||
/**
|
||||
* Stripe Service constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->stripe = Stripe::make(env('STRIPE_SECRET'), '2020-03-02');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get default payment option or set new default payment
|
||||
*
|
||||
* @param $request
|
||||
* @param $user
|
||||
* @return mixed
|
||||
*/
|
||||
public function getOrSetDefaultPaymentMethod($request, $user)
|
||||
{
|
||||
// Check payment method
|
||||
if (! $request->has('payment.meta.pm') && $user->hasDefaultPaymentMethod()) {
|
||||
|
||||
// Get default payment
|
||||
return $user->defaultPaymentMethod()->paymentMethod;
|
||||
|
||||
} else if ( $request->has('payment.meta.pm') && $user->hasDefaultPaymentMethod() ) {
|
||||
|
||||
// Set new payment
|
||||
return $user->addPaymentMethod($request->input('payment.meta.pm'))->paymentMethod;
|
||||
|
||||
} else if ( $request->has('payment.meta.pm') && ! $user->hasDefaultPaymentMethod() ) {
|
||||
|
||||
// Set new payment
|
||||
return $user->updateDefaultPaymentMethod($request->input('payment.meta.pm'))->paymentMethod;
|
||||
|
||||
} else {
|
||||
|
||||
throw new HttpException(400, 'Something went wrong.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new subscription or replace by new subscription
|
||||
*
|
||||
* @param $request
|
||||
* @param $user
|
||||
* @param $paymentMethod
|
||||
*/
|
||||
public function createOrReplaceSubscription($request, $user)
|
||||
{
|
||||
try {
|
||||
|
||||
// Get payment method
|
||||
$paymentMethod = $this->getOrSetDefaultPaymentMethod($request, $user);
|
||||
|
||||
// Check if user have subscription
|
||||
if ($user->subscribed('main')) {
|
||||
|
||||
// Change subscription plan
|
||||
$user->subscription('main')->skipTrial()->swap($request->input('plan.data.id'));
|
||||
|
||||
} else {
|
||||
|
||||
// Create subscription
|
||||
$user->newSubscription('main', $request->input('plan.data.id'))->create($paymentMethod);
|
||||
}
|
||||
|
||||
} catch (IncompletePayment $exception) {
|
||||
|
||||
throw new HttpException(400, 'We can\'t charge your card');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create plan
|
||||
*
|
||||
* @param $request
|
||||
* @return mixed
|
||||
*/
|
||||
public function createPlan($request)
|
||||
{
|
||||
$product = $this->stripe->products()->create([
|
||||
'name' => $request->input('attributes.name'),
|
||||
'description' => $request->input('attributes.description'),
|
||||
'metadata' => [
|
||||
'capacity' => $request->input('attributes.capacity')
|
||||
]
|
||||
]);
|
||||
|
||||
$plan = $this->stripe->plans()->create([
|
||||
'id' => Str::slug($request->input('attributes.name')),
|
||||
'amount' => $request->input('attributes.price'),
|
||||
'currency' => 'USD',
|
||||
'interval' => 'month',
|
||||
'product' => $product['id'],
|
||||
]);
|
||||
|
||||
return compact('plan', 'product');
|
||||
}
|
||||
|
||||
/**
|
||||
* Update plan
|
||||
*
|
||||
* @param $request
|
||||
* @param $id
|
||||
*/
|
||||
public function updatePlan($request, $id)
|
||||
{
|
||||
$plan_colls = ['is_active', 'price'];
|
||||
$product_colls = ['name', 'description', 'capacity'];
|
||||
|
||||
$plan = $this->stripe->plans()->find($id);
|
||||
|
||||
// Update product
|
||||
if (in_array($request->name, $product_colls)) {
|
||||
|
||||
if ($request->name === 'capacity') {
|
||||
$this->stripe->products()->update($plan['product'], ['metadata' => ['capacity' => $request->value]]);
|
||||
}
|
||||
if ($request->name === 'name') {
|
||||
$this->stripe->products()->update($plan['product'], ['name' => $request->value]);
|
||||
}
|
||||
if ($request->name === 'description') {
|
||||
$this->stripe->products()->update($plan['product'], ['description' => $request->value]);
|
||||
}
|
||||
}
|
||||
|
||||
// Update plan
|
||||
if (in_array($request->name, $plan_colls)) {
|
||||
|
||||
if ($request->name === 'is_active') {
|
||||
$this->stripe->plans()->update($id, ['active' => $request->value]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get plan details
|
||||
*
|
||||
* @param $id
|
||||
* @return mixed
|
||||
*/
|
||||
public function getPlan($id)
|
||||
{
|
||||
$plan = $this->stripe->plans()->find($id);
|
||||
$product = $this->stripe->products()->find($plan['product']);
|
||||
|
||||
return compact('plan', 'product');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all plans
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getPlans()
|
||||
{
|
||||
// Get stripe plans
|
||||
$stripe_plans = $this->stripe->plans()->all();
|
||||
|
||||
// Plans container
|
||||
$plans = [];
|
||||
|
||||
foreach ($stripe_plans['data'] as $plan) {
|
||||
|
||||
// Get stripe product
|
||||
$product = $this->stripe->products()->find($plan['product']);
|
||||
|
||||
// Push data to $plan container
|
||||
array_push($plans, [
|
||||
'plan' => $plan,
|
||||
'product' => $product,
|
||||
]);
|
||||
}
|
||||
|
||||
return $plans;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all active plans
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getActivePlans()
|
||||
{
|
||||
// Get stripe plans
|
||||
$stripe_plans = $this->stripe->plans()->all();
|
||||
|
||||
// Plans container
|
||||
$plans = [];
|
||||
|
||||
foreach ($stripe_plans['data'] as $plan) {
|
||||
|
||||
if ($plan['active']) {
|
||||
|
||||
// Get stripe product
|
||||
$product = $this->stripe->products()->find($plan['product']);
|
||||
|
||||
// Push data to $plan container
|
||||
array_push($plans, [
|
||||
'plan' => $plan,
|
||||
'product' => $product,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
return $plans;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete plan
|
||||
*
|
||||
* @param $slug
|
||||
*/
|
||||
public function deletePlan($slug)
|
||||
{
|
||||
$this->stripe->plans()->delete($slug);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user