1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00

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\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());
}

View File

@ -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;

View File

@ -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);
}
/**

View File

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

View File

@ -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');
}

View File

@ -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');
}

View File

@ -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) {

View File

@ -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');
}