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

Merge pull request #86 from blkmutt/master

Made PHP-Payments Updates
This commit is contained in:
Hillel Coren 2014-04-12 21:54:39 +03:00
commit df9e53c0f9
20 changed files with 493 additions and 72 deletions

1
.gitignore vendored
View File

@ -20,3 +20,4 @@
/ninja.sublime-workspace /ninja.sublime-workspace
/tests/_log /tests/_log
.idea .idea
.project

View File

@ -165,23 +165,55 @@ class AccountController extends \BaseController {
$account = Account::with('account_gateways')->findOrFail(Auth::user()->account_id); $account = Account::with('account_gateways')->findOrFail(Auth::user()->account_id);
$accountGateway = null; $accountGateway = null;
$config = null; $config = null;
$configFields = null;
if (count($account->account_gateways) > 0) if (count($account->account_gateways) > 0)
{ {
$accountGateway = $account->account_gateways[0]; $accountGateway = $account->account_gateways[0];
$config = $accountGateway->config; $config = $accountGateway->config;
$configFields = json_decode($config);
foreach($configFields as $configField => $value)
{
$configFields->$configField = str_repeat('*', strlen($value));
}
}
$recommendedGateways = Gateway::remember(DEFAULT_QUERY_CACHE)
->where('recommended', '=', '1')
->orderBy('sort_order')
->get();
$recommendedGatewayArray = array();
foreach($recommendedGateways as $recommendedGateway)
{
$newRow = count($recommendedGatewayArray) + 1 == round(count($recommendedGateways) / 2);
$arrayItem = array(
'value' => $recommendedGateway->id,
'data-imageUrl' => $recommendedGateway->getLogoUrl(),
'data-siteUrl' => $recommendedGateway->site_url,
'data-newRow' => $newRow
);
$recommendedGatewayArray[$recommendedGateway->name] = $arrayItem;
} }
$data = [ $data = [
'account' => $account, 'account' => $account,
'accountGateway' => $accountGateway, 'accountGateway' => $accountGateway,
'config' => json_decode($config), 'config' => $configFields,
'gateways' => Gateway::remember(DEFAULT_QUERY_CACHE)->get(), 'gateways' => Gateway::remember(DEFAULT_QUERY_CACHE)
->orderBy('name')
->get(),
'recommendedGateways' => $recommendedGatewayArray,
]; ];
foreach ($data['gateways'] as $gateway) foreach ($data['gateways'] as $gateway)
{ {
$gateway->fields = Omnipay::create($gateway->provider)->getDefaultParameters(); $paymentLibrary = $gateway->paymentlibrary;
$gateway->fields = $gateway->getFields();
if ($accountGateway && $accountGateway->gateway_id == $gateway->id) if ($accountGateway && $accountGateway->gateway_id == $gateway->id)
{ {
@ -511,15 +543,28 @@ class AccountController extends \BaseController {
if ($gatewayId = Input::get('gateway_id')) if ($gatewayId = Input::get('gateway_id'))
{ {
$gateway = Gateway::findOrFail($gatewayId); $gateway = Gateway::findOrFail($gatewayId);
$fields = Omnipay::create($gateway->provider)->getDefaultParameters();
$paymentLibrary = $gateway->paymentlibrary;
$fields = $gateway->getFields();
foreach ($fields as $field => $details) foreach ($fields as $field => $details)
{ {
if (!in_array($field, ['testMode', 'developerMode', 'headerImageUrl', 'solutionType', 'landingPage', 'brandName'])) if (!in_array($field, ['testMode', 'developerMode', 'headerImageUrl', 'solutionType', 'landingPage', 'brandName']))
{
if(strtolower($gateway->name) == 'beanstream')
{
if(in_array($field, ['merchant_id', 'passCode']))
{ {
$rules[$gateway->id.'_'.$field] = 'required'; $rules[$gateway->id.'_'.$field] = 'required';
} }
} }
else
{
$rules[$gateway->id.'_'.$field] = 'required';
}
}
}
} }
$validator = Validator::make(Input::all(), $rules); $validator = Validator::make(Input::all(), $rules);

View File

@ -129,8 +129,10 @@ class PaymentController extends \BaseController
private function getPaymentDetails($invoice, $input = null) private function getPaymentDetails($invoice, $input = null)
{ {
$key = $invoice->invoice_number . '_details'; $key = $invoice->invoice_number . '_details';
$gateway = $invoice->client->account->account_gateways[0]->gateway;
$paymentLibrary = $gateway->paymentlibrary;
if ($input) if ($input && $paymentLibrary->id == PAYMENT_LIBRARY_OMNIPAY)
{ {
$data = [ $data = [
'firstName' => $input['first_name'], 'firstName' => $input['first_name'],
@ -152,6 +154,49 @@ class PaymentController extends \BaseController
]; ];
Session::put($key, $data); Session::put($key, $data);
}
else if ($input && $paymentLibrary->id == PAYMENT_LIBRARY_PHP_PAYMENTS)
{
$input = Input::all();
$data = [
'first_name' => $input['first_name'],
'last_name' => $input['last_name'],
'cc_number' => $input['card_number'],
'cc_exp' => $input['expiration_month'].$input['expiration_year'],
'cc_code' => $input['cvv'],
'street' => $input['address1'],
'street2' => $input['address2'],
'city' => $input['city'],
'state' => $input['state'],
'postal_code' => $input['postal_code'],
'amt' => $invoice->amount,
'ship_to_street' => $input['address1'],
'ship_to_city' => $input['city'],
'ship_to_state' => $input['state'],
'ship_to_postal_code' => $input['postal_code'],
'currency_code' => $invoice->client->currency->code,
];
switch($gateway->id)
{
case GATEWAY_BEANSTREAM:
$data['phone'] = $input['phone'];
$data['email'] = $input['email'];
$data['country'] = $input['country'];
$data['ship_to_country'] = $input['country'];
break;
case GATEWAY_BRAINTREE:
$data['ship_to_state'] = 'Ohio'; //$input['state'];
break;
}
if(strlen($data['cc_exp']) == 5)
{
$data['cc_exp'] = '0'.$data['cc_exp'];
}
Session::put($key, $data);
return $data;
} }
else if (Session::get($key)) else if (Session::get($key))
{ {
@ -162,6 +207,8 @@ class PaymentController extends \BaseController
$data = []; $data = [];
} }
if($paymentLibrary->id == PAYMENT_LIBRARY_OMNIPAY)
{
$card = new CreditCard($data); $card = new CreditCard($data);
return [ return [
@ -172,12 +219,18 @@ class PaymentController extends \BaseController
'cancelUrl' => URL::to('/') 'cancelUrl' => URL::to('/')
]; ];
} }
else
{
return $data;
}
}
public function show_payment($invitationKey) public function show_payment($invitationKey)
{ {
// For PayPal Express we redirect straight to their site // For PayPal Express we redirect straight to their site
$invitation = Invitation::with('invoice.client.account')->where('invitation_key', '=', $invitationKey)->firstOrFail(); $invitation = Invitation::with('invoice.client.account', 'invoice.client.account.account_gateways.gateway')->where('invitation_key', '=', $invitationKey)->firstOrFail();
$account = $invitation->invoice->client->account; $account = $invitation->invoice->client->account;
if ($account->isGatewayConfigured(GATEWAY_PAYPAL_EXPRESS)) if ($account->isGatewayConfigured(GATEWAY_PAYPAL_EXPRESS))
{ {
if (Session::has('error')) if (Session::has('error'))
@ -191,16 +244,21 @@ class PaymentController extends \BaseController
} }
} }
$invitation = Invitation::with('contact', 'invoice.client')->where('invitation_key', '=', $invitationKey)->firstOrFail(); $invitation = Invitation::with('invoice.invoice_items', 'invoice.client.currency', 'invoice.client.account.account_gateways.gateway')->where('invitation_key', '=', $invitationKey)->firstOrFail();
$invoice = $invitation->invoice; $invoice = $invitation->invoice;
$client = $invoice->client; $client = $invoice->client;
$gateway = $invoice->client->account->account_gateways[0]->gateway;
$paymentLibrary = $gateway->paymentlibrary;
$data = [ $data = [
'showBreadcrumbs' => false, 'showBreadcrumbs' => false,
'invitationKey' => $invitationKey, 'invitationKey' => $invitationKey,
'invoice' => $invoice, 'invoice' => $invoice,
'client' => $client, 'client' => $client,
'contact' => $invitation->contact 'contact' => $invitation->contact,
'paymentLibrary' => $paymentLibrary ,
'gateway' => $gateway,
'countries' => Country::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
]; ];
return View::make('payments.payment', $data); return View::make('payments.payment', $data);
@ -235,7 +293,7 @@ class PaymentController extends \BaseController
$invitation = Invitation::with('invoice.invoice_items', 'invoice.client.currency', 'invoice.client.account.account_gateways.gateway')->where('invitation_key', '=', $invitationKey)->firstOrFail(); $invitation = Invitation::with('invoice.invoice_items', 'invoice.client.currency', 'invoice.client.account.account_gateways.gateway')->where('invitation_key', '=', $invitationKey)->firstOrFail();
$invoice = $invitation->invoice; $invoice = $invitation->invoice;
$accountGateway = $invoice->client->account->account_gateways[0]; $accountGateway = $invoice->client->account->account_gateways[0];
$gateway = self::createGateway($accountGateway); $paymentLibrary = $accountGateway->gateway->paymentlibrary;
if ($onSite) if ($onSite)
{ {
@ -248,8 +306,12 @@ class PaymentController extends \BaseController
$client->save(); $client->save();
} }
try try
{ {
if($paymentLibrary->id == PAYMENT_LIBRARY_OMNIPAY)
{
$gateway = self::createGateway($accountGateway);
$details = self::getPaymentDetails($invoice, Input::all()); $details = self::getPaymentDetails($invoice, Input::all());
$response = $gateway->purchase($details)->send(); $response = $gateway->purchase($details)->send();
$ref = $response->getTransactionReference(); $ref = $response->getTransactionReference();
@ -282,11 +344,56 @@ class PaymentController extends \BaseController
} }
else else
{ {
$errorMessage = trans('texts.payment_error') . "\n\n" . $response->getMessage(); Session::flash('error', $response->getMessage());
Session::flash('error', $errorMessage); return Utils::fatalError('Sorry, there was an error processing your payment. Please try again later.<p>', $response->getMessage());
Utils::logError($errorMessage); }
}
else if ($paymentLibrary->id == PAYMENT_LIBRARY_PHP_PAYMENTS)
{
$gateway = $accountGateway->gateway;
$provider = $gateway->provider;
$p = new PHP_Payments(array('mode' => 'test'));
$config = Payment_Utility::load('config', 'drivers/'.$provider);
switch($gateway->id)
{
case GATEWAY_BEANSTREAM:
$config['delay_charge'] = FALSE;
$config['bill_outstanding'] = TRUE;
break;
case GATEWAY_AMAZON:
$config['return_url'] = URL::to('complete');
$config['abandon_url'] = URL::to('/');
$config['immediate_return'] = 0;
$config['process_immediate'] = 1;
$config['ipn_url'] = URL::to('ipn');
$config['collect_shipping_address'] = false;
break;
}
$details = self::getPaymentDetails($invoice, Input::all());
$response = $p->oneoff_payment($provider, $details, $config);
if (strtolower($response->status) == 'success')
{
$payment = self::createPayment($invitation, $response->response_message);
$invoice->invoice_status_id = INVOICE_STATUS_PAID;
$invoice->save();
Event::fire('invoice.paid', $payment);
Session::flash('message', trans('texts.applied_payment'));
return Redirect::to('view/' . $payment->invitation->invitation_key); return Redirect::to('view/' . $payment->invitation->invitation_key);
} }
else
{
Session::flash('error', $response->response_message);
return Utils::fatalError('Sorry, there was an error processing your payment. Please try again later.<p>', $response->response_message);
}
}
} }
catch (\Exception $e) catch (\Exception $e)
{ {

View File

@ -0,0 +1,61 @@
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePaymentLibraries extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::dropIfExists('payment_libraries');
Schema::create('payment_libraries', function($t)
{
$t->increments('id');
$t->timestamps();
$t->string('name');
$t->boolean('visible')->default(true);
});
DB::table('payment_libraries')->insert(['name' => 'Omnipay']);
DB::table('payment_libraries')->insert(['name' => 'PHP-Payments']);
Schema::table('gateways', function($table)
{
$table->unsignedInteger('payment_library_id')->default(1);
});
DB::table('gateways')->update(['payment_library_id' => 1]);
Schema::table('gateways', function($table)
{
$table->foreign('payment_library_id')->references('id')->on('payment_libraries')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
if (Schema::hasColumn('gateways', 'payment_library_id'))
{
Schema::table('gateways', function($table)
{
$table->dropForeign('gateways_payment_library_id_foreign');
$table->dropColumn('payment_library_id');
});
}
Schema::dropIfExists('payment_libraries');
}
}

View File

@ -0,0 +1,49 @@
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddSortAndRecommendedToGateways extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('gateways', function($table)
{
$table->unsignedInteger('sort_order')->default(10000);
$table->boolean('recommended');
$table->string('site_url', 200);
});
}
public function down()
{
if (Schema::hasColumn('gateways', 'sort_order'))
{
Schema::table('gateways', function($table)
{
$table->dropColumn('sort_order');
});
}
if (Schema::hasColumn('gateways', 'recommended'))
{
Schema::table('gateways', function($table)
{
$table->dropColumn('recommended');
});
}
if (Schema::hasColumn('gateways', 'site_url'))
{
Schema::table('gateways', function($table)
{
$table->dropColumn('site_url');
});
}
}
}

View File

@ -18,6 +18,9 @@ class DatabaseSeeder extends Seeder {
$this->call('CountriesSeeder'); $this->call('CountriesSeeder');
$this->command->info('Seeded the countries!'); $this->command->info('Seeded the countries!');
$this->call('PaymentLibrariesSeeder');
$this->command->info('Seeded the Payment Libraries!');
} }
} }

View File

@ -0,0 +1,35 @@
<?php
class PaymentLibrariesSeeder extends Seeder
{
public function run()
{
$gateways = [
array('name'=>'BeanStream', 'provider'=>'BeanStream', 'payment_library_id' => 2),
array('name'=>'Psigate', 'provider'=>'Psigate', 'payment_library_id' => 2)
];
$updateProviders = array(
0 => 'AuthorizeNet_AIM',
1 => 'BeanStream',
2 => 'iTransact',
3 => 'FirstData_Connect',
4 => 'PayPal_Pro',
5 => 'TwoCheckout'
);
foreach ($gateways as $gateway)
{
Gateway::create($gateway);
}
Gateway::whereIn('provider', $updateProviders)->update(array('recommended' => 1));
Gateway::where('provider', '=', 'AuthorizeNet_AIM')->update(array('sort_order' => 5, 'site_url' => 'http://www.authorize.net/'));
Gateway::where('provider', '=', 'BeanStream')->update(array('sort_order' => 10, 'site_url' => 'http://www.beanstream.com/'));
Gateway::where('provider', '=', 'FirstData_Connect')->update(array('sort_order' => 20, 'site_url' => 'https://www.firstdata.com/'));
Gateway::where('provider', '=', 'PayPal_Pro')->update(array('sort_order' => 25, 'site_url' => 'https://www.paypal.com/'));
Gateway::where('provider', '=', 'TwoCheckout')->update(array('sort_order' => 30, 'site_url' => 'https://www.2checkout.com/'));
}
}

View File

@ -4,4 +4,35 @@ class Gateway extends Eloquent
{ {
public $timestamps = false; public $timestamps = false;
protected $softDelete = false; protected $softDelete = false;
public function paymentlibrary()
{
return $this->belongsTo('PaymentLibrary', 'payment_library_id');
}
public function getLogoUrl()
{
return '/images/gateways/logo_'.$this->provider.'.png';
}
public function getFields()
{
$paymentLibrary = $this->paymentlibrary;
if($paymentLibrary->id == PAYMENT_LIBRARY_OMNIPAY)
{
$fields = Omnipay::create($this->provider)->getDefaultParameters();
}
else
{
$fields = Payment_Utility::load('config', 'drivers/'.$this->provider);
}
if($fields == null)
{
$fields = array();
}
return $fields;
}
} }

View File

@ -0,0 +1,11 @@
<?php
class PaymentLibrary extends Eloquent
{
protected $table = 'payment_libraries';
public function gateways()
{
return $this->hasMany('Gateway', 'payment_library_id');
}
}

View File

@ -234,6 +234,16 @@ define('DEFAULT_LOCALE', 'en');
define('GATEWAY_PAYPAL_EXPRESS', 17); define('GATEWAY_PAYPAL_EXPRESS', 17);
define('NINJA_ACCOUNT_KEY', 'zg4ylmzDkdkPOT8yoKQw9LTWaoZJx79h'); define('NINJA_ACCOUNT_KEY', 'zg4ylmzDkdkPOT8yoKQw9LTWaoZJx79h');
define('PAYMENT_LIBRARY_OMNIPAY', 1);
define('PAYMENT_LIBRARY_PHP_PAYMENTS', 2);
define('GATEWAY_BEANSTREAM', 29);
define('GATEWAY_AMAZON', 30);
define('GATEWAY_BLUEPAY', 31);
define('GATEWAY_BRAINTREE', 32);
define('GATEWAY_GOOGLE', 33);
define('GATEWAY_PSIGATE', 34);
define('GATEWAY_QUICKBOOKS', 35);
if (Auth::check() && !Session::has(SESSION_TIMEZONE)) if (Auth::check() && !Session::has(SESSION_TIMEZONE))
{ {

View File

@ -11,6 +11,15 @@
{{ Former::legend('Payment Gateway') }} {{ Former::legend('Payment Gateway') }}
{{Former::label('Lorem Ipsum goes here.')}}
<div class="two-column">
{{ Former::radios('recommendedGateway_id')
->label('Recommended Gateways')
->radios($recommendedGateways)
->class('recommended-gateway')}}
</div>
@if ($accountGateway) @if ($accountGateway)
{{ Former::populateField('gateway_id', $accountGateway->gateway_id) }} {{ Former::populateField('gateway_id', $accountGateway->gateway_id) }}
@foreach ($accountGateway->fields as $field => $junk) @foreach ($accountGateway->fields as $field => $junk)
@ -22,12 +31,14 @@
@endforeach @endforeach
@endif @endif
{{ Former::select('gateway_id')->label('Provider')->addOption('', '') {{ Former::select('gateway_id')->label('PayPal & Other Gateways')->addOption('', '')
->fromQuery($gateways, 'name', 'id')->onchange('setFieldsShown()'); }} ->dataClass('gateway-dropdown')
->fromQuery($gateways, 'name', 'id')
->onchange('setFieldsShown()'); }}
@foreach ($gateways as $gateway) @foreach ($gateways as $gateway)
<div id="gateway_{{ $gateway->id }}_div" style="display: none"> <div id="gateway_{{ $gateway->id }}_div" class='gateway-fields' style="display: none">
@foreach ($gateway->fields as $field => $details) @foreach ($gateway->fields as $field => $details)
@if (in_array($field, ['solutionType', 'landingPage', 'headerImageUrl', 'brandName'])) @if (in_array($field, ['solutionType', 'landingPage', 'headerImageUrl', 'brandName']))
@ -51,21 +62,54 @@
<script type="text/javascript"> <script type="text/javascript">
var gateways = {{ $gateways }};
function setFieldsShown() { function setFieldsShown() {
var val = $('#gateway_id').val(); var val = $('#gateway_id').val();
for (var i=0; i<gateways.length; i++) { var activeElement = $('.recommended-gateway[value=' + val + ']');
var gateway = gateways[i]; var recommendedRadios = $('#recommendedGateway_id');
if (val == gateway.id) {
$('#gateway_' + gateway.id + '_div').show(); $('.gateway-fields').hide();
} else { $('#gateway_' + val + '_div').show();
$('#gateway_' + gateway.id + '_div').hide();
} if(activeElement && !activeElement.attr('checked'))
{
activeElement.attr('checked', true);
} }
} }
$(function() { $(document).ready(function() {
$('.recommended-gateway').change(
function(){
var recVal = $(this).val();
$('#gateway_id').val(recVal);
setFieldsShown(); setFieldsShown();
}
);
$('select[data-class=gateway-dropdown]').change(function(){
$('.recommended-gateway').attr('checked', false);
var activeElement = $('.recommended-gateway[value=' + $(this).val() + ']');
if(activeElement)
{
activeElement.attr('checked', true);
}
});
$('.recommended-gateway').each(function(){
var contents = $(this).parent().contents();
contents[contents.length - 1].nodeValue = '';
$(this).after('<img src="' +$(this).attr('data-imageUrl') + '" /><br />');
$(this).parent().children().last().after('<a href="' + $(this).attr('data-siteUrl') + '">Create an account</a>');
if($(this).attr('data-newRow') && true)
{
}
});
setFieldsShown();
$('.two-column .form-group .col-lg-8').removeClass('col-lg-8');
$('.two-column .form-group .col-sm-8').removeClass('col-sm-8');
}); });
</script> </script>

View File

@ -19,7 +19,10 @@
'address1' => 'required', 'address1' => 'required',
'city' => 'required', 'city' => 'required',
'state' => 'required', 'state' => 'required',
'postal_code' => 'required' 'postal_code' => 'required',
'country' => 'required',
'phone' => 'required',
'email' => 'required'
)) }} )) }}
{{ Former::populate($client) }} {{ Former::populate($client) }}
{{ Former::populateField('first_name', $contact->first_name) }} {{ Former::populateField('first_name', $contact->first_name) }}
@ -70,6 +73,14 @@
{{ Former::text('state')->label('State/Province') }} {{ Former::text('state')->label('State/Province') }}
{{ Former::text('postal_code') }} {{ Former::text('postal_code') }}
<?php if(strtolower($gateway->name) == 'beanstream') { ?>
{{ Former::select('country')->addOption('','')->label('Country')
->fromQuery($countries, 'name', 'iso_3166_2') }}
{{ Former::text('phone') }}
{{ Former::text('email') }}
<?php } ?>
<?php echo($gateway->name); ?>
{{ Former::actions( Button::primary_submit_lg(trans('texts.pay_now') . ' - ' . Utils::formatMoney($invoice->amount, $client->currency_id) )) }} {{ Former::actions( Button::primary_submit_lg(trans('texts.pay_now') . ' - ' . Utils::formatMoney($invoice->amount, $client->currency_id) )) }}
</div> </div>

View File

@ -32,7 +32,8 @@
"app/database/migrations", "app/database/migrations",
"app/database/seeds", "app/database/seeds",
"app/tests/TestCase.php", "app/tests/TestCase.php",
"app/libraries" "app/libraries",
"vendor/php-payments/lib"
], ],
"psr-0" : { "psr-0" : {
"ninja" : "app/" "ninja" : "app/"

View File

@ -538,7 +538,19 @@ body {
transition: all 0.5s ease; transition: all 0.5s ease;
} }
/***********************************************
New/edit invoice page
************************************************/
.two-column .form-group div {
-webkit-column-count:2; /* Chrome, Safari, Opera */
-moz-column-count:2; /* Firefox */
column-count:2;
}
.two-column .form-group div .radio {
margin-left:10px;
}
/*********************************************** /***********************************************
Add mouse over drop down to header menu Add mouse over drop down to header menu

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.8 KiB