Viewing file: PaymentGatewayController.php (11.54 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\Deposit;
use App\Models\Plan;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Log;
use Modules\PaymentGateway\PaymentGatewayProvider\ProcessPayment;
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) {
Log::error($ex);
return redirect()->route('customer.deposit.index')->withErrors(['msg' => trans('Invalid Payment')]);
}
//end
}
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.billing.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']]);
}
}
}
|