public function createToken($gateway, $details, $accountGateway, $client, $contactId) { $tokenResponse = $gateway->createCard($details)->send(); $cardReference = $tokenResponse->getCardReference(); if ($cardReference) { $token = AccountGatewayToken::where('client_id', '=', $client->id)->where('account_gateway_id', '=', $accountGateway->id)->first(); if (!$token) { $token = new AccountGatewayToken(); $token->account_id = $client->account->id; $token->contact_id = $contactId; $token->account_gateway_id = $accountGateway->id; $token->client_id = $client->id; } $token->token = $cardReference; $token->save(); } else { $this->lastError = $tokenResponse->getMessage(); } return $cardReference; }
public function do_payment($invitationKey, $onSite = true, $useToken = false) { $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', 'country_id' => 'required'); if ($onSite) { $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.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')); /* if ($onSite) { $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(); } */ try { $gateway = self::createGateway($accountGateway); $details = self::getPaymentDetails($invitation, $useToken || !$onSite ? false : Input::all()); 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')) { $tokenResponse = $gateway->createCard($details)->send(); $cardReference = $tokenResponse->getCardReference(); if ($cardReference) { $details['cardReference'] = $cardReference; $token = AccountGatewayToken::where('client_id', '=', $client->id)->where('account_gateway_id', '=', $accountGateway->id)->first(); if (!$token) { $token = new AccountGatewayToken(); $token->account_id = $account->id; $token->contact_id = $invitation->contact_id; $token->account_gateway_id = $accountGateway->id; $token->client_id = $client->id; } $token->token = $cardReference; $token->save(); } } } $response = $gateway->purchase($details)->send(); $ref = $response->getTransactionReference(); if (!$ref) { Session::flash('error', $response->getMessage()); if ($onSite) { return Redirect::to('payment/' . $invitationKey)->withInput(); } else { return Redirect::to('view/' . $invitationKey); } } if ($response->isSuccessful()) { $payment = self::createPayment($invitation, $ref); Session::flash('message', trans('texts.applied_payment')); return Redirect::to('view/' . $payment->invitation->invitation_key); } elseif ($response->isRedirect()) { $invitation->transaction_reference = $ref; $invitation->save(); Session::put('transaction_reference', $ref); Session::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) { $errorMessage = trans('texts.payment_error'); Session::flash('error', $errorMessage . "<p>" . $e->getMessage()); Utils::logError(Utils::getErrorString($e)); if ($onSite) { return Redirect::to('payment/' . $invitationKey)->withInput(); } else { return Redirect::to('view/' . $invitationKey); } } }