feat: ✨ Moved Stripe functions to extension
This commit is contained in:
parent
e6ef07139d
commit
a34825a692
400
app/Extensions/PaymentGateways/Stripe/index.php
Normal file
400
app/Extensions/PaymentGateways/Stripe/index.php
Normal file
|
@ -0,0 +1,400 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Events\PaymentEvent;
|
||||||
|
use App\Events\UserUpdateCreditsEvent;
|
||||||
|
use App\Models\PartnerDiscount;
|
||||||
|
use App\Models\Payment;
|
||||||
|
use App\Models\Product;
|
||||||
|
use App\Models\ShopProduct;
|
||||||
|
use App\Models\User;
|
||||||
|
use App\Notifications\ConfirmPaymentNotification;
|
||||||
|
use Illuminate\Http\RedirectResponse;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Support\Facades\Redirect;
|
||||||
|
use Stripe\Exception\SignatureVerificationException;
|
||||||
|
use Stripe\Stripe;
|
||||||
|
use Stripe\StripeClient;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Request $request
|
||||||
|
* @param ShopProduct $shopProduct
|
||||||
|
*/
|
||||||
|
function StripePay(Request $request)
|
||||||
|
{
|
||||||
|
$user = Auth::user();
|
||||||
|
$shopProduct = ShopProduct::findOrFail($request->shopProduct);
|
||||||
|
|
||||||
|
// check if the price is valid for stripe
|
||||||
|
if (!checkPriceAmount($shopProduct->getTotalPrice(), strtoupper($shopProduct->currency_code), 'stripe')) {
|
||||||
|
Redirect::route('home')->with('error', __('The product you chose can\'t be purchased with this payment method. The total amount is too small. Please buy a bigger amount or try a different payment method.'))->send();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// create payment
|
||||||
|
$payment = Payment::create([
|
||||||
|
'user_id' => $user->id,
|
||||||
|
'payment_id' => null,
|
||||||
|
'payment_method' => 'stripe',
|
||||||
|
'type' => $shopProduct->type,
|
||||||
|
'status' => 'open',
|
||||||
|
'amount' => $shopProduct->quantity,
|
||||||
|
'price' => $shopProduct->price - ($shopProduct->price * PartnerDiscount::getDiscount() / 100),
|
||||||
|
'tax_value' => $shopProduct->getTaxValue(),
|
||||||
|
'total_price' => $shopProduct->getTotalPrice(),
|
||||||
|
'tax_percent' => $shopProduct->getTaxPercent(),
|
||||||
|
'currency_code' => $shopProduct->currency_code,
|
||||||
|
'shop_item_product_id' => $shopProduct->id,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$stripeClient = getStripeClient();
|
||||||
|
$request = $stripeClient->checkout->sessions->create([
|
||||||
|
'line_items' => [
|
||||||
|
[
|
||||||
|
'price_data' => [
|
||||||
|
'currency' => $shopProduct->currency_code,
|
||||||
|
'product_data' => [
|
||||||
|
'name' => $shopProduct->display . (PartnerDiscount::getDiscount() ? (' (' . __('Discount') . ' ' . PartnerDiscount::getDiscount() . '%)') : ''),
|
||||||
|
'description' => $shopProduct->description,
|
||||||
|
],
|
||||||
|
'unit_amount_decimal' => round($shopProduct->getPriceAfterDiscount() * 100, 2),
|
||||||
|
],
|
||||||
|
'quantity' => 1,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'price_data' => [
|
||||||
|
'currency' => $shopProduct->currency_code,
|
||||||
|
'product_data' => [
|
||||||
|
'name' => __('Tax'),
|
||||||
|
'description' => $shopProduct->getTaxPercent() . '%',
|
||||||
|
],
|
||||||
|
'unit_amount_decimal' => round($shopProduct->getTaxValue(), 2) * 100,
|
||||||
|
],
|
||||||
|
'quantity' => 1,
|
||||||
|
],
|
||||||
|
],
|
||||||
|
|
||||||
|
'mode' => 'payment',
|
||||||
|
'payment_method_types' => str_getcsv(config('SETTINGS::PAYMENTS:STRIPE:METHODS')),
|
||||||
|
'success_url' => route('payment.StripeSuccess', ['payment' => $payment->id]) . '&session_id={CHECKOUT_SESSION_ID}',
|
||||||
|
'cancel_url' => route('payment.Cancel'),
|
||||||
|
]);
|
||||||
|
|
||||||
|
Redirect::to($request->url)->send();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Request $request
|
||||||
|
*/
|
||||||
|
function StripeSuccess(Request $request)
|
||||||
|
{
|
||||||
|
$user = Auth::user();
|
||||||
|
error_log('StripeSuccess');
|
||||||
|
$payment = Payment::findOrFail($request->input('payment'));
|
||||||
|
$shopProduct = ShopProduct::findOrFail($payment->shop_item_product_id);
|
||||||
|
|
||||||
|
|
||||||
|
$stripeClient = getStripeClient();
|
||||||
|
try {
|
||||||
|
//get stripe data
|
||||||
|
$paymentSession = $stripeClient->checkout->sessions->retrieve($request->input('session_id'));
|
||||||
|
$paymentIntent = $stripeClient->paymentIntents->retrieve($paymentSession->payment_intent);
|
||||||
|
|
||||||
|
//get DB entry of this payment ID if existing
|
||||||
|
$paymentDbEntry = Payment::where('payment_id', $paymentSession->payment_intent)->count();
|
||||||
|
|
||||||
|
// check if payment is 100% completed and payment does not exist in db already
|
||||||
|
if ($paymentSession->status == 'complete' && $paymentIntent->status == 'succeeded' && $paymentDbEntry == 0) {
|
||||||
|
|
||||||
|
//update payment
|
||||||
|
$payment->update([
|
||||||
|
'payment_id' => $paymentSession->payment_intent,
|
||||||
|
'status' => 'paid',
|
||||||
|
]);
|
||||||
|
|
||||||
|
//payment notification
|
||||||
|
$user->notify(new ConfirmPaymentNotification($payment));
|
||||||
|
|
||||||
|
event(new UserUpdateCreditsEvent($user));
|
||||||
|
event(new PaymentEvent($user, $payment, $shopProduct));
|
||||||
|
|
||||||
|
//redirect back to home
|
||||||
|
Redirect::route('home')->with('success', 'Payment successful')->send();
|
||||||
|
} else {
|
||||||
|
if ($paymentIntent->status == 'processing') {
|
||||||
|
|
||||||
|
//update payment
|
||||||
|
$payment->update([
|
||||||
|
'payment_id' => $paymentSession->payment_intent,
|
||||||
|
'status' => 'processing',
|
||||||
|
]);
|
||||||
|
|
||||||
|
// todo invoice
|
||||||
|
event(new PaymentEvent($user, $payment, $shopProduct));
|
||||||
|
|
||||||
|
Redirect::route('home')->with('success', 'Your payment is being processed')->send();
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($paymentDbEntry == 0 && $paymentIntent->status != 'processing') {
|
||||||
|
$stripeClient->paymentIntents->cancel($paymentIntent->id);
|
||||||
|
|
||||||
|
//redirect back to home
|
||||||
|
Redirect::route('home')->with('info', __('Your payment has been canceled!'))->send();
|
||||||
|
} else {
|
||||||
|
abort(402);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception $e) {
|
||||||
|
if (env('APP_ENV') == 'local') {
|
||||||
|
dd($e->getMessage());
|
||||||
|
} else {
|
||||||
|
abort(422);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Request $request
|
||||||
|
*/
|
||||||
|
function handleStripePaymentSuccessHook($paymentIntent)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
// Get payment db entry
|
||||||
|
$payment = Payment::where('payment_id', $paymentIntent->id)->first();
|
||||||
|
$user = User::where('id', $payment->user_id)->first();
|
||||||
|
$shopProduct = ShopProduct::findOrFail($payment->shop_item_product_id);
|
||||||
|
|
||||||
|
if ($paymentIntent->status == 'succeeded' && $payment->status == 'processing') {
|
||||||
|
|
||||||
|
|
||||||
|
//update payment db entry status
|
||||||
|
$payment->update(['status' => 'paid']);
|
||||||
|
|
||||||
|
//payment notification
|
||||||
|
$user->notify(new ConfirmPaymentNotification($payment));
|
||||||
|
event(new UserUpdateCreditsEvent($user));
|
||||||
|
event(new PaymentEvent($user, $payment, $shopProduct));
|
||||||
|
}
|
||||||
|
} catch (Exception $ex) {
|
||||||
|
abort(422);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Request $request
|
||||||
|
*/
|
||||||
|
function StripeWebhooks(Request $request)
|
||||||
|
{
|
||||||
|
Stripe::setApiKey(getStripeSecret());
|
||||||
|
|
||||||
|
try {
|
||||||
|
$payload = @file_get_contents('php://input');
|
||||||
|
$sig_header = $request->header('Stripe-Signature');
|
||||||
|
$event = null;
|
||||||
|
$event = \Stripe\Webhook::constructEvent(
|
||||||
|
$payload,
|
||||||
|
$sig_header,
|
||||||
|
getStripeEndpointSecret()
|
||||||
|
);
|
||||||
|
} catch (\UnexpectedValueException $e) {
|
||||||
|
// Invalid payload
|
||||||
|
|
||||||
|
abort(400);
|
||||||
|
} catch (SignatureVerificationException $e) {
|
||||||
|
// Invalid signature
|
||||||
|
|
||||||
|
abort(400);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle the event
|
||||||
|
switch ($event->type) {
|
||||||
|
case 'payment_intent.succeeded':
|
||||||
|
$paymentIntent = $event->data->object; // contains a \Stripe\PaymentIntent
|
||||||
|
handleStripePaymentSuccessHook($paymentIntent);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
echo 'Received unknown event type ' . $event->type;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return \Stripe\StripeClient
|
||||||
|
*/
|
||||||
|
function getStripeClient()
|
||||||
|
{
|
||||||
|
return new StripeClient(getStripeSecret());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
function getStripeSecret()
|
||||||
|
{
|
||||||
|
return env('APP_ENV') == 'local'
|
||||||
|
? config('SETTINGS::PAYMENTS:STRIPE:TEST_SECRET')
|
||||||
|
: config('SETTINGS::PAYMENTS:STRIPE:SECRET');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
function getStripeEndpointSecret()
|
||||||
|
{
|
||||||
|
return env('APP_ENV') == 'local'
|
||||||
|
? config('SETTINGS::PAYMENTS:STRIPE:ENDPOINT_TEST_SECRET')
|
||||||
|
: config('SETTINGS::PAYMENTS:STRIPE:ENDPOINT_SECRET');
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @param $amount
|
||||||
|
* @param $currencyCode
|
||||||
|
* @param $payment_method
|
||||||
|
* @return bool
|
||||||
|
* @description check if the amount is higher than the minimum amount for the stripe gateway
|
||||||
|
*/
|
||||||
|
function checkPriceAmount($amount, $currencyCode, $payment_method)
|
||||||
|
{
|
||||||
|
$minimums = [
|
||||||
|
"USD" => [
|
||||||
|
"paypal" => 0,
|
||||||
|
"stripe" => 0.5
|
||||||
|
],
|
||||||
|
"AED" => [
|
||||||
|
"paypal" => 0,
|
||||||
|
"stripe" => 2
|
||||||
|
],
|
||||||
|
"AUD" => [
|
||||||
|
"paypal" => 0,
|
||||||
|
"stripe" => 0.5
|
||||||
|
],
|
||||||
|
"BGN" => [
|
||||||
|
"paypal" => 0,
|
||||||
|
"stripe" => 1
|
||||||
|
],
|
||||||
|
"BRL" => [
|
||||||
|
"paypal" => 0,
|
||||||
|
"stripe" => 0.5
|
||||||
|
],
|
||||||
|
"CAD" => [
|
||||||
|
"paypal" => 0,
|
||||||
|
"stripe" => 0.5
|
||||||
|
],
|
||||||
|
"CHF" => [
|
||||||
|
"paypal" => 0,
|
||||||
|
"stripe" => 0.5
|
||||||
|
],
|
||||||
|
"CZK" => [
|
||||||
|
"paypal" => 0,
|
||||||
|
"stripe" => 15
|
||||||
|
],
|
||||||
|
"DKK" => [
|
||||||
|
"paypal" => 0,
|
||||||
|
"stripe" => 2.5
|
||||||
|
],
|
||||||
|
"EUR" => [
|
||||||
|
"paypal" => 0,
|
||||||
|
"stripe" => 0.5
|
||||||
|
],
|
||||||
|
"GBP" => [
|
||||||
|
"paypal" => 0,
|
||||||
|
"stripe" => 0.3
|
||||||
|
],
|
||||||
|
"HKD" => [
|
||||||
|
"paypal" => 0,
|
||||||
|
"stripe" => 4
|
||||||
|
],
|
||||||
|
"HRK" => [
|
||||||
|
"paypal" => 0,
|
||||||
|
"stripe" => 0.5
|
||||||
|
],
|
||||||
|
"HUF" => [
|
||||||
|
"paypal" => 0,
|
||||||
|
"stripe" => 175
|
||||||
|
],
|
||||||
|
"INR" => [
|
||||||
|
"paypal" => 0,
|
||||||
|
"stripe" => 0.5
|
||||||
|
],
|
||||||
|
"JPY" => [
|
||||||
|
"paypal" => 0,
|
||||||
|
"stripe" => 0.5
|
||||||
|
],
|
||||||
|
"MXN" => [
|
||||||
|
"paypal" => 0,
|
||||||
|
"stripe" => 10
|
||||||
|
],
|
||||||
|
"MYR" => [
|
||||||
|
"paypal" => 0,
|
||||||
|
"stripe" => 2
|
||||||
|
],
|
||||||
|
"NOK" => [
|
||||||
|
"paypal" => 0,
|
||||||
|
"stripe" => 3
|
||||||
|
],
|
||||||
|
"NZD" => [
|
||||||
|
"paypal" => 0,
|
||||||
|
"stripe" => 0.5
|
||||||
|
],
|
||||||
|
"PLN" => [
|
||||||
|
"paypal" => 0,
|
||||||
|
"stripe" => 2
|
||||||
|
],
|
||||||
|
"RON" => [
|
||||||
|
"paypal" => 0,
|
||||||
|
"stripe" => 2
|
||||||
|
],
|
||||||
|
"SEK" => [
|
||||||
|
"paypal" => 0,
|
||||||
|
"stripe" => 3
|
||||||
|
],
|
||||||
|
"SGD" => [
|
||||||
|
"paypal" => 0,
|
||||||
|
"stripe" => 0.5
|
||||||
|
],
|
||||||
|
"THB" => [
|
||||||
|
"paypal" => 0,
|
||||||
|
"stripe" => 10
|
||||||
|
]
|
||||||
|
];
|
||||||
|
return $amount >= $minimums[$currencyCode][$payment_method];
|
||||||
|
}
|
||||||
|
|
||||||
|
function getStripeConfig()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
"name" => "Stripe",
|
||||||
|
"description" => "Stripe payment gateway",
|
||||||
|
"mode" => [
|
||||||
|
"type" => "select",
|
||||||
|
"label" => "Mode",
|
||||||
|
"value" => config("APP_ENV") == 'local' ? "sandbox" : "live",
|
||||||
|
"options" => [
|
||||||
|
"sandbox" => "Sandbox",
|
||||||
|
"live" => "Live",
|
||||||
|
],
|
||||||
|
],
|
||||||
|
"TEST_SECRET" => [
|
||||||
|
"type" => "text",
|
||||||
|
"label" => "Test Secret Key",
|
||||||
|
"value" => config("SETTINGS::PAYMENTS:STRIPE:TEST_SECRET"),
|
||||||
|
],
|
||||||
|
"SECRET" => [
|
||||||
|
"type" => "text",
|
||||||
|
"label" => "Live Secret Key",
|
||||||
|
"value" => config("SETTINGS::PAYMENTS:STRIPE:SECRET"),
|
||||||
|
],
|
||||||
|
"ENDPOINT_TEST_SECRET" => [
|
||||||
|
"type" => "text",
|
||||||
|
"label" => "Test Endpoint Secret",
|
||||||
|
"value" => config("SETTINGS::PAYMENTS:STRIPE:ENDPOINT_TEST_SECRET"),
|
||||||
|
],
|
||||||
|
"ENDPOINT_SECRET" => [
|
||||||
|
"type" => "text",
|
||||||
|
"label" => "Live Endpoint Secret",
|
||||||
|
"value" => config("SETTINGS::PAYMENTS:STRIPE:ENDPOINT_SECRET"),
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
|
@ -60,8 +60,7 @@ class PaymentController extends Controller
|
||||||
$paymentGateways[] = $payment;
|
$paymentGateways[] = $payment;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return view('store.checkout')->with([
|
return view('store.checkout')->with([
|
||||||
'product' => $shopProduct,
|
'product' => $shopProduct,
|
||||||
'discountpercent' => PartnerDiscount::getDiscount(),
|
'discountpercent' => PartnerDiscount::getDiscount(),
|
||||||
|
@ -83,8 +82,8 @@ class PaymentController extends Controller
|
||||||
{
|
{
|
||||||
//dd($shopProduct);
|
//dd($shopProduct);
|
||||||
//check if the product is really free or the discount is 100%
|
//check if the product is really free or the discount is 100%
|
||||||
if($shopProduct->getTotalPrice()>0) return redirect()->route('home')->with('error', __('An error ocured. Please try again.'));
|
if ($shopProduct->getTotalPrice() > 0) return redirect()->route('home')->with('error', __('An error ocured. Please try again.'));
|
||||||
|
|
||||||
//give product
|
//give product
|
||||||
/** @var User $user */
|
/** @var User $user */
|
||||||
$user = Auth::user();
|
$user = Auth::user();
|
||||||
|
@ -92,14 +91,14 @@ class PaymentController extends Controller
|
||||||
//not updating server limit
|
//not updating server limit
|
||||||
|
|
||||||
//update User with bought item
|
//update User with bought item
|
||||||
if ($shopProduct->type=="Credits") {
|
if ($shopProduct->type == "Credits") {
|
||||||
$user->increment('credits', $shopProduct->quantity);
|
$user->increment('credits', $shopProduct->quantity);
|
||||||
}elseif ($shopProduct->type=="Server slots"){
|
} elseif ($shopProduct->type == "Server slots") {
|
||||||
$user->increment('server_limit', $shopProduct->quantity);
|
$user->increment('server_limit', $shopProduct->quantity);
|
||||||
}
|
}
|
||||||
|
|
||||||
//skipped the referral commission, because the user did not pay anything.
|
//skipped the referral commission, because the user did not pay anything.
|
||||||
|
|
||||||
//not giving client role
|
//not giving client role
|
||||||
|
|
||||||
//store payment
|
//store payment
|
||||||
|
@ -110,7 +109,7 @@ class PaymentController extends Controller
|
||||||
'type' => $shopProduct->type,
|
'type' => $shopProduct->type,
|
||||||
'status' => 'paid',
|
'status' => 'paid',
|
||||||
'amount' => $shopProduct->quantity,
|
'amount' => $shopProduct->quantity,
|
||||||
'price' => $shopProduct->price - ($shopProduct->price*PartnerDiscount::getDiscount()/100),
|
'price' => $shopProduct->price - ($shopProduct->price * PartnerDiscount::getDiscount() / 100),
|
||||||
'tax_value' => $shopProduct->getTaxValue(),
|
'tax_value' => $shopProduct->getTaxValue(),
|
||||||
'tax_percent' => $shopProduct->getTaxPercent(),
|
'tax_percent' => $shopProduct->getTaxPercent(),
|
||||||
'total_price' => $shopProduct->getTotalPrice(),
|
'total_price' => $shopProduct->getTotalPrice(),
|
||||||
|
@ -126,8 +125,8 @@ class PaymentController extends Controller
|
||||||
return redirect()->route('home')->with('success', __('Your credit balance has been increased!'));
|
return redirect()->route('home')->with('success', __('Your credit balance has been increased!'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function pay(Request $request)
|
public function pay(Request $request)
|
||||||
{
|
{
|
||||||
$product = ShopProduct::find($request->product_id);
|
$product = ShopProduct::find($request->product_id);
|
||||||
$paymentGateway = $request->payment_method;
|
$paymentGateway = $request->payment_method;
|
||||||
|
|
||||||
|
@ -142,416 +141,6 @@ class PaymentController extends Controller
|
||||||
return redirect()->route('store.index')->with('info', 'Payment was Canceled');
|
return redirect()->route('store.index')->with('info', 'Payment was Canceled');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Request $request
|
|
||||||
* @param ShopProduct $shopProduct
|
|
||||||
* @return RedirectResponse
|
|
||||||
*/
|
|
||||||
public function StripePay(Request $request, ShopProduct $shopProduct)
|
|
||||||
{
|
|
||||||
if(!$this->checkAmount($shopProduct->getTotalPrice(), strtoupper($shopProduct->currency_code), "stripe")) return redirect()->route('home')->with('error', __('The product you chose can´t be purchased with this payment method. The total amount is too small. Please buy a bigger amount or try a different payment method.'));
|
|
||||||
$stripeClient = $this->getStripeClient();
|
|
||||||
|
|
||||||
$request = $stripeClient->checkout->sessions->create([
|
|
||||||
'line_items' => [
|
|
||||||
[
|
|
||||||
'price_data' => [
|
|
||||||
'currency' => $shopProduct->currency_code,
|
|
||||||
'product_data' => [
|
|
||||||
'name' => $shopProduct->display.(PartnerDiscount::getDiscount() ? (' ('.__('Discount').' '.PartnerDiscount::getDiscount().'%)') : ''),
|
|
||||||
'description' => $shopProduct->description,
|
|
||||||
],
|
|
||||||
'unit_amount_decimal' => round($shopProduct->getPriceAfterDiscount() * 100, 2),
|
|
||||||
],
|
|
||||||
'quantity' => 1,
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'price_data' => [
|
|
||||||
'currency' => $shopProduct->currency_code,
|
|
||||||
'product_data' => [
|
|
||||||
'name' => __('Tax'),
|
|
||||||
'description' => $shopProduct->getTaxPercent().'%',
|
|
||||||
],
|
|
||||||
'unit_amount_decimal' => round($shopProduct->getTaxValue(), 2) * 100,
|
|
||||||
],
|
|
||||||
'quantity' => 1,
|
|
||||||
],
|
|
||||||
],
|
|
||||||
|
|
||||||
'mode' => 'payment',
|
|
||||||
'payment_method_types' => str_getcsv(config('SETTINGS::PAYMENTS:STRIPE:METHODS')),
|
|
||||||
'success_url' => route('payment.StripeSuccess', ['product' => $shopProduct->id]).'&session_id={CHECKOUT_SESSION_ID}',
|
|
||||||
'cancel_url' => route('payment.Cancel'),
|
|
||||||
]);
|
|
||||||
|
|
||||||
return redirect($request->url, 303);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Request $request
|
|
||||||
*/
|
|
||||||
public function StripeSuccess(Request $request)
|
|
||||||
{
|
|
||||||
/** @var ShopProduct $shopProduct */
|
|
||||||
$shopProduct = ShopProduct::findOrFail($request->input('product'));
|
|
||||||
|
|
||||||
/** @var User $user */
|
|
||||||
$user = Auth::user();
|
|
||||||
|
|
||||||
$stripeClient = $this->getStripeClient();
|
|
||||||
|
|
||||||
try {
|
|
||||||
//get stripe data
|
|
||||||
$paymentSession = $stripeClient->checkout->sessions->retrieve($request->input('session_id'));
|
|
||||||
$paymentIntent = $stripeClient->paymentIntents->retrieve($paymentSession->payment_intent);
|
|
||||||
|
|
||||||
//get DB entry of this payment ID if existing
|
|
||||||
$paymentDbEntry = Payment::where('payment_id', $paymentSession->payment_intent)->count();
|
|
||||||
|
|
||||||
// check if payment is 100% completed and payment does not exist in db already
|
|
||||||
if ($paymentSession->status == 'complete' && $paymentIntent->status == 'succeeded' && $paymentDbEntry == 0) {
|
|
||||||
|
|
||||||
//update server limit
|
|
||||||
if (config('SETTINGS::USER:SERVER_LIMIT_AFTER_IRL_PURCHASE') !== 0) {
|
|
||||||
if ($user->server_limit < config('SETTINGS::USER:SERVER_LIMIT_AFTER_IRL_PURCHASE')) {
|
|
||||||
$user->update(['server_limit' => config('SETTINGS::USER:SERVER_LIMIT_AFTER_IRL_PURCHASE')]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//update User with bought item
|
|
||||||
if ($shopProduct->type == 'Credits') {
|
|
||||||
$user->increment('credits', $shopProduct->quantity);
|
|
||||||
} elseif ($shopProduct->type == 'Server slots') {
|
|
||||||
$user->increment('server_limit', $shopProduct->quantity);
|
|
||||||
}
|
|
||||||
|
|
||||||
//update role give Referral-reward
|
|
||||||
if ($user->role == 'member') {
|
|
||||||
$user->update(['role' => 'client']);
|
|
||||||
|
|
||||||
if ((config('SETTINGS::REFERRAL:MODE') == 'commission' || config('SETTINGS::REFERRAL:MODE') == 'both') && $shopProduct->type == 'Credits') {
|
|
||||||
if ($ref_user = DB::table('user_referrals')->where('registered_user_id', '=', $user->id)->first()) {
|
|
||||||
$ref_user = User::findOrFail($ref_user->referral_id);
|
|
||||||
$increment = number_format($shopProduct->quantity / 100 * config('SETTINGS::REFERRAL:PERCENTAGE'), 0, '', '');
|
|
||||||
$ref_user->increment('credits', $increment);
|
|
||||||
|
|
||||||
//LOGS REFERRALS IN THE ACTIVITY LOG
|
|
||||||
activity()
|
|
||||||
->performedOn($user)
|
|
||||||
->causedBy($ref_user)
|
|
||||||
->log('gained '.$increment.' '.config('SETTINGS::SYSTEM:CREDITS_DISPLAY_NAME').' for commission-referral of '.$user->name.' (ID:'.$user->id.')');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//store paid payment
|
|
||||||
$payment = Payment::create([
|
|
||||||
'user_id' => $user->id,
|
|
||||||
'payment_id' => $paymentSession->payment_intent,
|
|
||||||
'payment_method' => 'stripe',
|
|
||||||
'type' => $shopProduct->type,
|
|
||||||
'status' => 'paid',
|
|
||||||
'amount' => $shopProduct->quantity,
|
|
||||||
'price' => $shopProduct->price - ($shopProduct->price * PartnerDiscount::getDiscount() / 100),
|
|
||||||
'tax_value' => $shopProduct->getTaxValue(),
|
|
||||||
'total_price' => $shopProduct->getTotalPrice(),
|
|
||||||
'tax_percent' => $shopProduct->getTaxPercent(),
|
|
||||||
'currency_code' => $shopProduct->currency_code,
|
|
||||||
'shop_item_product_id' => $shopProduct->id,
|
|
||||||
]);
|
|
||||||
|
|
||||||
//payment notification
|
|
||||||
$user->notify(new ConfirmPaymentNotification($payment));
|
|
||||||
|
|
||||||
event(new UserUpdateCreditsEvent($user));
|
|
||||||
|
|
||||||
//only create invoice if SETTINGS::INVOICE:ENABLED is true
|
|
||||||
if (config('SETTINGS::INVOICE:ENABLED') == 'true') {
|
|
||||||
$this->createInvoice($user, $payment, 'paid', $shopProduct->currency_code);
|
|
||||||
}
|
|
||||||
|
|
||||||
//redirect back to home
|
|
||||||
return redirect()->route('home')->with('success', __('Your credit balance has been increased!'));
|
|
||||||
} else {
|
|
||||||
if ($paymentIntent->status == 'processing') {
|
|
||||||
|
|
||||||
//store processing payment
|
|
||||||
$payment = Payment::create([
|
|
||||||
'user_id' => $user->id,
|
|
||||||
'payment_id' => $paymentSession->payment_intent,
|
|
||||||
'payment_method' => 'stripe',
|
|
||||||
'type' => $shopProduct->type,
|
|
||||||
'status' => 'processing',
|
|
||||||
'amount' => $shopProduct->quantity,
|
|
||||||
'price' => $shopProduct->price,
|
|
||||||
'tax_value' => $shopProduct->getTaxValue(),
|
|
||||||
'total_price' => $shopProduct->getTotalPrice(),
|
|
||||||
'tax_percent' => $shopProduct->getTaxPercent(),
|
|
||||||
'currency_code' => $shopProduct->currency_code,
|
|
||||||
'shop_item_product_id' => $shopProduct->id,
|
|
||||||
]);
|
|
||||||
|
|
||||||
//only create invoice if SETTINGS::INVOICE:ENABLED is true
|
|
||||||
if (config('SETTINGS::INVOICE:ENABLED') == 'true') {
|
|
||||||
$this->createInvoice($user, $payment, 'paid', $shopProduct->currency_code);
|
|
||||||
}
|
|
||||||
|
|
||||||
//redirect back to home
|
|
||||||
return redirect()->route('home')->with('success', __('Your payment is being processed!'));
|
|
||||||
}
|
|
||||||
if ($paymentDbEntry == 0 && $paymentIntent->status != 'processing') {
|
|
||||||
$stripeClient->paymentIntents->cancel($paymentIntent->id);
|
|
||||||
|
|
||||||
//redirect back to home
|
|
||||||
return redirect()->route('home')->with('success', __('Your payment has been canceled!'));
|
|
||||||
} else {
|
|
||||||
abort(402);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (HttpException $ex) {
|
|
||||||
if (env('APP_ENV') == 'local') {
|
|
||||||
echo $ex->statusCode;
|
|
||||||
dd($ex->getMessage());
|
|
||||||
} else {
|
|
||||||
abort(422);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Request $request
|
|
||||||
*/
|
|
||||||
protected function handleStripePaymentSuccessHook($paymentIntent)
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
// Get payment db entry
|
|
||||||
$payment = Payment::where('payment_id', $paymentIntent->id)->first();
|
|
||||||
$user = User::where('id', $payment->user_id)->first();
|
|
||||||
|
|
||||||
if ($paymentIntent->status == 'succeeded' && $payment->status == 'processing') {
|
|
||||||
|
|
||||||
//update server limit
|
|
||||||
if (config('SETTINGS::USER:SERVER_LIMIT_AFTER_IRL_PURCHASE') !== 0) {
|
|
||||||
if ($user->server_limit < config('SETTINGS::USER:SERVER_LIMIT_AFTER_IRL_PURCHASE')) {
|
|
||||||
$user->update(['server_limit' => config('SETTINGS::USER:SERVER_LIMIT_AFTER_IRL_PURCHASE')]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//update User with bought item
|
|
||||||
if ($shopProduct->type == 'Credits') {
|
|
||||||
$user->increment('credits', $shopProduct->quantity);
|
|
||||||
} elseif ($shopProduct->type == 'Server slots') {
|
|
||||||
$user->increment('server_limit', $shopProduct->quantity);
|
|
||||||
}
|
|
||||||
|
|
||||||
//update role give Referral-reward
|
|
||||||
if ($user->role == 'member') {
|
|
||||||
$user->update(['role' => 'client']);
|
|
||||||
|
|
||||||
if ((config('SETTINGS::REFERRAL:MODE') == 'commission' || config('SETTINGS::REFERRAL:MODE') == 'both') && $shopProduct->type == 'Credits') {
|
|
||||||
if ($ref_user = DB::table('user_referrals')->where('registered_user_id', '=', $user->id)->first()) {
|
|
||||||
$ref_user = User::findOrFail($ref_user->referral_id);
|
|
||||||
$increment = number_format($shopProduct->quantity / 100 * config('SETTINGS::REFERRAL:PERCENTAGE'), 0, '', '');
|
|
||||||
$ref_user->increment('credits', $increment);
|
|
||||||
|
|
||||||
//LOGS REFERRALS IN THE ACTIVITY LOG
|
|
||||||
activity()
|
|
||||||
->performedOn($user)
|
|
||||||
->causedBy($ref_user)
|
|
||||||
->log('gained '.$increment.' '.config('SETTINGS::SYSTEM:CREDITS_DISPLAY_NAME').' for commission-referral of '.$user->name.' (ID:'.$user->id.')');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//update payment db entry status
|
|
||||||
$payment->update(['status' => 'paid']);
|
|
||||||
|
|
||||||
//payment notification
|
|
||||||
$user->notify(new ConfirmPaymentNotification($payment));
|
|
||||||
event(new UserUpdateCreditsEvent($user));
|
|
||||||
|
|
||||||
//only create invoice if SETTINGS::INVOICE:ENABLED is true
|
|
||||||
if (config('SETTINGS::INVOICE:ENABLED') == 'true') {
|
|
||||||
$this->createInvoice($user, $payment, 'paid', strtoupper($paymentIntent->currency));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (HttpException $ex) {
|
|
||||||
abort(422);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Request $request
|
|
||||||
*/
|
|
||||||
public function StripeWebhooks(Request $request)
|
|
||||||
{
|
|
||||||
\Stripe\Stripe::setApiKey($this->getStripeSecret());
|
|
||||||
|
|
||||||
try {
|
|
||||||
$payload = @file_get_contents('php://input');
|
|
||||||
$sig_header = $request->header('Stripe-Signature');
|
|
||||||
$event = null;
|
|
||||||
$event = \Stripe\Webhook::constructEvent(
|
|
||||||
$payload,
|
|
||||||
$sig_header,
|
|
||||||
$this->getStripeEndpointSecret()
|
|
||||||
);
|
|
||||||
} catch (\UnexpectedValueException $e) {
|
|
||||||
// Invalid payload
|
|
||||||
|
|
||||||
abort(400);
|
|
||||||
} catch (\Stripe\Exception\SignatureVerificationException $e) {
|
|
||||||
// Invalid signature
|
|
||||||
|
|
||||||
abort(400);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle the event
|
|
||||||
switch ($event->type) {
|
|
||||||
case 'payment_intent.succeeded':
|
|
||||||
$paymentIntent = $event->data->object; // contains a \Stripe\PaymentIntent
|
|
||||||
$this->handleStripePaymentSuccessHook($paymentIntent);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
echo 'Received unknown event type '.$event->type;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return \Stripe\StripeClient
|
|
||||||
*/
|
|
||||||
protected function getStripeClient()
|
|
||||||
{
|
|
||||||
return new \Stripe\StripeClient($this->getStripeSecret());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
protected function getStripeSecret()
|
|
||||||
{
|
|
||||||
return env('APP_ENV') == 'local'
|
|
||||||
? config('SETTINGS::PAYMENTS:STRIPE:TEST_SECRET')
|
|
||||||
: config('SETTINGS::PAYMENTS:STRIPE:SECRET');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
protected function getStripeEndpointSecret()
|
|
||||||
{
|
|
||||||
return env('APP_ENV') == 'local'
|
|
||||||
? config('SETTINGS::PAYMENTS:STRIPE:ENDPOINT_TEST_SECRET')
|
|
||||||
: config('SETTINGS::PAYMENTS:STRIPE:ENDPOINT_SECRET');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function checkAmount($amount, $currencyCode, $payment_method)
|
|
||||||
{
|
|
||||||
$minimums = [
|
|
||||||
"USD" => [
|
|
||||||
"paypal" => 0,
|
|
||||||
"stripe" => 0.5
|
|
||||||
],
|
|
||||||
"AED" => [
|
|
||||||
"paypal" => 0,
|
|
||||||
"stripe" => 2
|
|
||||||
],
|
|
||||||
"AUD" => [
|
|
||||||
"paypal" => 0,
|
|
||||||
"stripe" => 0.5
|
|
||||||
],
|
|
||||||
"BGN" => [
|
|
||||||
"paypal" => 0,
|
|
||||||
"stripe" => 1
|
|
||||||
],
|
|
||||||
"BRL" => [
|
|
||||||
"paypal" => 0,
|
|
||||||
"stripe" => 0.5
|
|
||||||
],
|
|
||||||
"CAD" => [
|
|
||||||
"paypal" => 0,
|
|
||||||
"stripe" => 0.5
|
|
||||||
],
|
|
||||||
"CHF" => [
|
|
||||||
"paypal" => 0,
|
|
||||||
"stripe" => 0.5
|
|
||||||
],
|
|
||||||
"CZK" => [
|
|
||||||
"paypal" => 0,
|
|
||||||
"stripe" => 15
|
|
||||||
],
|
|
||||||
"DKK" => [
|
|
||||||
"paypal" => 0,
|
|
||||||
"stripe" => 2.5
|
|
||||||
],
|
|
||||||
"EUR" => [
|
|
||||||
"paypal" => 0,
|
|
||||||
"stripe" => 0.5
|
|
||||||
],
|
|
||||||
"GBP" => [
|
|
||||||
"paypal" => 0,
|
|
||||||
"stripe" => 0.3
|
|
||||||
],
|
|
||||||
"HKD" => [
|
|
||||||
"paypal" => 0,
|
|
||||||
"stripe" => 4
|
|
||||||
],
|
|
||||||
"HRK" => [
|
|
||||||
"paypal" => 0,
|
|
||||||
"stripe" => 0.5
|
|
||||||
],
|
|
||||||
"HUF" => [
|
|
||||||
"paypal" => 0,
|
|
||||||
"stripe" => 175
|
|
||||||
],
|
|
||||||
"INR" => [
|
|
||||||
"paypal" => 0,
|
|
||||||
"stripe" => 0.5
|
|
||||||
],
|
|
||||||
"JPY" => [
|
|
||||||
"paypal" => 0,
|
|
||||||
"stripe" => 0.5
|
|
||||||
],
|
|
||||||
"MXN" => [
|
|
||||||
"paypal" => 0,
|
|
||||||
"stripe" => 10
|
|
||||||
],
|
|
||||||
"MYR" => [
|
|
||||||
"paypal" => 0,
|
|
||||||
"stripe" => 2
|
|
||||||
],
|
|
||||||
"NOK" => [
|
|
||||||
"paypal" => 0,
|
|
||||||
"stripe" => 3
|
|
||||||
],
|
|
||||||
"NZD" => [
|
|
||||||
"paypal" => 0,
|
|
||||||
"stripe" => 0.5
|
|
||||||
],
|
|
||||||
"PLN" => [
|
|
||||||
"paypal" => 0,
|
|
||||||
"stripe" => 2
|
|
||||||
],
|
|
||||||
"RON" => [
|
|
||||||
"paypal" => 0,
|
|
||||||
"stripe" => 2
|
|
||||||
],
|
|
||||||
"SEK" => [
|
|
||||||
"paypal" => 0,
|
|
||||||
"stripe" => 3
|
|
||||||
],
|
|
||||||
"SGD" => [
|
|
||||||
"paypal" => 0,
|
|
||||||
"stripe" => 0.5
|
|
||||||
],
|
|
||||||
"THB" => [
|
|
||||||
"paypal" => 0,
|
|
||||||
"stripe" => 10
|
|
||||||
]
|
|
||||||
];
|
|
||||||
return $amount >= $minimums[$currencyCode][$payment_method];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return JsonResponse|mixed
|
* @return JsonResponse|mixed
|
||||||
*
|
*
|
||||||
|
@ -564,9 +153,7 @@ class PaymentController extends Controller
|
||||||
return datatables($query)
|
return datatables($query)
|
||||||
|
|
||||||
->addColumn('user', function (Payment $payment) {
|
->addColumn('user', function (Payment $payment) {
|
||||||
return
|
return ($payment->user) ? '<a href="' . route('admin.users.show', $payment->user->id) . '">' . $payment->user->name . '</a>' : __('Unknown user');
|
||||||
($payment->user)?'<a href="'.route('admin.users.show', $payment->user->id).'">'.$payment->user->name.'</a>':__('Unknown user');
|
|
||||||
|
|
||||||
})
|
})
|
||||||
->editColumn('price', function (Payment $payment) {
|
->editColumn('price', function (Payment $payment) {
|
||||||
return $payment->formatToCurrency($payment->price);
|
return $payment->formatToCurrency($payment->price);
|
||||||
|
@ -575,18 +162,20 @@ class PaymentController extends Controller
|
||||||
return $payment->formatToCurrency($payment->tax_value);
|
return $payment->formatToCurrency($payment->tax_value);
|
||||||
})
|
})
|
||||||
->editColumn('tax_percent', function (Payment $payment) {
|
->editColumn('tax_percent', function (Payment $payment) {
|
||||||
return $payment->tax_percent.' %';
|
return $payment->tax_percent . ' %';
|
||||||
})
|
})
|
||||||
->editColumn('total_price', function (Payment $payment) {
|
->editColumn('total_price', function (Payment $payment) {
|
||||||
return $payment->formatToCurrency($payment->total_price);
|
return $payment->formatToCurrency($payment->total_price);
|
||||||
})
|
})
|
||||||
|
|
||||||
->editColumn('created_at', function (Payment $payment) {
|
->editColumn('created_at', function (Payment $payment) {
|
||||||
return ['display' => $payment->created_at ? $payment->created_at->diffForHumans() : '',
|
return [
|
||||||
'raw' => $payment->created_at ? strtotime($payment->created_at) : ''];
|
'display' => $payment->created_at ? $payment->created_at->diffForHumans() : '',
|
||||||
|
'raw' => $payment->created_at ? strtotime($payment->created_at) : ''
|
||||||
|
];
|
||||||
})
|
})
|
||||||
->addColumn('actions', function (Payment $payment) {
|
->addColumn('actions', function (Payment $payment) {
|
||||||
return '<a data-content="'.__('Download').'" data-toggle="popover" data-trigger="hover" data-placement="top" href="'.route('admin.invoices.downloadSingleInvoice', 'id='.$payment->payment_id).'" class="btn btn-sm text-white btn-info mr-1"><i class="fas fa-file-download"></i></a>';
|
return '<a data-content="' . __('Download') . '" data-toggle="popover" data-trigger="hover" data-placement="top" href="' . route('admin.invoices.downloadSingleInvoice', 'id=' . $payment->payment_id) . '" class="btn btn-sm text-white btn-info mr-1"><i class="fas fa-file-download"></i></a>';
|
||||||
})
|
})
|
||||||
->rawColumns(['actions', 'user'])
|
->rawColumns(['actions', 'user'])
|
||||||
->make(true);
|
->make(true);
|
||||||
|
|
Loading…
Reference in a new issue