public function completeOnsitePurchase($input = false, $paymentMethod = false)
{
$this->input = count($input) ? $input : false;
$gateway = $this->gateway();
if ($input) {
$this->updateClient();
}
// load or create token
if ($this->isGatewayType(GATEWAY_TYPE_TOKEN)) {
if (!$paymentMethod) {
$paymentMethod = PaymentMethod::clientId($this->client()->id)->wherePublicId($this->sourceId)->firstOrFail();
}
if (!$this->meetsGatewayTypeLimits($paymentMethod->payment_type->gateway_type_id)) {
// The customer must have hacked the URL
Session::flash('error', trans('texts.limits_not_met'));
return redirect()->to('view/' . $this->invitation->invitation_key);
}
} else {
if ($this->shouldCreateToken()) {
$paymentMethod = $this->createToken();
}
if (!$this->meetsGatewayTypeLimits($this->gatewayType)) {
// The customer must have hacked the URL
Session::flash('error', trans('texts.limits_not_met'));
return redirect()->to('view/' . $this->invitation->invitation_key);
}
}
if ($this->isTwoStep()) {
return;
}
// prepare and process payment
$data = $this->paymentDetails($paymentMethod);
$response = $gateway->purchase($data)->send();
$this->purchaseResponse = (array) $response->getData();
// parse the transaction reference
if ($this->transactionReferenceParam) {
$ref = $this->purchaseResponse[$this->transactionReferenceParam];
} else {
$ref = $response->getTransactionReference();
}
// wrap up
if ($response->isSuccessful() && $ref) {
$payment = $this->createPayment($ref, $paymentMethod);
// TODO move this to stripe driver
if ($this->invitation->invoice->account->account_key == NINJA_ACCOUNT_KEY) {
Session::flash('trackEventCategory', '/account');
Session::flash('trackEventAction', '/buy_pro_plan');
Session::flash('trackEventAmount', $payment->amount);
}
return $payment;
} elseif ($response->isRedirect()) {
$this->invitation->transaction_reference = $ref;
$this->invitation->save();
//Session::put('transaction_reference', $ref);
Session::save();
$response->redirect();
} else {
throw new Exception($response->getMessage() ?: trans('texts.payment_error'));
}
}