Better WePay configuration support

This commit is contained in:
Joshua Dwire 2016-05-14 17:23:20 -04:00
parent ce04c994dd
commit 947cb4a6f7
12 changed files with 144 additions and 100 deletions

View File

@ -1,5 +1,6 @@
<?php namespace App\Http\Controllers;
use App\Models\AccountGateway;
use Auth;
use File;
use Image;
@ -420,6 +421,7 @@ class AccountController extends BaseController
$account = Auth::user()->account;
$account->load('account_gateways');
$count = count($account->account_gateways);
$trashedCount = AccountGateway::scope($account->id)->withTrashed()->count();
if ($accountGateway = $account->getGatewayConfig(GATEWAY_STRIPE)) {
if (! $accountGateway->getPublishableStripeKey()) {
@ -427,7 +429,7 @@ class AccountController extends BaseController
}
}
if ($count == 0) {
if ($trashedCount == 0) {
return Redirect::to('gateways/create');
} else {
$switchToWepay = WEPAY_CLIENT_ID && !$account->getGatewayConfig(GATEWAY_WEPAY);

View File

@ -45,10 +45,6 @@ class AccountGatewayController extends BaseController
$accountGateway = AccountGateway::scope($publicId)->firstOrFail();
$config = $accountGateway->getConfig();
if ($accountGateway->gateway_id == GATEWAY_WEPAY) {
return Redirect::to('gateways');
}
foreach ($config as $field => $value) {
$config->$field = str_repeat('*', strlen($value));
}
@ -109,31 +105,7 @@ class AccountGatewayController extends BaseController
$paymentTypes = [];
foreach (Gateway::$paymentTypes as $type) {
if ($accountGateway || !$account->getGatewayByType($type)) {
if ($type == PAYMENT_TYPE_CREDIT_CARD && $account->getGatewayByType(PAYMENT_TYPE_STRIPE)) {
// Stripe is already handling credit card payments
continue;
}
if ($type == PAYMENT_TYPE_STRIPE && $account->getGatewayByType(PAYMENT_TYPE_CREDIT_CARD)) {
// Another gateway is already handling credit card payments
continue;
}
if ($type == PAYMENT_TYPE_DIRECT_DEBIT && $stripeGateway = $account->getGatewayByType(PAYMENT_TYPE_STRIPE)) {
if (!empty($stripeGateway->getAchEnabled())) {
// Stripe is already handling ACH payments
continue;
}
}
if ($type == PAYMENT_TYPE_PAYPAL && $braintreeGateway = $account->getGatewayConfig(GATEWAY_BRAINTREE)) {
if (!empty($braintreeGateway->getPayPalEnabled())) {
// PayPal is already enabled
continue;
}
}
if ($accountGateway || $account->canAddGateway($type)) {
$paymentTypes[$type] = $type == PAYMENT_TYPE_CREDIT_CARD ? trans('texts.other_providers'): trans('texts.'.strtolower($type));
if ($type == PAYMENT_TYPE_BITCOIN) {
@ -162,7 +134,7 @@ class AccountGatewayController extends BaseController
foreach ($gateways as $gateway) {
$fields = $gateway->getFields();
asort($fields);
$gateway->fields = $fields;
$gateway->fields = $gateway->id == GATEWAY_WEPAY ? [] : $fields;
if ($accountGateway && $accountGateway->gateway_id == $gateway->id) {
$accountGateway->fields = $gateway->fields;
}
@ -193,7 +165,7 @@ class AccountGatewayController extends BaseController
$ids = Input::get('bulk_public_id');
$count = $this->accountGatewayService->bulk($ids, $action);
Session::flash('message', trans('texts.archived_account_gateway'));
Session::flash('message', trans("texts.{$action}d_account_gateway"));
return Redirect::to('settings/' . ACCOUNT_PAYMENTS);
}
@ -208,10 +180,6 @@ class AccountGatewayController extends BaseController
$paymentType = Input::get('payment_type_id');
$gatewayId = Input::get('gateway_id');
if ($gatewayId == GATEWAY_WEPAY) {
return $this->setupWePay();
}
if ($paymentType == PAYMENT_TYPE_PAYPAL) {
$gatewayId = GATEWAY_PAYPAL_EXPRESS;
} elseif ($paymentType == PAYMENT_TYPE_BITCOIN) {
@ -245,6 +213,7 @@ class AccountGatewayController extends BaseController
}
}
if ($gatewayId != GATEWAY_WEPAY) {
foreach ($fields as $field => $details) {
if (!in_array($field, $optional)) {
if (strtolower($gateway->name) == 'beanstream') {
@ -256,6 +225,7 @@ class AccountGatewayController extends BaseController
}
}
}
}
$creditcards = Input::get('creditCardTypes');
$validator = Validator::make(Input::all(), $rules);
@ -274,9 +244,15 @@ class AccountGatewayController extends BaseController
} else {
$accountGateway = AccountGateway::createNew();
$accountGateway->gateway_id = $gatewayId;
if ($gatewayId == GATEWAY_WEPAY && !$this->setupWePay($accountGateway, $wepayResponse)) {
return $wepayResponse;
}
}
$config = new stdClass();
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
@ -289,6 +265,9 @@ class AccountGatewayController extends BaseController
$config->$field = $value;
}
}
} else {
$config = clone $oldConfig;
}
$publishableKey = Input::get('publishable_key');
if ($publishableKey = str_replace('*', '', $publishableKey)) {
@ -349,6 +328,9 @@ class AccountGatewayController extends BaseController
$account->save();
}
if(isset($wepayResponse)) {
return $wepayResponse;
} else {
if ($accountGatewayPublicId) {
$message = trans('texts.updated_gateway');
} else {
@ -356,10 +338,10 @@ class AccountGatewayController extends BaseController
}
Session::flash('message', $message);
return Redirect::to("gateways/{$accountGateway->public_id}/edit");
}
}
}
protected function getWePayUpdateUri($accountGateway)
{
@ -378,7 +360,7 @@ class AccountGatewayController extends BaseController
return $update_uri_data->uri;
}
protected function setupWePay()
protected function setupWePay($accountGateway, &$response)
{
$user = Auth::user();
$account = $user->account;
@ -441,7 +423,6 @@ class AccountGatewayController extends BaseController
$gateway->delete();
}
$accountGateway = AccountGateway::createNew();
$accountGateway->gateway_id = GATEWAY_WEPAY;
$accountGateway->setConfig(array(
'userId' => $wepayUser->user_id,
@ -451,7 +432,6 @@ class AccountGatewayController extends BaseController
'accountId' => $wepayAccount->account_id,
'testMode' => WEPAY_ENVIRONMENT == WEPAY_STAGE,
));
$account->account_gateways()->save($accountGateway);
if ($confirmationRequired) {
Session::flash('message', trans('texts.created_wepay_confirmation_required'));
@ -461,14 +441,17 @@ class AccountGatewayController extends BaseController
'redirect_uri' => URL::to('gateways'),
));
return Redirect::to($updateUri->uri);
$response = Redirect::to($updateUri->uri);
return true;
}
return Redirect::to("gateways");
$response = Redirect::to("gateways/{$accountGateway->public_id}/edit");
return true;
} catch (\WePayException $e) {
Session::flash('error', $e->getMessage());
return Redirect::to('gateways/create')
$response = Redirect::to('gateways/create')
->withInput();
return false;
}
}

