1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/controllers/PaymentController.php

719 lines
28 KiB
PHP
Raw Normal View History

2013-11-26 13:45:07 +01:00
<?php
2014-01-06 19:03:00 +01:00
use ninja\repositories\PaymentRepository;
2014-05-25 16:25:58 +02:00
use ninja\repositories\InvoiceRepository;
2014-07-15 22:36:40 +02:00
use ninja\repositories\AccountRepository;
use ninja\mailers\ContactMailer;
2014-01-06 19:03:00 +01:00
2015-01-11 13:30:08 +01:00
class PaymentController extends \BaseController
2013-11-26 13:45:07 +01:00
{
2014-01-06 19:03:00 +01:00
protected $creditRepo;
public function __construct(PaymentRepository $paymentRepo, InvoiceRepository $invoiceRepo, AccountRepository $accountRepo, ContactMailer $contactMailer)
2014-01-06 19:03:00 +01:00
{
parent::__construct();
$this->paymentRepo = $paymentRepo;
2014-05-25 16:25:58 +02:00
$this->invoiceRepo = $invoiceRepo;
2014-07-15 22:36:40 +02:00
$this->accountRepo = $accountRepo;
$this->contactMailer = $contactMailer;
2015-01-11 13:30:08 +01:00
}
2014-01-06 19:03:00 +01:00
public function index()
{
2013-12-03 23:00:01 +01:00
return View::make('list', array(
2015-01-11 13:30:08 +01:00
'entityType' => ENTITY_PAYMENT,
2014-06-15 22:54:58 +02:00
'title' => trans('texts.payments'),
2015-01-11 13:30:08 +01:00
'columns' => Utils::trans(['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
public function clientIndex()
{
return View::make('public_list', array(
'showClientHeader' => true,
'hideLogo' => Session::get('white_label'),
2015-01-11 13:30:08 +01:00
'entityType' => ENTITY_PAYMENT,
'title' => trans('texts.payments'),
2015-01-11 13:30:08 +01:00
'columns' => Utils::trans(['invoice', 'transaction_reference', 'method', 'payment_amount', 'payment_date']),
));
}
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'));
2015-01-11 13:30:08 +01:00
$table = Datatable::query($payments);
2013-11-29 13:09:21 +01:00
2013-12-04 17:20:14 +01:00
if (!$clientPublicId) {
2015-01-11 13:30:08 +01:00
$table->addColumn('checkbox', function ($model) { return '<input type="checkbox" name="ids[]" value="'.$model->public_id.'" '.Utils::getEntityRowClass($model).'>'; });
2013-11-29 13:09:21 +01:00
}
2015-01-11 13:30:08 +01:00
$table->addColumn('invoice_number', function ($model) { return $model->invoice_public_id ? link_to('invoices/'.$model->invoice_public_id.'/edit', $model->invoice_number, ['class' => Utils::getEntityRowClass($model)]) : ''; });
2013-12-03 23:00:01 +01:00
2013-12-04 17:20:14 +01:00
if (!$clientPublicId) {
2015-01-11 13:30:08 +01:00
$table->addColumn('client_name', function ($model) { return link_to('clients/'.$model->client_public_id, Utils::getClientDisplayName($model)); });
}
$table->addColumn('transaction_reference', function ($model) { return $model->transaction_reference ? $model->transaction_reference : '<i>Manual entry</i>'; })
->addColumn('payment_type', function ($model) { return $model->payment_type ? $model->payment_type : ($model->account_gateway_id ? '<i>Online payment</i>' : ''); });
return $table->addColumn('amount', function ($model) { return Utils::formatMoney($model->amount, $model->currency_id); })
->addColumn('payment_date', function ($model) { return Utils::dateToString($model->payment_date); })
->addColumn('dropdown', function ($model) {
if ($model->is_deleted) {
2014-11-23 22:47:10 +01:00
return '<div style="height:38px"/>';
}
2015-01-11 13:30:08 +01:00
2014-11-23 22:47:10 +01:00
$str = '<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">
2014-03-27 13:25:31 +01:00
'.trans('texts.select').' <span class="caret"></span>
2013-12-05 16:23:24 +01:00
</button>
2014-11-23 22:47:10 +01:00
<ul class="dropdown-menu" role="menu">';
2015-01-11 13:30:08 +01:00
if (!$model->deleted_at || $model->deleted_at == '0000-00-00') {
$str .= '<li><a href="javascript:archiveEntity('.$model->public_id.')">'.trans('texts.archive_payment').'</a></li>';
} else {
$str .= '<li><a href="javascript:restoreEntity('.$model->public_id.')">'.trans('texts.restore_payment').'</a></li>';
2014-11-23 22:47:10 +01:00
}
2015-01-11 13:30:08 +01:00
return $str.'<li><a href="javascript:deleteEntity('.$model->public_id.')">'.trans('texts.delete_payment').'</a></li></ul>
2013-12-05 16:23:24 +01:00
</div>';
2015-01-11 13:30:08 +01:00
})
->make();
2013-11-26 13:45:07 +01:00
}
public function getClientDatatable()
{
$search = Input::get('sSearch');
$invitationKey = Session::get('invitation_key');
$invitation = Invitation::where('invitation_key', '=', $invitationKey)->with('contact.client')->first();
2015-01-11 13:30:08 +01:00
if (!$invitation) {
return [];
}
$invoice = $invitation->invoice;
2015-01-11 13:30:08 +01:00
if (!$invoice || $invoice->is_deleted) {
return [];
}
$payments = $this->paymentRepo->findForContact($invitation->contact->id, Input::get('sSearch'));
return Datatable::query($payments)
2015-01-11 13:30:08 +01:00
->addColumn('invoice_number', function ($model) { return $model->invitation_key ? link_to('/view/'.$model->invitation_key, $model->invoice_number) : $model->invoice_number; })
->addColumn('transaction_reference', function ($model) { return $model->transaction_reference ? $model->transaction_reference : '<i>Manual entry</i>'; })
->addColumn('payment_type', function ($model) { return $model->payment_type ? $model->payment_type : ($model->account_gateway_id ? '<i>Online payment</i>' : ''); })
->addColumn('amount', function ($model) { return Utils::formatMoney($model->amount, $model->currency_id); })
->addColumn('payment_date', function ($model) { return Utils::dateToString($model->payment_date); })
->make();
}
2014-01-02 14:21:15 +01:00
public function create($clientPublicId = 0, $invoicePublicId = 0)
2015-01-11 13:30:08 +01:00
{
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-05-20 23:40:09 +02:00
'invoices' => Invoice::scope()->where('is_recurring', '=', false)->where('is_quote', '=', false)
->with('client', 'invoice_status')->orderBy('invoice_number')->get(),
2015-01-11 13:30:08 +01:00
'payment' => null,
'method' => 'POST',
'url' => "payments",
2014-06-15 22:54:58 +02:00
'title' => trans('texts.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(),
2015-01-11 13:30:08 +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)
{
2015-01-11 13:30:08 +01:00
$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-05-20 23:40:09 +02:00
'invoices' => Invoice::scope()->where('is_recurring', '=', false)->where('is_quote', '=', false)
->with('client', 'invoice_status')->orderBy('invoice_number')->get(),
2015-01-11 13:30:08 +01:00
'payment' => $payment,
'method' => 'PUT',
'url' => 'payments/'.$publicId,
2014-06-15 22:54:58 +02: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(),
2015-01-11 13:30:08 +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)
{
2015-01-11 13:30:08 +01:00
$gateway = Omnipay::create($accountGateway->gateway->provider);
2014-02-02 19:14:56 +01:00
$config = json_decode($accountGateway->config);
2015-01-11 13:30:08 +01:00
2014-02-02 19:14:56 +01:00
/*
2014-05-25 20:38:40 +02:00
$gateway->setSolutionType("Sole");
2014-02-02 19:14:56 +01:00
$gateway->setLandingPage("Billing");
*/
2015-01-11 13:30:08 +01:00
foreach ($config as $key => $val) {
if (!$val) {
2014-02-02 19:14:56 +01:00
continue;
}
2015-01-11 13:30:08 +01:00
$function = "set".ucfirst($key);
2014-02-02 19:14:56 +01:00
$gateway->$function($val);
}
2015-01-11 13:30:08 +01:00
if (Utils::isNinjaDev()) {
$gateway->setTestMode(true);
}
2014-05-25 20:38:40 +02:00
2015-01-11 13:30:08 +01:00
return $gateway;
2014-02-02 19:14:56 +01:00
}
private function getLicensePaymentDetails($input, $affiliate)
2014-07-15 22:36:40 +02:00
{
$data = self::convertInputForOmnipay($input);
$card = new CreditCard($data);
2015-01-11 13:30:08 +01:00
2014-07-15 22:36:40 +02:00
return [
'amount' => $affiliate->price,
2014-07-15 22:36:40 +02:00
'card' => $card,
'currency' => 'USD',
'returnUrl' => URL::to('license_complete'),
'cancelUrl' => URL::to('/')
];
}
private function convertInputForOmnipay($input)
{
return [
'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-02-02 19:14:56 +01:00
private function getPaymentDetails($invoice, $input = null)
{
2015-01-11 13:30:08 +01:00
$key = $invoice->invoice_number.'_details';
$gateway = $invoice->client->account->account_gateways[0]->gateway;
$paymentLibrary = $gateway->paymentlibrary;
$currencyCode = $invoice->client->currency ? $invoice->client->currency->code : ($invoice->account->currency ? $invoice->account->currency->code : 'USD');
2015-01-11 13:30:08 +01:00
if ($input && $paymentLibrary->id == PAYMENT_LIBRARY_OMNIPAY) {
2014-07-15 22:36:40 +02:00
$data = self::convertInputForOmnipay($input);
2014-02-02 19:14:56 +01:00
Session::put($key, $data);
2015-01-11 13:30:08 +01:00
} elseif ($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'],
2014-11-25 19:40:54 +01:00
'currency_code' => $currencyCode,
];
2015-01-11 13:30:08 +01:00
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;
}
2015-01-11 13:30:08 +01:00
if (strlen($data['cc_exp']) == 5) {
$data['cc_exp'] = '0'.$data['cc_exp'];
}
Session::put($key, $data);
2015-01-11 13:30:08 +01:00
return $data;
2015-01-11 13:30:08 +01:00
} elseif (Session::get($key)) {
$data = Session::get($key);
2015-01-11 13:30:08 +01:00
} else {
$data = [];
2014-02-02 19:14:56 +01:00
}
2015-01-11 13:30:08 +01:00
if ($paymentLibrary->id == PAYMENT_LIBRARY_OMNIPAY) {
$card = new CreditCard($data);
2015-01-11 13:30:08 +01:00
return [
'amount' => $invoice->amount,
'card' => $card,
2014-11-25 19:40:54 +01:00
'currency' => $currencyCode,
'returnUrl' => URL::to('complete'),
'cancelUrl' => URL::to('/')
];
2015-01-11 13:30:08 +01:00
} else {
return $data;
}
2014-02-02 19:14:56 +01:00
}
2015-01-11 13:30:08 +01:00
2014-02-02 19:14:56 +01:00
public function show_payment($invitationKey)
{
// For PayPal Express we redirect straight to their site
$invitation = Invitation::with('invoice.client.account', 'invoice.client.account.account_gateways.gateway')->where('invitation_key', '=', $invitationKey)->firstOrFail();
$account = $invitation->invoice->client->account;
2015-01-11 13:30:08 +01:00
if ($account->isGatewayConfigured(GATEWAY_PAYPAL_EXPRESS)) {
if (Session::has('error')) {
2014-03-20 23:12:07 +01:00
Session::reflash();
2015-01-11 13:30:08 +01:00
return Redirect::to('view/'.$invitationKey);
} else {
2014-03-20 23:12:07 +01:00
return self::do_payment($invitationKey, false);
2015-01-11 13:30:08 +01:00
}
}
$invitation = Invitation::with('invoice.invoice_items', 'invoice.client.currency', 'invoice.client.account.account_gateways.gateway')->where('invitation_key', '=', $invitationKey)->firstOrFail();
2015-01-11 13:30:08 +01:00
$invoice = $invitation->invoice;
$client = $invoice->client;
2015-01-11 13:30:08 +01:00
$accountGateway = $invoice->client->account->account_gateways[0];
$gateway = $invoice->client->account->account_gateways[0]->gateway;
$paymentLibrary = $gateway->paymentlibrary;
$acceptedCreditCardTypes = $accountGateway->getCreditcardTypes();
2014-02-02 19:14:56 +01:00
$data = [
2014-02-17 00:09:34 +01:00
'showBreadcrumbs' => false,
2014-07-15 22:36:40 +02:00
'hideHeader' => true,
2015-01-11 13:30:08 +01:00
'url' => 'payment/'.$invitationKey,
2014-07-15 22:36:40 +02:00
'amount' => $invoice->amount,
2014-02-02 19:14:56 +01:00
'client' => $client,
'contact' => $invitation->contact,
'paymentLibrary' => $paymentLibrary,
'gateway' => $gateway,
2015-01-11 13:30:08 +01:00
'acceptedCreditCardTypes' => $acceptedCreditCardTypes,
2014-07-15 22:36:40 +02:00
'countries' => Country::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
2015-01-11 13:30:08 +01:00
'currencyId' => $client->currency_id,
2014-07-15 22:36:40 +02:00
];
return View::make('payments.payment', $data);
}
2015-01-11 13:30:08 +01:00
2014-07-15 22:36:40 +02:00
public function show_license_payment()
{
2015-01-11 13:30:08 +01:00
if (Input::has('return_url')) {
2014-07-15 22:36:40 +02:00
Session::set('return_url', Input::get('return_url'));
2015-01-11 13:30:08 +01:00
}
if (Input::has('affiliate_key')) {
if ($affiliate = Affiliate::where('affiliate_key', '=', Input::get('affiliate_key'))->first()) {
Session::set('affiliate_id', $affiliate->id);
2014-07-15 22:36:40 +02:00
}
}
Session::set('product_id', Input::get('product_id', PRODUCT_ONE_CLICK_INSTALL));
2015-01-11 13:30:08 +01:00
if (!Session::get('affiliate_id')) {
return Utils::fatalError();
2014-07-15 22:36:40 +02:00
}
2015-01-11 13:30:08 +01:00
if (Utils::isNinjaDev() && Input::has('test_mode')) {
2014-07-15 22:36:40 +02:00
Session::set('test_mode', Input::get('test_mode'));
}
2015-01-11 13:30:08 +01:00
$account = $this->accountRepo->getNinjaAccount();
2014-07-15 22:36:40 +02:00
$account->load('account_gateways.gateway');
2015-01-11 13:30:08 +01:00
$accountGateway = $account->account_gateways[0];
2014-07-15 22:36:40 +02:00
$gateway = $accountGateway->gateway;
$paymentLibrary = $gateway->paymentlibrary;
$acceptedCreditCardTypes = $accountGateway->getCreditcardTypes();
$affiliate = Affiliate::find(Session::get('affiliate_id'));
$data = [
'showBreadcrumbs' => false,
'hideHeader' => true,
'url' => 'license',
'amount' => $affiliate->price,
2014-07-15 22:36:40 +02:00
'client' => false,
'contact' => false,
'paymentLibrary' => $paymentLibrary,
'gateway' => $gateway,
2015-01-11 13:30:08 +01:00
'acceptedCreditCardTypes' => $acceptedCreditCardTypes,
'countries' => Country::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
2014-07-15 22:36:40 +02:00
'currencyId' => 1,
'paymentTitle' => $affiliate->payment_title,
2015-01-11 13:30:08 +01:00
'paymentSubtitle' => $affiliate->payment_subtitle,
2014-02-02 19:14:56 +01:00
];
return View::make('payments.payment', $data);
}
2015-01-11 13:30:08 +01:00
public function do_license_payment()
2014-07-15 22:36:40 +02:00
{
$testMode = Session::get('test_mode') === 'true';
$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',
);
$validator = Validator::make(Input::all(), $rules);
2015-01-11 13:30:08 +01:00
if ($validator->fails()) {
2014-07-15 22:36:40 +02:00
return Redirect::to('license')
->withErrors($validator);
2015-01-11 13:30:08 +01:00
}
2014-07-15 22:36:40 +02:00
2015-01-11 13:30:08 +01:00
$account = $this->accountRepo->getNinjaAccount();
2014-07-15 22:36:40 +02:00
$account->load('account_gateways.gateway');
2015-01-11 13:30:08 +01:00
$accountGateway = $account->account_gateways[0];
2014-07-15 22:36:40 +02:00
2015-01-11 13:30:08 +01:00
try {
$affiliate = Affiliate::find(Session::get('affiliate_id'));
2015-01-11 13:30:08 +01:00
if ($testMode) {
2014-07-15 22:36:40 +02:00
$ref = 'TEST_MODE';
2015-01-11 13:30:08 +01:00
} else {
2014-07-15 22:36:40 +02:00
$gateway = self::createGateway($accountGateway);
$details = self::getLicensePaymentDetails(Input::all(), $affiliate);
2015-01-11 13:30:08 +01:00
$response = $gateway->purchase($details)->send();
2014-07-15 23:12:32 +02:00
$ref = $response->getTransactionReference();
2015-01-11 13:30:08 +01:00
if (!$ref) {
Session::flash('error', $response->getMessage());
2014-07-15 22:36:40 +02:00
return Redirect::to('license')->withInput();
}
2015-01-11 13:30:08 +01:00
if (!$response->isSuccessful()) {
Session::flash('error', $response->getMessage());
2014-07-15 22:36:40 +02:00
Utils::logError($response->getMessage());
2015-01-11 13:30:08 +01:00
return Redirect::to('license')->withInput();
}
2014-07-15 22:36:40 +02:00
}
2014-07-30 19:33:57 +02:00
$licenseKey = Utils::generateLicense();
2015-01-11 13:30:08 +01:00
$license = new License();
2014-07-15 22:36:40 +02:00
$license->first_name = Input::get('first_name');
$license->last_name = Input::get('last_name');
$license->email = Input::get('email');
$license->transaction_reference = $ref;
2014-07-30 19:33:57 +02:00
$license->license_key = $licenseKey;
2014-07-15 22:36:40 +02:00
$license->affiliate_id = Session::get('affiliate_id');
$license->product_id = Session::get('product_id');
2015-01-11 13:30:08 +01:00
$license->save();
2014-07-15 22:36:40 +02:00
2014-07-30 19:33:57 +02:00
$data = [
2014-08-05 16:22:51 +02:00
'message' => $affiliate->payment_subtitle,
2014-07-30 19:33:57 +02:00
'license' => $licenseKey,
2015-01-11 13:30:08 +01:00
'hideHeader' => true,
2014-07-30 19:33:57 +02:00
];
$name = "{$license->first_name} {$license->last_name}";
$this->contactMailer->sendLicensePaymentConfirmation($name, $license->email, $affiliate->price, $license->license_key, $license->product_id);
2015-01-11 13:30:08 +01:00
if (Session::has('return_url')) {
return Redirect::away(Session::get('return_url')."?license_key={$license->license_key}&product_id=".Session::get('product_id'));
} else {
return View::make('public.license', $data);
2015-01-11 13:30:08 +01:00
}
} catch (\Exception $e) {
2014-07-15 22:36:40 +02:00
$errorMessage = trans('texts.payment_error');
2015-01-11 13:30:08 +01:00
Session::flash('error', $errorMessage);
2014-12-03 23:05:38 +01:00
Utils::logError(Utils::getErrorString($e));
2015-01-11 13:30:08 +01:00
2014-07-15 22:36:40 +02:00
return Redirect::to('license')->withInput();
2015-01-11 13:30:08 +01:00
}
2014-07-15 22:36:40 +02:00
}
public function claim_license()
{
$licenseKey = Input::get('license_key');
$productId = Input::get('product_id', PRODUCT_ONE_CLICK_INSTALL);
$license = License::where('license_key', '=', $licenseKey)
->where('is_claimed', '=', false)
->where('product_id', '=', $productId)
->first();
2014-07-15 22:36:40 +02:00
2015-01-11 13:30:08 +01:00
if ($license) {
if ($license->transaction_reference != 'TEST_MODE') {
2014-08-05 16:22:51 +02:00
$license->is_claimed = true;
$license->save();
}
2014-07-15 22:36:40 +02:00
return $productId == PRODUCT_INVOICE_DESIGNS ? $_ENV['INVOICE_DESIGNS'] : 'valid';
2015-01-11 13:30:08 +01:00
} else {
2014-07-15 22:36:40 +02:00
return 'invalid';
}
}
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',
);
2015-01-11 13:30:08 +01:00
if ($onSite) {
$validator = Validator::make(Input::all(), $rules);
2015-01-11 13:30:08 +01:00
if ($validator->fails()) {
return Redirect::to('payment/'.$invitationKey)
->withErrors($validator);
2015-01-11 13:30:08 +01:00
}
}
$invitation = Invitation::with('invoice.invoice_items', 'invoice.client.currency', 'invoice.client.account.account_gateways.gateway')->where('invitation_key', '=', $invitationKey)->firstOrFail();
2015-01-11 13:30:08 +01:00
$invoice = $invitation->invoice;
$accountGateway = $invoice->client->account->account_gateways[0];
$paymentLibrary = $accountGateway->gateway->paymentlibrary;
2014-02-02 19:14:56 +01:00
2015-01-11 13:30:08 +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();
}
2015-01-11 13:30:08 +01:00
try {
if ($paymentLibrary->id == PAYMENT_LIBRARY_OMNIPAY) {
$gateway = self::createGateway($accountGateway);
$details = self::getPaymentDetails($invoice, Input::all());
2015-01-11 13:30:08 +01:00
$response = $gateway->purchase($details)->send();
$ref = $response->getTransactionReference();
2015-01-11 13:30:08 +01:00
if (!$ref) {
Session::flash('error', $response->getMessage());
return Redirect::to('payment/'.$invitationKey)
->withInput();
}
2015-01-11 13:30:08 +01:00
if ($response->isSuccessful()) {
$payment = self::createPayment($invitation, $ref);
2015-01-11 13:30:08 +01:00
Session::flash('message', trans('texts.applied_payment'));
return Redirect::to('view/'.$payment->invitation->invitation_key);
} elseif ($response->isRedirect()) {
$invitation->transaction_reference = $ref;
$invitation->save();
2015-01-11 13:30:08 +01:00
$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());
}
2015-01-11 13:30:08 +01:00
} elseif ($paymentLibrary->id == PAYMENT_LIBRARY_PHP_PAYMENTS) {
$gateway = $accountGateway->gateway;
$provider = $gateway->provider;
$p = new PHP_Payments(array('mode' => 'test'));
2015-01-11 13:30:08 +01:00
$config = Payment_Utility::load('config', 'drivers/'.$provider);
2015-01-11 13:30:08 +01:00
switch ($gateway->id) {
case GATEWAY_BEANSTREAM:
2015-01-11 13:30:08 +01:00
$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;
}
2015-01-11 13:30:08 +01:00
$details = self::getPaymentDetails($invoice, Input::all());
2015-01-11 13:30:08 +01:00
$response = $p->oneoff_payment($provider, $details, $config);
2015-01-11 13:30:08 +01:00
if (strtolower($response->status) == 'success') {
$payment = self::createPayment($invitation, $response->response_message);
2015-01-11 13:30:08 +01:00
Session::flash('message', trans('texts.applied_payment'));
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);
}
}
2015-01-11 13:30:08 +01:00
} catch (\Exception $e) {
2014-04-02 14:28:23 +02:00
$errorMessage = trans('texts.payment_error');
2015-01-11 13:30:08 +01:00
Session::flash('error', $errorMessage);
2014-12-03 23:05:38 +01:00
Utils::logError(Utils::getErrorString($e));
2015-01-11 13:30:08 +01:00
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];
2014-05-25 15:40:09 +02:00
2015-01-11 13:30:08 +01:00
if ($invoice->account->account_key == NINJA_ACCOUNT_KEY) {
2014-05-25 15:40:09 +02:00
$account = Account::find($invoice->client->public_id);
$account->pro_plan_paid = date_create()->format('Y-m-d');
$account->save();
}
2015-01-11 13:30:08 +01:00
if ($invoice->is_quote) {
2014-05-25 16:48:00 +02:00
$invoice = $this->invoiceRepo->cloneInvoice($invoice, $invoice->id);
2014-05-25 15:40:09 +02:00
}
2015-01-11 13:30:08 +01:00
2014-03-20 23:12:07 +01:00
$payment = Payment::createNew($invitation);
2014-02-02 19:14:56 +01:00
$payment->invitation_id = $invitation->id;
$payment->account_gateway_id = $accountGateway->id;
$payment->invoice_id = $invoice->id;
2015-01-11 13:30:08 +01:00
$payment->amount = $invoice->amount;
2014-02-02 19:14:56 +01:00
$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');
2015-01-11 13:30:08 +01:00
if ($payerId) {
$payment->payer_id = $payerId;
2014-02-02 19:14:56 +01:00
}
2015-01-11 13:30:08 +01:00
2014-02-02 19:14:56 +01:00
$payment->save();
2015-01-11 13:30:08 +01:00
2014-05-25 16:25:58 +02:00
Event::fire('invoice.paid', $payment);
2015-01-11 13:30:08 +01:00
2014-02-02 19:14:56 +01:00
return $payment;
}
public function offsite_payment()
{
$payerId = Request::query('PayerID');
2015-01-11 13:30:08 +01:00
$token = Request::query('token');
2014-02-02 19:14:56 +01:00
$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);
2015-01-11 13:30:08 +01:00
try {
2014-02-02 19:14:56 +01:00
$details = self::getPaymentDetails($invoice);
$response = $gateway->completePurchase($details)->send();
$ref = $response->getTransactionReference();
2015-01-11 13:30:08 +01:00
if ($response->isSuccessful()) {
$payment = self::createPayment($invitation, $ref, $payerId);
2014-02-02 19:14:56 +01:00
2015-01-11 13:30:08 +01:00
Session::flash('message', trans('texts.applied_payment'));
return Redirect::to('view/'.$invitation->invitation_key);
} else {
$errorMessage = trans('texts.payment_error')."\n\n".$response->getMessage();
Session::flash('error', $errorMessage);
2014-04-02 14:28:23 +02:00
Utils::logError($errorMessage);
2015-01-11 13:30:08 +01:00
return Redirect::to('view/'.$invitation->invitation_key);
2014-02-02 19:14:56 +01:00
}
2015-01-11 13:30:08 +01:00
} catch (\Exception $e) {
2014-04-02 14:28:23 +02:00
$errorMessage = trans('texts.payment_error');
2015-01-11 13:30:08 +01:00
Session::flash('error', $errorMessage);
Utils::logError($errorMessage."\n\n".$e->getMessage());
return Redirect::to('view/'.$invitation->invitation_key);
2014-02-02 19:14:56 +01:00
}
}
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)
{
2015-01-11 13:30:08 +01:00
if ($errors = $this->paymentRepo->getErrors(Input::all())) {
$url = $publicId ? 'payments/'.$publicId.'/edit' : 'payments/create';
2013-12-05 16:23:24 +01:00
return Redirect::to($url)
2014-01-16 22:12:46 +01:00
->withErrors($errors)
2013-12-05 16:23:24 +01:00
->withInput();
2015-01-11 13:30:08 +01:00
} else {
2014-01-06 19:03:00 +01:00
$this->paymentRepo->save($publicId, Input::all());
2013-12-05 16:23:24 +01:00
2014-04-02 14:28:23 +02:00
Session::flash('message', trans('texts.created_payment'));
2015-01-11 13:30:08 +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
2015-01-11 13:30:08 +01:00
if ($count > 0) {
$message = Utils::pluralize($action.'d_payment', $count);
2014-01-12 19:55:33 +01:00
Session::flash('message', $message);
}
2015-01-11 13:30:08 +01:00
2013-12-05 16:23:24 +01:00
return Redirect::to('payments');
2013-12-01 21:58:25 +01:00
}
2015-01-11 13:30:08 +01:00
}