【问题标题】:Laravel stripe payment form isn't handled just postedLaravel Stripe 付款表单未处理,刚刚发布
【发布时间】:2019-07-21 20:04:09
【问题描述】:

我使用的是来自 Stripe 的默认卡片元素,可以在 here 找到。表单渲染和验证条包括作品和渲染。但是,我从来没有生成过stripeToken,所以订阅失败是因为:

This customer has no attached payment source

当我转储我的请求时,stripeTokenNULL。我认为这是因为条纹表单处理程序对我根本不起作用,它们包含的事件侦听器永远不会触发。

看起来表单只是像普通表单一样发布,而不是条纹添加的阻止默认 JS 侦听器。

<form action="{{ route('subscriptionCreate') }}" method="post" id="payment-form">
  @csrf
  <input type="hidden" name="plan" value="{{ $plan->id }}">
  <div class="form-row">
    <label for="card-element">
      Credit or debit card
    </label>
    <div id="card-element">
      <!-- A Stripe Element will be inserted here. -->
    </div>

    <div id="card-errors" role="alert"></div>
  </div>

  <button>Submit Payment</button>
</form>

元素示例中包含的 Javascript;

<script>

// Create a Stripe client.
var stripe = Stripe('###Removed###');

// Create an instance of Elements.
var elements = stripe.elements();

// Custom styling can be passed to options when creating an Element.
// (Note that this demo uses a wider set of styles than the guide below.)
var style = {
  base: {
    color: '#32325d',
    fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
    fontSmoothing: 'antialiased',
    fontSize: '16px',
    '::placeholder': {
      color: '#aab7c4'
    }
  },
  invalid: {
    color: '#fa755a',
    iconColor: '#fa755a'
  }
};

// Create an instance of the card Element.
var card = elements.create('card', {style: style});

// Add an instance of the card Element into the `card-element` <div>.
card.mount('#card-element');

// Handle real-time validation errors from the card Element.
card.addEventListener('change', function(event) {
  var displayError = document.getElementById('card-errors');
  if (event.error) {
    displayError.textContent = event.error.message;
  } else {
    displayError.textContent = '';
  }
});

// Handle form submission.
var form = document.getElementById('payment-form');
form.addEventListener('submit', function(event) {
  event.preventDefault();

  stripe.createToken(card).then(function(result) {
    if (result.error) {
      // Inform the user if there was an error.
      var errorElement = document.getElementById('card-errors');
      errorElement.textContent = result.error.message;
    } else {
      // Send the token to your server.
      stripeTokenHandler(result.token);
    }
  });
});


// // Submit the form with the token ID.
function stripeTokenHandler(token) {
  // Insert the token ID into the form so it gets submitted to the server
  var form = document.getElementById('payment-form');
  var hiddenInput = document.createElement('input');
  hiddenInput.setAttribute('type', 'hidden');
  hiddenInput.setAttribute('name', 'stripeToken');
  hiddenInput.setAttribute('value', token.id);
  form.appendChild(hiddenInput);

  // Submit the form
  form.submit();
}
</script>

这是我的控制器,正如我提到的 $request-&gt;stripeToken 不存在我认为它不会被添加到表单中。

public function create(Request $request)
{

    $plan = Plan::findOrFail($request->get('plan'));

    $user = $request->user();

    $request->user()
        ->newSubscription($plan->name, $plan->stripe_plan)
        ->create($request->stripeToken,[
            'email' => $user->email
        ]);

    return redirect()->route('home')->with('success', 'Your plan subscribed successfully');
}

【问题讨论】:

  • 在按下提交按钮后提交表单之前,您的浏览器控制台是否有任何错误?
  • @Markus 没有错误显示
  • 我也有同样的问题,Laravel 也是如此。我根本无法让它工作,这真的让我很烦恼。如果有人可以帮助找到解决方案,我会很高兴...
  • 卡片显示出来了吗,我已经集成了超过 11 个支付网关,非常乐意提供帮助
  • 我只需要获取更多信息,以便更好地帮助您,首先让我知道您是否可以插入信用卡详细信息

