mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-14 07:02:34 +01:00
0fbda85a59
- Removed unused uses - Type hinting for method parameters - Removed commented code - Introduced comments for classes and methods - Short array syntax
207 lines
6.5 KiB
PHP
207 lines
6.5 KiB
PHP
<?php namespace App\Http\Controllers;
|
|
|
|
use Session;
|
|
use Input;
|
|
use Utils;
|
|
use View;
|
|
use Exception;
|
|
use App\Models\Invitation;
|
|
use App\Models\Account;
|
|
use App\Models\Payment;
|
|
use App\Models\PaymentMethod;
|
|
use App\Services\PaymentService;
|
|
use App\Ninja\Mailers\UserMailer;
|
|
use App\Http\Requests\CreateOnlinePaymentRequest;
|
|
|
|
/**
|
|
* Class OnlinePaymentController
|
|
*/
|
|
class OnlinePaymentController extends BaseController
|
|
{
|
|
/**
|
|
* @var PaymentService
|
|
*/
|
|
protected $paymentService;
|
|
|
|
/**
|
|
* @var UserMailer
|
|
*/
|
|
protected $userMailer;
|
|
|
|
/**
|
|
* OnlinePaymentController constructor.
|
|
*
|
|
* @param PaymentService $paymentService
|
|
* @param UserMailer $userMailer
|
|
*/
|
|
public function __construct(PaymentService $paymentService, UserMailer $userMailer)
|
|
{
|
|
$this->paymentService = $paymentService;
|
|
$this->userMailer = $userMailer;
|
|
}
|
|
|
|
/**
|
|
* @param $invitationKey
|
|
* @param bool $gatewayType
|
|
* @param bool $sourceId
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function showPayment($invitationKey, $gatewayType = false, $sourceId = false)
|
|
{
|
|
$invitation = Invitation::with('invoice.invoice_items', 'invoice.client.currency', 'invoice.client.account.account_gateways.gateway')
|
|
->where('invitation_key', '=', $invitationKey)->firstOrFail();
|
|
|
|
if ( ! $gatewayType) {
|
|
$gatewayType = Session::get($invitation->id . 'gateway_type');
|
|
}
|
|
|
|
$paymentDriver = $invitation->account->paymentDriver($invitation, $gatewayType);
|
|
|
|
try {
|
|
return $paymentDriver->startPurchase(Input::all(), $sourceId);
|
|
} catch (Exception $exception) {
|
|
return $this->error($paymentDriver, $exception);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param CreateOnlinePaymentRequest $request
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function doPayment(CreateOnlinePaymentRequest $request)
|
|
{
|
|
$invitation = $request->invitation;
|
|
$gatewayType = Session::get($invitation->id . 'gateway_type');
|
|
$paymentDriver = $invitation->account->paymentDriver($invitation, $gatewayType);
|
|
|
|
try {
|
|
$paymentDriver->completeOnsitePurchase($request->all());
|
|
|
|
if ($paymentDriver->isTwoStep()) {
|
|
Session::flash('warning', trans('texts.bank_account_verification_next_steps'));
|
|
} else {
|
|
Session::flash('message', trans('texts.applied_payment'));
|
|
}
|
|
return redirect()->to('view/' . $invitation->invitation_key);
|
|
} catch (Exception $exception) {
|
|
return $this->error($paymentDriver, $exception, true);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param bool $invitationKey
|
|
* @param bool $gatewayType
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function offsitePayment($invitationKey = false, $gatewayType = false)
|
|
{
|
|
$invitationKey = $invitationKey ?: Session::get('invitation_key');
|
|
$invitation = Invitation::with('invoice.invoice_items', 'invoice.client.currency', 'invoice.client.account.account_gateways.gateway')
|
|
->where('invitation_key', '=', $invitationKey)->firstOrFail();
|
|
|
|
$gatewayType = $gatewayType ?: Session::get($invitation->id . 'gateway_type');
|
|
$paymentDriver = $invitation->account->paymentDriver($invitation, $gatewayType);
|
|
|
|
if ($error = Input::get('error_description') ?: Input::get('error')) {
|
|
return $this->error($paymentDriver, $error);
|
|
}
|
|
|
|
try {
|
|
$paymentDriver->completeOffsitePurchase(Input::all());
|
|
Session::flash('message', trans('texts.applied_payment'));
|
|
return redirect()->to('view/' . $invitation->invitation_key);
|
|
} catch (Exception $exception) {
|
|
return $this->error($paymentDriver, $exception);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param $paymentDriver
|
|
* @param $exception
|
|
* @param bool $showPayment
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
private function error($paymentDriver, $exception, $showPayment = false)
|
|
{
|
|
if (is_string($exception)) {
|
|
$displayError = $exception;
|
|
$logError = $exception;
|
|
} else {
|
|
$displayError = $exception->getMessage();
|
|
$logError = Utils::getErrorString($exception);
|
|
}
|
|
|
|
$message = sprintf('%s: %s', ucwords($paymentDriver->providerName()), $displayError);
|
|
Session::flash('error', $message);
|
|
|
|
$message = sprintf('Payment Error [%s]: %s', $paymentDriver->providerName(), $logError);
|
|
Utils::logError($message, 'PHP', true);
|
|
|
|
$route = $showPayment ? 'payment/' : 'view/';
|
|
return redirect()->to($route . $paymentDriver->invitation->invitation_key);
|
|
}
|
|
|
|
/**
|
|
* @param $routingNumber
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function getBankInfo($routingNumber) {
|
|
if (strlen($routingNumber) != 9 || !preg_match('/\d{9}/', $routingNumber)) {
|
|
return response()->json([
|
|
'message' => 'Invalid routing number',
|
|
], 400);
|
|
}
|
|
|
|
$data = PaymentMethod::lookupBankData($routingNumber);
|
|
|
|
if (is_string($data)) {
|
|
return response()->json([
|
|
'message' => $data,
|
|
], 500);
|
|
} elseif (!empty($data)) {
|
|
return response()->json($data);
|
|
}
|
|
|
|
return response()->json([
|
|
'message' => 'Bank not found',
|
|
], 404);
|
|
}
|
|
|
|
/**
|
|
* @param $accountKey
|
|
* @param $gatewayId
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function handlePaymentWebhook($accountKey, $gatewayId)
|
|
{
|
|
$gatewayId = intval($gatewayId);
|
|
|
|
$account = Account::where('accounts.account_key', '=', $accountKey)->first();
|
|
|
|
if (!$account) {
|
|
return response()->json([
|
|
'message' => 'Unknown account',
|
|
], 404);
|
|
}
|
|
|
|
$accountGateway = $account->getGatewayConfig(intval($gatewayId));
|
|
|
|
if (!$accountGateway) {
|
|
return response()->json([
|
|
'message' => 'Unknown gateway',
|
|
], 404);
|
|
}
|
|
|
|
$paymentDriver = $accountGateway->paymentDriver();
|
|
|
|
try {
|
|
$result = $paymentDriver->handleWebHook(Input::all());
|
|
return response()->json(['message' => $result]);
|
|
} catch (Exception $exception) {
|
|
Utils::logError($exception->getMessage(), 'PHP');
|
|
return response()->json(['message' => $exception->getMessage()], 500);
|
|
}
|
|
}
|
|
|
|
}
|