2021-06-05 09:26:32 +00:00
< ? php
namespace App\Http\Controllers\Admin ;
2021-09-09 19:06:08 +00:00
use App\Events\UserUpdateCreditsEvent ;
2021-06-05 09:26:32 +00:00
use App\Http\Controllers\Controller ;
2022-08-27 16:59:07 +00:00
use App\Models\PartnerDiscount ;
2021-06-05 09:26:32 +00:00
use App\Models\Payment ;
2022-01-05 07:57:41 +00:00
use App\Models\Settings ;
2023-01-05 17:01:42 +00:00
use App\Models\ShopProduct ;
2021-06-22 21:22:57 +00:00
use App\Models\User ;
2021-12-17 10:22:29 +00:00
use App\Notifications\ConfirmPaymentNotification ;
2023-01-05 17:01:42 +00:00
use App\Notifications\InvoiceNotification ;
2021-06-23 09:28:57 +00:00
use Exception ;
2021-06-05 09:26:32 +00:00
use Illuminate\Contracts\Foundation\Application ;
use Illuminate\Contracts\View\Factory ;
use Illuminate\Contracts\View\View ;
2021-06-23 09:28:57 +00:00
use Illuminate\Http\JsonResponse ;
2021-06-05 09:26:32 +00:00
use Illuminate\Http\RedirectResponse ;
use Illuminate\Http\Request ;
use Illuminate\Support\Facades\Auth ;
2022-06-07 08:05:19 +00:00
use Illuminate\Support\Facades\DB ;
use Illuminate\Support\Facades\Log ;
2021-11-30 13:52:36 +00:00
use Illuminate\Support\Facades\Storage ;
2021-11-30 17:40:56 +00:00
use LaravelDaily\Invoices\Classes\Buyer ;
use LaravelDaily\Invoices\Classes\InvoiceItem ;
2021-11-26 19:42:38 +00:00
use LaravelDaily\Invoices\Classes\Party ;
2021-11-30 17:40:56 +00:00
use LaravelDaily\Invoices\Invoice ;
2021-06-05 09:26:32 +00:00
use PayPalCheckoutSdk\Core\PayPalHttpClient ;
use PayPalCheckoutSdk\Core\ProductionEnvironment ;
use PayPalCheckoutSdk\Core\SandboxEnvironment ;
use PayPalCheckoutSdk\Orders\OrdersCaptureRequest ;
use PayPalCheckoutSdk\Orders\OrdersCreateRequest ;
use PayPalHttp\HttpException ;
2021-12-13 01:48:02 +00:00
use Stripe\Stripe ;
2022-02-06 20:10:05 +00:00
use Symfony\Component\Intl\Currencies ;
2021-12-13 01:48:02 +00:00
2021-06-05 09:26:32 +00:00
class PaymentController extends Controller
{
2021-06-22 19:25:04 +00:00
/**
* @ return Application | Factory | View
*/
public function index ()
{
2021-06-05 09:26:32 +00:00
return view ( 'admin.payments.index' ) -> with ([
2023-01-05 17:01:42 +00:00
'payments' => Payment :: paginate ( 15 ),
2021-06-05 09:26:32 +00:00
]);
}
/**
2023-01-05 17:01:42 +00:00
* @ param Request $request
* @ param ShopProduct $shopProduct
2021-06-05 09:26:32 +00:00
* @ return Application | Factory | View
*/
2022-05-30 09:07:10 +00:00
public function checkOut ( Request $request , ShopProduct $shopProduct )
2021-06-05 09:26:32 +00:00
{
return view ( 'store.checkout' ) -> with ([
2023-01-05 17:01:42 +00:00
'product' => $shopProduct ,
'discountpercent' => PartnerDiscount :: getDiscount (),
'discountvalue' => PartnerDiscount :: getDiscount () * $shopProduct -> price / 100 ,
'discountedprice' => $shopProduct -> getPriceAfterDiscount (),
'taxvalue' => $shopProduct -> getTaxValue (),
'taxpercent' => $shopProduct -> getTaxPercent (),
'total' => $shopProduct -> getTotalPrice (),
2021-06-05 09:26:32 +00:00
]);
}
/**
2023-01-05 17:01:42 +00:00
* @ param Request $request
* @ param ShopProduct $shopProduct
2021-06-05 09:26:32 +00:00
* @ return RedirectResponse
*/
2023-01-04 17:59:52 +00:00
public function FreePay ( Request $request , ShopProduct $shopProduct )
{
//dd($shopProduct);
//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.' ));
//give product
/** @var User $user */
$user = Auth :: user ();
//not updating server limit
//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 );
}
//skipped the referral commission, because the user did not pay anything.
//not giving client role
//store payment
$payment = Payment :: create ([
'user_id' => $user -> id ,
'payment_id' => uniqid (),
'payment_method' => 'free' ,
'type' => $shopProduct -> type ,
'status' => 'paid' ,
'amount' => $shopProduct -> quantity ,
'price' => $shopProduct -> price - ( $shopProduct -> price * PartnerDiscount :: getDiscount () / 100 ),
'tax_value' => $shopProduct -> getTaxValue (),
'tax_percent' => $shopProduct -> getTaxPercent (),
'total_price' => $shopProduct -> getTotalPrice (),
'currency_code' => $shopProduct -> currency_code ,
'shop_item_product_id' => $shopProduct -> id ,
]);
event ( new UserUpdateCreditsEvent ( $user ));
//not sending an invoice
//redirect back to home
return redirect () -> route ( 'home' ) -> with ( 'success' , __ ( 'Your credit balance has been increased!' ));
}
2021-06-05 09:26:32 +00:00
/**
* @ param Request $request
2022-05-30 09:07:10 +00:00
* @ param ShopProduct $shopProduct
2021-06-05 09:26:32 +00:00
* @ return RedirectResponse
*/
2022-05-30 09:07:10 +00:00
public function PaypalPay ( Request $request , ShopProduct $shopProduct )
2021-06-05 09:26:32 +00:00
{
2023-01-04 17:59:52 +00:00
if ( ! $this -> checkAmount ( $shopProduct -> getTotalPrice (), strtoupper ( $shopProduct -> currency_code ), " paypal " )) 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.' ));
2021-06-05 09:26:32 +00:00
$request = new OrdersCreateRequest ();
$request -> prefer ( 'return=representation' );
$request -> body = [
2023-01-05 17:01:42 +00:00
'intent' => 'CAPTURE' ,
'purchase_units' => [
2021-06-05 09:26:32 +00:00
[
2023-01-05 17:01:42 +00:00
'reference_id' => uniqid (),
'description' => $shopProduct -> display . ( PartnerDiscount :: getDiscount () ? ( ' (' . __ ( 'Discount' ) . ' ' . PartnerDiscount :: getDiscount () . '%)' ) : '' ),
'amount' => [
'value' => $shopProduct -> getTotalPrice (),
2022-05-30 09:07:10 +00:00
'currency_code' => strtoupper ( $shopProduct -> currency_code ),
2021-12-17 09:00:33 +00:00
'breakdown' => [
2023-01-05 17:01:42 +00:00
'item_total' => [
2022-05-30 09:07:10 +00:00
'currency_code' => strtoupper ( $shopProduct -> currency_code ),
2022-08-27 16:59:07 +00:00
'value' => $shopProduct -> getPriceAfterDiscount (),
2021-12-17 09:00:33 +00:00
],
2023-01-05 17:01:42 +00:00
'tax_total' => [
2022-05-30 09:07:10 +00:00
'currency_code' => strtoupper ( $shopProduct -> currency_code ),
'value' => $shopProduct -> getTaxValue (),
2023-01-05 17:01:42 +00:00
],
],
],
],
],
'application_context' => [
'cancel_url' => route ( 'payment.Cancel' ),
'return_url' => route ( 'payment.PaypalSuccess' , [ 'product' => $shopProduct -> id ]),
'brand_name' => config ( 'app.name' , 'Laravel' ),
'shipping_preference' => 'NO_SHIPPING' ,
2021-11-05 06:59:25 +00:00
],
2021-12-12 23:58:47 +00:00
2021-06-05 09:26:32 +00:00
];
try {
// Call API with your client and get a response for your call
$response = $this -> getPayPalClient () -> execute ( $request );
2023-01-05 17:01:42 +00:00
2021-06-05 09:26:32 +00:00
return redirect () -> away ( $response -> result -> links [ 1 ] -> href );
// If call returns body in response, you can get the deserialized version from the result attribute of the response
} catch ( HttpException $ex ) {
echo $ex -> statusCode ;
dd ( json_decode ( $ex -> getMessage ()));
}
}
/**
* @ return PayPalHttpClient
*/
protected function getPayPalClient ()
{
$environment = env ( 'APP_ENV' ) == 'local'
2021-12-12 23:58:47 +00:00
? new SandboxEnvironment ( $this -> getPaypalClientId (), $this -> getPaypalClientSecret ())
: new ProductionEnvironment ( $this -> getPaypalClientId (), $this -> getPaypalClientSecret ());
2021-06-05 09:26:32 +00:00
return new PayPalHttpClient ( $environment );
}
/**
* @ return string
*/
2021-12-12 23:58:47 +00:00
protected function getPaypalClientId ()
2021-06-05 09:26:32 +00:00
{
2023-01-05 17:01:42 +00:00
return env ( 'APP_ENV' ) == 'local' ? config ( 'SETTINGS::PAYMENTS:PAYPAL:SANDBOX_CLIENT_ID' ) : config ( 'SETTINGS::PAYMENTS:PAYPAL:CLIENT_ID' );
2021-06-05 09:26:32 +00:00
}
/**
* @ return string
*/
2021-12-12 23:58:47 +00:00
protected function getPaypalClientSecret ()
2021-06-05 09:26:32 +00:00
{
2023-01-05 17:01:42 +00:00
return env ( 'APP_ENV' ) == 'local' ? config ( 'SETTINGS::PAYMENTS:PAYPAL:SANDBOX_SECRET' ) : config ( 'SETTINGS::PAYMENTS:PAYPAL:SECRET' );
2021-06-05 09:26:32 +00:00
}
/**
2023-01-05 17:01:42 +00:00
* @ param Request $laravelRequest
2021-06-05 09:26:32 +00:00
*/
2021-12-12 23:58:47 +00:00
public function PaypalSuccess ( Request $laravelRequest )
2021-06-05 09:26:32 +00:00
{
2022-05-30 09:07:10 +00:00
/** @var ShopProduct $shopProduct */
$shopProduct = ShopProduct :: findOrFail ( $laravelRequest -> input ( 'product' ));
2021-12-14 01:17:19 +00:00
2021-06-22 21:22:57 +00:00
/** @var User $user */
$user = Auth :: user ();
2021-06-05 09:26:32 +00:00
$request = new OrdersCaptureRequest ( $laravelRequest -> input ( 'token' ));
$request -> prefer ( 'return=representation' );
try {
// Call API with your client and get a response for your call
$response = $this -> getPayPalClient () -> execute ( $request );
if ( $response -> statusCode == 201 || $response -> statusCode == 200 ) {
//update server limit
2022-01-25 21:52:19 +00:00
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' )]);
2021-06-22 19:25:04 +00:00
}
2021-06-05 09:26:32 +00:00
}
2021-12-12 23:58:47 +00:00
2022-05-30 07:23:35 +00:00
//update User with bought item
2023-01-05 17:01:42 +00:00
if ( $shopProduct -> type == 'Credits' ) {
2022-05-30 09:07:10 +00:00
$user -> increment ( 'credits' , $shopProduct -> quantity );
2023-01-05 17:01:42 +00:00
} elseif ( $shopProduct -> type == 'Server slots' ) {
2022-05-30 09:07:10 +00:00
$user -> increment ( 'server_limit' , $shopProduct -> quantity );
2022-05-30 07:23:35 +00:00
}
2022-10-18 21:33:33 +00:00
//give referral commission always
2023-01-05 17:01:42 +00:00
if (( config ( 'SETTINGS::REFERRAL:MODE' ) == 'commission' || config ( 'SETTINGS::REFERRAL:MODE' ) == 'both' ) && $shopProduct -> type == 'Credits' && config ( 'SETTINGS::REFERRAL::ALWAYS_GIVE_COMMISSION' ) == 'true' ) {
if ( $ref_user = DB :: table ( 'user_referrals' ) -> where ( 'registered_user_id' , '=' , $user -> id ) -> first ()) {
2022-10-18 21:33:33 +00:00
$ref_user = User :: findOrFail ( $ref_user -> referral_id );
2023-01-05 17:01:42 +00:00
$increment = number_format ( $shopProduct -> quantity * ( PartnerDiscount :: getCommission ( $ref_user -> id )) / 100 , 0 , '' , '' );
2022-10-18 21:33:33 +00:00
$ref_user -> increment ( 'credits' , $increment );
//LOGS REFERRALS IN THE ACTIVITY LOG
activity ()
-> performedOn ( $user )
-> causedBy ( $ref_user )
2023-01-05 17:01:42 +00:00
-> log ( 'gained ' . $increment . ' ' . config ( 'SETTINGS::SYSTEM:CREDITS_DISPLAY_NAME' ) . ' for commission-referral of ' . $user -> name . ' (ID:' . $user -> id . ')' );
2022-10-18 21:33:33 +00:00
}
}
2022-05-30 07:23:35 +00:00
2022-06-07 08:05:19 +00:00
//update role give Referral-reward
2021-06-22 21:22:57 +00:00
if ( $user -> role == 'member' ) {
$user -> update ([ 'role' => 'client' ]);
2022-06-07 08:05:19 +00:00
2022-10-18 21:33:33 +00:00
//give referral commission only on first purchase
2023-01-05 17:01:42 +00:00
if (( config ( 'SETTINGS::REFERRAL:MODE' ) == 'commission' || config ( 'SETTINGS::REFERRAL:MODE' ) == 'both' ) && $shopProduct -> type == 'Credits' && config ( 'SETTINGS::REFERRAL::ALWAYS_GIVE_COMMISSION' ) == 'false' ) {
if ( $ref_user = DB :: table ( 'user_referrals' ) -> where ( 'registered_user_id' , '=' , $user -> id ) -> first ()) {
2022-06-07 08:05:19 +00:00
$ref_user = User :: findOrFail ( $ref_user -> referral_id );
2023-01-05 17:01:42 +00:00
$increment = number_format ( $shopProduct -> quantity * ( PartnerDiscount :: getCommission ( $ref_user -> id )) / 100 , 0 , '' , '' );
2022-06-07 08:05:19 +00:00
$ref_user -> increment ( 'credits' , $increment );
2022-06-08 08:11:55 +00:00
//LOGS REFERRALS IN THE ACTIVITY LOG
activity ()
-> performedOn ( $user )
-> causedBy ( $ref_user )
2023-01-05 17:01:42 +00:00
-> log ( 'gained ' . $increment . ' ' . config ( 'SETTINGS::SYSTEM:CREDITS_DISPLAY_NAME' ) . ' for commission-referral of ' . $user -> name . ' (ID:' . $user -> id . ')' );
2022-06-07 08:05:19 +00:00
}
}
2021-06-05 09:26:32 +00:00
}
//store payment
2021-06-22 21:22:57 +00:00
$payment = Payment :: create ([
'user_id' => $user -> id ,
2021-06-05 09:26:32 +00:00
'payment_id' => $response -> result -> id ,
2021-12-15 10:41:57 +00:00
'payment_method' => 'paypal' ,
2022-05-30 09:07:10 +00:00
'type' => $shopProduct -> type ,
2021-12-15 11:16:30 +00:00
'status' => 'paid' ,
2022-05-30 09:07:10 +00:00
'amount' => $shopProduct -> quantity ,
2023-01-05 17:01:42 +00:00
'price' => $shopProduct -> price - ( $shopProduct -> price * PartnerDiscount :: getDiscount () / 100 ),
2022-05-30 09:07:10 +00:00
'tax_value' => $shopProduct -> getTaxValue (),
'tax_percent' => $shopProduct -> getTaxPercent (),
'total_price' => $shopProduct -> getTotalPrice (),
'currency_code' => $shopProduct -> currency_code ,
'shop_item_product_id' => $shopProduct -> id ,
2021-06-05 09:26:32 +00:00
]);
2021-09-09 19:06:08 +00:00
event ( new UserUpdateCreditsEvent ( $user ));
2022-01-15 16:53:47 +00:00
//only create invoice if SETTINGS::INVOICE:ENABLED is true
if ( config ( 'SETTINGS::INVOICE:ENABLED' ) == 'true' ) {
2022-05-30 09:07:10 +00:00
$this -> createInvoice ( $user , $payment , 'paid' , $shopProduct -> currency_code );
2022-01-15 16:52:19 +00:00
}
2021-06-05 09:26:32 +00:00
//redirect back to home
2021-12-15 12:33:57 +00:00
return redirect () -> route ( 'home' ) -> with ( 'success' , __ ( 'Your credit balance has been increased!' ));
2021-06-05 09:26:32 +00:00
}
// If call returns body in response, you can get the deserialized version from the result attribute of the response
if ( env ( 'APP_ENV' ) == 'local' ) {
dd ( $response );
} else {
abort ( 500 );
}
} catch ( HttpException $ex ) {
if ( env ( 'APP_ENV' ) == 'local' ) {
echo $ex -> statusCode ;
dd ( $ex -> getMessage ());
} else {
abort ( 422 );
}
}
}
/**
2023-01-05 17:01:42 +00:00
* @ param Request $request
2021-06-05 09:26:32 +00:00
*/
2021-12-14 01:17:19 +00:00
public function Cancel ( Request $request )
2021-06-05 09:26:32 +00:00
{
2021-11-05 07:45:29 +00:00
return redirect () -> route ( 'store.index' ) -> with ( 'success' , 'Payment was Canceled' );
2021-06-05 09:26:32 +00:00
}
2021-06-23 09:28:57 +00:00
2021-12-17 09:00:33 +00:00
/**
2023-01-05 17:01:42 +00:00
* @ param Request $request
* @ param ShopProduct $shopProduct
2021-12-13 01:48:02 +00:00
* @ return RedirectResponse
*/
2022-05-30 09:07:10 +00:00
public function StripePay ( Request $request , ShopProduct $shopProduct )
2021-12-13 01:48:02 +00:00
{
2023-01-04 17:59:52 +00:00
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.' ));
2021-12-15 09:56:58 +00:00
$stripeClient = $this -> getStripeClient ();
2021-12-15 10:41:57 +00:00
$request = $stripeClient -> checkout -> sessions -> create ([
2021-12-13 01:48:02 +00:00
'line_items' => [
[
2021-12-17 09:00:33 +00:00
'price_data' => [
2022-05-30 09:07:10 +00:00
'currency' => $shopProduct -> currency_code ,
2021-12-17 09:00:33 +00:00
'product_data' => [
2023-01-05 17:01:42 +00:00
'name' => $shopProduct -> display . ( PartnerDiscount :: getDiscount () ? ( ' (' . __ ( 'Discount' ) . ' ' . PartnerDiscount :: getDiscount () . '%)' ) : '' ),
2022-05-30 09:07:10 +00:00
'description' => $shopProduct -> description ,
2021-12-17 09:00:33 +00:00
],
2022-08-27 16:59:07 +00:00
'unit_amount_decimal' => round ( $shopProduct -> getPriceAfterDiscount () * 100 , 2 ),
2021-12-17 09:00:33 +00:00
],
'quantity' => 1 ,
2021-12-13 01:48:02 +00:00
],
[
'price_data' => [
2022-05-30 09:07:10 +00:00
'currency' => $shopProduct -> currency_code ,
2021-12-13 01:48:02 +00:00
'product_data' => [
2022-08-27 16:59:07 +00:00
'name' => __ ( 'Tax' ),
2023-01-05 17:01:42 +00:00
'description' => $shopProduct -> getTaxPercent () . '%' ,
2021-12-13 01:48:02 +00:00
],
2022-05-30 09:07:10 +00:00
'unit_amount_decimal' => round ( $shopProduct -> getTaxValue (), 2 ) * 100 ,
2021-12-17 09:00:33 +00:00
],
'quantity' => 1 ,
2023-01-05 17:01:42 +00:00
],
2021-12-13 01:48:02 +00:00
],
2021-12-14 00:09:27 +00:00
2021-12-13 01:48:02 +00:00
'mode' => 'payment' ,
2023-01-05 17:01:42 +00:00
'payment_method_types' => str_getcsv ( config ( 'SETTINGS::PAYMENTS:STRIPE:METHODS' )),
'success_url' => route ( 'payment.StripeSuccess' , [ 'product' => $shopProduct -> id ]) . '&session_id={CHECKOUT_SESSION_ID}' ,
2021-12-15 09:56:58 +00:00
'cancel_url' => route ( 'payment.Cancel' ),
2021-12-17 09:00:33 +00:00
]);
2021-12-13 01:48:02 +00:00
2021-12-17 09:00:33 +00:00
return redirect ( $request -> url , 303 );
2021-12-13 01:48:02 +00:00
}
2021-12-14 01:17:19 +00:00
/**
2023-01-05 17:01:42 +00:00
* @ param Request $request
2021-12-14 01:17:19 +00:00
*/
public function StripeSuccess ( Request $request )
{
2022-05-30 09:07:10 +00:00
/** @var ShopProduct $shopProduct */
$shopProduct = ShopProduct :: findOrFail ( $request -> input ( 'product' ));
2021-12-14 01:17:19 +00:00
/** @var User $user */
$user = Auth :: user ();
2021-12-15 09:56:58 +00:00
$stripeClient = $this -> getStripeClient ();
2021-12-14 01:17:19 +00:00
2021-12-17 09:00:33 +00:00
try {
2021-12-16 17:03:38 +00:00
//get stripe data
$paymentSession = $stripeClient -> checkout -> sessions -> retrieve ( $request -> input ( 'session_id' ));
$paymentIntent = $stripeClient -> paymentIntents -> retrieve ( $paymentSession -> payment_intent );
2021-12-14 01:17:19 +00:00
2021-12-16 17:03:38 +00:00
//get DB entry of this payment ID if existing
$paymentDbEntry = Payment :: where ( 'payment_id' , $paymentSession -> payment_intent ) -> count ();
2021-12-14 01:17:19 +00:00
2021-12-16 17:03:38 +00:00
// check if payment is 100% completed and payment does not exist in db already
2023-01-05 17:01:42 +00:00
if ( $paymentSession -> status == 'complete' && $paymentIntent -> status == 'succeeded' && $paymentDbEntry == 0 ) {
2021-12-16 17:03:38 +00:00
//update server limit
2022-01-25 21:52:19 +00:00
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' )]);
2021-12-16 17:03:38 +00:00
}
2021-12-14 01:17:19 +00:00
}
2022-05-30 07:23:35 +00:00
//update User with bought item
2023-01-05 17:01:42 +00:00
if ( $shopProduct -> type == 'Credits' ) {
2022-05-30 09:07:10 +00:00
$user -> increment ( 'credits' , $shopProduct -> quantity );
2023-01-05 17:01:42 +00:00
} elseif ( $shopProduct -> type == 'Server slots' ) {
2022-05-30 09:07:10 +00:00
$user -> increment ( 'server_limit' , $shopProduct -> quantity );
2022-05-30 07:23:35 +00:00
}
2022-06-07 08:05:19 +00:00
//update role give Referral-reward
2021-12-16 17:03:38 +00:00
if ( $user -> role == 'member' ) {
$user -> update ([ 'role' => 'client' ]);
2022-06-07 08:05:19 +00:00
2023-01-05 17:01:42 +00:00
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 ()) {
2022-06-07 08:05:19 +00:00
$ref_user = User :: findOrFail ( $ref_user -> referral_id );
2023-01-05 17:01:42 +00:00
$increment = number_format ( $shopProduct -> quantity / 100 * config ( 'SETTINGS::REFERRAL:PERCENTAGE' ), 0 , '' , '' );
2022-06-07 08:05:19 +00:00
$ref_user -> increment ( 'credits' , $increment );
2022-06-08 08:11:55 +00:00
//LOGS REFERRALS IN THE ACTIVITY LOG
activity ()
-> performedOn ( $user )
-> causedBy ( $ref_user )
2023-01-05 17:01:42 +00:00
-> log ( 'gained ' . $increment . ' ' . config ( 'SETTINGS::SYSTEM:CREDITS_DISPLAY_NAME' ) . ' for commission-referral of ' . $user -> name . ' (ID:' . $user -> id . ')' );
2022-06-07 08:05:19 +00:00
}
}
2021-12-16 17:03:38 +00:00
}
2021-12-14 01:17:19 +00:00
2021-12-17 09:00:33 +00:00
//store paid payment
2021-12-16 17:03:38 +00:00
$payment = Payment :: create ([
'user_id' => $user -> id ,
'payment_id' => $paymentSession -> payment_intent ,
'payment_method' => 'stripe' ,
2022-05-30 09:07:10 +00:00
'type' => $shopProduct -> type ,
2021-12-16 17:03:38 +00:00
'status' => 'paid' ,
2022-05-30 09:07:10 +00:00
'amount' => $shopProduct -> quantity ,
2023-01-05 17:01:42 +00:00
'price' => $shopProduct -> price - ( $shopProduct -> price * PartnerDiscount :: getDiscount () / 100 ),
2022-05-30 09:07:10 +00:00
'tax_value' => $shopProduct -> getTaxValue (),
'total_price' => $shopProduct -> getTotalPrice (),
'tax_percent' => $shopProduct -> getTaxPercent (),
'currency_code' => $shopProduct -> currency_code ,
'shop_item_product_id' => $shopProduct -> id ,
2021-12-16 17:03:38 +00:00
]);
//payment notification
$user -> notify ( new ConfirmPaymentNotification ( $payment ));
event ( new UserUpdateCreditsEvent ( $user ));
2022-01-15 16:53:47 +00:00
//only create invoice if SETTINGS::INVOICE:ENABLED is true
if ( config ( 'SETTINGS::INVOICE:ENABLED' ) == 'true' ) {
2022-05-30 09:07:10 +00:00
$this -> createInvoice ( $user , $payment , 'paid' , $shopProduct -> currency_code );
2022-01-15 16:52:19 +00:00
}
2021-12-17 10:22:29 +00:00
2021-12-16 17:03:38 +00:00
//redirect back to home
return redirect () -> route ( 'home' ) -> with ( 'success' , __ ( 'Your credit balance has been increased!' ));
2021-12-17 09:00:33 +00:00
} else {
2023-01-05 17:01:42 +00:00
if ( $paymentIntent -> status == 'processing' ) {
2021-12-17 09:00:33 +00:00
//store processing payment
$payment = Payment :: create ([
'user_id' => $user -> id ,
'payment_id' => $paymentSession -> payment_intent ,
'payment_method' => 'stripe' ,
2022-05-30 09:07:10 +00:00
'type' => $shopProduct -> type ,
2021-12-17 09:00:33 +00:00
'status' => 'processing' ,
2022-05-30 09:07:10 +00:00
'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 ,
2021-12-17 09:00:33 +00:00
]);
2022-01-15 16:53:47 +00:00
//only create invoice if SETTINGS::INVOICE:ENABLED is true
if ( config ( 'SETTINGS::INVOICE:ENABLED' ) == 'true' ) {
2022-05-30 09:07:10 +00:00
$this -> createInvoice ( $user , $payment , 'paid' , $shopProduct -> currency_code );
2022-01-15 16:52:19 +00:00
}
2021-12-17 10:22:29 +00:00
2021-12-16 17:03:38 +00:00
//redirect back to home
return redirect () -> route ( 'home' ) -> with ( 'success' , __ ( 'Your payment is being processed!' ));
}
2023-01-05 17:01:42 +00:00
if ( $paymentDbEntry == 0 && $paymentIntent -> status != 'processing' ) {
2021-12-16 17:03:38 +00:00
$stripeClient -> paymentIntents -> cancel ( $paymentIntent -> id );
//redirect back to home
return redirect () -> route ( 'home' ) -> with ( 'success' , __ ( 'Your payment has been canceled!' ));
2021-12-17 09:00:33 +00:00
} else {
2021-12-16 17:03:38 +00:00
abort ( 402 );
}
}
2021-12-17 09:00:33 +00:00
} catch ( HttpException $ex ) {
2021-12-14 01:17:19 +00:00
if ( env ( 'APP_ENV' ) == 'local' ) {
echo $ex -> statusCode ;
dd ( $ex -> getMessage ());
} else {
abort ( 422 );
}
}
}
2021-12-16 17:03:38 +00:00
/**
2023-01-05 17:01:42 +00:00
* @ param Request $request
2021-12-16 17:03:38 +00:00
*/
2021-12-17 10:22:29 +00:00
protected function handleStripePaymentSuccessHook ( $paymentIntent )
2021-12-16 17:03:38 +00:00
{
2021-12-17 09:00:33 +00:00
try {
// Get payment db entry
$payment = Payment :: where ( 'payment_id' , $paymentIntent -> id ) -> first ();
$user = User :: where ( 'id' , $payment -> user_id ) -> first ();
2021-12-16 17:03:38 +00:00
2021-12-17 09:00:33 +00:00
if ( $paymentIntent -> status == 'succeeded' && $payment -> status == 'processing' ) {
2022-05-30 07:23:35 +00:00
2021-12-17 09:00:33 +00:00
//update server limit
2022-01-25 21:52:19 +00:00
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' )]);
2021-12-17 09:00:33 +00:00
}
}
2022-05-30 07:23:35 +00:00
//update User with bought item
2023-01-05 17:01:42 +00:00
if ( $shopProduct -> type == 'Credits' ) {
2022-05-30 09:07:10 +00:00
$user -> increment ( 'credits' , $shopProduct -> quantity );
2023-01-05 17:01:42 +00:00
} elseif ( $shopProduct -> type == 'Server slots' ) {
2022-05-30 09:07:10 +00:00
$user -> increment ( 'server_limit' , $shopProduct -> quantity );
2022-05-30 07:23:35 +00:00
}
2021-12-17 09:00:33 +00:00
2022-06-07 08:05:19 +00:00
//update role give Referral-reward
2021-12-17 09:00:33 +00:00
if ( $user -> role == 'member' ) {
$user -> update ([ 'role' => 'client' ]);
2022-06-07 08:05:19 +00:00
2023-01-05 17:01:42 +00:00
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 ()) {
2022-06-07 08:05:19 +00:00
$ref_user = User :: findOrFail ( $ref_user -> referral_id );
2023-01-05 17:01:42 +00:00
$increment = number_format ( $shopProduct -> quantity / 100 * config ( 'SETTINGS::REFERRAL:PERCENTAGE' ), 0 , '' , '' );
2022-06-07 08:05:19 +00:00
$ref_user -> increment ( 'credits' , $increment );
2022-06-08 08:11:55 +00:00
//LOGS REFERRALS IN THE ACTIVITY LOG
activity ()
-> performedOn ( $user )
-> causedBy ( $ref_user )
2023-01-05 17:01:42 +00:00
-> log ( 'gained ' . $increment . ' ' . config ( 'SETTINGS::SYSTEM:CREDITS_DISPLAY_NAME' ) . ' for commission-referral of ' . $user -> name . ' (ID:' . $user -> id . ')' );
2022-06-07 08:05:19 +00:00
}
}
2021-12-17 09:00:33 +00:00
}
//update payment db entry status
$payment -> update ([ 'status' => 'paid' ]);
2021-12-16 17:03:38 +00:00
2021-12-17 09:00:33 +00:00
//payment notification
$user -> notify ( new ConfirmPaymentNotification ( $payment ));
event ( new UserUpdateCreditsEvent ( $user ));
2021-12-17 10:22:29 +00:00
2022-01-15 16:53:47 +00:00
//only create invoice if SETTINGS::INVOICE:ENABLED is true
if ( config ( 'SETTINGS::INVOICE:ENABLED' ) == 'true' ) {
2022-02-06 20:10:05 +00:00
$this -> createInvoice ( $user , $payment , 'paid' , strtoupper ( $paymentIntent -> currency ));
2022-01-15 16:52:19 +00:00
}
2021-12-17 09:00:33 +00:00
}
} catch ( HttpException $ex ) {
abort ( 422 );
}
}
2021-12-16 17:03:38 +00:00
2021-12-17 09:00:33 +00:00
/**
2023-01-05 17:01:42 +00:00
* @ param Request $request
2021-12-17 09:00:33 +00:00
*/
public function StripeWebhooks ( Request $request )
{
\Stripe\Stripe :: setApiKey ( $this -> getStripeSecret ());
2021-12-16 17:03:38 +00:00
try {
$payload = @ file_get_contents ( 'php://input' );
$sig_header = $request -> header ( 'Stripe-Signature' );
$event = null ;
$event = \Stripe\Webhook :: constructEvent (
2021-12-17 09:00:33 +00:00
$payload ,
$sig_header ,
$this -> getStripeEndpointSecret ()
2021-12-16 17:03:38 +00:00
);
2021-12-17 09:00:33 +00:00
} catch ( \UnexpectedValueException $e ) {
2021-12-16 17:03:38 +00:00
// Invalid payload
abort ( 400 );
2021-12-17 09:00:33 +00:00
} catch ( \Stripe\Exception\SignatureVerificationException $e ) {
2021-12-16 17:03:38 +00:00
// Invalid signature
abort ( 400 );
}
// Handle the event
switch ( $event -> type ) {
case 'payment_intent.succeeded' :
$paymentIntent = $event -> data -> object ; // contains a \Stripe\PaymentIntent
2021-12-17 10:22:29 +00:00
$this -> handleStripePaymentSuccessHook ( $paymentIntent );
2021-12-16 17:03:38 +00:00
break ;
default :
2023-01-05 17:01:42 +00:00
echo 'Received unknown event type ' . $event -> type ;
2021-12-16 17:03:38 +00:00
}
}
2021-12-15 11:31:16 +00:00
/**
* @ return \Stripe\StripeClient
2021-12-15 09:56:58 +00:00
*/
protected function getStripeClient ()
{
2021-12-15 11:31:16 +00:00
return new \Stripe\StripeClient ( $this -> getStripeSecret ());
2021-12-13 01:48:02 +00:00
}
/**
* @ return string
*/
2021-12-15 09:56:58 +00:00
protected function getStripeSecret ()
2021-12-13 01:48:02 +00:00
{
2021-12-15 11:24:03 +00:00
return env ( 'APP_ENV' ) == 'local'
2023-01-05 17:01:42 +00:00
? config ( 'SETTINGS::PAYMENTS:STRIPE:TEST_SECRET' )
: config ( 'SETTINGS::PAYMENTS:STRIPE:SECRET' );
2021-12-13 01:48:02 +00:00
}
2021-12-16 17:03:38 +00:00
/**
* @ return string
*/
protected function getStripeEndpointSecret ()
{
return env ( 'APP_ENV' ) == 'local'
2023-01-05 17:01:42 +00:00
? config ( 'SETTINGS::PAYMENTS:STRIPE:ENDPOINT_TEST_SECRET' )
: config ( 'SETTINGS::PAYMENTS:STRIPE:ENDPOINT_SECRET' );
2021-12-16 17:03:38 +00:00
}
2022-02-06 20:10:05 +00:00
protected function createInvoice ( $user , $payment , $paymentStatus , $currencyCode )
2021-12-17 10:22:29 +00:00
{
2022-05-30 09:07:10 +00:00
$shopProduct = ShopProduct :: where ( 'id' , $payment -> shop_item_product_id ) -> first ();
2021-12-17 10:22:29 +00:00
//create invoice
2023-01-05 17:01:42 +00:00
$lastInvoiceID = \App\Models\Invoice :: where ( 'invoice_name' , 'like' , '%' . now () -> format ( 'mY' ) . '%' ) -> count ( 'id' );
2021-12-17 10:22:29 +00:00
$newInvoiceID = $lastInvoiceID + 1 ;
$logoPath = storage_path ( 'app/public/logo.png' );
$seller = new Party ([
2023-01-05 17:01:42 +00:00
'name' => config ( 'SETTINGS::INVOICE:COMPANY_NAME' ),
'phone' => config ( 'SETTINGS::INVOICE:COMPANY_PHONE' ),
'address' => config ( 'SETTINGS::INVOICE:COMPANY_ADDRESS' ),
'vat' => config ( 'SETTINGS::INVOICE:COMPANY_VAT' ),
2021-12-17 10:22:29 +00:00
'custom_fields' => [
2023-01-05 17:01:42 +00:00
'E-Mail' => config ( 'SETTINGS::INVOICE:COMPANY_MAIL' ),
'Web' => config ( 'SETTINGS::INVOICE:COMPANY_WEBSITE' ),
2021-12-17 10:22:29 +00:00
],
]);
$customer = new Buyer ([
'name' => $user -> name ,
'custom_fields' => [
'E-Mail' => $user -> email ,
'Client ID' => $user -> id ,
],
]);
$item = ( new InvoiceItem ())
2022-05-30 09:07:10 +00:00
-> title ( $shopProduct -> description )
-> pricePerUnit ( $shopProduct -> price );
2021-12-17 10:22:29 +00:00
2021-12-22 12:48:21 +00:00
$notes = [
2023-01-05 17:01:42 +00:00
__ ( 'Payment method' ) . ': ' . $payment -> payment_method ,
2021-12-22 12:48:21 +00:00
];
2023-01-05 17:01:42 +00:00
$notes = implode ( '<br>' , $notes );
2021-12-22 12:48:21 +00:00
2021-12-17 10:22:29 +00:00
$invoice = Invoice :: make ()
-> template ( 'controlpanel' )
2023-01-05 17:01:42 +00:00
-> name ( __ ( 'Invoice' ))
2021-12-17 10:22:29 +00:00
-> buyer ( $customer )
-> seller ( $seller )
2022-08-27 18:35:17 +00:00
-> discountByPercent ( PartnerDiscount :: getDiscount ())
2022-05-30 09:07:10 +00:00
-> taxRate ( floatval ( $shopProduct -> getTaxPercent ()))
2021-12-17 10:22:29 +00:00
-> shipping ( 0 )
-> addItem ( $item )
-> status ( __ ( $paymentStatus ))
-> series ( now () -> format ( 'mY' ))
2023-01-05 17:01:42 +00:00
-> delimiter ( '-' )
2021-12-17 10:22:29 +00:00
-> sequence ( $newInvoiceID )
2023-01-05 17:01:42 +00:00
-> serialNumberFormat ( config ( 'SETTINGS::INVOICE:PREFIX' ) . '{DELIMITER}{SERIES}{SEQUENCE}' )
2022-02-06 20:10:05 +00:00
-> currencyCode ( $currencyCode )
-> currencySymbol ( Currencies :: getSymbol ( $currencyCode ))
2021-12-22 12:48:21 +00:00
-> notes ( $notes );
2021-12-17 10:22:29 +00:00
if ( file_exists ( $logoPath )) {
$invoice -> logo ( $logoPath );
}
2021-12-16 17:03:38 +00:00
2021-12-17 10:22:29 +00:00
//Save the invoice in "storage\app\invoice\USER_ID\YEAR"
2023-01-05 17:01:42 +00:00
$invoice -> filename = $invoice -> getSerialNumber () . '.pdf' ;
2021-12-17 10:22:29 +00:00
$invoice -> render ();
2023-01-05 17:01:42 +00:00
Storage :: disk ( 'local' ) -> put ( 'invoice/' . $user -> id . '/' . now () -> format ( 'Y' ) . '/' . $invoice -> filename , $invoice -> output );
2021-12-17 09:00:33 +00:00
2021-12-17 10:22:29 +00:00
\App\Models\Invoice :: create ([
'invoice_user' => $user -> id ,
'invoice_name' => $invoice -> getSerialNumber (),
'payment_id' => $payment -> payment_id ,
]);
//Send Invoice per Mail
$user -> notify ( new InvoiceNotification ( $invoice , $user , $payment ));
}
2021-12-17 09:00:33 +00:00
2023-01-04 17:59:52 +00:00
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 ];
}
2021-06-23 09:28:57 +00:00
/**
* @ return JsonResponse | mixed
2023-01-05 17:01:42 +00:00
*
2021-06-23 09:28:57 +00:00
* @ throws Exception
*/
public function dataTable ()
{
$query = Payment :: with ( 'user' );
return datatables ( $query )
2023-01-05 23:13:50 +00:00
2023-01-05 23:01:23 +00:00
-> addColumn ( 'user' , function ( Payment $payment ) {
2022-08-27 17:44:27 +00:00
return
( $payment -> user ) ? '<a href="' . route ( 'admin.users.show' , $payment -> user -> id ) . '">' . $payment -> user -> name . '</a>' : __ ( 'Unknown user' );
2023-01-05 23:13:50 +00:00
2021-06-23 09:28:57 +00:00
})
-> editColumn ( 'price' , function ( Payment $payment ) {
2021-11-05 08:00:18 +00:00
return $payment -> formatToCurrency ( $payment -> price );
})
-> editColumn ( 'tax_value' , function ( Payment $payment ) {
return $payment -> formatToCurrency ( $payment -> tax_value );
})
2021-12-15 11:16:30 +00:00
-> editColumn ( 'tax_percent' , function ( Payment $payment ) {
2023-01-05 17:01:42 +00:00
return $payment -> tax_percent . ' %' ;
2021-12-15 11:16:30 +00:00
})
2021-11-05 08:00:18 +00:00
-> editColumn ( 'total_price' , function ( Payment $payment ) {
return $payment -> formatToCurrency ( $payment -> total_price );
2021-06-23 09:28:57 +00:00
})
2021-12-15 11:16:30 +00:00
2021-06-23 09:28:57 +00:00
-> editColumn ( 'created_at' , function ( Payment $payment ) {
2023-01-05 23:01:23 +00:00
return [ 'display' => $payment -> created_at ? $payment -> created_at -> diffForHumans () : '' ,
'raw' => $payment -> created_at ? strtotime ( $payment -> created_at ) : '' ];
2021-06-23 09:28:57 +00:00
})
2022-01-04 08:05:53 +00:00
-> addColumn ( 'actions' , function ( Payment $payment ) {
2023-01-05 17:01:42 +00:00
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>' ;
2022-01-04 08:05:53 +00:00
})
2022-08-27 17:44:27 +00:00
-> rawColumns ([ 'actions' , 'user' ])
2022-01-04 08:05:53 +00:00
-> make ( true );
2021-06-23 09:28:57 +00:00
}
2023-01-05 17:01:42 +00:00
}