diff --git a/app/Http/Controllers/OnlinePaymentController.php b/app/Http/Controllers/OnlinePaymentController.php index 05e912ffe555..93dd5a624141 100644 --- a/app/Http/Controllers/OnlinePaymentController.php +++ b/app/Http/Controllers/OnlinePaymentController.php @@ -19,6 +19,7 @@ use App\Http\Requests\CreateOnlinePaymentRequest; use App\Ninja\Repositories\ClientRepository; use App\Ninja\Repositories\InvoiceRepository; use App\Services\InvoiceService; +use App\Models\GatewayType; /** * Class OnlinePaymentController @@ -59,7 +60,7 @@ class OnlinePaymentController extends BaseController * @param bool $sourceId * @return \Illuminate\Http\RedirectResponse */ - public function showPayment($invitationKey, $gatewayType = false, $sourceId = false) + public function showPayment($invitationKey, $gatewayTypeAlias = false, $sourceId = false) { if ( ! $invitation = $this->invoiceRepo->findInvoiceByInvitation($invitationKey)) { return response()->view('error', [ @@ -74,11 +75,15 @@ class OnlinePaymentController extends BaseController $invitation = $invitation->load('invoice.client.account.account_gateways.gateway'); - if ( ! $gatewayType) { - $gatewayType = Session::get($invitation->id . 'gateway_type'); + if ( ! $gatewayTypeAlias) { + $gatewayTypeId = Session::get($invitation->id . 'gateway_type'); + } elseif ($gatewayTypeAlias != GATEWAY_TYPE_TOKEN) { + $gatewayTypeId = GatewayType::getIdFromAlias($gatewayTypeAlias); + } else { + $gatewayTypeId = $gatewayTypeAlias; } - $paymentDriver = $invitation->account->paymentDriver($invitation, $gatewayType); + $paymentDriver = $invitation->account->paymentDriver($invitation, $gatewayTypeId); try { return $paymentDriver->startPurchase(Input::all(), $sourceId); @@ -94,8 +99,8 @@ class OnlinePaymentController extends BaseController public function doPayment(CreateOnlinePaymentRequest $request) { $invitation = $request->invitation; - $gatewayType = Session::get($invitation->id . 'gateway_type'); - $paymentDriver = $invitation->account->paymentDriver($invitation, $gatewayType); + $gatewayTypeId = Session::get($invitation->id . 'gateway_type'); + $paymentDriver = $invitation->account->paymentDriver($invitation, $gatewayTypeId); try { $paymentDriver->completeOnsitePurchase($request->all()); @@ -113,17 +118,24 @@ class OnlinePaymentController extends BaseController /** * @param bool $invitationKey - * @param bool $gatewayType + * @param mixed $gatewayTypeAlias * @return \Illuminate\Http\RedirectResponse */ - public function offsitePayment($invitationKey = false, $gatewayType = false) + public function offsitePayment($invitationKey = false, $gatewayTypeAlias = 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 ( ! $gatewayTypeAlias) { + $gatewayTypeId = Session::get($invitation->id . 'gateway_type'); + } elseif ($gatewayTypeAlias != GATEWAY_TYPE_TOKEN) { + $gatewayTypeId = GatewayType::getIdFromAlias($gatewayTypeAlias); + } else { + $gatewayTypeId = $gatewayTypeAlias; + } + + $paymentDriver = $invitation->account->paymentDriver($invitation, $gatewayTypeId); if ($error = Input::get('error_description') ?: Input::get('error')) { return $this->error($paymentDriver, $error); @@ -227,7 +239,7 @@ class OnlinePaymentController extends BaseController } } - public function handleBuyNow(ClientRepository $clientRepo, InvoiceService $invoiceService, $gatewayType = false) + public function handleBuyNow(ClientRepository $clientRepo, InvoiceService $invoiceService, $gatewayTypeAlias = false) { $account = Account::whereAccountKey(Input::get('account_key'))->first(); $redirectUrl = Input::get('redirect_url', URL::previous()); @@ -275,8 +287,8 @@ class OnlinePaymentController extends BaseController $invitation = $invoice->invitations[0]; $link = $invitation->getLink(); - if ($gatewayType) { - return redirect()->to($invitation->getLink('payment') . "/{$gatewayType}"); + if ($gatewayTypeAlias) { + return redirect()->to($invitation->getLink('payment') . "/{$gatewayTypeAlias}"); } else { return redirect()->to($invitation->getLink()); } diff --git a/app/Models/Account.php b/app/Models/Account.php index 3e430c5d45ce..d8c07382ab08 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -604,14 +604,14 @@ class Account extends Eloquent /** * @param bool $invitation - * @param bool $gatewayType + * @param mixed $gatewayTypeId * @return bool */ - public function paymentDriver($invitation = false, $gatewayType = false) + public function paymentDriver($invitation = false, $gatewayTypeId = false) { /** @var AccountGateway $accountGateway */ - if ($accountGateway = $this->getGatewayByType($gatewayType)) { - return $accountGateway->paymentDriver($invitation, $gatewayType); + if ($accountGateway = $this->getGatewayByType($gatewayTypeId)) { + return $accountGateway->paymentDriver($invitation, $gatewayTypeId); } return false; diff --git a/app/Models/AccountGateway.php b/app/Models/AccountGateway.php index a2250b9bfc24..241d1013f884 100644 --- a/app/Models/AccountGateway.php +++ b/app/Models/AccountGateway.php @@ -73,14 +73,14 @@ class AccountGateway extends EntityModel /** * @param bool $invitation - * @param bool $gatewayType + * @param mixed $gatewayTypeId * @return mixed */ - public function paymentDriver($invitation = false, $gatewayType = false) + public function paymentDriver($invitation = false, $gatewayTypeId = false) { $class = static::paymentDriverClass($this->gateway->provider); - return new $class($this, $invitation, $gatewayType); + return new $class($this, $invitation, $gatewayTypeId); } /** diff --git a/app/Models/GatewayType.php b/app/Models/GatewayType.php index 38fdf48d4eb2..b695dcf3df21 100644 --- a/app/Models/GatewayType.php +++ b/app/Models/GatewayType.php @@ -1,6 +1,8 @@ name; } + + public static function getAliasFromId($id) + { + return Utils::getFromCache($id, 'gatewayTypes')->alias; + } + + public static function getIdFromAlias($alias) + { + return Cache::get('gatewayTypes')->where('alias', $alias)->first()->id; + } } diff --git a/app/Ninja/PaymentDrivers/BasePaymentDriver.php b/app/Ninja/PaymentDrivers/BasePaymentDriver.php index 700c4c6e5bad..1b473307d5d1 100644 --- a/app/Ninja/PaymentDrivers/BasePaymentDriver.php +++ b/app/Ninja/PaymentDrivers/BasePaymentDriver.php @@ -14,6 +14,7 @@ use App\Models\Account; use App\Models\Payment; use App\Models\PaymentMethod; use App\Models\Country; +use App\Models\GatewayType; class BasePaymentDriver { @@ -166,12 +167,14 @@ class BasePaymentDriver // check if a custom view exists for this provider protected function paymentView() { - $file = sprintf('%s/views/payments/%s/%s.blade.php', resource_path(), $this->providerName(), $this->gatewayType); + $gatewayTypeAlias = GatewayType::getAliasFromId($this->gatewayType); + + $file = sprintf('%s/views/payments/%s/%s.blade.php', resource_path(), $this->providerName(), $gatewayTypeAlias); if (file_exists($file)) { - return sprintf('payments.%s/%s', $this->providerName(), $this->gatewayType); + return sprintf('payments.%s/%s', $this->providerName(), $gatewayTypeAlias); } else { - return sprintf('payments.%s', $this->gatewayType); + return sprintf('payments.%s', $gatewayTypeAlias); } } @@ -331,7 +334,8 @@ class BasePaymentDriver protected function paymentDetails($paymentMethod = false) { $invoice = $this->invoice(); - $completeUrl = url('complete/' . $this->invitation->invitation_key . '/' . $this->gatewayType); + $gatewayTypeAlias = GatewayType::getAliasFromId($this->gatewayType); + $completeUrl = url('complete/' . $this->invitation->invitation_key . '/' . $gatewayTypeAlias); $data = [ 'amount' => $invoice->getRequestedAmount(), @@ -795,9 +799,11 @@ class BasePaymentDriver continue; } + $gatewayTypeAlias = GatewayType::getAliasFromId($gatewayTypeId); + $links[] = [ - 'url' => $this->paymentUrl($gatewayTypeId), - 'label' => trans("texts.{$gatewayTypeId}") + 'url' => $this->paymentUrl($gatewayTypeAlias), + 'label' => trans("texts.{$gatewayTypeAlias}") ]; } @@ -830,13 +836,15 @@ class BasePaymentDriver return true; } - protected function paymentUrl($gatewayType) + protected function paymentUrl($gatewayTypeAlias) { $account = $this->account(); - $url = URL::to("/payment/{$this->invitation->invitation_key}/{$gatewayType}"); + $url = URL::to("/payment/{$this->invitation->invitation_key}/{$gatewayTypeAlias}"); + + $gatewayTypeId = GatewayType::getIdFromAlias($gatewayTypeAlias); // PayPal doesn't allow being run in an iframe so we need to open in new tab - if ($gatewayType === GATEWAY_TYPE_PAYPAL) { + if ($gatewayTypeId === GATEWAY_TYPE_PAYPAL) { $url .= '#braintree_paypal'; if ($account->iframe_url) { diff --git a/database/migrations/2016_09_05_150625_create_gateway_types.php b/database/migrations/2016_09_05_150625_create_gateway_types.php index 1fec411bc9af..2e41e3b34451 100644 --- a/database/migrations/2016_09_05_150625_create_gateway_types.php +++ b/database/migrations/2016_09_05_150625_create_gateway_types.php @@ -16,6 +16,7 @@ class CreateGatewayTypes extends Migration Schema::create('gateway_types', function($t) { $t->increments('id'); + $t->string('alias'); $t->string('name'); }); @@ -39,6 +40,12 @@ class CreateGatewayTypes extends Migration $t->foreign('gateway_type_id')->references('id')->on('gateway_types')->onDelete('cascade'); }); + + Schema::table('payment_types', function($t) + { + $t->unsignedInteger('gateway_type_id')->nullable(); + $t->foreign('gateway_type_id')->references('id')->on('gateway_types')->onDelete('cascade'); + }); } /** * Reverse the migrations. @@ -47,6 +54,12 @@ class CreateGatewayTypes extends Migration */ public function down() { + Schema::table('payment_types', function($t) + { + $t->dropForeign('payment_types_gateway_type_id_foreign'); + $t->dropColumn('gateway_type_id'); + }); + Schema::dropIfExists('account_gateway_settings'); Schema::dropIfExists('gateway_types'); } diff --git a/database/seeds/DatabaseSeeder.php b/database/seeds/DatabaseSeeder.php index 7f52d2826fee..e6c3f632046c 100644 --- a/database/seeds/DatabaseSeeder.php +++ b/database/seeds/DatabaseSeeder.php @@ -17,6 +17,7 @@ class DatabaseSeeder extends Seeder $this->call('CountriesSeeder'); $this->call('PaymentLibrariesSeeder'); $this->call('FontsSeeder'); + $this->call('GatewayTypesSeeder'); $this->call('BanksSeeder'); $this->call('InvoiceStatusSeeder'); $this->call('PaymentStatusSeeder'); @@ -25,7 +26,6 @@ class DatabaseSeeder extends Seeder $this->call('InvoiceDesignsSeeder'); $this->call('PaymentTermsSeeder'); $this->call('PaymentTypesSeeder'); - $this->call('GatewayTypesSeeder'); $this->call('LanguageSeeder'); $this->call('IndustrySeeder'); } diff --git a/database/seeds/GatewayTypesSeeder.php b/database/seeds/GatewayTypesSeeder.php index 33ae1d2984e3..d97eef5ca230 100644 --- a/database/seeds/GatewayTypesSeeder.php +++ b/database/seeds/GatewayTypesSeeder.php @@ -10,11 +10,11 @@ class GatewayTypesSeeder extends Seeder $gateway_types = [ - ['name' => 'Credit Card'], - ['name' => 'Bank Transfer'], - ['name' => 'PayPal'], - ['name' => 'Bitcoin'], - ['name' => 'Dwolla'], + ['alias' => 'credit_card', 'name' => 'Credit Card'], + ['alias' => 'bank_transfer', 'name' => 'Bank Transfer'], + ['alias' => 'paypal', 'name' => 'PayPal'], + ['alias' => 'bitcoin', 'name' => 'Bitcoin'], + ['alias' => 'dwolla', 'name' => 'Dwolla'], ]; foreach ($gateway_types as $gateway_type) { diff --git a/database/seeds/UpdateSeeder.php b/database/seeds/UpdateSeeder.php index a183a64ad3a6..eb99890c94ce 100644 --- a/database/seeds/UpdateSeeder.php +++ b/database/seeds/UpdateSeeder.php @@ -13,6 +13,7 @@ class UpdateSeeder extends Seeder $this->call('PaymentLibrariesSeeder'); $this->call('FontsSeeder'); + $this->call('GatewayTypesSeeder'); $this->call('BanksSeeder'); $this->call('InvoiceStatusSeeder'); $this->call('PaymentStatusSeeder'); @@ -21,7 +22,6 @@ class UpdateSeeder extends Seeder $this->call('InvoiceDesignsSeeder'); $this->call('PaymentTermsSeeder'); $this->call('PaymentTypesSeeder'); - $this->call('GatewayTypesSeeder'); $this->call('LanguageSeeder'); $this->call('IndustrySeeder'); }