【问题标题】:Laravel Spark 403 - REDIRECT HOST MISMATCH when 3D Secure PaymentLaravel Spark 403 - 3D 安全支付时重定向主机不匹配
【发布时间】:2021-02-25 22:21:15
【问题描述】:

我正在使用 Laravel Spark 为我的客户计费。如果他们将使用 3D 安全卡付款(我使用来自 stripe doc 的 https://stripe.com/docs/testing#regulatory-cards: 4000 0027 6000 3184 的卡号对其进行测试)我有以下问题:

条带弹出窗口/模式打开,我点击“完成身份验证”。

之后身份验证过程开始,用户被定向到http://127.0.0.1:8000/stripe/payment/pi_xxx?redirect=/home

在这里我得到以下 Laravel 错误(我在控制台或其他地方没有找到其他错误原因):

我已经在VerifyCsrfToken 类中添加了stripe/*... 也许我需要在我的服务器上测试这个案例?

很奇怪,我想这表明我对条纹没有任何问题,而是使用 laravel。当我删除查询参数?redirect=home 我得到这个屏幕:

当我继续时,我没有被重定向.. 当然因为没有重定向 uri...

以前有人遇到过这个问题吗?

【问题讨论】:

  • 射击,我很高兴看到这个错误,我正在经历与我们所说的完全相同的事情。然后我向下滚动,没有答案!我也会检查解决方案,如果我找到它会分享。当我进行单次收费时,我遇到了类似的错误,有一个很大的错误,用户会收到一封确认付款的电子邮件。很烦人。
  • @helloworld 我也已经构建了一个解决方法。我明天会把它贴在这里。在我看来,这是 Spark 库中的一个错误。

标签: php laravel stripe-payments laravel-cashier laravel-spark


【解决方案1】:

对我来说,这是 Spark 中的一个错误。我搜索了 Stripe 使用重定向的所有情况。对我来说,这可能是一个错误的一个迹象是:

subscription-notice.blade.php 文件中的链接构建如下:

{!! __('Please :linkOpen confirm your payment :linkClose to activate your subscription!', ['linkOpen' => '<a href="/'.config('cashier.path').'/payment/'.auth()->user()->subscription()->latestPayment()->id.'?redirect='.url('/home').'">', 'linkClose' => '</a>']) !!}

'?redirect='.url('/home').' 部分使用主机地址创建一个完整的有效 URL。 不仅是相对路径!在我的情况下,这些相对路径会遇到 403 错误。 就像在RegisterController

/**
 * Handle a registration request for the application.
 *
 * @param  \Laravel\Spark\Contracts\Http\Requests\Auth\RegisterRequest  $request
 * @return \Illuminate\Http\Response
 */
public function register(RegisterRequest $request)
{
    list($user, $paymentId) = Spark::interact(
        Register::class, [$request]
    );

    Auth::login($user);

    event(new UserRegistered($user));

    if ($user instanceof MustVerifyEmail && ! $user->hasVerifiedEmail()) {
        $user->sendEmailVerificationNotification();
    }

    return response()->json([
        'redirect' => $paymentId ?
            '/'.config('cashier.path').'/payment/'.$paymentId.'?redirect='.$this->redirectPath()
            : $this->redirectPath(),
    ]);
}

$this-&gt;redirectPath() 返回一个相对路径。我已将此部分更改为:

return response()->json([
        'redirect' => $paymentId ?
            '/'.config('cashier.path').'/payment/'.$paymentId.'?redirect='.config('app.url').$this->redirectPath()
            : $this->redirectPath(),
    ]);

在这种情况下,我从配置中获取主机地址并将其放在相对路径的前面。

为了更好的理解,这里使用上面返回的URL(register-stripe.js):

    /*
     * After obtaining the Stripe token, send the registration to Spark.
     */
    sendRegistration(paymentMethod) {
        this.registerForm.stripe_payment_method = paymentMethod;

        Spark.post('/register', this.registerForm)
            .then(response => {
                window.location = response.redirect;
            });
    }

在某些情况下,我需要覆盖一些 JavaScript 或 PHP 源...

  • 注册过程(此处显示)
  • 支付信息更新流程
  • 使用现有帐户创建订阅

我希望我可以帮助其他人!如有必要,我还可以发布我在 cmets 中更改重定向 URL 的确切位置。

【讨论】:

    【解决方案2】:

    我想出了一个(肮脏的?)解决方法:

    在我的 StripWebHookController 中有一些代码可以为用户发出通知:

    if ($billable) {
            $model = config('cashier.model');
            $notifiable = $billable instanceof $model ? $billable : $billable->owner;
            if (in_array(Notifiable::class, class_uses_recursive($notifiable))) {
                $payment = new Payment(StripePaymentIntent::retrieve(
                    $payload['data']['object']['payment_intent'],
                    $billable->stripeOptions()
                ));
                $notifiable->notify(new $notification($payment));
            }
        }
    

    现在,这是一个通知,显然是在 Cashier 的 PaymentController(位于 /var/www/html/site/vendor/laravel/cashier/src/Http/Controllers)中创建的 StripePaymentIntent 通知

    有一个 VerifyRedirectUrl 中间件导致了问题。因此,当您将其注释掉时,403 就会消失:

    public function __construct()
    {
        //$this->middleware(VerifyRedirectUrl::class);
    }
    

    但是,接受付款后的“返回”按钮不起作用,所以我会检查一下。但就目前而言,这个 403 至少已经消失了。如果我找不到其他解决方案,我会选择这个。

    【讨论】:

    • 后退按钮仍然不起作用,作为一个肮脏的解决方法,我更改了包含该后退按钮的刀片文件。位于此处:/var/www/html/site/vendor/laravel/cashier/resources/views# more payment.blade.php 并添加了这个
    猜你喜欢
    • 2021-06-16
    • 2019-10-25
    • 2015-05-19
    • 2016-10-26
    • 2021-10-11
    • 1970-01-01
    • 2016-06-09
    • 2017-09-02
    • 2017-06-07
    相关资源
    最近更新 更多