Support setting redirect URL for buy now button

This commit is contained in:
Hillel Coren 2016-12-02 14:51:05 +02:00
parent f59b2e1c98
commit c8d4941fa6
4 changed files with 36 additions and 7 deletions

View File

@ -117,7 +117,8 @@ class OnlinePaymentController extends BaseController
} else {
Session::flash('message', trans('texts.applied_payment'));
}
return redirect()->to('view/' . $invitation->invitation_key);
return $this->completePurchase($invitation);
} catch (Exception $exception) {
return $this->error($paymentDriver, $exception, true);
}
@ -152,12 +153,22 @@ class OnlinePaymentController extends BaseController
if ($paymentDriver->completeOffsitePurchase(Input::all())) {
Session::flash('message', trans('texts.applied_payment'));
}
return redirect()->to($invitation->getLink());
return $this->completePurchase($invitation, true);
} catch (Exception $exception) {
return $this->error($paymentDriver, $exception);
}
}
private function completePurchase($invitation, $isOffsite = false)
{
if ($redirectUrl = session('redirect_url:' . $invitation->invitation_key)) {
return redirect()->to($redirectUrl . '?invoice_id=' . $invitation->invoice->public_id);
} else {
// Allow redirecting to iFrame for offsite payments
return redirect()->to($invitation->getLink('view', ! $isOffsite));
}
}
/**
* @param $paymentDriver
* @param $exception
@ -253,17 +264,18 @@ class OnlinePaymentController extends BaseController
}
$account = Account::whereAccountKey(Input::get('account_key'))->first();
$redirectUrl = Input::get('redirect_url', URL::previous());
$redirectUrl = Input::get('redirect_url');
$failureUrl = URL::previous();
if ( ! $account || ! $account->enable_buy_now_buttons || ! $account->hasFeature(FEATURE_BUY_NOW_BUTTONS)) {
return redirect()->to("{$redirectUrl}/?error=invalid account");
return redirect()->to("{$failureUrl}/?error=invalid account");
}
Auth::onceUsingId($account->users[0]->id);
$product = Product::scope(Input::get('product_id'))->first();
if ( ! $product) {
return redirect()->to("{$redirectUrl}/?error=invalid product");
return redirect()->to("{$failureUrl}/?error=invalid product");
}
$rules = [
@ -274,7 +286,7 @@ class OnlinePaymentController extends BaseController
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return redirect()->to("{$redirectUrl}/?error=" . $validator->errors()->first());
return redirect()->to("{$failureUrl}/?error=" . $validator->errors()->first());
}
$data = [
@ -300,6 +312,10 @@ class OnlinePaymentController extends BaseController
$invitation = $invoice->invitations[0];
$link = $invitation->getLink();
if ($redirectUrl) {
session(['redirect_url:' . $invitation->invitation_key => $redirectUrl]);
}
if ($gatewayTypeAlias) {
return redirect()->to($invitation->getLink('payment') . "/{$gatewayTypeAlias}");
} else {

View File

@ -2255,6 +2255,8 @@ $LANG = array(
'live_preview_help' => 'Display a live PDF preview on the invoice page.<br/>Disable this to improve performance when editing invoices.',
'force_pdfjs_help' => 'Replace the built-in PDF viewer in :chrome_link and :firefox_link.<br/>Enable this if your browser is automatically downloading the PDF.',
'force_pdfjs' => 'PDF Viewer',
'redirect_url' => 'Redirect URL',
'redirect_url_help' => 'Optionally specify a URL to redirect to after a payment is made.',
);

View File

@ -155,6 +155,11 @@
->inlineHelp('buy_now_buttons_warning')
->addGroupClass('product-select') !!}
{!! Former::text('redirect_url')
->onchange('updateBuyNowButtons()')
->placeholder('https://www.example.com')
->help('redirect_url_help') !!}
{!! Former::checkboxes('client_fields')
->onchange('updateBuyNowButtons()')
->checkboxes([
@ -274,6 +279,7 @@
var productId = $('#product').val();
var landingPage = $('input[name=landing_page_type]:checked').val()
var paymentType = landingPage == 'payment' ? '/' + $('#payment_type').val() : '';
var redirectUrl = $('#redirect_url').val();
var form = '';
var link = '';
@ -294,6 +300,11 @@
}
@endforeach
if (redirectUrl) {
link += '&redirect_url=' + encodeURIComponent(redirectUrl);
form += '<input type="hidden" name="redirect_url" value="' + redirectUrl + '"/>' + "\n";
}
form += '<input type="submit" value="Buy Now" name="submit"/>' + "\n" + '</form>';
}