Laravel 如何转跳到指定页面并携带数据

目前在使用一个基于Laravel开源的外卖点餐系统(Tastyigniter),需要制定一个REDSYS(我这边西班牙的银行刷卡接口)的接口。
因为Tastyigniter自带了Paypal的付款接口,跟我需要的REDSYS差不多,基本就是复制过来直接用了。
最后结账转跳的时候,paypal是直接使用Redirect::to进行转跳,但是REDSYS必须要按照POST进行提交,所以无法直接使用Redirect

        try {
            //创建接口
            $gateway = $this->createGateway($host);
            //发送请求
            $response = $gateway->purchase($fields)->send();
            //转跳到支付页面
            if ($response->isRedirect())
                return Redirect::to($response->getRedirectUrl());


            //LOG记录
            $order->logPaymentAttempt('Payment error -> '.$response->getMessage(), 0, $fields, $response->getData());
            throw new ApplicationException($response->getMessage());
        }

现在的做法是,创建一个路由和Controller,先转跳到创建的路由,输出input type=hidden表单和自动提交按钮转跳到支付页面。
但是转跳是需要传送$response,里面是需要提交的数据。
试过用Redirect::route(“redsys”)->with(“data”,$response)和Session::put()都无法正常传递给Controller

之前在Tastyigniter社区提问过,但是可能由于问题太过简单然后没有解答

由于没有laravel开发经验,一直无法实现,请大家帮忙解答一下!
感谢

《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
最佳答案
public function processPaymentForm($data, $host, $order)
{
    try {
        if ($response->isRedirect()){
            // 这里把要传的数据缓存
           $uuid = Str::uuid();
           Cache::put($uuid, "你需要的数据")
          // 这里跳转到构造自动提交的控制器
         return redirect()->route('autoSubmitForm', [$uuid])
        }
}

新建 控制器, 比如 PayController

class PayController extends Controler{
    public function autoSubmitForm($key){
         $data = Cacke::get($key)
        return view('autoSubmitForm', compact('data));
        // 在试图文件中,构建表单, 可以用 jQuery 等控制, 在试图加载完成后自动提交
    }
}

我能想到的也就这些了!!

2年前 评论
讨论数量: 7
public function pay(Request $request){
    $params =  [
        'data' => $response
    ]

   $request->request->add($params);
   $proxy = Request::create('redsys', 'POST');

   return Route::dispatch($proxy);
}
2年前 评论

@Eyeswap 谢谢解答,但还不是很理解 这个方法是写在controllers里面吗?需要怎么把$response从插件类传到controller方法里?

试了一下直接在插件类里面转跳

            $request = Request::create( $response->getRedirectUrl(), 'POST', $response->getData() );
            return Route::dispatch( $request );

也无法正常转跳,前端是AJAX提交,返回了一个空数组 之前是用Redirect::to 可以正常转跳,就是不能携带数据

2年前 评论

构造自动提交的表单

2年前 评论

@大毛 @Eyeswap 对,这就是我想要做的 我定义了一个表单的路由,我需要怎么把数据从支付类传到这个表单路由?

支付插件类

public function processPaymentForm($data, $host, $order)
{
    $this->validatePaymentMethod($order, $host);

    $fields = $this->getPaymentFormFields($order, $data);

    try {
        $gateway = $this->createGateway($host);
        $response = $gateway->purchase($fields)->send();
        if ($response->isRedirect()){
            //此处传输数据并转跳到表单页面进行自动提交
        }

        $order->logPaymentAttempt('Payment error -> '.$response->getMessage(), 0, $fields, $response->getData());
        throw new ApplicationException($response->getMessage());
    }
    catch (Exception $ex) {
        $order->logPaymentAttempt('Payment error -> '.$ex->getMessage(), 0, $fields, []);
        throw new ApplicationException($ex->getMessage().'Sorry, there was an error processing your payment. Please try again later.');
    }
}

路由

Route::any('/redsys', '\Jiongze\RedsysGateway\Controllers\Redsysjump@index');

控制器

public function index(Request $request){
    //返回携带数据并自动转跳的表单
}

我想的是这样,不知道有没有错,具体代码不知道怎么写

2年前 评论
大毛 2年前
public function processPaymentForm($data, $host, $order)
{
    try {
        if ($response->isRedirect()){
            // 这里把要传的数据缓存
           $uuid = Str::uuid();
           Cache::put($uuid, "你需要的数据")
          // 这里跳转到构造自动提交的控制器
         return redirect()->route('autoSubmitForm', [$uuid])
        }
}

新建 控制器, 比如 PayController

class PayController extends Controler{
    public function autoSubmitForm($key){
         $data = Cacke::get($key)
        return view('autoSubmitForm', compact('data));
        // 在试图文件中,构建表单, 可以用 jQuery 等控制, 在试图加载完成后自动提交
    }
}

我能想到的也就这些了!!

2年前 评论

直接用 php 的 http client 提交 post 请求不可以吗。

2年前 评论

其实这个类似于支付宝的pc网站post付款, 所有的参数发给前端,让前端post提交就好,提交结果再转到服务器,大部分应用于网站支付

2年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!