View File

@ -1412,6 +1412,37 @@ class Account extends Eloquent
public function getFontFolders(){
return array_map(function($item){return $item['folder'];}, $this->getFontsData());
}
public function canAddGateway($type){
if($this->getGatewayByType($type)) {
return false;
}
if ($type == PAYMENT_TYPE_CREDIT_CARD && $this->getGatewayByType(PAYMENT_TYPE_STRIPE)) {
// Stripe is already handling credit card payments
return false;
}
if ($type == PAYMENT_TYPE_STRIPE && $this->getGatewayByType(PAYMENT_TYPE_CREDIT_CARD)) {
// Another gateway is already handling credit card payments
return false;
}
if ($type == PAYMENT_TYPE_DIRECT_DEBIT && $stripeGateway = $this->getGatewayByType(PAYMENT_TYPE_STRIPE)) {
if (!empty($stripeGateway->getAchEnabled())) {
// Stripe is already handling ACH payments
return false;
}
}
if ($type == PAYMENT_TYPE_PAYPAL && $braintreeGateway = $this->getGatewayConfig(GATEWAY_BRAINTREE)) {
if (!empty($braintreeGateway->getPayPalEnabled())) {
// PayPal is already enabled
return false;
}
}
return true;
}
}
Account::updated(function ($account) {

View File

@ -15,10 +15,14 @@ class AccountGatewayRepository extends BaseRepository
public function find($accountId)
{
return DB::table('account_gateways')
$query = DB::table('account_gateways')
->join('gateways', 'gateways.id', '=', 'account_gateways.gateway_id')
->where('account_gateways.deleted_at', '=', null)
->where('account_gateways.account_id', '=', $accountId)
->select('account_gateways.id', 'account_gateways.public_id', 'gateways.name', 'account_gateways.deleted_at', 'account_gateways.gateway_id');
->where('account_gateways.account_id', '=', $accountId);
if (!\Session::get('show_trash:gateway')) {
$query->where('account_gateways.deleted_at', '=', null);
}
return $query->select('account_gateways.id', 'account_gateways.public_id', 'gateways.name', 'account_gateways.deleted_at', 'account_gateways.gateway_id');
}
}

View File

@ -42,7 +42,9 @@ class AccountGatewayService extends BaseService
[
'name',
function ($model) {
if ($model->gateway_id != GATEWAY_WEPAY) {
if ($model->deleted_at) {
return $model->name;
} elseif ($model->gateway_id != GATEWAY_WEPAY) {
return link_to("gateways/{$model->public_id}/edit", $model->name)->toHtml();
} else {
$accountGateway = AccountGateway::find($model->id);
@ -89,20 +91,12 @@ class AccountGatewayService extends BaseService
{
return [
[
uctrans('texts.edit_gateway'),
function ($model) {
return URL::to("gateways/{$model->public_id}/edit");
},
function($model) {
return $model->gateway_id != GATEWAY_WEPAY;
}
], [
uctrans('texts.resend_confirmation_email'),
function ($model) {
return $model->resendConfirmationUrl;
},
function($model) {
return $model->gateway_id == GATEWAY_WEPAY && !empty($model->resendConfirmationUrl);
return !$model->deleted_at && $model->gateway_id == GATEWAY_WEPAY && !empty($model->resendConfirmationUrl);
}
], [
uctrans('texts.finish_setup'),
@ -110,7 +104,15 @@ class AccountGatewayService extends BaseService
return $model->setupUrl;
},
function($model) {
return $model->gateway_id == GATEWAY_WEPAY && !empty($model->setupUrl);
return !$model->deleted_at && $model->gateway_id == GATEWAY_WEPAY && !empty($model->setupUrl);
}
] , [
uctrans('texts.edit_gateway'),
function ($model) {
return URL::to("gateways/{$model->public_id}/edit");
},
function($model) {
return !$model->deleted_at;
}
], [
uctrans('texts.manage_wepay_account'),
@ -123,7 +125,7 @@ class AccountGatewayService extends BaseService
);
},
function($model) {
return $model->gateway_id == GATEWAY_WEPAY;
return !$model->deleted_at && $model->gateway_id == GATEWAY_WEPAY;
}
]
];

View File

@ -60,11 +60,7 @@ class DatatableService
$str .= '<div class="tr-status"></div>';
}
$str .= '<div class="btn-group tr-action" style="height:auto;display:none">
<button type="button" class="btn btn-xs btn-default dropdown-toggle" data-toggle="dropdown" style="width:100px">
'.trans('texts.select').' <span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu">';
$dropdown_contents = '';
$lastIsDivider = false;
if (!$model->deleted_at || $model->deleted_at == '0000-00-00') {
@ -78,7 +74,7 @@ class DatatableService
list($value, $url, $visible) = $action;
if ($visible($model)) {
if($value == '--divider--'){
$str .= "<li class=\"divider\"></li>";
$dropdown_contents .= "<li class=\"divider\"></li>";
$lastIsDivider = true;
}
else {
@ -89,13 +85,13 @@ class DatatableService
$attributes = ' '.$urlVal['attributes'];
}
$str .= "<li><a href=\"$urlStr\"{$attributes}>{$value}</a></li>";
$dropdown_contents .= "<li><a href=\"$urlStr\"{$attributes}>{$value}</a></li>";
$hasAction = true;
$lastIsDivider = false;
}
}
} elseif ( ! $lastIsDivider) {
$str .= "<li class=\"divider\"></li>";
$dropdown_contents .= "<li class=\"divider\"></li>";
$lastIsDivider = true;
}
}
@ -105,24 +101,35 @@ class DatatableService
}
if ( $can_edit && ! $lastIsDivider) {
$str .= "<li class=\"divider\"></li>";
$dropdown_contents .= "<li class=\"divider\"></li>";
}
if (($entityType != ENTITY_USER || $model->public_id) && $can_edit) {
$str .= "<li><a href=\"javascript:archiveEntity({$model->public_id})\">"
$dropdown_contents .= "<li><a href=\"javascript:archiveEntity({$model->public_id})\">"
. trans("texts.archive_{$entityType}") . "</a></li>";
}
} else if($can_edit) {
$str .= "<li><a href=\"javascript:restoreEntity({$model->public_id})\">"
if ($entityType != ENTITY_ACCOUNT_GATEWAY || Auth::user()->account->canAddGateway(\App\Models\Gateway::getPaymentType($model->gateway_id))) {
$dropdown_contents .= "<li><a href=\"javascript:restoreEntity({$model->public_id})\">"
. trans("texts.restore_{$entityType}") . "</a></li>";
}
}
if (property_exists($model, 'is_deleted') && !$model->is_deleted && $can_edit) {
$str .= "<li><a href=\"javascript:deleteEntity({$model->public_id})\">"
$dropdown_contents .= "<li><a href=\"javascript:deleteEntity({$model->public_id})\">"
. trans("texts.delete_{$entityType}") . "</a></li>";
}
return $str.'</ul></div></center>';
if (!empty($dropdown_contents)) {
$str .= '<div class="btn-group tr-action" style="height:auto;display:none">
<button type="button" class="btn btn-xs btn-default dropdown-toggle" data-toggle="dropdown" style="width:100px">
'.trans('texts.select').' <span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu">';
$str .= $dropdown_contents . '</ul>';
}
return $str.'</div></center>';
});
}

