简体   繁体   English

无法使用 Laravel 在 Stripe 中发送令牌

[英]Unable to send Token in Stripe with Laravel

The Error I am getting when I try to checkout after putting card details:输入卡详细信息后尝试结帐时遇到的错误:

Invalid source object: must be a dictionary or a non-empty string.无效的源 object:必须是字典或非空字符串。 See API docs at https://stripe.com/docs'请参阅https://stripe.com/docs 上的API 文档

Here I taking all the card information:在这里,我获取了所有卡信息:

<form method="post" action="{{route('checkout')}}" enctype="multipart/form-data" id="checkout-form">
@csrf
<div class="col-sm-3">
   <div class="form-group">
       <label>Card Number*</label>
            <input type="text" class="form-control" name="card-number" id="card-number" placeholder="4111 1111 1111 1111"> </div>
</div>
<div class="col-sm-3">
      <div class="form-group">
           <label>Card CVC*</label>
               <input type="text" class="form-control" name="card-cvc" id="card-cvc" placeholder="111"> </div>
</div>
<div class="col-sm-3">
     <div class="form-group">
          <label>Card Expiry Month*</label>
               <input type="text" class="form-control" name="card-month" id="card-month" placeholder="12"> </div>
 </div>
 <div class="col-sm-3">
      <div class="form-group">
           <label>Card Expiry Year*</label>
           <input type="text" class="form-control" name="card-expiry-year" id="card-expiry-year" placeholder="2020"> </div>
 </div>
 <div class="col-sm-6">
      <div class="form-group">
           <label>Card Name*</label>
           <input type="text" class="form-control" name="card-name" id="card-name" placeholder="John"> </div>
 </div>
</form>

I am sending the Token through script:我通过脚本发送令牌:

<script>
var stripe = Stripe('test_key');

var $form = $('#checkout-form');

$form.submit(function (event) {
    $('charge-error').addClass('hidden');
    $form.find('button').prop('disabled' , true);
    Stripe.card.createToken({
        number: $('.card-number').val(),
        cvc: $('.card-cvc').val(),
        exp_month: $('.card-expiry-month').val(),
        exp_year: $('.card-expiry-year').val(),
        name: $('.card-name').val(),

    }, stripeResponseHandler);
    return false;
});

function stripeResponseHandler(status, response) {

    var $form = $('#checkout-form');

    if (response.error){
        $('.charge-error').removeClass('hidden');
        $('.charge-error').text(response.error.message);
        $form.find('button').prop('disabled', false);
    } else {
        var token = response.id;
        $form.append($('<input type="hidden" name="stripeToken" />').val(token));

        //Submit the form:
        $form.get(0).submit();
    }
}

and this is the controller Function:这是 controller Function:

$stripe = new \Stripe\StripeClient('secret_test_key');
    try {
        $charge = $stripe->charges->create([
                "amount" => 6800,
                "currency" => "usd",
                "source" => $request->input('stripeToken'),
                "description" => "Charge for test@expamle.com"
            ]
        );
    } catch (\Exception $e) {
        return redirect()->route('checkout')->with('error', $e->getMessage());
    }

If the replace the "source" => $request->input('stripeToken'), with "source" => "visa", then the code works fine.如果将“source” => $request->input('stripeToken') 替换为“source” => “visa”,则代码可以正常工作。

Why am I getting this error?为什么我会收到此错误? Please help.请帮忙。

At the line where you have "source" => $request->input('stripeToken') try to print that input out before carrying out the charge to see if the value is actually there or is in a valid format.在你有"source" => $request->input('stripeToken')尝试在执行收费之前打印该输入,以查看该值是否实际存在或采用有效格式。 If the value is not there ensure the variable you have called token assigned to the hidden field actually has the correct value.如果该值不存在,请确保您调用的变量 token 分配给隐藏字段实际上具有正确的值。

If you're using Stripe.js v2 you need to set the publishable key like this:如果您使用的是 Stripe.js v2,您需要像这样设置可发布密钥:

Stripe.setPublishableKey('pk_test_xxx');

not what you're currently doing:不是你目前正在做的事情:

var stripe = Stripe('test_key');

(see https://stripe.com/docs/stripe-js/v2#setting-publishable-key ) (见https://stripe.com/docs/stripe-js/v2#setting-publishable-key

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM