Notes
Notes - notes.io |
<?php
namespace AppHttpControllersAPI;
use AppHttpControllersController;
use IlluminateHttpRequest;
use IlluminateSupportFacadesAuth;
use AppJobsSendNotificationJob;
use appHelpersNotificationHelper;
use Stripe;
use AppModelsTrainerBankDetail;
use AppModelsUser;
use AppModelsPaymentHistory;
use AppHttpRequestsAPIPaymentAddUserPaymentInformationRequest;
use AppHttpRequestsAPIPaymentUserPaymentRequest;
use AppHttpRequestsAPIPaymentCreateTrainerDetailRequest;
use AppHttpRequestsAPIPaymentVerifyTrainerAccountRequest;
use AppHttpResourcesAPIPaymentAlreadyAddTrainerBankDetailResource;
use IlluminateSupportFacadesLog;
// use File;
// use Response;
class PaymentController extends Controller
{
public function createTrainerBankDetails(CreateTrainerDetailRequest $request){
try{
$user = Auth::user();
$stripe = new StripeStripeClient(env('STRIPE_SECRET_KEY'));
if($user->stripe_customer_id != null || $user->stripe_customer_id != ""){
$customer = $stripe->customers->retrieve(
$user->stripe_customer_id
);
} else {
$customer = $stripe->customers->create([
'name' => $user->name,
'email' => $user->email,
'phone' => $user->phone
]);
$user->update(['stripe_customer_id' => $customer['id']]);
}
if($user->stripe_source_bank_id != null || $user->stripe_source_bank_id != ""){
$bankDetailDelete = $stripe->customers->deleteSource(
$user->stripe_customer_id,
$user->stripe_source_bank_id
);
$bank_details = $stripe->tokens->create([
'bank_account' => [
'country' => "US",
'currency' => 'usd',
'account_holder_name' => $request->account_holder_name,
'account_holder_type' => $request->account_holder_type,
'routing_number' => $request->routing_number,
'account_number' => $request->account_number
]
]);
} else {
$bank_details = $stripe->tokens->create([
'bank_account' => [
'country' => "US",
'currency' => 'usd',
'account_holder_name' => $request->account_holder_name,
'account_holder_type' => $request->account_holder_type,
'routing_number' => $request->routing_number,
'account_number' => $request->account_number
]
]);
}
$user->update(['stripe_source_bank_id' => $bank_details['bank_account']['id']]);
$source = $stripe->customers->createSource(
$customer['id'],
['source' => $bank_details['id']]
);
TrainerBankDetail::updateOrCreate(
['user_id' => $user->id],
['country' => 'US',
'currency' => 'USD',
'account_holder_name' => $request->account_holder_name,
'account_holder_type' => $request->account_holder_type,
'routing_number' => $request->routing_number,
'account_number' => $request->account_number,
'is_account_verified' => '0'
]);
return response()->json([
'message' => 'Customer information added successfully!!!',
'status' => 'Success',
]);
} catch(Exception $e) {
Log::channel('paymentcheck')->info('info', [$e->getMessage(), $e->getLine()]);
return response()->json([
'message' => $e->getMessage(),
'status' => 'Error',
]);
}
}
public function verifyTrainerAccount(VerifyTrainerAccountRequest $request){
try{
$user = Auth::user();
$stripe = new StripeStripeClient(env('STRIPE_SECRET_KEY'));
if(!is_numeric($request->first_amount) || !is_numeric($request->second_amount)){
throw new Exception("Invalid amounts.");
}
$verifySource = $stripe->customers->verifySource(
$user->stripe_customer_id,
$user->stripe_source_bank_id,
['amounts' => [$request->first_amount, $request->second_amount]]
);
if($verifySource){
$data = TrainerBankDetail::where('user_id', $user->id)->update(['is_account_verified' => '1']);
if($data){
return response()->json([
'message' => 'Account verified !!',
'status' => 'Success',
]);
}
} else {
throw new Exception("Your bank account number is not valid. Please enter a valid account number.");
}
} catch (Exception $e) {
Log::channel('paymentcheck')->info('info', [$e->getMessage(), $e->getLine()]);
return response()->json([
'message' => $e->getMessage(),
'status' => 'Error',
]);
}
}
public function removeTrainerAccount(){
try{
$user = Auth::user();
$stripe = new StripeStripeClient(env('STRIPE_SECRET_KEY'));
$data = TrainerBankDetail::where('user_id', $user->id)->delete();
if($data){
$stripe->customers->delete(
$user->stripe_customer_id
);
$user->update(['stripe_customer_id' => Null, 'stripe_source_bank_id' => Null]);
return response()->json([
'message' => 'Your account remove successfully!!',
'status' => 'Success',
]);
} else {
return response()->json([
'message' => 'No data found!!',
'status' => 'Error',
]);
}
} catch (Exception $e) {
Log::channel('paymentcheck')->info('info', [$e->getMessage(), $e->getLine()]);
return response()->json([
'message' => $e->getMessage(),
'status' => 'Error',
]);
}
}
public function isAlreadyVerifiedAccount(Request $request){
try{
$is_already_verified = TrainerBankDetail::where('user_id', Auth::user()->id)->orderBy('id', 'DESC')->first();
if($is_already_verified->is_account_verified == 1){
return response()->json([
'message' => 'Account already verified !!',
'status' => 'Success',
]);
} else {
return response()->json([
'message' => 'Account is not verified. Please verify first!!',
'status' => 'Error',
]);
}
} catch (Exception $e) {
Log::channel('paymentcheck')->info('info', [$e->getMessage(), $e->getLine()]);
return response()->json([
'message' => $e->getMessage(),
'status' => 'Error',
]);
}
}
public function getTrainerBankDetail(Request $request, $id){
try{
$is_already_add_bank_details = TrainerBankDetail::where('user_id', $id)->orderBy('id', 'DESC')->first();
if($is_already_add_bank_details){
return response()->json([
'data' => new AlreadyAddTrainerBankDetailResource($is_already_add_bank_details),
'message' => 'Already added account details !!',
'status' => 200,
]);
} else {
return response()->json([
'message' => 'Not added your account details!!',
'status' => 404,
]);
}
} catch (Exception $e) {
Log::channel('paymentcheck')->info('info', [$e->getMessage(), $e->getLine()]);
return response()->json([
'message' => $e->getMessage(),
'status' => 404,
]);
}
}
public function userPayment(UserPaymentRequest $request)
{
try{
StripeStripe::setApiKey(env('STRIPE_SECRET_KEY'));
$amount = $request->amount;
$amount *= 100;
$amount = (int) $amount;
$payment_intent = StripePaymentIntent::create([
'description' => 'Stripe Payment',
'amount' => $amount,
'currency' => 'usd',
'payment_method_types' => ['card'],
]);
$payment_intent_data = $payment_intent;
$intent = $payment_intent->client_secret;
return response()->json([
'client_secret' => $intent,
'data' => $payment_intent_data,
'status' => 'Success',
]);
} catch (Exception $e) {
Log::channel('paymentcheck')->info('info', [$e->getMessage(), $e->getLine()]);
return response()->json([
'message' => $e->getMessage(),
'status' => 404,
]);
}
}
public function addUserPaymentInformation(AddUserPaymentInformationRequest $request){
try {
// $user_id = Auth::user()->id;
$result = PaymentHistory::create($request->authorize());
if($result){
SendNotificationJob::dispatch(['type_id' => $this->notifiType['checkPaymentStatus'], 'data' => $result, 'authUser' => Auth::user()->id ]);
return response()->json([
'payment_data' => $result,
'message' => 'Record saved successfully',
'status' => 'Success',
]);
}
} catch (Exception $e) {
Log::channel('paymentcheck')->info('info', [$e->getMessage(), $e->getLine()]);
return response()->json([
'message' => $e->getMessage(),
'status' => 'Error',
]);
}
}
public function getDetailPaymentData(Request $request){
try {
$user_id = Auth::user()->id;
$result = PaymentHistory::where('user_id', $user_id)->latest('id')->first();
if($result){
return response()->json([
'payment_data' => $result,
'message' => 'Record saved successfully',
'status' => 'Success',
]);
}
} catch (Exception $e) {
Log::channel('paymentcheck')->info('info', [$e->getMessage(), $e->getLine()]);
return response()->json([
'message' => $e->getMessage(),
'status' => 'Error',
]);
}
}
}
|
Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 12 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team