151 lines
		
	
	
		
			5.1 KiB
		
	
	
	
		
			PHP
		
	
	
	
			
		
		
	
	
			151 lines
		
	
	
		
			5.1 KiB
		
	
	
	
		
			PHP
		
	
	
	
| <?php
 | |
| 
 | |
| namespace App\Http\Controllers;
 | |
| 
 | |
| use App\Enums\PaymentTransactionStatus;
 | |
| use App\Models\PaymentAggregator;
 | |
| use App\Models\PaymentTransaction;
 | |
| use GuzzleHttp\Client;
 | |
| use Illuminate\Http\Request;
 | |
| use Illuminate\Support\Facades\Lang;
 | |
| use Illuminate\Support\Facades\Log;
 | |
| use function Symfony\Component\Translation\t;
 | |
| 
 | |
| class PaymentController extends Controller
 | |
| {
 | |
|     public function getMethods(Request $request)
 | |
|     {
 | |
|         $aggregator = PaymentAggregator::where('status', 1)->first();
 | |
|         if (!$aggregator) {
 | |
|             return $this->errorResponse(trans('errors.model_not_found', ['model' => 'methods']));
 | |
|         }
 | |
| 
 | |
|         switch (strtolower($aggregator->name)) {
 | |
|             case 'yoomee':
 | |
|                 return app(YoomeeController::class)->getMethods();
 | |
|             case 'yoomeev2':
 | |
|                 return app(YoomeeV2Controller::class)->getMethods();
 | |
|             case 'cinetpay':
 | |
|                 return app(CinetpayController::class)->getMethods();
 | |
|             default:
 | |
|                 return $this->errorResponse(__('errors.unexpected_error'));
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     public function pay(Request $request)
 | |
|     {
 | |
|         $this->validate($request, [
 | |
|             'payment_method' => 'nullable|string',
 | |
|             'aggregator_id' => 'required_without:payment_method|integer|exists:payment_aggregators,id',
 | |
|         ]);
 | |
| 
 | |
|         $payment_method = $request->input('payment_method');
 | |
| 
 | |
|         if($payment_method == 'CREDIT_CARD'){
 | |
|             $aggregator = PaymentAggregator::where('name','like','%stripe%')->firstOrFail();
 | |
|             $data = $request->all();
 | |
|             $request = new Request();
 | |
|             $request->merge(array_merge($data,[
 | |
|                 'payment_method' => 'CREDIT_CARD',
 | |
|                 'aggregator_id' => $aggregator->id
 | |
|             ]));
 | |
|         }else{
 | |
|             $aggregator = PaymentAggregator::findOrFail($request->input('aggregator_id'));
 | |
|         }
 | |
| 
 | |
|         switch (strtolower($aggregator->name)) {
 | |
|             case 'yoomee':
 | |
|                 return app(YoomeeController::class)->pay($request);
 | |
|             case 'yoomeev2':
 | |
|                 return app(YoomeeV2Controller::class)->initPay($request);
 | |
|             case 'cinetpay':
 | |
|                 return app(CinetpayController::class)->pay($request);
 | |
|             case 'stripe':
 | |
|                 return app(StripeController::class)->pay($request);
 | |
|             default:
 | |
|                 return $this->errorResponse(__('errors.unexpected_error'));
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     public function payOut(Request $request)
 | |
|     {
 | |
|         $this->validate($request, [
 | |
|             'network_name' => 'nullable|string',
 | |
|             'payment_method' => 'required|string|in:WALLET,CARD',
 | |
|         ]);
 | |
| 
 | |
|         $paymentMethod = $request->input('payment_method');
 | |
| 
 | |
|         if($paymentMethod === 'WALLET'){
 | |
|             $networkName = strtolower($request->input('network_name'));
 | |
| 
 | |
|             if(str_contains($networkName,'orange') || str_contains($networkName,'mtn')){
 | |
|                 return app(CinetpayController::class)->payOut($request);
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         if($paymentMethod == 'CARD'){
 | |
|             return app(StripeController::class)->payOut($request);
 | |
|         }
 | |
| 
 | |
| 
 | |
|         return $this->errorResponse(__('errors.network_not_taken'));
 | |
|     }
 | |
| 
 | |
|     public function paymentResult(Request $request)
 | |
|     {
 | |
|         if($request->has('transaction_id')){
 | |
|             return $this->successResponse($request->all());
 | |
|         }else{
 | |
|             return $this->errorResponse($request->all());
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     public function checkout(Request $request, $payment_token)
 | |
|     {
 | |
|         $transaction = PaymentTransaction::where('payment_token',$payment_token)->firstOrFail();
 | |
| 
 | |
|         $transaction_id = $transaction->transaction_id;
 | |
|         $method = $transaction->payment_method;
 | |
|         $amount = money($transaction->amount, $transaction->currency)->format(app()->getLocale());
 | |
|         $receiver = config('variables.receiver_name');
 | |
|         $receiver_logo = asset('assets/images/logo.jpeg');
 | |
| 
 | |
| 
 | |
|         if($transaction->status == PaymentTransactionStatus::INITIATED){
 | |
|             return view('checkout',compact('payment_token','method','amount', 'receiver','receiver_logo'));
 | |
|         }
 | |
| 
 | |
|         if($transaction->status == PaymentTransactionStatus::PENDING){
 | |
|             return view('verify-payment',compact('transaction_id','method','amount', 'receiver','receiver_logo'));
 | |
|         }
 | |
| 
 | |
|         $status = $transaction->status == PaymentTransactionStatus::ACCEPTED;
 | |
| 
 | |
| 
 | |
|         return view('payment-status',compact('transaction_id','method','amount', 'receiver','receiver_logo','status'));
 | |
|     }
 | |
| 
 | |
| 
 | |
|     public function checkStatus(Request $request, $transaction_id)
 | |
|     {
 | |
|         $transaction = PaymentTransaction::where('transaction_id',$transaction_id)->firstOrFail();
 | |
| 
 | |
|         if($transaction->status == PaymentTransactionStatus::ACCEPTED){
 | |
|             return $this->successResponse([
 | |
|                 'message' => 'Payment Accepted',
 | |
|                 'transaction_id' => $transaction->transaction_id,
 | |
|                 'status' => $transaction->status
 | |
|             ]);
 | |
| 
 | |
|         }else{
 | |
| 
 | |
|             return $this->errorResponse([
 | |
|                 'message' => 'Payment '.$transaction->status,
 | |
|                 'status' => $transaction->status
 | |
|             ]);
 | |
|         }
 | |
| 
 | |
|     }
 | |
| }
 |