Viewing file: PaymentGatewayController.php (15.3 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
namespace Modules\PaymentGateway\Http\Controllers;
use App\Events\SendMail; use App\Models\BillingRequest; use App\Models\Customer; use App\Models\CustomerWallet; use App\Models\Deposit; use App\Models\Transaction; use Illuminate\Http\Request; use Illuminate\Routing\Controller; use Illuminate\Support\Facades\Log; use Modules\PaymentGateway\PaymentGatewayProvider\ProcessPayment; use Omnipay\Omnipay;
class PaymentGatewayController extends Controller { public function index() { return view('paymentgateway::index'); }
public function process(Request $request) { if (!$request->confirm){ return redirect()->route('customer.deposit.index')->withErrors(['msg' => trans('You have to checked the checkbox confirmation!')]); } if (!$request->amount){ return redirect()->route('customer.deposit.index')->withErrors(['msg' => trans('Invalid amount')]); } $data['amount'] = $request->amount; return view('paymentgateway::process', $data); }
public function payNow(Request $request) { $user = auth()->guard('customer')->user(); if ($request->amount <= 0) { return redirect()->route('customer.deposit.index')->withErrors(['msg' => trans('Invalid amount')]); } $preDeposit = Deposit::where(['customer_id' => $user->id, 'status' => 'pending'])->first(); if ($preDeposit) { return redirect()->route('customer.deposit.index')->with('fail', trans('You already have a pending request. Please wait for the admin reply.')); } if ($request->payment_type == 'offline') { $deposit = new Deposit(); $deposit->customer_id = $user->id; $deposit->amount = $request->amount; $deposit->payment_status = 'unpaid'; $deposit->payment_type = $request->payment_type; $deposit->save(); return redirect()->route('customer.deposit.index')->with('success', trans('Congratulations! Your requested amount successfully deposit')); } $deposit = new Deposit(); $deposit->customer_id = $user->id; $deposit->amount = $request->amount; $deposit->payment_status = 'unpaid'; $deposit->payment_type = $request->payment_type; $deposit->save();
try { $emailTemplate = get_email_template('plan_request'); if ($emailTemplate) { $regTemp = str_replace('{customer_name}', $user->first_name.' '.$user->last_name, $emailTemplate->body); SendMail::dispatch($user->email, $emailTemplate->subject, $regTemp); }
if (!in_array($request->payment_type,['flutterwave','vogue_pay', 'offline', 'coinpay'])) { $processPayment = new ProcessPayment(); $processResult = $processPayment->set_gateway($request->payment_type) ->request($request) ->deposit($deposit) ->process(); if ($processResult->error_message) { return redirect()->route('customer.deposit.index')->withErrors(['failed' => $processResult->error_message]); } if ($processResult->return_view) { return $processResult->return_view; } elseif ($processResult->will_redirect && $processResult->redirect_url) { return redirect()->to($processResult->redirect_url); } else { return redirect()->route('customer.deposit.index')->with('success', trans('Congratulations! Your requested amount successfully deposit')); } } else{ return redirect()->route('customer.deposit.index')->with('success', trans('Congratulations! Your requested amount successfully deposit')); } } catch (\Exception $ex) { dd($ex); Log::error($ex); return redirect()->route('customer.deposit.index')->withErrors(['msg' => trans('Invalid Payment')]); }
//end
}
public function paymentSuccess(Request $request) {
try { $credentials = json_decode(get_settings('payment_gateway'));
if (!isset($credentials) || !isset($credentials->paypal_payment_mode) || !$credentials->paypal_payment_mode || (!$credentials->paypal_client_id || !$credentials->paypal_client_secret)) { throw new \Exception('Credentials not found. Please contact with the administrator'); } $mode = isset($credentials->paypal_payment_mode) && $credentials->paypal_payment_mode && $credentials->paypal_payment_mode == 'live' ? 'false' : 'true'; $gateway = Omnipay::create('PayPal_Rest'); $gateway->setClientId($credentials->paypal_client_id); $gateway->setSecret($credentials->paypal_client_secret); $gateway->setTestMode($mode);
if ($request->paymentId && $request->PayerID) { $transaction = $gateway->completePurchase(array( 'payer_id' => $request->input('PayerID'), 'transactionReference' => $request->input('paymentId'), )); $response = $transaction->send();
if (!$response->isSuccessful()) { return redirect()->route('customer.deposit.index')->withErrors(['msg' => trans('Invalid payment')]); } } else { return redirect()->route('customer.deposit.index')->withErrors(['msg' => trans('Transaction is declined')]); }
$paymentId = $request->paymentId; $user_plan_id = $request->plan; $user = $request->user; if (!$paymentId || !$user_plan_id || !$user) { return redirect()->route('customer.deposit.index')->withErrors(['msg' => trans('Invalid payment')]); }
if (!$response) return redirect()->route('customer.deposit.index')->withErrors(['msg' => trans('Invalid payment')]);
try {
$customer = Customer::where('id', $user)->first();
if(!$customer){ return redirect()->route('customer.deposit.index')->withErrors(['failed'=>'Invalid Customer']); }
$depositRequest = Deposit::where('id', $user_plan_id)->firstOrFail(); if (!$depositRequest) { return redirect()->route('customer.deposit.index')->withErrors(['failed' => 'Invalid Customer']); }
$customerWallet = CustomerWallet::where('customer_id', $depositRequest->customer_id)->first();
if($customerWallet) { Transaction::create([ 'customer_id' => $customerWallet->customer_id, 'amount' => $depositRequest->amount, 'payment_type' => $depositRequest->payment_type, 'type' => 'deposit', 'payment_status' => 'paid', 'status' => 'approved', ]); $totalAmount = $customerWallet->amount + $depositRequest->amount; $depositRequest->update(['status' => 'approved']); $depositRequest->update(['payment_status' => 'paid']); $customerWallet->update(['amount' => $totalAmount]); }
return redirect()->route('customer.deposit.index')->with('success', trans('Congratulations! Your plan successfully changed'));
} catch (\Exception $ex) { return redirect()->route('customer.deposit.index')->withErrors(['failed' => 'Something went wrong try again']); } return redirect()->route('customer.deposit.index')->with('success', trans('Congratulations! Your plan successfully changed')); } catch (\Exception $ex) { return redirect()->route('customer.deposit.index')->withErrors(['errors' => $ex->getMessage()]); } }
function PayPalPayment($plan, $planReq) { $credentials = json_decode(get_settings('payment_gateway')); if (!isset($credentials) || (!$credentials->paypal_client_id || !$credentials->paypal_client_secret)) { return redirect()->route('customer.deposit.index')->withErrors(['msg' => trans('Invalid payment')]); } $apiContext = $this->getPayPalApiContext($credentials->paypal_client_id, $credentials->paypal_client_secret); $payer = new \PayPal\Api\Payer(); $payer->setPaymentMethod('paypal');
$amount = new \PayPal\Api\Amount(); $amount->setTotal($plan->price); $amount->setCurrency('USD'); //TODO:: get the currency
$transaction = new \PayPal\Api\Transaction(); $transaction->setAmount($amount);
$redirectUrls = new \PayPal\Api\RedirectUrls(); $redirectUrls->setReturnUrl(route('paymentgateway::payment.process.success', ['plan' => $planReq->id, 'user' => $planReq->customer_id])) ->setCancelUrl(route('paymentgateway::payment.process.cancel'));
$payment = new \PayPal\Api\Payment(); $payment->setIntent('sale') ->setPayer($payer) ->setTransactions(array($transaction)) ->setRedirectUrls($redirectUrls);
try { $payment->create($apiContext); return $payment; } catch (\PayPal\Exception\PayPalConnectionException $ex) { // This will print the detailed information on the exception. //REALLY HELPFUL FOR DEBUGGING Log::error($ex->getData()); } return null; }
function getPayPalApiContext($client_id, $secret_key) {
return new \PayPal\Rest\ApiContext( new \PayPal\Auth\OAuthTokenCredential( $client_id, // ClientID $secret_key // ClientSecret ) ); }
public function paymentCancel() { return redirect()->route('customer.deposit.index')->withErrors(['msg' => trans('Payment has been cancelled')]); }
public function checkValidPayment(Request $request){ $plan = Plan::where('id', $request->plan_id)->first(); if ($plan->price==$request->price){ return response()->json(['status'=>'success']); }else{ return abort(404); } }
function edie($error_msg) { \Log::error($error_msg); exit(); }
public function webhook(Request $request){ $settings = json_decode(get_settings('payment_gateway')); $merchant_id = isset($settings->merchate_id)?$settings->merchate_id:''; $ipn_secret = isset($settings->ipn_secret)?$settings->ipn_secret:'';
$txn_id = isset($request->txn_id)?$request->txn_id:''; if($txn_id){ $payment = BillingRequest::where("transaction_id", $txn_id)->first(); $plan = Plan::where("id", $payment->plan_id)->first(); }else{ abort('404'); }
$order_total = isset($plan->price)?$plan->price:0; //BTC if (!isset($request->ipn_mode) || $request->ipn_mode != 'hmac') { $this->edie("IPN Mode is not HMAC"); }
if (!isset($_SERVER['HTTP_HMAC']) || empty($_SERVER['HTTP_HMAC'])) { $this->edie("No HMAC Signature Sent."); }
if (!isset($request->merchant) || $request->merchant != trim($merchant_id)) { $this->edie("No or incorrect merchant id."); }
$hmac = hash_hmac("sha512", $request, trim($ipn_secret)); if (!hash_equals($hmac, $_SERVER['HTTP_HMAC'])) { $this->edie("HMAC signature does not match."); }
$amount1 = floatval($request->amount1); //IN USD $amount2 = floatval($request->amount2); //IN BTC
$status = intval($request->status);
if ($amount1 < $order_total) { $this->edie("Amount is lesser than order total"); }
if ($status >= 100 || $status == 2) { // Payment is complete $payment->status = 'accepted'; $payment->save(); } die("IPN OK"); }
public function coinPayment(Request $request){ $user = auth('customer')->user(); $plan = Plan::find($request->plan_id); if (!$plan) return response()->json(['status'=>'failed','message' => trans('Plan not found')]);
if (isset($pre_plan) && $pre_plan->plan_id == $request->id) { return response()->json(['status'=>'failed','message', 'You are already subscribed to this plan']); } if ($request->payment_type == 'offline') { $preBilling = BillingRequest::where(['customer_id' => $user->id, 'status' => 'pending'])->first(); if ($preBilling) { return response()->json(['status'=>'failed','message'=> trans('You already have a pending request. Please wait for the admin reply.')]); } } $planReq = new BillingRequest(); $planReq->admin_id = $plan->admin_id; $planReq->customer_id = $user->id; $planReq->plan_id = $plan->id; $planReq->other_info = json_encode($request->only('payment_type')); $planReq->save();
$settings = json_decode(get_settings('payment_gateway')); $private_key = isset($settings->private_key)?$settings->private_key:''; $public_key = isset($settings->public_key)?$settings->public_key:'';
$cps_api = new \CoinpaymentsAPI($private_key, $public_key,'json');
// Enter amount for the transaction $settings = json_decode(get_settings('local_setting')); $currency1=isset($settings->currency_code)?$settings->currency_code:'USD'; $currency2=isset($request->coin_payment_type)?strtoupper($request->coin_payment_type):'BTC';
// Enter buyer email below $amount = $plan->price;
$url= route('paymentgateway::coin.payment'); $userName= $user->full_name; $userEmail= $user->email; $itemName= $plan->title;
$data=[ 'amount'=>$amount, 'currency1'=>$currency1, 'currency2'=>$currency2, 'buyer_name'=>$userName, 'buyer_email'=>$userEmail, 'item_name'=>$itemName, 'ipn_url'=>$url, ];
$transaction_response = $cps_api->CreateCustomTransaction($data);
if ($transaction_response['error'] == 'ok') {
if(isset($transaction_response['result']) && isset($transaction_response['result']['txn_id'])) { $planReq->transaction_id = $transaction_response['result']['txn_id']; $planReq->save(); }
$status_url = isset($transaction_response['result']) && isset($transaction_response['result']['status_url']) ? $transaction_response['result']['status_url'] : ''; $responseAmount=isset($transaction_response['result']) && isset($transaction_response['result']['amount'])?$transaction_response['result']['amount']:'';
$data=[ 'status_url'=>$status_url, 'amount'=>$responseAmount, 'currency'=>$currency2, ]; return response()->json(['status'=>'success', 'data'=>$data]); } else { throw new \Exception($transaction_response['error']); return response()->json(['status'=>'failed', 'message'=>$transaction_response['error']]); } } }
|