Use numeric ID for gateway_types table

This commit is contained in:
Joshua Dwire 2016-09-09 17:19:32 -04:00
parent 2fc0265d06
commit 4c1f96e740
9 changed files with 81 additions and 36 deletions

View File

@ -19,6 +19,7 @@ use App\Http\Requests\CreateOnlinePaymentRequest;
use App\Ninja\Repositories\ClientRepository; use App\Ninja\Repositories\ClientRepository;
use App\Ninja\Repositories\InvoiceRepository; use App\Ninja\Repositories\InvoiceRepository;
use App\Services\InvoiceService; use App\Services\InvoiceService;
use App\Models\GatewayType;
/** /**
* Class OnlinePaymentController * Class OnlinePaymentController
@ -59,7 +60,7 @@ class OnlinePaymentController extends BaseController
* @param bool $sourceId * @param bool $sourceId
* @return \Illuminate\Http\RedirectResponse * @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)) { if ( ! $invitation = $this->invoiceRepo->findInvoiceByInvitation($invitationKey)) {
return response()->view('error', [ return response()->view('error', [
@ -74,11 +75,15 @@ class OnlinePaymentController extends BaseController
$invitation = $invitation->load('invoice.client.account.account_gateways.gateway'); $invitation = $invitation->load('invoice.client.account.account_gateways.gateway');
if ( ! $gatewayType) { if ( ! $gatewayTypeAlias) {
$gatewayType = Session::get($invitation->id . 'gateway_type'); $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 { try {
return $paymentDriver->startPurchase(Input::all(), $sourceId); return $paymentDriver->startPurchase(Input::all(), $sourceId);
@ -94,8 +99,8 @@ class OnlinePaymentController extends BaseController
public function doPayment(CreateOnlinePaymentRequest $request) public function doPayment(CreateOnlinePaymentRequest $request)
{ {
$invitation = $request->invitation; $invitation = $request->invitation;
$gatewayType = Session::get($invitation->id . 'gateway_type'); $gatewayTypeId = Session::get($invitation->id . 'gateway_type');
$paymentDriver = $invitation->account->paymentDriver($invitation, $gatewayType); $paymentDriver = $invitation->account->paymentDriver($invitation, $gatewayTypeId);
try { try {
$paymentDriver->completeOnsitePurchase($request->all()); $paymentDriver->completeOnsitePurchase($request->all());
@ -113,17 +118,24 @@ class OnlinePaymentController extends BaseController
/** /**
* @param bool $invitationKey * @param bool $invitationKey
* @param bool $gatewayType * @param mixed $gatewayTypeAlias
* @return \Illuminate\Http\RedirectResponse * @return \Illuminate\Http\RedirectResponse
*/ */
public function offsitePayment($invitationKey = false, $gatewayType = false) public function offsitePayment($invitationKey = false, $gatewayTypeAlias = false)
{ {
$invitationKey = $invitationKey ?: Session::get('invitation_key'); $invitationKey = $invitationKey ?: Session::get('invitation_key');
$invitation = Invitation::with('invoice.invoice_items', 'invoice.client.currency', 'invoice.client.account.account_gateways.gateway') $invitation = Invitation::with('invoice.invoice_items', 'invoice.client.currency', 'invoice.client.account.account_gateways.gateway')
->where('invitation_key', '=', $invitationKey)->firstOrFail(); ->where('invitation_key', '=', $invitationKey)->firstOrFail();
$gatewayType = $gatewayType ?: Session::get($invitation->id . 'gateway_type'); if ( ! $gatewayTypeAlias) {
$paymentDriver = $invitation->account->paymentDriver($invitation, $gatewayType); $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')) { if ($error = Input::get('error_description') ?: Input::get('error')) {
return $this->error($paymentDriver, $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(); $account = Account::whereAccountKey(Input::get('account_key'))->first();
$redirectUrl = Input::get('redirect_url', URL::previous()); $redirectUrl = Input::get('redirect_url', URL::previous());
@ -275,8 +287,8 @@ class OnlinePaymentController extends BaseController
$invitation = $invoice->invitations[0]; $invitation = $invoice->invitations[0];
$link = $invitation->getLink(); $link = $invitation->getLink();
if ($gatewayType) { if ($gatewayTypeAlias) {
return redirect()->to($invitation->getLink('payment') . "/{$gatewayType}"); return redirect()->to($invitation->getLink('payment') . "/{$gatewayTypeAlias}");
} else { } else {
return redirect()->to($invitation->getLink()); return redirect()->to($invitation->getLink());
} }

View File

@ -604,14 +604,14 @@ class Account extends Eloquent
/** /**
* @param bool $invitation * @param bool $invitation
* @param bool $gatewayType * @param mixed $gatewayTypeId
* @return bool * @return bool
*/ */
public function paymentDriver($invitation = false, $gatewayType = false) public function paymentDriver($invitation = false, $gatewayTypeId = false)
{ {
/** @var AccountGateway $accountGateway */ /** @var AccountGateway $accountGateway */
if ($accountGateway = $this->getGatewayByType($gatewayType)) { if ($accountGateway = $this->getGatewayByType($gatewayTypeId)) {
return $accountGateway->paymentDriver($invitation, $gatewayType); return $accountGateway->paymentDriver($invitation, $gatewayTypeId);
} }
return false; return false;

View File

@ -73,14 +73,14 @@ class AccountGateway extends EntityModel
/** /**
* @param bool $invitation * @param bool $invitation
* @param bool $gatewayType * @param mixed $gatewayTypeId
* @return mixed * @return mixed
*/ */
public function paymentDriver($invitation = false, $gatewayType = false) public function paymentDriver($invitation = false, $gatewayTypeId = false)
{ {
$class = static::paymentDriverClass($this->gateway->provider); $class = static::paymentDriverClass($this->gateway->provider);
return new $class($this, $invitation, $gatewayType); return new $class($this, $invitation, $gatewayTypeId);
} }
/** /**

View File

@ -1,6 +1,8 @@
<?php namespace App\Models; <?php namespace App\Models;
use Eloquent; use Eloquent;
use Cache;
use Utils;
/** /**
* Class GatewayType * Class GatewayType
@ -19,4 +21,14 @@ class GatewayType extends Eloquent
{ {
return $this->name; return $this->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;
}
} }

View File

@ -14,6 +14,7 @@ use App\Models\Account;
use App\Models\Payment; use App\Models\Payment;
use App\Models\PaymentMethod; use App\Models\PaymentMethod;
use App\Models\Country; use App\Models\Country;
use App\Models\GatewayType;
class BasePaymentDriver class BasePaymentDriver
{ {
@ -166,12 +167,14 @@ class BasePaymentDriver
// check if a custom view exists for this provider // check if a custom view exists for this provider
protected function paymentView() 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)) { if (file_exists($file)) {
return sprintf('payments.%s/%s', $this->providerName(), $this->gatewayType); return sprintf('payments.%s/%s', $this->providerName(), $gatewayTypeAlias);
} else { } else {
return sprintf('payments.%s', $this->gatewayType); return sprintf('payments.%s', $gatewayTypeAlias);
} }
} }
@ -331,7 +334,8 @@ class BasePaymentDriver
protected function paymentDetails($paymentMethod = false) protected function paymentDetails($paymentMethod = false)
{ {
$invoice = $this->invoice(); $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 = [ $data = [
'amount' => $invoice->getRequestedAmount(), 'amount' => $invoice->getRequestedAmount(),
@ -795,9 +799,11 @@ class BasePaymentDriver
continue; continue;
} }
$gatewayTypeAlias = GatewayType::getAliasFromId($gatewayTypeId);
$links[] = [ $links[] = [
'url' => $this->paymentUrl($gatewayTypeId), 'url' => $this->paymentUrl($gatewayTypeAlias),
'label' => trans("texts.{$gatewayTypeId}") 'label' => trans("texts.{$gatewayTypeAlias}")
]; ];
} }
@ -830,13 +836,15 @@ class BasePaymentDriver
return true; return true;
} }
protected function paymentUrl($gatewayType) protected function paymentUrl($gatewayTypeAlias)
{ {
$account = $this->account(); $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 // 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'; $url .= '#braintree_paypal';
if ($account->iframe_url) { if ($account->iframe_url) {

View File

@ -16,6 +16,7 @@ class CreateGatewayTypes extends Migration
Schema::create('gateway_types', function($t) Schema::create('gateway_types', function($t)
{ {
$t->increments('id'); $t->increments('id');
$t->string('alias');
$t->string('name'); $t->string('name');
}); });
@ -39,6 +40,12 @@ class CreateGatewayTypes extends Migration
$t->foreign('gateway_type_id')->references('id')->on('gateway_types')->onDelete('cascade'); $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. * Reverse the migrations.
@ -47,6 +54,12 @@ class CreateGatewayTypes extends Migration
*/ */
public function down() 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('account_gateway_settings');
Schema::dropIfExists('gateway_types'); Schema::dropIfExists('gateway_types');
} }

View File

@ -17,6 +17,7 @@ class DatabaseSeeder extends Seeder
$this->call('CountriesSeeder'); $this->call('CountriesSeeder');
$this->call('PaymentLibrariesSeeder'); $this->call('PaymentLibrariesSeeder');
$this->call('FontsSeeder'); $this->call('FontsSeeder');
$this->call('GatewayTypesSeeder');
$this->call('BanksSeeder'); $this->call('BanksSeeder');
$this->call('InvoiceStatusSeeder'); $this->call('InvoiceStatusSeeder');
$this->call('PaymentStatusSeeder'); $this->call('PaymentStatusSeeder');
@ -25,7 +26,6 @@ class DatabaseSeeder extends Seeder
$this->call('InvoiceDesignsSeeder'); $this->call('InvoiceDesignsSeeder');
$this->call('PaymentTermsSeeder'); $this->call('PaymentTermsSeeder');
$this->call('PaymentTypesSeeder'); $this->call('PaymentTypesSeeder');
$this->call('GatewayTypesSeeder');
$this->call('LanguageSeeder'); $this->call('LanguageSeeder');
$this->call('IndustrySeeder'); $this->call('IndustrySeeder');
} }

View File

@ -10,11 +10,11 @@ class GatewayTypesSeeder extends Seeder
$gateway_types = [ $gateway_types = [
['name' => 'Credit Card'], ['alias' => 'credit_card', 'name' => 'Credit Card'],
['name' => 'Bank Transfer'], ['alias' => 'bank_transfer', 'name' => 'Bank Transfer'],
['name' => 'PayPal'], ['alias' => 'paypal', 'name' => 'PayPal'],
['name' => 'Bitcoin'], ['alias' => 'bitcoin', 'name' => 'Bitcoin'],
['name' => 'Dwolla'], ['alias' => 'dwolla', 'name' => 'Dwolla'],
]; ];
foreach ($gateway_types as $gateway_type) { foreach ($gateway_types as $gateway_type) {

View File

@ -13,6 +13,7 @@ class UpdateSeeder extends Seeder
$this->call('PaymentLibrariesSeeder'); $this->call('PaymentLibrariesSeeder');
$this->call('FontsSeeder'); $this->call('FontsSeeder');
$this->call('GatewayTypesSeeder');
$this->call('BanksSeeder'); $this->call('BanksSeeder');
$this->call('InvoiceStatusSeeder'); $this->call('InvoiceStatusSeeder');
$this->call('PaymentStatusSeeder'); $this->call('PaymentStatusSeeder');
@ -21,7 +22,6 @@ class UpdateSeeder extends Seeder
$this->call('InvoiceDesignsSeeder'); $this->call('InvoiceDesignsSeeder');
$this->call('PaymentTermsSeeder'); $this->call('PaymentTermsSeeder');
$this->call('PaymentTypesSeeder'); $this->call('PaymentTypesSeeder');
$this->call('GatewayTypesSeeder');
$this->call('LanguageSeeder'); $this->call('LanguageSeeder');
$this->call('IndustrySeeder'); $this->call('IndustrySeeder');
} }