Laravel 8 - 在线支付przelewy24网关实现

4
我希望在我的项目中实现在线支付。我使用来自波兰的Przelewy24网关。
对于Laravel 8,我选择了适用于此处的插件: https://github.com/mnastalski/przelewy24-php 我遇到的问题是网关反馈有关转账是否已收到或已预订的信息。
我联系了Przelewy24技术支持,并从他们那里收到了以下日志信息:
[responseBody] => 重定向到“/登录” 重定向到“/登录”。 [httpResponseCode] => 302
我尝试了很多方法,但不幸的是没有任何结果。 下面的代码已经被修改了很多次。
后端
-- web.php --

Route::post('/order_status', 'Payment_Controller@order_status')->name('order_status');
Auth::routes([ 'verify' => true ]);
...
...
Route::get('/orderPaymentMethods/', 'Payment_Controller@paymentMethods')->name('orderPayments_paymentMethods');
Route::get('/order-success', 'Payment_Controller@order_success')->name('order_success'); 

-- Payment_Controller.php

private function create_instance()
    {
        return new Przelewy24([
            'merchant_id' => $this->partnerId,
            'crc' => $this->crc,
            'pos_id' => $this->posId,
            'live' => false,  // `true` for production/live mode
        ]);
    }

    public function create_transaction()
    {
        $this->currency = $this->settings->first()->currency;

        if (!$this->sessionId) {
            $this->sessionId = $this->set_sessionId();
        }

        $this->checkCart();     // this function is checking if cart is empty or not
        $this->get_totalPrice_from_cart();         // this function is geting total price of cart
        $this->user_email = \Auth::user()->email;

        try {
            $this->transaction = $this->create_instance()->transaction([
                'session_id' => $this->sessionId,
                'url_return' => route('order_success'),
                'url_status' => route('order_status'),
                'amount' => $this->price,
                'description' => $this->description,
                'email' => $this->user_email,
                'currency' => $this->currency,
            ]);

            if ($this->transaction->token()) {
                /** TRANSACTION STARTED */
                return $this->transaction;
            }
            return 'Create transaction failed';

        } catch (ApiResponseException $e) {
            return 'Error(transaction): (' . $e->getCode() . ') - ' . $e->getMessage();
        }
    }

    public function set_sessionId()
    {
        // for testing purpose 1h session ID
        $this->sessionId = md5($this->posId . '|' . $this->partnerId . '|' . $this->price . '|' . $this->currency . '|' . $this->crc . '|' . date('Ymd-H'));

        return $this->sessionId;
    }

    public function order_success()
    {
        // this is returning page from przelewy24
        return view('order_success');
    }

    public function order_status()
    {
        // this is returning status and it doesn't work.
        // $webhook is always empty because as i mention before - support from przelewy24 getting Error 302 -redirecting to '/login'

        $this->checkCart();
        $instance = $this->create_instance();
        $webhook = $instance->handleWebhook();

        try {
            $verify = $instance->verify([
                'session_id' => $webhook->sessionId(),
                'order_id' => $webhook->orderId(),
                'amount' => $webhook->amount(),
            ]);
        } catch (ApiResponseException $e) {
            return $e->getMessage();
        }

        return response()->json($verify);
    }

前端

-- cartPage_paymentMethod.blade.php --

<html>
...
...
@php
  /** PAYMENT */

$transaction = new App\Http\Controllers\Payment_Controller();
$sessionId = $transaction->set_sessionId();
$create_trans = $transaction->create_transaction();
$transaction_url = '';
if(is_string($create_trans))
    echo $create_trans;
else
    $transaction_url = $create_trans->redirectUrl();

@endphp

<form method="post" action="{{$transaction_url}}">
    @csrf
    <button class="btn btn-primary">{{__('labels.toPayment')}}</button>
</form>

-- order_success.blade.php -- //


@section('content')

    <div id="message">Waiting for payment...</div>
    <div id="response">-</div>

    @push('custom-js')
        <script type="text/javascript">
            $(document).ready(function() {

                setTimeout(function() {

                    $.ajaxSetup({
                        headers: {
                            "Content-Type"    : "application/json",
                            "Accept"          : "application/json",
                            "cache-control": "no-cache",
                            "X-Requested-With": "XMLHttpRequest",
                            'X-CSRF-TOKEN'    : $('meta[name="csrf-token"]').attr('content')
                        },
                    });
                    $.ajax({
                        type       : "POST",
                        url        : '{{route('order_status')}}',
                        dataType   : "JSON",
                        headers    : {
                        },
                        processData: false,
                        success    : function(resp) {
                            console.log(resp);
                            $('#response').html(resp);
                        },
                        error      : function(xhr) {
                            console.log(xhr);
                        }
                    })
                }, 1000)

            })
        </script>
    @endpush
@endsection

我认为Laravel可能存在不允许外部脚本的问题,或者它试图强制脚本登录(即重定向到登录页面),但是在我的看法中,“order_status”无需用户登录。
目前还没有对数据库进行读写操作。这是我接下来要跟进的步骤,因为上述代码终于开始正常工作了。我已经苦苦挣扎了两个星期……所以,请帮帮我!
2个回答

1

首先的猜测是该路由受到了验证保护,但问题中并未提到。

`order_status` 在我看来用户不必登录

仔细检查路由的中间件,确保没有认证,如果没有帮助,建议使用监视包,例如 telescope 查看具体情况。

我相信这就足够为您解决问题了。

更新

`order_status` 路由在 Web 中间件中,受到 CSRF 保护。因此解决方案是将其从 CSRF 保护中排除,可以参考 here


我安装了Telescope,但是它会重定向到/login页面,但我找不到原因。我检查了所有与此请求/响应相关的日志,但没有发现任何问题。如果您能帮忙查看,我可以在这里发送图片。 - Kafus
我检查了“Payload”选项卡,可以看到从支付网关发送的所有付款数据,因此信息已正确发送,但在“响应”选项卡中,我可以看到信息:重定向到/登录。 - Kafus
实际上我已经安装了这个包,但是无法尝试它。我没有有效的商户ID来启动该过程。在Telescope中,请求详情中有一个中间件行。它是否包含auth?如果是,请查找它被应用在哪里,或者可能通过在路由中添加withoutMiddleware来帮助解决问题。 - MHIdea
还有在 order_status 方法中,你有 $this->checkCart(),你确定其中没有授权检查吗?为什么你需要它在那里? - MHIdea
你能给我你的电子邮件吗?我可以将 ID 数据发送给你。 - Kafus
是的,csrf 阻止了来自支付网关的响应。谢谢! - Kafus

0
这里有一个问题: [responseBody] => 重定向到 "/ login"。重定向是 "/ login"。[httpResponseCode] => 302
看起来你的 Laravel 应用程序需要对此 URL 进行授权。但实际上不应该需要。请检查控制器文件的开头部分。如果有类似以下内容的代码:
public function __construct()
{
    $this->middleware('auth');
}

在order_success方法中删除它。或者只需创建另一个没有auth中间件的回调控制器。


这个类中没有__constructmiddleware。我甚至创建了一个新的,但是出现了相同的“错误”。 - Kafus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接