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 Auth;
|
|
|
|
use Datatable;
|
|
|
|
use DB;
|
|
|
|
use Input;
|
|
|
|
use Redirect;
|
|
|
|
use Session;
|
|
|
|
use View;
|
2015-04-01 21:57:02 +02:00
|
|
|
use Validator;
|
|
|
|
use stdClass;
|
2015-04-02 15:12:12 +02:00
|
|
|
use URL;
|
2015-06-03 19:55:48 +02:00
|
|
|
use Utils;
|
2016-05-12 04:55:37 +02:00
|
|
|
use WePay;
|
2015-03-26 07:24:02 +01:00
|
|
|
use App\Models\Gateway;
|
2015-04-01 21:57:02 +02:00
|
|
|
use App\Models\Account;
|
|
|
|
use App\Models\AccountGateway;
|
|
|
|
|
2015-03-24 09:21:12 +01:00
|
|
|
use App\Ninja\Repositories\AccountRepository;
|
2015-11-05 23:37:04 +01:00
|
|
|
use App\Services\AccountGatewayService;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
class AccountGatewayController extends BaseController
|
|
|
|
{
|
2015-11-05 23:37:04 +01:00
|
|
|
protected $accountGatewayService;
|
|
|
|
|
|
|
|
public function __construct(AccountGatewayService $accountGatewayService)
|
|
|
|
{
|
2016-03-02 14:36:42 +01:00
|
|
|
//parent::__construct();
|
2015-11-05 23:37:04 +01:00
|
|
|
|
|
|
|
$this->accountGatewayService = $accountGatewayService;
|
|
|
|
}
|
|
|
|
|
2015-10-21 13:11:08 +02:00
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
return Redirect::to('settings/' . ACCOUNT_PAYMENTS);
|
|
|
|
}
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
public function getDatatable()
|
|
|
|
{
|
2015-11-05 23:37:04 +01:00
|
|
|
return $this->accountGatewayService->getDatatable(Auth::user()->account_id);
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function edit($publicId)
|
|
|
|
{
|
|
|
|
$accountGateway = AccountGateway::scope($publicId)->firstOrFail();
|
2015-11-01 19:21:11 +01:00
|
|
|
$config = $accountGateway->getConfig();
|
2016-01-18 10:13:39 +01:00
|
|
|
|
2015-11-01 19:21:11 +01:00
|
|
|
foreach ($config as $field => $value) {
|
|
|
|
$config->$field = str_repeat('*', strlen($value));
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$data = self::getViewModel($accountGateway);
|
|
|
|
$data['url'] = 'gateways/'.$publicId;
|
|
|
|
$data['method'] = 'PUT';
|
|
|
|
$data['title'] = trans('texts.edit_gateway') . ' - ' . $accountGateway->gateway->name;
|
2015-11-01 19:21:11 +01:00
|
|
|
$data['config'] = $config;
|
2015-05-31 14:37:29 +02:00
|
|
|
$data['hiddenFields'] = Gateway::$hiddenFields;
|
2015-04-15 18:35:41 +02:00
|
|
|
$data['paymentTypeId'] = $accountGateway->getPaymentType();
|
2015-04-16 19:12:56 +02:00
|
|
|
$data['selectGateways'] = Gateway::where('id', '=', $accountGateway->gateway_id)->get();
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
return View::make('accounts.account_gateway', $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function update($publicId)
|
|
|
|
{
|
|
|
|
return $this->save($publicId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function store()
|
|
|
|
{
|
|
|
|
return $this->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Displays the form for account creation
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
|
|
|
$data = self::getViewModel();
|
|
|
|
$data['url'] = 'gateways';
|
|
|
|
$data['method'] = 'POST';
|
|
|
|
$data['title'] = trans('texts.add_gateway');
|
2015-10-02 10:32:13 +02:00
|
|
|
$data['selectGateways'] = Gateway::where('payment_library_id', '=', 1)
|
|
|
|
->where('id', '!=', GATEWAY_PAYPAL_EXPRESS)
|
|
|
|
->where('id', '!=', GATEWAY_BITPAY)
|
2016-01-18 10:13:39 +01:00
|
|
|
->where('id', '!=', GATEWAY_GOCARDLESS)
|
2015-10-02 10:32:13 +02:00
|
|
|
->where('id', '!=', GATEWAY_DWOLLA)
|
2016-04-28 22:38:01 +02:00
|
|
|
->where('id', '!=', GATEWAY_STRIPE)
|
2016-05-15 04:32:27 +02:00
|
|
|
->where('id', '!=', GATEWAY_WEPAY)
|
2015-10-02 10:32:13 +02:00
|
|
|
->orderBy('name')->get();
|
2015-05-31 14:37:29 +02:00
|
|
|
$data['hiddenFields'] = Gateway::$hiddenFields;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2016-02-03 19:59:07 +01:00
|
|
|
if ( ! \Request::secure() && ! Utils::isNinjaDev()) {
|
|
|
|
Session::flash('warning', trans('texts.enable_https'));
|
|
|
|
}
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
return View::make('accounts.account_gateway', $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getViewModel($accountGateway = false)
|
|
|
|
{
|
|
|
|
$selectedCards = $accountGateway ? $accountGateway->accepted_credit_cards : 0;
|
2016-05-12 04:55:37 +02:00
|
|
|
$user = Auth::user();
|
|
|
|
$account =$user->account;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-04-15 18:35:41 +02:00
|
|
|
$paymentTypes = [];
|
2015-05-31 14:37:29 +02:00
|
|
|
foreach (Gateway::$paymentTypes as $type) {
|
2016-05-14 23:23:20 +02:00
|
|
|
if ($accountGateway || $account->canAddGateway($type)) {
|
2016-04-29 23:50:21 +02:00
|
|
|
$paymentTypes[$type] = $type == PAYMENT_TYPE_CREDIT_CARD ? trans('texts.other_providers'): trans('texts.'.strtolower($type));
|
2015-04-15 18:35:41 +02:00
|
|
|
|
|
|
|
if ($type == PAYMENT_TYPE_BITCOIN) {
|
|
|
|
$paymentTypes[$type] .= ' - BitPay';
|
|
|
|
}
|
2016-01-18 10:13:39 +01:00
|
|
|
if ($type == PAYMENT_TYPE_DIRECT_DEBIT) {
|
|
|
|
$paymentTypes[$type] .= ' - GoCardless';
|
|
|
|
}
|
2015-04-15 18:35:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
$creditCardsArray = unserialize(CREDIT_CARDS);
|
|
|
|
$creditCards = [];
|
|
|
|
foreach ($creditCardsArray as $card => $name) {
|
|
|
|
if ($selectedCards > 0 && ($selectedCards & $card) == $card) {
|
|
|
|
$creditCards[$name['text']] = ['value' => $card, 'data-imageUrl' => asset($name['card']), 'checked' => 'checked'];
|
|
|
|
} else {
|
|
|
|
$creditCards[$name['text']] = ['value' => $card, 'data-imageUrl' => asset($name['card'])];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$account->load('account_gateways');
|
|
|
|
$currentGateways = $account->account_gateways;
|
2015-04-15 18:35:41 +02:00
|
|
|
$gateways = Gateway::where('payment_library_id', '=', 1)->orderBy('name')->get();
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
foreach ($gateways as $gateway) {
|
2015-05-31 14:37:29 +02:00
|
|
|
$fields = $gateway->getFields();
|
|
|
|
asort($fields);
|
2016-05-14 23:23:20 +02:00
|
|
|
$gateway->fields = $gateway->id == GATEWAY_WEPAY ? [] : $fields;
|
2015-03-16 22:45:25 +01:00
|
|
|
if ($accountGateway && $accountGateway->gateway_id == $gateway->id) {
|
|
|
|
$accountGateway->fields = $gateway->fields;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$tokenBillingOptions = [];
|
|
|
|
for ($i=1; $i<=4; $i++) {
|
|
|
|
$tokenBillingOptions[$i] = trans("texts.token_billing_{$i}");
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
2015-04-15 18:35:41 +02:00
|
|
|
'paymentTypes' => $paymentTypes,
|
2015-03-16 22:45:25 +01:00
|
|
|
'account' => $account,
|
2016-05-12 04:55:37 +02:00
|
|
|
'user' => $user,
|
2015-03-16 22:45:25 +01:00
|
|
|
'accountGateway' => $accountGateway,
|
|
|
|
'config' => false,
|
|
|
|
'gateways' => $gateways,
|
|
|
|
'creditCardTypes' => $creditCards,
|
|
|
|
'tokenBillingOptions' => $tokenBillingOptions,
|
|
|
|
'countGateways' => count($currentGateways)
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-05 23:37:04 +01:00
|
|
|
public function bulk()
|
|
|
|
{
|
|
|
|
$action = Input::get('bulk_action');
|
|
|
|
$ids = Input::get('bulk_public_id');
|
|
|
|
$count = $this->accountGatewayService->bulk($ids, $action);
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2016-05-14 23:23:20 +02:00
|
|
|
Session::flash('message', trans("texts.{$action}d_account_gateway"));
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-10-14 16:15:39 +02:00
|
|
|
return Redirect::to('settings/' . ACCOUNT_PAYMENTS);
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores new account
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function save($accountGatewayPublicId = false)
|
|
|
|
{
|
|
|
|
$rules = array();
|
2015-04-16 19:12:56 +02:00
|
|
|
$paymentType = Input::get('payment_type_id');
|
2015-04-06 13:46:02 +02:00
|
|
|
$gatewayId = Input::get('gateway_id');
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-04-16 19:12:56 +02:00
|
|
|
if ($paymentType == PAYMENT_TYPE_PAYPAL) {
|
|
|
|
$gatewayId = GATEWAY_PAYPAL_EXPRESS;
|
|
|
|
} elseif ($paymentType == PAYMENT_TYPE_BITCOIN) {
|
|
|
|
$gatewayId = GATEWAY_BITPAY;
|
2016-01-18 10:13:39 +01:00
|
|
|
} elseif ($paymentType == PAYMENT_TYPE_DIRECT_DEBIT) {
|
|
|
|
$gatewayId = GATEWAY_GOCARDLESS;
|
2015-06-01 22:07:37 +02:00
|
|
|
} elseif ($paymentType == PAYMENT_TYPE_DWOLLA) {
|
2015-05-31 14:37:29 +02:00
|
|
|
$gatewayId = GATEWAY_DWOLLA;
|
2016-04-28 22:38:01 +02:00
|
|
|
} elseif ($paymentType == PAYMENT_TYPE_STRIPE) {
|
|
|
|
$gatewayId = GATEWAY_STRIPE;
|
2015-04-16 19:12:56 +02:00
|
|
|
}
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
if (!$gatewayId) {
|
|
|
|
Session::flash('error', trans('validation.required', ['attribute' => 'gateway']));
|
|
|
|
return Redirect::to('gateways/create')
|
|
|
|
->withInput();
|
|
|
|
}
|
|
|
|
|
|
|
|
$gateway = Gateway::findOrFail($gatewayId);
|
|
|
|
$fields = $gateway->getFields();
|
2015-06-03 19:55:48 +02:00
|
|
|
$optional = array_merge(Gateway::$hiddenFields, Gateway::$optionalFields);
|
|
|
|
|
2015-07-01 21:01:12 +02:00
|
|
|
if ($gatewayId == GATEWAY_DWOLLA) {
|
2015-06-03 19:55:48 +02:00
|
|
|
$optional = array_merge($optional, ['key', 'secret']);
|
2016-02-01 14:58:52 +01:00
|
|
|
} elseif ($gatewayId == GATEWAY_STRIPE) {
|
2016-02-18 18:00:21 +01:00
|
|
|
if (Utils::isNinjaDev()) {
|
2016-02-11 16:12:27 +01:00
|
|
|
// do nothing - we're unable to acceptance test with StripeJS
|
|
|
|
} else {
|
2016-02-18 18:00:21 +01:00
|
|
|
$rules['publishable_key'] = 'required';
|
2016-04-28 22:38:01 +02:00
|
|
|
$rules['enable_ach'] = 'boolean';
|
2016-02-11 16:12:27 +01:00
|
|
|
}
|
2015-06-03 19:55:48 +02:00
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2016-05-14 23:23:20 +02:00
|
|
|
if ($gatewayId != GATEWAY_WEPAY) {
|
|
|
|
foreach ($fields as $field => $details) {
|
|
|
|
if (!in_array($field, $optional)) {
|
|
|
|
if (strtolower($gateway->name) == 'beanstream') {
|
|
|
|
if (in_array($field, ['merchant_id', 'passCode'])) {
|
|
|
|
$rules[$gateway->id . '_' . $field] = 'required';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$rules[$gateway->id . '_' . $field] = 'required';
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$creditcards = Input::get('creditCardTypes');
|
|
|
|
$validator = Validator::make(Input::all(), $rules);
|
|
|
|
|
|
|
|
if ($validator->fails()) {
|
|
|
|
return Redirect::to('gateways/create')
|
|
|
|
->withErrors($validator)
|
|
|
|
->withInput();
|
|
|
|
} else {
|
|
|
|
$account = Account::with('account_gateways')->findOrFail(Auth::user()->account_id);
|
2015-04-16 19:12:56 +02:00
|
|
|
$oldConfig = null;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
if ($accountGatewayPublicId) {
|
|
|
|
$accountGateway = AccountGateway::scope($accountGatewayPublicId)->firstOrFail();
|
2015-11-01 19:21:11 +01:00
|
|
|
$oldConfig = $accountGateway->getConfig();
|
2015-03-16 22:45:25 +01:00
|
|
|
} else {
|
|
|
|
$accountGateway = AccountGateway::createNew();
|
|
|
|
$accountGateway->gateway_id = $gatewayId;
|
2016-05-14 23:23:20 +02:00
|
|
|
|
2016-05-15 04:32:27 +02:00
|
|
|
if ($gatewayId == GATEWAY_WEPAY) {
|
|
|
|
if(!$this->setupWePay($accountGateway, $wepayResponse)) {
|
|
|
|
return $wepayResponse;
|
|
|
|
}
|
|
|
|
$oldConfig = $accountGateway->getConfig();
|
2016-05-14 23:23:20 +02:00
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$config = new stdClass();
|
2016-05-14 23:23:20 +02:00
|
|
|
|
|
|
|
if ($gatewayId != GATEWAY_WEPAY) {
|
|
|
|
foreach ($fields as $field => $details) {
|
|
|
|
$value = trim(Input::get($gateway->id . '_' . $field));
|
|
|
|
// if the new value is masked use the original value
|
|
|
|
if ($oldConfig && $value && $value === str_repeat('*', strlen($value))) {
|
|
|
|
$value = $oldConfig->$field;
|
|
|
|
}
|
|
|
|
if (!$value && ($field == 'testMode' || $field == 'developerMode')) {
|
|
|
|
// do nothing
|
|
|
|
} else {
|
|
|
|
$config->$field = $value;
|
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
2016-05-15 04:32:27 +02:00
|
|
|
} elseif($oldConfig) {
|
2016-05-14 23:23:20 +02:00
|
|
|
$config = clone $oldConfig;
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2015-11-29 21:13:50 +01:00
|
|
|
$publishableKey = Input::get('publishable_key');
|
|
|
|
if ($publishableKey = str_replace('*', '', $publishableKey)) {
|
|
|
|
$config->publishableKey = $publishableKey;
|
|
|
|
} elseif ($oldConfig && property_exists($oldConfig, 'publishableKey')) {
|
|
|
|
$config->publishableKey = $oldConfig->publishableKey;
|
|
|
|
}
|
|
|
|
|
2016-04-28 22:38:01 +02:00
|
|
|
$plaidClientId = Input::get('plaid_client_id');
|
|
|
|
if ($plaidClientId = str_replace('*', '', $plaidClientId)) {
|
|
|
|
$config->plaidClientId = $plaidClientId;
|
|
|
|
} elseif ($oldConfig && property_exists($oldConfig, 'plaidClientId')) {
|
|
|
|
$config->plaidClientId = $oldConfig->plaidClientId;
|
|
|
|
}
|
|
|
|
|
|
|
|
$plaidSecret = Input::get('plaid_secret');
|
|
|
|
if ($plaidSecret = str_replace('*', '', $plaidSecret)) {
|
|
|
|
$config->plaidSecret = $plaidSecret;
|
|
|
|
} elseif ($oldConfig && property_exists($oldConfig, 'plaidSecret')) {
|
|
|
|
$config->plaidSecret = $oldConfig->plaidSecret;
|
|
|
|
}
|
|
|
|
|
|
|
|
$plaidPublicKey = Input::get('plaid_public_key');
|
|
|
|
if ($plaidPublicKey = str_replace('*', '', $plaidPublicKey)) {
|
|
|
|
$config->plaidPublicKey = $plaidPublicKey;
|
|
|
|
} elseif ($oldConfig && property_exists($oldConfig, 'plaidPublicKey')) {
|
|
|
|
$config->plaidPublicKey = $oldConfig->plaidPublicKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($gatewayId == GATEWAY_STRIPE) {
|
|
|
|
$config->enableAch = boolval(Input::get('enable_ach'));
|
|
|
|
}
|
|
|
|
|
2016-05-07 04:33:03 +02:00
|
|
|
if ($gatewayId == GATEWAY_BRAINTREE) {
|
|
|
|
$config->enablePayPal = boolval(Input::get('enable_paypal'));
|
|
|
|
}
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
$cardCount = 0;
|
|
|
|
if ($creditcards) {
|
|
|
|
foreach ($creditcards as $card => $value) {
|
|
|
|
$cardCount += intval($value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-16 19:12:56 +02:00
|
|
|
$accountGateway->accepted_credit_cards = $cardCount;
|
2015-07-12 21:43:45 +02:00
|
|
|
$accountGateway->show_address = Input::get('show_address') ? true : false;
|
|
|
|
$accountGateway->update_address = Input::get('update_address') ? true : false;
|
2015-11-01 19:21:11 +01:00
|
|
|
$accountGateway->setConfig($config);
|
2015-04-16 19:12:56 +02:00
|
|
|
|
|
|
|
if ($accountGatewayPublicId) {
|
2015-03-16 22:45:25 +01:00
|
|
|
$accountGateway->save();
|
|
|
|
} else {
|
|
|
|
$account->account_gateways()->save($accountGateway);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Input::get('token_billing_type_id')) {
|
|
|
|
$account->token_billing_type_id = Input::get('token_billing_type_id');
|
|
|
|
$account->save();
|
|
|
|
}
|
|
|
|
|
2016-05-14 23:23:20 +02:00
|
|
|
if(isset($wepayResponse)) {
|
|
|
|
return $wepayResponse;
|
2015-03-16 22:45:25 +01:00
|
|
|
} else {
|
2016-05-14 23:23:20 +02:00
|
|
|
if ($accountGatewayPublicId) {
|
|
|
|
$message = trans('texts.updated_gateway');
|
|
|
|
} else {
|
|
|
|
$message = trans('texts.created_gateway');
|
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2016-05-14 23:23:20 +02:00
|
|
|
Session::flash('message', $message);
|
|
|
|
return Redirect::to("gateways/{$accountGateway->public_id}/edit");
|
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-12 04:55:37 +02:00
|
|
|
protected function getWePayUpdateUri($accountGateway)
|
|
|
|
{
|
|
|
|
if ($accountGateway->gateway_id != GATEWAY_WEPAY) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$wepay = Utils::setupWePay($accountGateway);
|
|
|
|
|
|
|
|
$update_uri_data = $wepay->request('account/get_update_uri', array(
|
|
|
|
'account_id' => $accountGateway->getConfig()->accountId,
|
|
|
|
'mode' => 'iframe',
|
|
|
|
'redirect_uri' => URL::to('/gateways'),
|
|
|
|
));
|
|
|
|
|
|
|
|
return $update_uri_data->uri;
|
|
|
|
}
|
|
|
|
|
2016-05-14 23:23:20 +02:00
|
|
|
protected function setupWePay($accountGateway, &$response)
|
2016-05-12 04:55:37 +02:00
|
|
|
{
|
|
|
|
$user = Auth::user();
|
|
|
|
$account = $user->account;
|
|
|
|
|
|
|
|
$validator = Validator::make(Input::all(), array(
|
|
|
|
'company_name' => 'required',
|
|
|
|
'description' => 'required',
|
|
|
|
'tos_agree' => 'required',
|
|
|
|
'first_name' => 'required',
|
|
|
|
'last_name' => 'required',
|
|
|
|
'email' => 'required',
|
|
|
|
));
|
|
|
|
|
|
|
|
if ($validator->fails()) {
|
|
|
|
return Redirect::to('gateways/create')
|
|
|
|
->withErrors($validator)
|
|
|
|
->withInput();
|
|
|
|
}
|
|
|
|
|
|
|
|
try{
|
|
|
|
$wepay = Utils::setupWePay();
|
|
|
|
|
|
|
|
$wepayUser = $wepay->request('user/register/', array(
|
|
|
|
'client_id' => WEPAY_CLIENT_ID,
|
|
|
|
'client_secret' => WEPAY_CLIENT_SECRET,
|
|
|
|
'email' => Input::get('email'),
|
|
|
|
'first_name' => Input::get('first_name'),
|
|
|
|
'last_name' => Input::get('last_name'),
|
|
|
|
'original_ip' => \Request::getClientIp(true),
|
|
|
|
'original_device' => \Request::server('HTTP_USER_AGENT'),
|
|
|
|
'tos_acceptance_time' => time(),
|
2016-05-12 17:09:07 +02:00
|
|
|
'redirect_uri' => URL::to('gateways'),
|
2016-05-15 04:22:06 +02:00
|
|
|
'callback_uri' => URL::to(env('WEBHOOK_PREFIX','').'paymenthook/'.$account->account_key.'/'.GATEWAY_WEPAY),
|
2016-05-12 04:55:37 +02:00
|
|
|
'scope' => 'manage_accounts,collect_payments,view_user,preapprove_payments,send_money',
|
|
|
|
));
|
|
|
|
|
|
|
|
$accessToken = $wepayUser->access_token;
|
|
|
|
$accessTokenExpires = $wepayUser->expires_in ? (time() + $wepayUser->expires_in) : null;
|
|
|
|
|
|
|
|
$wepay = new WePay($accessToken);
|
|
|
|
|
|
|
|
$wepayAccount = $wepay->request('account/create/', array(
|
|
|
|
'name' => Input::get('company_name'),
|
|
|
|
'description' => Input::get('description'),
|
|
|
|
'theme_object' => json_decode(WEPAY_THEME),
|
|
|
|
));
|
|
|
|
|
|
|
|
try {
|
|
|
|
$wepay->request('user/send_confirmation/', []);
|
|
|
|
$confirmationRequired = true;
|
|
|
|
} catch(\WePayException $ex){
|
|
|
|
if ($ex->getMessage() == 'This access_token is already approved.') {
|
|
|
|
$confirmationRequired = false;
|
|
|
|
} else {
|
|
|
|
throw $ex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-12 17:09:07 +02:00
|
|
|
if (($gateway = $account->getGatewayByType(PAYMENT_TYPE_CREDIT_CARD)) || ($gateway = $account->getGatewayByType(PAYMENT_TYPE_STRIPE))) {
|
|
|
|
$gateway->delete();
|
|
|
|
}
|
|
|
|
|
2016-05-12 04:55:37 +02:00
|
|
|
$accountGateway->gateway_id = GATEWAY_WEPAY;
|
|
|
|
$accountGateway->setConfig(array(
|
|
|
|
'userId' => $wepayUser->user_id,
|
|
|
|
'accessToken' => $accessToken,
|
|
|
|
'tokenType' => $wepayUser->token_type,
|
|
|
|
'tokenExpires' => $accessTokenExpires,
|
|
|
|
'accountId' => $wepayAccount->account_id,
|
2016-05-13 15:30:22 +02:00
|
|
|
'testMode' => WEPAY_ENVIRONMENT == WEPAY_STAGE,
|
2016-05-12 04:55:37 +02:00
|
|
|
));
|
|
|
|
|
|
|
|
if ($confirmationRequired) {
|
|
|
|
Session::flash('message', trans('texts.created_wepay_confirmation_required'));
|
|
|
|
} else {
|
|
|
|
$updateUri = $wepay->request('/account/get_update_uri', array(
|
|
|
|
'account_id' => $wepayAccount->account_id,
|
|
|
|
'redirect_uri' => URL::to('gateways'),
|
|
|
|
));
|
|
|
|
|
2016-05-14 23:23:20 +02:00
|
|
|
$response = Redirect::to($updateUri->uri);
|
|
|
|
return true;
|
2016-05-12 04:55:37 +02:00
|
|
|
}
|
|
|
|
|
2016-05-14 23:23:20 +02:00
|
|
|
$response = Redirect::to("gateways/{$accountGateway->public_id}/edit");
|
|
|
|
return true;
|
2016-05-12 04:55:37 +02:00
|
|
|
} catch (\WePayException $e) {
|
|
|
|
Session::flash('error', $e->getMessage());
|
2016-05-14 23:23:20 +02:00
|
|
|
$response = Redirect::to('gateways/create')
|
2016-05-12 04:55:37 +02:00
|
|
|
->withInput();
|
2016-05-14 23:23:20 +02:00
|
|
|
return false;
|
2016-05-12 04:55:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function resendConfirmation($publicId = false)
|
|
|
|
{
|
|
|
|
$accountGateway = AccountGateway::scope($publicId)->firstOrFail();
|
|
|
|
|
|
|
|
if ($accountGateway->gateway_id == GATEWAY_WEPAY) {
|
|
|
|
try {
|
|
|
|
$wepay = Utils::setupWePay($accountGateway);
|
|
|
|
$wepay->request('user/send_confirmation', []);
|
|
|
|
|
|
|
|
Session::flash('message', trans('texts.resent_confirmation_email'));
|
|
|
|
} catch (\WePayException $e) {
|
|
|
|
Session::flash('error', $e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Redirect::to("gateways/{$accountGateway->public_id}/edit");
|
|
|
|
}
|
2016-05-12 17:09:07 +02:00
|
|
|
|
|
|
|
public function switchToWepay()
|
|
|
|
{
|
|
|
|
$data = self::getViewModel();
|
|
|
|
$data['url'] = 'gateways';
|
|
|
|
$data['method'] = 'POST';
|
|
|
|
unset($data['gateways']);
|
|
|
|
|
|
|
|
if ( ! \Request::secure() && ! Utils::isNinjaDev()) {
|
|
|
|
Session::flash('warning', trans('texts.enable_https'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return View::make('accounts.account_gateway_switch_wepay', $data);
|
|
|
|
}
|
2015-06-01 22:07:37 +02:00
|
|
|
}
|