标签: javascript php laravel-5 stripe-payments


【解决方案1】:

我创建了https://jsfiddle.net/s8foxw9r/2/,确认stripeTokenHandler 正在工作(并且令牌正在正确生成)。

如果您打开 jsfiddle URL,然后在浏览器中打开开发人员工具并查看控制台输出,您将看到传递给 stripeTokenHandler()token 对象的转储。

当您点击Submit Payment 时,页面POSTs 到https://postman-echo.com/post 将转储您的请求。您会注意到请求看起来像这样:

{
  "args": {},
  "data": "",
  "files": {},
  "form": {
    "stripeToken": "tok_1F07d72eZvKYlo2CqBprboVK"
  },
  "headers": {
    "x-forwarded-proto": "https",
    "host": "postman-echo.com",
    "content-length": "40",
    "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
    "accept-encoding": "gzip, deflate, br",
    "accept-language": "en-US,en;q=0.9",
    "cache-control": "no-cache",
    "content-type": "application/x-www-form-urlencoded",
    "cookie": "sails.sid=s%3AHyTHsNyIhRvFINR3EGiXw1Kf12oufx84.jd6rEiCaqHsrM8eOGN1x%2ByzU%2BMatjM4l5S1Ekxhxdyo",
    "origin": "https://fiddle.jshell.net",
    "pragma": "no-cache",
    "referer": "https://fiddle.jshell.net/",
    "upgrade-insecure-requests": "1",
    "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36",
    "x-forwarded-port": "443"
  },
  "json": {
    "stripeToken": "tok_1F07d72eZvKYlo2CqBprboVK"
  },
  "url": "https://postman-echo.com/post"
}

重要的部分是headers.content-typejson 属性。这意味着stripe.js 库POST通过application/x-www-form-urlencoded MIME 类型编辑了表单,并且包含了stripeToken

这意味着您的问题是以下一种/两种:

  1. 如果您没有在浏览器中看到生成的令牌,则您的条带私钥可能不正确。我会先使用您的条带测试私钥,它看起来像 pk_test_xxx
  2. 您的 Laravel 服务器设置不正确。很多潜在问题,需要更多信息,不幸的是我从未使用过 Laravel。

最后,我找到了一个 Laravel 的 Stripe 集成包,可能值得一试:https://github.com/cartalyst/stripe-laravel

【讨论】:

    【解决方案2】:

    我之前也遇到过同样的问题 这对我来说是这样的:

    composer require stripe/stripe-php
    

    然后使用您的密钥更新 env 文件

    STRIPE_KEY=xxxxxxxxxxxxxxxx
    STRIPE_SECRET=xxxxxxxxxxxxxxx
    

    然后用你的控制器

    use Stripe;
        public function create(Request $request)
        {
            $plan = Plan::findOrFail($request->get('plan'));
            $user = $request->user();
            $request->user()
            ->newSubscription($plan->name, $plan->stripe_plan);
           // getting the plan cost
            $amount = $plan->plan_amount;
    
            Stripe\Stripe::setApiKey(env('STRIPE_SECRET'));
            Stripe\Charge::create ([
                    "amount" => ($amount * 100),
                    "currency" => "usd",
                    "source" => $request->stripeToken,
                    "description" => "Test payment" 
            ]);
        return redirect()->route('home')->with('success', 'Your plan subscribed successfully');
         }
    

    【讨论】:

    • 在您的示例中,您有newSubscriptionStripe\Charge,您肯定不会同时拥有这两者,或者这是一次性付款和订阅的示例?
    【解决方案3】:

    默认的 Laravel 安装将包含一个 app.js 文件。通过简单地删除或注释掉包含在您的标头中的此文件,Stripe 可以使用 Stripe Elements 生成令牌。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-28
      • 1970-01-01
      • 1970-01-01
      • 2012-01-17
      • 2013-01-02
      • 2011-04-25
      • 2018-03-06
      • 1970-01-01
      相关资源
      最近更新 更多