2015-03-17 02:30:56 +01:00
|
|
|
<?php namespace App\Http\Controllers;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-03-26 07:24:02 +01:00
|
|
|
use Datatable;
|
|
|
|
use Input;
|
|
|
|
use Redirect;
|
2015-04-15 18:35:41 +02:00
|
|
|
use Request;
|
2015-03-26 07:24:02 +01:00
|
|
|
use Session;
|
|
|
|
use Utils;
|
|
|
|
use View;
|
2015-04-02 15:06:16 +02:00
|
|
|
use Validator;
|
|
|
|
use Omnipay;
|
|
|
|
use CreditCard;
|
|
|
|
use URL;
|
2015-04-08 15:19:17 +02:00
|
|
|
use Cache;
|
2015-03-31 19:42:37 +02:00
|
|
|
use App\Models\Invoice;
|
2015-04-02 15:06:16 +02:00
|
|
|
use App\Models\Invitation;
|
2015-03-31 19:42:37 +02:00
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\PaymentType;
|
2015-04-14 20:58:07 +02:00
|
|
|
use App\Models\License;
|
2015-04-15 18:35:41 +02:00
|
|
|
use App\Models\Payment;
|
2015-05-08 10:21:29 +02:00
|
|
|
use App\Models\Affiliate;
|
2015-03-26 07:24:02 +01:00
|
|
|
use App\Ninja\Repositories\PaymentRepository;
|
|
|
|
use App\Ninja\Repositories\InvoiceRepository;
|
|
|
|
use App\Ninja\Repositories\AccountRepository;
|
|
|
|
use App\Ninja\Mailers\ContactMailer;
|
2015-09-10 19:50:09 +02:00
|
|
|
use App\Services\PaymentService;
|
2015-03-26 07:24:02 +01:00
|
|
|
|
|
|
|
class PaymentController extends BaseController
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2015-09-10 19:50:09 +02:00
|
|
|
public function __construct(PaymentRepository $paymentRepo, InvoiceRepository $invoiceRepo, AccountRepository $accountRepo, ContactMailer $contactMailer, PaymentService $paymentService)
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->paymentRepo = $paymentRepo;
|
|
|
|
$this->invoiceRepo = $invoiceRepo;
|
|
|
|
$this->accountRepo = $accountRepo;
|
|
|
|
$this->contactMailer = $contactMailer;
|
2015-09-10 19:50:09 +02:00
|
|
|
$this->paymentService = $paymentService;
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
return View::make('list', array(
|
|
|
|
'entityType' => ENTITY_PAYMENT,
|
|
|
|
'title' => trans('texts.payments'),
|
|
|
|
'columns' => Utils::trans(['checkbox', 'invoice', 'client', 'transaction_reference', 'method', 'payment_amount', 'payment_date', 'action']),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function clientIndex()
|
|
|
|
{
|
|
|
|
$invitationKey = Session::get('invitation_key');
|
|
|
|
if (!$invitationKey) {
|
|
|
|
return Redirect::to('/setup');
|
|
|
|
}
|
|
|
|
|
|
|
|
$invitation = Invitation::with('account')->where('invitation_key', '=', $invitationKey)->first();
|
2015-07-30 16:44:47 +02:00
|
|
|
$account = $invitation->account;
|
|
|
|
$color = $account->primary_color ? $account->primary_color : '#0b4d78';
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
$data = [
|
|
|
|
'color' => $color,
|
2015-07-30 16:44:47 +02:00
|
|
|
'hideLogo' => $account->isWhiteLabel(),
|
2015-03-16 22:45:25 +01:00
|
|
|
'entityType' => ENTITY_PAYMENT,
|
|
|
|
'title' => trans('texts.payments'),
|
|
|
|
'columns' => Utils::trans(['invoice', 'transaction_reference', 'method', 'payment_amount', 'payment_date'])
|
|
|
|
];
|
|
|
|
|
|
|
|
return View::make('public_list', $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDatatable($clientPublicId = null)
|
|
|
|
{
|
|
|
|
$payments = $this->paymentRepo->find($clientPublicId, Input::get('sSearch'));
|
|
|
|
$table = Datatable::query($payments);
|
|
|
|
|
|
|
|
if (!$clientPublicId) {
|
|
|
|
$table->addColumn('checkbox', function ($model) { return '<input type="checkbox" name="ids[]" value="'.$model->public_id.'" '.Utils::getEntityRowClass($model).'>'; });
|
|
|
|
}
|
|
|
|
|
|
|
|
$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)]) : ''; });
|
|
|
|
|
|
|
|
if (!$clientPublicId) {
|
|
|
|
$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>'; })
|
2015-06-04 22:53:58 +02:00
|
|
|
->addColumn('payment_type', function ($model) { return $model->payment_type ? $model->payment_type : ($model->account_gateway_id ? $model->gateway_name : ''); });
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
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 || $model->invoice_is_deleted) {
|
|
|
|
return '<div style="height:38px"/>';
|
|
|
|
}
|
|
|
|
|
|
|
|
$str = '<div class="btn-group tr-action" style="visibility:hidden;">
|
|
|
|
<button type="button" class="btn btn-xs btn-default dropdown-toggle" data-toggle="dropdown">
|
|
|
|
'.trans('texts.select').' <span class="caret"></span>
|
|
|
|
</button>
|
|
|
|
<ul class="dropdown-menu" role="menu">';
|
|
|
|
|
|
|
|
if (!$model->deleted_at || $model->deleted_at == '0000-00-00') {
|
|
|
|
$str .= '<li><a href="payments/'.$model->public_id.'/edit">'.trans('texts.edit_payment').'</a></li>
|
|
|
|
<li class="divider"></li>
|
|
|
|
<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>';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $str.'<li><a href="javascript:deleteEntity('.$model->public_id.')">'.trans('texts.delete_payment').'</a></li></ul>
|
|
|
|
</div>';
|
|
|
|
})
|
|
|
|
->make();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getClientDatatable()
|
|
|
|
{
|
|
|
|
$search = Input::get('sSearch');
|
|
|
|
$invitationKey = Session::get('invitation_key');
|
|
|
|
$invitation = Invitation::where('invitation_key', '=', $invitationKey)->with('contact.client')->first();
|
|
|
|
|
|
|
|
if (!$invitation) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$invoice = $invitation->invoice;
|
|
|
|
|
|
|
|
if (!$invoice || $invoice->is_deleted) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$payments = $this->paymentRepo->findForContact($invitation->contact->id, Input::get('sSearch'));
|
|
|
|
|
|
|
|
return Datatable::query($payments)
|
|
|
|
->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();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function create($clientPublicId = 0, $invoicePublicId = 0)
|
|
|
|
{
|
2015-04-22 21:21:04 +02:00
|
|
|
$invoices = Invoice::scope()
|
|
|
|
->where('is_recurring', '=', false)
|
|
|
|
->where('is_quote', '=', false)
|
|
|
|
->where('invoices.balance', '>', 0)
|
|
|
|
->with('client', 'invoice_status')
|
|
|
|
->orderBy('invoice_number')->get();
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
$data = array(
|
|
|
|
'clientPublicId' => Input::old('client') ? Input::old('client') : $clientPublicId,
|
|
|
|
'invoicePublicId' => Input::old('invoice') ? Input::old('invoice') : $invoicePublicId,
|
|
|
|
'invoice' => null,
|
2015-04-22 21:21:04 +02:00
|
|
|
'invoices' => $invoices,
|
2015-03-16 22:45:25 +01:00
|
|
|
'payment' => null,
|
|
|
|
'method' => 'POST',
|
|
|
|
'url' => "payments",
|
|
|
|
'title' => trans('texts.new_payment'),
|
2015-04-08 15:19:17 +02:00
|
|
|
'paymentTypes' => Cache::get('paymentTypes'),
|
2015-03-16 22:45:25 +01:00
|
|
|
'clients' => Client::scope()->with('contacts')->orderBy('name')->get(), );
|
|
|
|
|
|
|
|
return View::make('payments.edit', $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function edit($publicId)
|
|
|
|
{
|
|
|
|
$payment = Payment::scope($publicId)->firstOrFail();
|
|
|
|
$payment->payment_date = Utils::fromSqlDate($payment->payment_date);
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'client' => null,
|
|
|
|
'invoice' => null,
|
|
|
|
'invoices' => Invoice::scope()->where('is_recurring', '=', false)->where('is_quote', '=', false)
|
|
|
|
->with('client', 'invoice_status')->orderBy('invoice_number')->get(),
|
|
|
|
'payment' => $payment,
|
|
|
|
'method' => 'PUT',
|
|
|
|
'url' => 'payments/'.$publicId,
|
|
|
|
'title' => trans('texts.edit_payment'),
|
2015-04-08 15:19:17 +02:00
|
|
|
'paymentTypes' => Cache::get('paymentTypes'),
|
2015-03-16 22:45:25 +01:00
|
|
|
'clients' => Client::scope()->with('contacts')->orderBy('name')->get(), );
|
|
|
|
|
|
|
|
return View::make('payments.edit', $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getLicensePaymentDetails($input, $affiliate)
|
|
|
|
{
|
2015-09-10 19:50:09 +02:00
|
|
|
$data = $this->paymentService->convertInputForOmnipay($input);
|
2015-03-16 22:45:25 +01:00
|
|
|
$card = new CreditCard($data);
|
|
|
|
|
|
|
|
return [
|
|
|
|
'amount' => $affiliate->price,
|
|
|
|
'card' => $card,
|
|
|
|
'currency' => 'USD',
|
|
|
|
'returnUrl' => URL::to('license_complete'),
|
|
|
|
'cancelUrl' => URL::to('/')
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2015-04-15 18:35:41 +02:00
|
|
|
public function show_payment($invitationKey, $paymentType = false)
|
2015-06-10 10:34:20 +02:00
|
|
|
{
|
2015-04-15 18:35:41 +02:00
|
|
|
$invitation = Invitation::with('invoice.invoice_items', 'invoice.client.currency', 'invoice.client.account.account_gateways.gateway')->where('invitation_key', '=', $invitationKey)->firstOrFail();
|
|
|
|
$invoice = $invitation->invoice;
|
|
|
|
$client = $invoice->client;
|
|
|
|
$account = $client->account;
|
|
|
|
$useToken = false;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-06-10 22:55:23 +02:00
|
|
|
if ($paymentType) {
|
|
|
|
$paymentType = 'PAYMENT_TYPE_' . strtoupper($paymentType);
|
|
|
|
} else {
|
2015-06-10 10:34:20 +02:00
|
|
|
$paymentType = Session::get('payment_type', $account->account_gateways[0]->getPaymentType());
|
|
|
|
}
|
|
|
|
if ($paymentType == PAYMENT_TYPE_TOKEN) {
|
2015-04-15 18:35:41 +02:00
|
|
|
$useToken = true;
|
|
|
|
$paymentType = PAYMENT_TYPE_CREDIT_CARD;
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
2015-04-15 18:35:41 +02:00
|
|
|
Session::put('payment_type', $paymentType);
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-09-20 23:05:02 +02:00
|
|
|
$accountGateway = $invoice->client->account->getGatewayByType($paymentType);
|
|
|
|
$gateway = $accountGateway->gateway;
|
|
|
|
$acceptedCreditCardTypes = $accountGateway->getCreditcardTypes();
|
|
|
|
|
2015-04-15 18:35:41 +02:00
|
|
|
// Handle offsite payments
|
2015-09-20 23:05:02 +02:00
|
|
|
if ($useToken || $paymentType != PAYMENT_TYPE_CREDIT_CARD || $gateway->id == GATEWAY_EWAY) {
|
2015-03-16 22:45:25 +01:00
|
|
|
if (Session::has('error')) {
|
|
|
|
Session::reflash();
|
|
|
|
return Redirect::to('view/'.$invitationKey);
|
|
|
|
} else {
|
2015-04-15 18:35:41 +02:00
|
|
|
return self::do_payment($invitationKey, false, $useToken);
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
2015-04-15 18:35:41 +02:00
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
$data = [
|
|
|
|
'showBreadcrumbs' => false,
|
|
|
|
'url' => 'payment/'.$invitationKey,
|
2015-04-16 21:57:12 +02:00
|
|
|
'amount' => $invoice->getRequestedAmount(),
|
2015-03-16 22:45:25 +01:00
|
|
|
'invoiceNumber' => $invoice->invoice_number,
|
|
|
|
'client' => $client,
|
|
|
|
'contact' => $invitation->contact,
|
|
|
|
'gateway' => $gateway,
|
|
|
|
'acceptedCreditCardTypes' => $acceptedCreditCardTypes,
|
2015-04-08 15:19:17 +02:00
|
|
|
'countries' => Cache::get('countries'),
|
2015-06-10 10:34:20 +02:00
|
|
|
'currencyId' => $client->getCurrencyId(),
|
2015-07-30 16:44:47 +02:00
|
|
|
'currencyCode' => $client->currency ? $client->currency->code : ($account->currency ? $account->currency->code : 'USD'),
|
2015-04-28 22:13:52 +02:00
|
|
|
'account' => $client->account,
|
2015-05-27 06:40:53 +02:00
|
|
|
'hideLogo' => $account->isWhiteLabel(),
|
2015-07-12 21:43:45 +02:00
|
|
|
'showAddress' => $accountGateway->show_address,
|
2015-03-16 22:45:25 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
return View::make('payments.payment', $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function show_license_payment()
|
|
|
|
{
|
|
|
|
if (Input::has('return_url')) {
|
|
|
|
Session::set('return_url', Input::get('return_url'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Input::has('affiliate_key')) {
|
|
|
|
if ($affiliate = Affiliate::where('affiliate_key', '=', Input::get('affiliate_key'))->first()) {
|
|
|
|
Session::set('affiliate_id', $affiliate->id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-02 12:59:03 +02:00
|
|
|
if (Input::has('product_id')) {
|
|
|
|
Session::set('product_id', Input::get('product_id'));
|
|
|
|
} else if (!Session::has('product_id')) {
|
|
|
|
Session::set('product_id', PRODUCT_ONE_CLICK_INSTALL);
|
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
if (!Session::get('affiliate_id')) {
|
|
|
|
return Utils::fatalError();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Utils::isNinjaDev() && Input::has('test_mode')) {
|
|
|
|
Session::set('test_mode', Input::get('test_mode'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$account = $this->accountRepo->getNinjaAccount();
|
|
|
|
$account->load('account_gateways.gateway');
|
|
|
|
$accountGateway = $account->getGatewayByType(Session::get('payment_type'));
|
|
|
|
$gateway = $accountGateway->gateway;
|
|
|
|
$acceptedCreditCardTypes = $accountGateway->getCreditcardTypes();
|
|
|
|
|
|
|
|
$affiliate = Affiliate::find(Session::get('affiliate_id'));
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'showBreadcrumbs' => false,
|
|
|
|
'hideHeader' => true,
|
|
|
|
'url' => 'license',
|
|
|
|
'amount' => $affiliate->price,
|
|
|
|
'client' => false,
|
|
|
|
'contact' => false,
|
|
|
|
'gateway' => $gateway,
|
|
|
|
'acceptedCreditCardTypes' => $acceptedCreditCardTypes,
|
2015-04-08 15:19:17 +02:00
|
|
|
'countries' => Cache::get('countries'),
|
2015-03-16 22:45:25 +01:00
|
|
|
'currencyId' => 1,
|
|
|
|
'paymentTitle' => $affiliate->payment_title,
|
|
|
|
'paymentSubtitle' => $affiliate->payment_subtitle,
|
2015-08-03 10:52:47 +02:00
|
|
|
'showAddress' => true,
|
2015-03-16 22:45:25 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
return View::make('payments.payment', $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function do_license_payment()
|
|
|
|
{
|
|
|
|
$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',
|
2015-04-28 22:13:52 +02:00
|
|
|
'country_id' => 'required',
|
2015-03-16 22:45:25 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$validator = Validator::make(Input::all(), $rules);
|
|
|
|
|
|
|
|
if ($validator->fails()) {
|
|
|
|
return Redirect::to('license')
|
2015-09-20 23:05:02 +02:00
|
|
|
->withErrors($validator)
|
|
|
|
->withInput();
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$account = $this->accountRepo->getNinjaAccount();
|
|
|
|
$account->load('account_gateways.gateway');
|
|
|
|
$accountGateway = $account->getGatewayByType(PAYMENT_TYPE_CREDIT_CARD);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$affiliate = Affiliate::find(Session::get('affiliate_id'));
|
|
|
|
|
|
|
|
if ($testMode) {
|
|
|
|
$ref = 'TEST_MODE';
|
|
|
|
} else {
|
2015-09-10 19:50:09 +02:00
|
|
|
$gateway = $this->paymentService->createGateway($accountGateway);
|
2015-03-16 22:45:25 +01:00
|
|
|
$details = self::getLicensePaymentDetails(Input::all(), $affiliate);
|
|
|
|
$response = $gateway->purchase($details)->send();
|
|
|
|
$ref = $response->getTransactionReference();
|
|
|
|
|
2015-09-10 19:50:09 +02:00
|
|
|
if (!$response->isSuccessful() || !$ref) {
|
|
|
|
$this->error('License', $response->getMessage(), $accountGateway);
|
2015-03-16 22:45:25 +01:00
|
|
|
return Redirect::to('license')->withInput();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$licenseKey = Utils::generateLicense();
|
|
|
|
|
|
|
|
$license = new License();
|
|
|
|
$license->first_name = Input::get('first_name');
|
|
|
|
$license->last_name = Input::get('last_name');
|
|
|
|
$license->email = Input::get('email');
|
|
|
|
$license->transaction_reference = $ref;
|
|
|
|
$license->license_key = $licenseKey;
|
|
|
|
$license->affiliate_id = Session::get('affiliate_id');
|
|
|
|
$license->product_id = Session::get('product_id');
|
|
|
|
$license->save();
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'message' => $affiliate->payment_subtitle,
|
|
|
|
'license' => $licenseKey,
|
|
|
|
'hideHeader' => true,
|
2015-06-01 15:06:18 +02:00
|
|
|
'productId' => $license->product_id
|
2015-03-16 22:45:25 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
$name = "{$license->first_name} {$license->last_name}";
|
|
|
|
$this->contactMailer->sendLicensePaymentConfirmation($name, $license->email, $affiliate->price, $license->license_key, $license->product_id);
|
|
|
|
|
|
|
|
if (Session::has('return_url')) {
|
2015-07-07 22:08:16 +02:00
|
|
|
$data['redirectTo'] = Session::get('return_url')."?license_key={$license->license_key}&product_id=".Session::get('product_id');
|
|
|
|
$data['message'] = "Redirecting to " . Session::get('return_url');
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
2015-07-07 22:08:16 +02:00
|
|
|
|
|
|
|
return View::make('public.license', $data);
|
2015-03-16 22:45:25 +01:00
|
|
|
} catch (\Exception $e) {
|
2015-09-10 19:50:09 +02:00
|
|
|
$this->error('License-Uncaught', false, $accountGateway, $e);
|
2015-03-16 22:45:25 +01:00
|
|
|
return Redirect::to('license')->withInput();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function claim_license()
|
|
|
|
{
|
|
|
|
$licenseKey = Input::get('license_key');
|
|
|
|
$productId = Input::get('product_id', PRODUCT_ONE_CLICK_INSTALL);
|
|
|
|
|
|
|
|
$license = License::where('license_key', '=', $licenseKey)
|
2015-06-03 19:55:48 +02:00
|
|
|
->where('is_claimed', '<', 5)
|
2015-03-16 22:45:25 +01:00
|
|
|
->where('product_id', '=', $productId)
|
|
|
|
->first();
|
|
|
|
|
|
|
|
if ($license) {
|
|
|
|
if ($license->transaction_reference != 'TEST_MODE') {
|
2015-04-14 20:58:07 +02:00
|
|
|
$license->is_claimed = $license->is_claimed + 1;
|
2015-03-16 22:45:25 +01:00
|
|
|
$license->save();
|
|
|
|
}
|
|
|
|
|
2015-05-19 21:14:00 +02:00
|
|
|
return $productId == PRODUCT_INVOICE_DESIGNS ? file_get_contents(storage_path() . '/invoice_designs.txt') : 'valid';
|
2015-03-16 22:45:25 +01:00
|
|
|
} else {
|
|
|
|
return 'invalid';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function do_payment($invitationKey, $onSite = true, $useToken = false)
|
|
|
|
{
|
2015-07-12 21:43:45 +02:00
|
|
|
$invitation = Invitation::with('invoice.invoice_items', 'invoice.client.currency', 'invoice.client.account.currency', 'invoice.client.account.account_gateways.gateway')->where('invitation_key', '=', $invitationKey)->firstOrFail();
|
|
|
|
$invoice = $invitation->invoice;
|
|
|
|
$client = $invoice->client;
|
|
|
|
$account = $client->account;
|
|
|
|
$accountGateway = $account->getGatewayByType(Session::get('payment_type'));
|
|
|
|
|
|
|
|
$rules = [
|
2015-03-16 22:45:25 +01:00
|
|
|
'first_name' => 'required',
|
|
|
|
'last_name' => 'required',
|
|
|
|
'card_number' => 'required',
|
|
|
|
'expiration_month' => 'required',
|
|
|
|
'expiration_year' => 'required',
|
|
|
|
'cvv' => 'required',
|
2015-07-12 21:43:45 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
if ($accountGateway->show_address) {
|
|
|
|
$rules = array_merge($rules, [
|
|
|
|
'address1' => 'required',
|
|
|
|
'city' => 'required',
|
|
|
|
'state' => 'required',
|
|
|
|
'postal_code' => 'required',
|
|
|
|
'country_id' => 'required',
|
|
|
|
]);
|
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
if ($onSite) {
|
|
|
|
$validator = Validator::make(Input::all(), $rules);
|
|
|
|
|
|
|
|
if ($validator->fails()) {
|
|
|
|
return Redirect::to('payment/'.$invitationKey)
|
2015-06-10 10:34:20 +02:00
|
|
|
->withErrors($validator)
|
|
|
|
->withInput();
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
2015-08-03 09:15:58 +02:00
|
|
|
|
|
|
|
if ($accountGateway->update_address) {
|
|
|
|
$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->country_id = Input::get('country_id');
|
|
|
|
$client->save();
|
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
2015-09-10 19:50:09 +02:00
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
try {
|
2015-09-20 23:05:02 +02:00
|
|
|
// For offsite payments send the client's details on file
|
|
|
|
// If we're using a token then we don't need to send any other data
|
|
|
|
if (!$onSite || $useToken) {
|
|
|
|
$data = false;
|
|
|
|
} else {
|
|
|
|
$data = Input::all();
|
|
|
|
}
|
|
|
|
|
2015-09-10 19:50:09 +02:00
|
|
|
$gateway = $this->paymentService->createGateway($accountGateway);
|
2015-09-20 23:05:02 +02:00
|
|
|
$details = $this->paymentService->getPaymentDetails($invitation, $data);
|
2015-09-10 19:50:09 +02:00
|
|
|
|
|
|
|
// check if we're creating/using a billing token
|
2015-04-15 18:35:41 +02:00
|
|
|
if ($accountGateway->gateway_id == GATEWAY_STRIPE) {
|
|
|
|
if ($useToken) {
|
|
|
|
$details['cardReference'] = $client->getGatewayToken();
|
|
|
|
} elseif ($account->token_billing_type_id == TOKEN_BILLING_ALWAYS || Input::get('token_billing')) {
|
2015-09-10 19:50:09 +02:00
|
|
|
$token = $this->paymentService->createToken($gateway, $details, $accountGateway, $client, $invitation->contact_id);
|
|
|
|
if ($token) {
|
|
|
|
$details['cardReference'] = $token;
|
2015-06-03 19:55:48 +02:00
|
|
|
} else {
|
2015-09-10 19:50:09 +02:00
|
|
|
$this->error('Token-No-Ref', $this->paymentService->lastError, $accountGateway);
|
2015-06-03 19:55:48 +02:00
|
|
|
return Redirect::to('payment/'.$invitationKey)->withInput();
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
}
|
2015-04-15 18:35:41 +02:00
|
|
|
}
|
2015-09-10 19:50:09 +02:00
|
|
|
|
2015-04-15 18:35:41 +02:00
|
|
|
$response = $gateway->purchase($details)->send();
|
2015-09-20 23:05:02 +02:00
|
|
|
|
|
|
|
if ($accountGateway->gateway_id == GATEWAY_EWAY) {
|
|
|
|
$ref = $response->getData()['AccessCode'];
|
|
|
|
$token = $response->getCardReference();
|
|
|
|
} else {
|
|
|
|
$ref = $response->getTransactionReference();
|
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-04-15 18:35:41 +02:00
|
|
|
if (!$ref) {
|
2015-09-10 19:50:09 +02:00
|
|
|
$this->error('No-Ref', $response->getMessage(), $accountGateway);
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-04-15 18:35:41 +02:00
|
|
|
if ($onSite) {
|
|
|
|
return Redirect::to('payment/'.$invitationKey)->withInput();
|
|
|
|
} else {
|
|
|
|
return Redirect::to('view/'.$invitationKey);
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
2015-04-15 18:35:41 +02:00
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-06-07 10:05:30 +02:00
|
|
|
if ($response->isSuccessful()) {
|
2015-09-10 19:50:09 +02:00
|
|
|
$payment = $this->paymentService->createPayment($invitation, $ref);
|
2015-04-15 18:35:41 +02:00
|
|
|
Session::flash('message', trans('texts.applied_payment'));
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-06-03 19:55:48 +02:00
|
|
|
if ($account->account_key == NINJA_ACCOUNT_KEY) {
|
|
|
|
Session::flash('trackEventCategory', '/account');
|
|
|
|
Session::flash('trackEventAction', '/buy_pro_plan');
|
|
|
|
}
|
|
|
|
|
2015-04-15 18:35:41 +02:00
|
|
|
return Redirect::to('view/'.$payment->invitation->invitation_key);
|
|
|
|
} elseif ($response->isRedirect()) {
|
|
|
|
$invitation->transaction_reference = $ref;
|
|
|
|
$invitation->save();
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-04-17 13:57:17 +02:00
|
|
|
Session::put('transaction_reference', $ref);
|
2015-04-15 18:35:41 +02:00
|
|
|
Session::save();
|
|
|
|
$response->redirect();
|
|
|
|
} else {
|
2015-09-10 19:50:09 +02:00
|
|
|
$this->error('Fatal', $response->getMessage(), $accountGateway);
|
2015-04-15 18:35:41 +02:00
|
|
|
return Utils::fatalError('Sorry, there was an error processing your payment. Please try again later.<p>', $response->getMessage());
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
2015-09-10 19:50:09 +02:00
|
|
|
$this->error('Uncaught', false, $accountGateway, $e);
|
2015-03-16 22:45:25 +01:00
|
|
|
if ($onSite) {
|
|
|
|
return Redirect::to('payment/'.$invitationKey)->withInput();
|
|
|
|
} else {
|
|
|
|
return Redirect::to('view/'.$invitationKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function offsite_payment()
|
|
|
|
{
|
|
|
|
$payerId = Request::query('PayerID');
|
|
|
|
$token = Request::query('token');
|
|
|
|
|
2015-04-17 13:57:17 +02:00
|
|
|
if (!$token) {
|
|
|
|
$token = Session::pull('transaction_reference');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$token) {
|
|
|
|
return redirect(NINJA_WEB_URL);
|
|
|
|
}
|
|
|
|
|
2015-03-16 22:45:25 +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->getGatewayByType(Session::get('payment_type'));
|
2015-09-10 19:50:09 +02:00
|
|
|
$gateway = $this->paymentService->createGateway($accountGateway);
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-07-01 21:01:12 +02:00
|
|
|
// Check for Dwolla payment error
|
|
|
|
if ($accountGateway->isGateway(GATEWAY_DWOLLA) && Input::get('error')) {
|
2015-09-10 19:50:09 +02:00
|
|
|
$this->error('Dwolla', Input::get('error_description'), $accountGateway);
|
2015-07-01 21:01:12 +02:00
|
|
|
return Redirect::to('view/'.$invitation->invitation_key);
|
|
|
|
}
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
try {
|
2015-04-17 13:57:17 +02:00
|
|
|
if (method_exists($gateway, 'completePurchase')) {
|
2015-09-10 19:50:09 +02:00
|
|
|
$details = $this->paymentService->getPaymentDetails($invitation);
|
2015-04-17 13:57:17 +02:00
|
|
|
$response = $gateway->completePurchase($details)->send();
|
|
|
|
$ref = $response->getTransactionReference();
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-04-17 13:57:17 +02:00
|
|
|
if ($response->isSuccessful()) {
|
2015-09-10 19:50:09 +02:00
|
|
|
$payment = $this->paymentService->createPayment($invitation, $ref, $payerId);
|
2015-04-17 13:57:17 +02:00
|
|
|
Session::flash('message', trans('texts.applied_payment'));
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-04-17 13:57:17 +02:00
|
|
|
return Redirect::to('view/'.$invitation->invitation_key);
|
|
|
|
} else {
|
2015-09-10 19:50:09 +02:00
|
|
|
$this->error('offsite', $response->getMessage(), $accountGateway);
|
2015-04-17 13:57:17 +02:00
|
|
|
return Redirect::to('view/'.$invitation->invitation_key);
|
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
} else {
|
2015-09-10 19:50:09 +02:00
|
|
|
$payment = $this->paymentService->createPayment($invitation, $token, $payerId);
|
2015-04-17 13:57:17 +02:00
|
|
|
Session::flash('message', trans('texts.applied_payment'));
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
return Redirect::to('view/'.$invitation->invitation_key);
|
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
2015-09-10 19:50:09 +02:00
|
|
|
$this->error('Offsite-uncaught', false, $accountGateway, $e);
|
2015-03-16 22:45:25 +01:00
|
|
|
return Redirect::to('view/'.$invitation->invitation_key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function store()
|
|
|
|
{
|
|
|
|
return $this->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function update($publicId)
|
|
|
|
{
|
|
|
|
return $this->save($publicId);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function save($publicId = null)
|
|
|
|
{
|
|
|
|
if (!$publicId && $errors = $this->paymentRepo->getErrors(Input::all())) {
|
|
|
|
$url = $publicId ? 'payments/'.$publicId.'/edit' : 'payments/create';
|
|
|
|
|
|
|
|
return Redirect::to($url)
|
|
|
|
->withErrors($errors)
|
|
|
|
->withInput();
|
|
|
|
} else {
|
2015-06-14 19:30:01 +02:00
|
|
|
$payment = $this->paymentRepo->save($publicId, Input::all());
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
if ($publicId) {
|
|
|
|
Session::flash('message', trans('texts.updated_payment'));
|
|
|
|
|
|
|
|
return Redirect::to('payments/');
|
|
|
|
} else {
|
2015-06-14 19:30:01 +02:00
|
|
|
if (Input::get('email_receipt')) {
|
|
|
|
$this->contactMailer->sendPaymentConfirmation($payment);
|
|
|
|
Session::flash('message', trans('texts.created_payment_emailed_client'));
|
|
|
|
} else {
|
|
|
|
Session::flash('message', trans('texts.created_payment'));
|
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
return Redirect::to('clients/'.Input::get('client'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function bulk()
|
|
|
|
{
|
|
|
|
$action = Input::get('action');
|
|
|
|
$ids = Input::get('id') ? Input::get('id') : Input::get('ids');
|
|
|
|
$count = $this->paymentRepo->bulk($ids, $action);
|
|
|
|
|
|
|
|
if ($count > 0) {
|
|
|
|
$message = Utils::pluralize($action.'d_payment', $count);
|
|
|
|
Session::flash('message', $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Redirect::to('payments');
|
|
|
|
}
|
2015-09-10 19:50:09 +02:00
|
|
|
|
|
|
|
private function error($type, $error, $accountGateway, $exception = false)
|
|
|
|
{
|
|
|
|
if (!$error) {
|
|
|
|
if ($exception) {
|
|
|
|
$error = $exception->getMessage();
|
|
|
|
} else {
|
|
|
|
$error = trans('texts.payment_error');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$message = '';
|
|
|
|
if ($accountGateway && $accountGateway->gateway) {
|
|
|
|
$message = $accountGateway->gateway->name . ': ';
|
|
|
|
}
|
|
|
|
$message .= $error;
|
|
|
|
|
|
|
|
Session::flash('error', $message);
|
|
|
|
Utils::logError("Payment Error [{$type}]: " . ($exception ? Utils::getErrorString($exception) : $message));
|
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|