View File

@ -30933,7 +30933,7 @@ function truncate(string, length){
// Show/hide the 'Select' option in the datalists
function actionListHandler() {
$('tbody tr').mouseover(function() {
$('tbody tr .tr-action').closest('tr').mouseover(function() {
$(this).closest('tr').find('.tr-action').show();
$(this).closest('tr').find('.tr-status').hide();
}).mouseout(function() {

View File

@ -1042,7 +1042,7 @@ function truncate(string, length){
// Show/hide the 'Select' option in the datalists
function actionListHandler() {
$('tbody tr').mouseover(function() {
$('tbody tr .tr-action').closest('tr').mouseover(function() {
$(this).closest('tr').find('.tr-action').show();
$(this).closest('tr').find('.tr-status').hide();
}).mouseout(function() {

View File

@ -1311,6 +1311,8 @@ $LANG = array(
'created_wepay_confirmation_required' => 'Please check your email and confirm your email address with WePay.',
'switch_to_wepay' => 'Switch to WePay',
'switch' => 'Switch',
'restore_account_gateway' => 'Restore Gateway',
'restored_account_gateway' => 'Successfully restored gateway',
);
return $LANG;

View File

@ -94,7 +94,7 @@
{!! Former::text('publishable_key') !!}
@endif
@if ($gateway->id == GATEWAY_STRIPE || $gateway->id == GATEWAY_BRAINTREE)
@if ($gateway->id == GATEWAY_STRIPE || $gateway->id == GATEWAY_BRAINTREE || $gateway->id == GATEWAY_WEPAY)
{!! Former::select('token_billing_type_id')
->options($tokenBillingOptions)
->help(trans('texts.token_billing_help')) !!}

View File

@ -8,7 +8,12 @@
{!! Button::success(trans('texts.switch_to_wepay'))
->asLinkTo(URL::to('/gateways/switch/wepay'))
->appendIcon(Icon::create('circle-arrow-up')) !!}
&nbsp;
@endif
<label for="trashed" style="font-weight:normal; margin-left: 10px;">
<input id="trashed" type="checkbox" onclick="setTrashVisible()"
{{ Session::get("show_trash:gateway") ? 'checked' : ''}}/>&nbsp; {{ trans('texts.show_archived_deleted')}} {{ Utils::transFlowText('gateways') }}
</label>
@if ($showAdd)
{!! Button::primary(trans('texts.add_gateway'))
@ -34,6 +39,14 @@
<script>
window.onDatatableReady = actionListHandler;
function setTrashVisible() {
var checked = $('#trashed').is(':checked');
var url = '{{ URL::to('view_archive/gateway') }}' + (checked ? '/true' : '/false');
$.get(url, function(data) {
refreshDatatable();
})
}
</script>
@stop

View File

@ -26,7 +26,7 @@
&nbsp;<label for="trashed" style="font-weight:normal; margin-left: 10px;">
<input id="trashed" type="checkbox" onclick="setTrashVisible()"
{{ Session::get("show_trash:{$entityType}") ? 'checked' : ''}}/>&nbsp; {{ trans('texts.show_archived_deleted')}} {{ Utils::transFlowText($entityType.'s') }}
</label>
</label>-onli
<div id="top_right_buttons" class="pull-right">
<input id="tableFilter" type="text" style="width:140px;margin-right:17px;background-color: white !important"