2013-11-26 13:45:07 +01:00
|
|
|
<?php
|
|
|
|
|
2014-01-06 19:03:00 +01:00
|
|
|
use ninja\repositories\PaymentRepository;
|
|
|
|
|
2013-11-26 13:45:07 +01:00
|
|
|
class PaymentController extends \BaseController
|
|
|
|
{
|
2014-01-06 19:03:00 +01:00
|
|
|
protected $creditRepo;
|
|
|
|
|
|
|
|
public function __construct(PaymentRepository $paymentRepo)
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->paymentRepo = $paymentRepo;
|
|
|
|
}
|
|
|
|
|
2013-11-26 13:45:07 +01:00
|
|
|
public function index()
|
|
|
|
{
|
2013-12-03 23:00:01 +01:00
|
|
|
return View::make('list', array(
|
|
|
|
'entityType'=>ENTITY_PAYMENT,
|
|
|
|
'title' => '- Payments',
|
2014-01-16 22:12:46 +01:00
|
|
|
'columns'=>['checkbox', 'Invoice', 'Client', 'Transaction Reference', 'Method', 'Payment Amount', 'Payment Date', 'Action']
|
2013-12-03 23:00:01 +01:00
|
|
|
));
|
2013-11-26 13:45:07 +01:00
|
|
|
}
|
|
|
|
|
2013-12-04 17:20:14 +01:00
|
|
|
public function getDatatable($clientPublicId = null)
|
2013-11-26 13:45:07 +01:00
|
|
|
{
|
2014-01-06 19:03:00 +01:00
|
|
|
$payments = $this->paymentRepo->find($clientPublicId, Input::get('sSearch'));
|
|
|
|
$table = Datatable::query($payments);
|
2013-11-29 13:09:21 +01:00
|
|
|
|
2013-12-04 17:20:14 +01:00
|
|
|
if (!$clientPublicId) {
|
|
|
|
$table->addColumn('checkbox', function($model) { return '<input type="checkbox" name="ids[]" value="' . $model->public_id . '">'; });
|
2013-11-29 13:09:21 +01:00
|
|
|
}
|
|
|
|
|
2014-01-16 22:12:46 +01:00
|
|
|
$table->addColumn('invoice_number', function($model) { return $model->invoice_public_id ? link_to('invoices/' . $model->invoice_public_id . '/edit', $model->invoice_number) : ''; });
|
2013-12-03 23:00:01 +01:00
|
|
|
|
2013-12-04 17:20:14 +01:00
|
|
|
if (!$clientPublicId) {
|
2013-12-30 21:17:45 +01:00
|
|
|
$table->addColumn('client_name', function($model) { return link_to('clients/' . $model->client_public_id, Utils::getClientDisplayName($model)); });
|
2014-01-16 22:12:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$table->addColumn('transaction_reference', function($model) { return $model->transaction_reference ? $model->transaction_reference : '<i>Manual entry</i>'; })
|
2014-02-04 14:16:22 +01:00
|
|
|
->addColumn('payment_type', function($model) { return $model->payment_type ? $model->payment_type : ($model->transaction_reference ? '<i>Online payment</i>' : ''); });
|
2013-12-03 23:00:01 +01:00
|
|
|
|
2014-01-16 22:12:46 +01:00
|
|
|
return $table->addColumn('amount', function($model) { return Utils::formatMoney($model->amount, $model->currency_id); })
|
2013-12-16 22:27:32 +01:00
|
|
|
->addColumn('payment_date', function($model) { return Utils::dateToString($model->payment_date); })
|
2013-12-05 16:23:24 +01:00
|
|
|
->addColumn('dropdown', function($model)
|
|
|
|
{
|
2013-12-05 21:25:20 +01:00
|
|
|
return '<div class="btn-group tr-action" style="visibility:hidden;">
|
2013-12-05 16:23:24 +01:00
|
|
|
<button type="button" class="btn btn-xs btn-default dropdown-toggle" data-toggle="dropdown">
|
|
|
|
Select <span class="caret"></span>
|
|
|
|
</button>
|
|
|
|
<ul class="dropdown-menu" role="menu">
|
2013-12-15 13:55:50 +01:00
|
|
|
<li><a href="javascript:archiveEntity(' . $model->public_id. ')">Archive Payment</a></li>
|
2013-12-05 16:23:24 +01:00
|
|
|
<li><a href="javascript:deleteEntity(' . $model->public_id. ')">Delete Payment</a></li>
|
|
|
|
</ul>
|
|
|
|
</div>';
|
|
|
|
})
|
2013-11-26 13:45:07 +01:00
|
|
|
->make();
|
|
|
|
}
|
|
|
|
|
2013-12-04 17:20:14 +01:00
|
|
|
|
2014-01-02 14:21:15 +01:00
|
|
|
public function create($clientPublicId = 0, $invoicePublicId = 0)
|
2013-12-04 17:20:14 +01:00
|
|
|
{
|
|
|
|
$data = array(
|
2014-01-14 12:52:56 +01:00
|
|
|
'clientPublicId' => Input::old('client') ? Input::old('client') : $clientPublicId,
|
|
|
|
'invoicePublicId' => Input::old('invoice') ? Input::old('invoice') : $invoicePublicId,
|
2013-12-05 16:23:24 +01:00
|
|
|
'invoice' => null,
|
2014-01-15 15:01:24 +01:00
|
|
|
'invoices' => Invoice::scope()->with('client', 'invoice_status')->orderBy('invoice_number')->get(),
|
2013-12-04 17:20:14 +01:00
|
|
|
'payment' => null,
|
|
|
|
'method' => 'POST',
|
2014-01-14 12:52:56 +01:00
|
|
|
'url' => "payments",
|
2013-12-05 16:23:24 +01:00
|
|
|
'title' => '- New Payment',
|
2014-01-15 15:01:24 +01:00
|
|
|
//'currencies' => Currency::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
|
2014-01-13 20:22:43 +01:00
|
|
|
'paymentTypes' => PaymentType::remember(DEFAULT_QUERY_CACHE)->orderBy('id')->get(),
|
2013-12-30 21:17:45 +01:00
|
|
|
'clients' => Client::scope()->with('contacts')->orderBy('name')->get());
|
2013-12-04 17:20:14 +01:00
|
|
|
|
|
|
|
return View::make('payments.edit', $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function edit($publicId)
|
|
|
|
{
|
|
|
|
$payment = Payment::scope($publicId)->firstOrFail();
|
2014-01-01 00:50:13 +01:00
|
|
|
$payment->payment_date = Utils::fromSqlDate($payment->payment_date);
|
|
|
|
|
2013-12-04 17:20:14 +01:00
|
|
|
$data = array(
|
2013-12-05 16:23:24 +01:00
|
|
|
'client' => null,
|
|
|
|
'invoice' => null,
|
2014-01-08 09:35:35 +01:00
|
|
|
'invoices' => Invoice::scope()->with('client', 'invoice_status')->orderBy('invoice_number')->get(array('public_id','invoice_number')),
|
2013-12-04 17:20:14 +01:00
|
|
|
'payment' => $payment,
|
|
|
|
'method' => 'PUT',
|
|
|
|
'url' => 'payments/' . $publicId,
|
2013-12-05 16:23:24 +01:00
|
|
|
'title' => '- Edit Payment',
|
2014-01-15 15:01:24 +01:00
|
|
|
//'currencies' => Currency::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
|
2014-01-13 20:22:43 +01:00
|
|
|
'paymentTypes' => PaymentType::remember(DEFAULT_QUERY_CACHE)->orderBy('id')->get(),
|
2013-12-30 21:17:45 +01:00
|
|
|
'clients' => Client::scope()->with('contacts')->orderBy('name')->get());
|
2013-12-04 17:20:14 +01:00
|
|
|
return View::make('payments.edit', $data);
|
|
|
|
}
|
|
|
|
|
2014-02-02 19:14:56 +01:00
|
|
|
private function createGateway($accountGateway)
|
|
|
|
{
|
|
|
|
$gateway = Omnipay::create($accountGateway->gateway->provider);
|
|
|
|
$config = json_decode($accountGateway->config);
|
|
|
|
|
|
|
|
/*
|
|
|
|
$gateway->setSolutionType ("Sole");
|
|
|
|
$gateway->setLandingPage("Billing");
|
|
|
|
*/
|
|
|
|
|
|
|
|
foreach ($config as $key => $val)
|
|
|
|
{
|
|
|
|
if (!$val)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$function = "set" . ucfirst($key);
|
|
|
|
$gateway->$function($val);
|
|
|
|
}
|
2014-03-19 00:28:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
if (!Utils::isProd())
|
|
|
|
{
|
|
|
|
$gateway->setTestMode(true);
|
|
|
|
}
|
|
|
|
*/
|
2014-02-02 19:14:56 +01:00
|
|
|
|
|
|
|
return $gateway;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getPaymentDetails($invoice, $input = null)
|
|
|
|
{
|
2014-03-19 00:28:43 +01:00
|
|
|
$key = $invoice->invoice_number . '_details';
|
|
|
|
|
2014-02-02 19:14:56 +01:00
|
|
|
if ($input)
|
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'firstName' => $input['first_name'],
|
|
|
|
'lastName' => $input['last_name'],
|
|
|
|
'number' => $input['card_number'],
|
|
|
|
'expiryMonth' => $input['expiration_month'],
|
|
|
|
'expiryYear' => $input['expiration_year'],
|
|
|
|
'cvv' => $input['cvv'],
|
|
|
|
'billingAddress1' => $input['address1'],
|
|
|
|
'billingAddress2' => $input['address2'],
|
|
|
|
'billingCity' => $input['city'],
|
|
|
|
'billingState' => $input['state'],
|
|
|
|
'billingPostcode' => $input['postal_code'],
|
|
|
|
'shippingAddress1' => $input['address1'],
|
|
|
|
'shippingAddress2' => $input['address2'],
|
|
|
|
'shippingCity' => $input['city'],
|
|
|
|
'shippingState' => $input['state'],
|
|
|
|
'shippingPostcode' => $input['postal_code'],
|
|
|
|
];
|
|
|
|
|
2014-03-19 00:28:43 +01:00
|
|
|
Session::put($key, $data);
|
|
|
|
}
|
|
|
|
else if (Session::get($key))
|
|
|
|
{
|
|
|
|
$data = Session::get($key);
|
2014-02-02 19:14:56 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-03-19 00:28:43 +01:00
|
|
|
$data = [];
|
2014-02-02 19:14:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$card = new CreditCard($data);
|
|
|
|
|
|
|
|
return [
|
|
|
|
'amount' => $invoice->amount,
|
|
|
|
'card' => $card,
|
|
|
|
'currency' => $invoice->client->currency->code,
|
|
|
|
'returnUrl' => URL::to('complete'),
|
2014-02-02 21:37:37 +01:00
|
|
|
'cancelUrl' => URL::to('/')
|
2014-02-02 19:14:56 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function show_payment($invitationKey)
|
|
|
|
{
|
2014-03-19 00:28:43 +01:00
|
|
|
// For PayPal Express we redirect straight to their site
|
2014-03-19 00:33:13 +01:00
|
|
|
$invitation = Invitation::with('invoice.client.account')->where('invitation_key', '=', $invitationKey)->firstOrFail();
|
|
|
|
$account = $invitation->invoice->client->account;
|
|
|
|
if ($account->isGatewayConfigured(GATEWAY_PAYPAL_EXPRESS))
|
2014-03-19 00:28:43 +01:00
|
|
|
{
|
|
|
|
return self::do_payment($invitationKey, false);
|
|
|
|
}
|
|
|
|
|
2014-02-02 19:14:56 +01:00
|
|
|
$invitation = Invitation::with('contact', 'invoice.client')->where('invitation_key', '=', $invitationKey)->firstOrFail();
|
|
|
|
$invoice = $invitation->invoice;
|
|
|
|
$client = $invoice->client;
|
|
|
|
|
|
|
|
$data = [
|
2014-02-17 00:09:34 +01:00
|
|
|
'showBreadcrumbs' => false,
|
2014-02-02 19:14:56 +01:00
|
|
|
'invitationKey' => $invitationKey,
|
|
|
|
'invoice' => $invoice,
|
|
|
|
'client' => $client,
|
|
|
|
'contact' => $invitation->contact
|
|
|
|
];
|
|
|
|
|
|
|
|
return View::make('payments.payment', $data);
|
|
|
|
}
|
|
|
|
|
2014-03-19 00:28:43 +01:00
|
|
|
public function do_payment($invitationKey, $onSite = true)
|
2014-02-02 19:14:56 +01:00
|
|
|
{
|
|
|
|
$rules = array(
|
|
|
|
'first_name' => 'required',
|
|
|
|
'last_name' => 'required',
|
|
|
|
'card_number' => 'required',
|
|
|
|
'expiration_month' => 'required',
|
|
|
|
'expiration_year' => 'required',
|
|
|
|
'cvv' => 'required',
|
|
|
|
'address1' => 'required',
|
|
|
|
'city' => 'required',
|
|
|
|
'state' => 'required',
|
|
|
|
'postal_code' => 'required',
|
|
|
|
);
|
|
|
|
|
2014-03-19 00:28:43 +01:00
|
|
|
if ($onSite)
|
2014-02-02 19:14:56 +01:00
|
|
|
{
|
2014-03-19 00:28:43 +01:00
|
|
|
$validator = Validator::make(Input::all(), $rules);
|
|
|
|
|
|
|
|
if ($validator->fails())
|
|
|
|
{
|
|
|
|
return Redirect::to('payment/' . $invitationKey)
|
|
|
|
->withErrors($validator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$invitation = Invitation::with('invoice.invoice_items', 'invoice.client.currency', 'invoice.client.account.account_gateways.gateway')->where('invitation_key', '=', $invitationKey)->firstOrFail();
|
|
|
|
$invoice = $invitation->invoice;
|
|
|
|
$accountGateway = $invoice->client->account->account_gateways[0];
|
|
|
|
$gateway = self::createGateway($accountGateway);
|
2014-02-02 19:14:56 +01:00
|
|
|
|
2014-03-19 00:28:43 +01:00
|
|
|
if ($onSite)
|
|
|
|
{
|
2014-02-02 21:37:37 +01:00
|
|
|
$client = $invoice->client;
|
|
|
|
$client->address1 = trim(Input::get('address1'));
|
|
|
|
$client->address2 = trim(Input::get('address2'));
|
|
|
|
$client->city = trim(Input::get('city'));
|
|
|
|
$client->state = trim(Input::get('state'));
|
|
|
|
$client->postal_code = trim(Input::get('postal_code'));
|
|
|
|
$client->save();
|
2014-03-19 00:28:43 +01:00
|
|
|
}
|
2014-02-02 21:37:37 +01:00
|
|
|
|
2014-03-19 00:28:43 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
$details = self::getPaymentDetails($invoice, Input::all());
|
|
|
|
$response = $gateway->purchase($details)->send();
|
|
|
|
$ref = $response->getTransactionReference();
|
|
|
|
|
|
|
|
if (!$ref)
|
2014-02-02 19:14:56 +01:00
|
|
|
{
|
2014-03-19 00:28:43 +01:00
|
|
|
Session::flash('error', $response->getMessage());
|
2014-02-02 19:14:56 +01:00
|
|
|
return Redirect::to('payment/' . $invitationKey)
|
|
|
|
->withInput();
|
|
|
|
}
|
2014-03-19 00:28:43 +01:00
|
|
|
|
|
|
|
if ($response->isSuccessful())
|
|
|
|
{
|
|
|
|
$payment = self::createPayment($invitation, $ref);
|
|
|
|
|
|
|
|
$invoice->invoice_status_id = INVOICE_STATUS_PAID;
|
|
|
|
$invoice->save();
|
|
|
|
|
|
|
|
Event::fire('invoice.paid', $payment);
|
|
|
|
|
|
|
|
Session::flash('message', 'Successfully applied payment');
|
|
|
|
return Redirect::to('view/' . $payment->invitation->invitation_key);
|
|
|
|
}
|
|
|
|
else if ($response->isRedirect())
|
|
|
|
{
|
|
|
|
$invitation->transaction_reference = $ref;
|
|
|
|
$invitation->save();
|
|
|
|
|
|
|
|
$response->redirect();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Session::flash('error', $response->getMessage());
|
|
|
|
return Utils::fatalError('Sorry, there was an error processing your payment. Please try again later.<p>', $response->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (\Exception $e)
|
|
|
|
{
|
|
|
|
Session::flash('error', $e->getMessage());
|
|
|
|
return Redirect::to('payment/' . $invitationKey)
|
|
|
|
->withInput();
|
2014-02-02 19:14:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function createPayment($invitation, $ref, $payerId = null)
|
|
|
|
{
|
|
|
|
$invoice = $invitation->invoice;
|
|
|
|
$accountGateway = $invoice->client->account->account_gateways[0];
|
|
|
|
|
|
|
|
$payment = Payment::createNew();
|
|
|
|
$payment->invitation_id = $invitation->id;
|
|
|
|
$payment->account_gateway_id = $accountGateway->id;
|
|
|
|
$payment->invoice_id = $invoice->id;
|
|
|
|
$payment->amount = $invoice->amount;
|
|
|
|
$payment->client_id = $invoice->client_id;
|
|
|
|
$payment->contact_id = $invitation->contact_id;
|
|
|
|
$payment->transaction_reference = $ref;
|
|
|
|
$payment->payment_date = date_create()->format('Y-m-d');
|
|
|
|
|
|
|
|
if ($payerId)
|
|
|
|
{
|
|
|
|
$payment->payer_id = $payerId;
|
|
|
|
}
|
|
|
|
|
|
|
|
$payment->save();
|
|
|
|
|
|
|
|
return $payment;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function offsite_payment()
|
|
|
|
{
|
|
|
|
$payerId = Request::query('PayerID');
|
|
|
|
$token = Request::query('token');
|
|
|
|
|
|
|
|
$invitation = Invitation::with('invoice.client.currency', 'invoice.client.account.account_gateways.gateway')->where('transaction_reference', '=', $token)->firstOrFail();
|
|
|
|
$invoice = $invitation->invoice;
|
|
|
|
|
|
|
|
$accountGateway = $invoice->client->account->account_gateways[0];
|
|
|
|
$gateway = self::createGateway($accountGateway);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$details = self::getPaymentDetails($invoice);
|
|
|
|
$response = $gateway->completePurchase($details)->send();
|
|
|
|
$ref = $response->getTransactionReference();
|
|
|
|
|
|
|
|
if ($response->isSuccessful())
|
|
|
|
{
|
|
|
|
$payment = self::createPayment($invitation, $ref, $payerId);
|
|
|
|
|
|
|
|
$invoice->invoice_status_id = INVOICE_STATUS_PAID;
|
|
|
|
$invoice->save();
|
|
|
|
|
|
|
|
Event::fire('invoice.paid', $payment);
|
|
|
|
|
|
|
|
Session::flash('message', 'Successfully applied payment');
|
|
|
|
return Redirect::to('view/' . $invitation->invitation_key);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-02 19:52:31 +01:00
|
|
|
Session::flash('error', $response->getMessage());
|
2014-02-02 19:50:20 +01:00
|
|
|
return Utils::fatalError('Sorry, there was an error processing your payment. Please try again later.<p>', $response->getMessage());
|
2014-02-02 19:14:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (\Exception $e)
|
|
|
|
{
|
2014-02-02 19:52:31 +01:00
|
|
|
Session::flash('error', $e->getMessage());
|
2014-02-02 19:14:56 +01:00
|
|
|
return Utils::fatalError('Sorry, there was an error processing your payment. Please try again later.<p>', $e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-05 16:23:24 +01:00
|
|
|
public function store()
|
|
|
|
{
|
|
|
|
return $this->save();
|
|
|
|
}
|
2013-12-04 17:20:14 +01:00
|
|
|
|
2013-12-05 16:23:24 +01:00
|
|
|
public function update($publicId)
|
2013-12-01 21:58:25 +01:00
|
|
|
{
|
2013-12-05 16:23:24 +01:00
|
|
|
return $this->save($publicId);
|
|
|
|
}
|
2013-12-01 21:58:25 +01:00
|
|
|
|
2013-12-05 16:23:24 +01:00
|
|
|
private function save($publicId = null)
|
|
|
|
{
|
2014-01-16 22:12:46 +01:00
|
|
|
if ($errors = $this->paymentRepo->getErrors(Input::all()))
|
2014-01-06 19:03:00 +01:00
|
|
|
{
|
2013-12-05 16:23:24 +01:00
|
|
|
$url = $publicId ? 'payments/' . $publicId . '/edit' : 'payments/create';
|
|
|
|
return Redirect::to($url)
|
2014-01-16 22:12:46 +01:00
|
|
|
->withErrors($errors)
|
2013-12-05 16:23:24 +01:00
|
|
|
->withInput();
|
2014-01-06 19:03:00 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->paymentRepo->save($publicId, Input::all());
|
2013-12-05 16:23:24 +01:00
|
|
|
|
|
|
|
$message = $publicId ? 'Successfully updated payment' : 'Successfully created payment';
|
|
|
|
Session::flash('message', $message);
|
2014-01-01 00:50:13 +01:00
|
|
|
return Redirect::to('clients/' . Input::get('client'));
|
2013-12-05 16:23:24 +01:00
|
|
|
}
|
2013-12-01 21:58:25 +01:00
|
|
|
}
|
|
|
|
|
2013-12-05 16:23:24 +01:00
|
|
|
public function bulk()
|
2013-12-01 21:58:25 +01:00
|
|
|
{
|
2013-12-05 16:23:24 +01:00
|
|
|
$action = Input::get('action');
|
2013-12-05 21:25:20 +01:00
|
|
|
$ids = Input::get('id') ? Input::get('id') : Input::get('ids');
|
2014-01-06 19:03:00 +01:00
|
|
|
$count = $this->paymentRepo->bulk($ids, $action);
|
2013-12-01 21:58:25 +01:00
|
|
|
|
2014-01-12 19:55:33 +01:00
|
|
|
if ($count > 0)
|
|
|
|
{
|
2014-01-14 12:52:56 +01:00
|
|
|
$message = Utils::pluralize('Successfully '.$action.'d ? payment', $count);
|
2014-01-12 19:55:33 +01:00
|
|
|
Session::flash('message', $message);
|
|
|
|
}
|
|
|
|
|
2013-12-05 16:23:24 +01:00
|
|
|
return Redirect::to('payments');
|
2013-12-01 21:58:25 +01:00
|
|
|
}
|
2013-12-05 16:23:24 +01:00
|
|
|
|
2013-11-26 13:45:07 +01:00
|
|
|
}
|