简体   繁体   English

条带致命错误无效的源对象

[英]Stripe Fatal Error Invalid source object

I'm trying to submit a stripeToken to charge.php without ajax but I'm getting following error: 我正在尝试在没有ajax的情况下将stripeToken提交到charge.php,但是出现以下错误:

Fatal error: Uncaught exception 'Stripe\\Error\\InvalidRequest' with message 'Invalid source object: must be a dictionary or a non-empty string. 致命错误:消息为“无效的源对象:”的未捕获异常“ Stripe \\ Error \\ InvalidRequest”必须是字典或非空字符串。

I believe it has something to do with the token not being appended to input. 我相信这与令牌没有附加到输入有关。 I have tried several different ways without success. 我尝试了几种不同的方法,但均未成功。

minimum markup 最低加价

<form action="charge.php" method="POST" id="payment-form">

//rest of the form

<input id="stripeToken" type="hidden" name="stripeToken" value="">
<input type="submit" class="submit" value="Submit Payment">
</form>

javascript JavaScript的

Stripe.setPublishableKey('pk_test_PUBLISHABLE'); //publishable key
var $form = $('#payment-form'); // declare form variable
$(function() {

  $form.submit(function(event) {
    // Disable the submit button to prevent repeated clicks:
    $form.find('.submit').prop('disabled', true);

    // Request a token from Stripe:
    Stripe.card.createToken($form, stripeResponseHandler);

    // Prevent the form from being submitted:
    return false;
  });
});

function stripeResponseHandler(status, response) {
  if (response.error) {

    // display errors in form

 } else {

    // Get the token
    var token = response.id;

    // Insert the token in the form:
    document.getElementById('stripeToken').value=token;
   // $form.append($('<input type="hidden" name="stripeToken">').val(token));

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

charge.php charge.php

require_once('config.php');

\Stripe\Stripe::setApiKey($mysk_key);

$stripeToken=$_POST['stripeToken'];//line of the error

  $customer = \Stripe\Customer::create(array(
    'email' => $email,
    'source'  =>$stripeToken, //line of the error
    'plan' => $myplan
  ));

UPDATE config.php 更新config.php

<?php

    require_once('vendor/init.php');

    $test_sk="sk_test_xxx";
    $live_sk="sk_live_xxx";
    $mykey_sk=$test_sk;

    $test_pk="pk_test_xxx";
    $live_pk="pk_live_xxx";
    $mykey_pk=$test_pk;

    $stripe = array(
      "secret_key"      =>$mykey_sk,
      "publishable_key" =>$mykey_pk
    );

    \Stripe\Stripe::setApiKey($stripe['secret_key']);
    ?>

Your using jQuery already so you might as well extract the token and assign it to the input using jQuery. 您已经在使用jQuery,因此您最好提取令牌并将其分配给使用jQuery的输入。

Make sure response.id is not null, than edit the value using jQuery. 确保response.id不为null,然后使用jQuery编辑值。

$('#stripeToken').val(token);

The stripe api will complain if you're passing an empty string so it's important that you make sure your response.id is not an empty string. 如果您传递的是空字符串,stripe api将会抱怨,因此确保您的response.id不是空字符串很重要。

You should review the docs on how to get a token from Stripe: 您应该查看有关如何从Stripe获取令牌的文档:

https://stripe.com/docs/stripe.js/v2#card-createToken https://stripe.com/docs/stripe.js/v2#card-createToken

You can pass a form element to createToken, but all your form fields would need a data-stripe attribute, and the value of the data-stripe attribute one of the keys they expect. 您可以将一个表单元素传递给createToken,但是所有表单字段都需要一个data-stripe属性,而data-stripe属性的值是它们期望的键之一。 Otherwise, you need to pass in the card parameters, like their example: 否则,您需要传递卡参数,例如其示例:

{
  number: $('.card-number').val(),
  cvc: $('.card-cvc').val(),
  exp_month: $('.card-expiry-month').val(),
  exp_year: $('.card-expiry-year').val(),
  address_zip: $('.address_zip').val() 
}

Since you didn't show us the rest of the form, we must assume you have data-stripe attributes, like: 由于您没有向我们显示表单的其余部分,因此我们必须假定您具有数据条带属性,例如:

<input type="text" data-stripe="number" id="number" /> 
<input type="text" data-stripe="cvc" id="cvc" /> 
<input type="text" data-stripe="exp_month" id="exp_month" /> 
<!-- and so on ... ->

Then createToken will attempt to get a token, and use the response handler you provided. 然后,createToken将尝试获取令牌,并使用您提供的响应处理程序。 You should be checking for response.error in that handler: 您应该在该处理程序中检查response.error:

stripeResponseHandler: function( status, response ){
    if( response.error ){
        alert('ERROR');
    }else{
        // Go ahead and post to the server ...
        // Or just output to the console to verify the response ...
        console.log( response );
    }
}

I'm assuming that response.id is acceptable to use, but in my particular code (that is working), I used response['id'] to get the stripe token value. 我假设使用response.id是可以接受的,但是在我的特定代码中(正在运行),我使用response ['id']获取条纹令牌值。

I use AJAX to POST to my server payment processing, but I suppose the way you are doing it would work. 我使用AJAX来过帐到我的服务器付款处理程序,但是我想您这样做的方式是可行的。 Just make sure to verify the contents of $_POST. 只要确保验证$ _POST的内容即可。

Make sure that you have included Stripejs like described here 请确保您已包括Stripejs像描述在这里

Here is the test file created. 这是创建的测试文件。 You can modify it according to your need. 您可以根据需要进行修改。 I used ajax for process the payment. 我用ajax处理付款。

index.php

<!DOCTYPE html>
<head>
  <meta charset="utf-8">
  <title>Stripe Test</title>
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<style>
* {
  font-family: "Helvetica Neue", Helvetica;
  font-size: 15px;
  font-variant: normal;
  padding: 0;
  margin: 0;
}

html {
  height: 100%;
}

body {
  background: #E6EBF1;
  align-items: center;
  min-height: 100%;
  display: flex;
  width: 100%;
}

form {
  width: 480px;
  margin: 20px auto;
}

.group {
  background: white;
  box-shadow: 0 7px 14px 0 rgba(49,49,93,0.10),
              0 3px 6px 0 rgba(0,0,0,0.08);
  border-radius: 4px;
  margin-bottom: 20px;
}

label {
  position: relative;
  color: #8898AA;
  font-weight: 300;
  height: 40px;
  line-height: 40px;
  margin-left: 20px;
  display: block;
}

.group label:not(:last-child) {
  border-bottom: 1px solid #F0F5FA;
}

label > span {
  width: 20%;
  text-align: right;
  float: left;
}

.field {
  background: transparent;
  font-weight: 300;
  border: 0;
  color: #31325F;
  outline: none;
  padding-right: 10px;
  padding-left: 10px;
  cursor: text;
  width: 70%;
  height: 40px;
  float: right;
}

.field::-webkit-input-placeholder { color: #CFD7E0; }
.field::-moz-placeholder { color: #CFD7E0; }
.field:-ms-input-placeholder { color: #CFD7E0; }

button {
  float: left;
  display: block;
  background: #666EE8;
  color: white;
  box-shadow: 0 7px 14px 0 rgba(49,49,93,0.10),
              0 3px 6px 0 rgba(0,0,0,0.08);
  border-radius: 4px;
  border: 0;
  margin-top: 20px;
  font-size: 15px;
  font-weight: 400;
  width: 100%;
  height: 40px;
  line-height: 38px;
  outline: none;
}

button:focus {
  background: #555ABF;
}

button:active {
  background: #43458B;
}

.outcome {
  float: left;
  width: 100%;
  padding-top: 8px;
  min-height: 24px;
  text-align: center;
}

.success, .error {
  display: none;
  font-size: 13px;
}

.success.visible, .error.visible {
  display: inline;
}

.error {
  color: #E4584C;
}

.success {
  color: #666EE8;
}

.success .token {
  font-weight: 500;
  font-size: 13px;
}
</style>
<script src="https://js.stripe.com/v3/"></script>
<form>
  <div class="group">
    <label>
      <span>Name</span>
      <input id="name" name="name" class="field" placeholder="" />
    </label>
     <label>
      <span>Email</span>
      <input name="email" id="email" class="field" placeholder="" />
    </label>
    <label>
      <span>Phone</span>
      <input class="field" placeholder="(123) 456-7890" id="phone" type="phone" />
    </label>
  </div>
  <div class="group">
    <label>
      <span>Card</span>
      <div id="card-element" class="field"></div>
    </label>
  </div>
  <button type="submit">Pay $25</button>
  <div class="outcome">
    <div class="error" role="alert"></div>
    <div class="success">
      Success! Your Stripe token is <span class="token"></span>
    </div>
  </div>
</form>
<script type="text/javascript">
var stripe = Stripe('YOUR-STRIPE-TOKEN');
var elements = stripe.elements();

var card = elements.create('card', {
  hidePostalCode:true, 
  style: {
    base: {
      iconColor: '#666EE8',
      color: '#31325F',
      lineHeight: '40px',
      fontWeight: 300,
      fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
      fontSize: '15px',

      '::placeholder': {
        color: '#CFD7E0',
      },
    },
  }
});
card.mount('#card-element');

function setOutcome(result) {
  var successElement = document.querySelector('.success');
  var errorElement = document.querySelector('.error');
  successElement.classList.remove('visible');
  errorElement.classList.remove('visible');

  if (result.token) {
    // Use the token to create a charge or a customer
    // https://stripe.com/docs/charges
    var stripe_token = result.token.id;
    successElement.querySelector('.token').textContent = stripe_token;
    successElement.classList.add('visible');

    // AJAX
    $.post( "do_payment.php", { stripeToken: stripe_token, name: $('#name').val(), "email":$('#email').val(), "phone": $('#phone').val() })
    .done(function( data ) {
      console.log(data);
    });



  } else if (result.error) {
    errorElement.textContent = result.error.message;
    errorElement.classList.add('visible');
  }
}

card.on('change', function(event) {
  setOutcome(event);
});

document.querySelector('form').addEventListener('submit', function(e) {
  e.preventDefault();
  var form = document.querySelector('form');
  var extraDetails = {
    name: form.querySelector('input[name=name]').value,
  };
  stripe.createToken(card, extraDetails).then(setOutcome);
});
</script>
</body>
</html>

For ajax process create a php file do_payment.php 对于ajax进程,请创建一个php文件do_payment.php

<?php
    require_once('vendor/init.php');

    $stripe = array(
        "secret_key" => "YOUR STRIPE SECRET KEY",
        "publishable_key" => "YOUR STRIPE PUBLISHABLE KEY"
    );

    \Stripe\Stripe::setApiKey($stripe['secret_key']);

    $token = $_POST['stripeToken'];

    $customer = \Stripe\Customer::create(array(
        'email' => 'customer@example.com',
        'source' => $token
    ));


    try {

        $charge = \Stripe\Charge::create(array(
            'customer' => $customer->id,
            'amount' => 2500, //Charge $25 See https://stripe.com/docs/currencies#zero-decimal
            'currency' => 'usd'
        ));
    } catch (RateLimit $e) {
        $body = $e->getJsonBody();
        return ['status' => 'failed', 'res' => $body['error']];
    } catch (InvalidRequest $e) {
        $body = $e->getJsonBody();
        return ['status' => 'failed', 'res' => $body['error']];
    } catch (Authentication $e) {
        $body = $e->getJsonBody();
        return ['status' => 'failed', 'res' => $body['error']];
    } catch (ApiConnection $e) {
        $body = $e->getJsonBody();
        return ['status' => 'failed', 'res' => $body['error']];
    } catch (Base $e) {
        $body = $e->getJsonBody();
        return ['status' => 'failed', 'res' => $body['error']];
    } catch (Exception $e) {
        return ['status' => 'failed', 'res' => json_encode($e->getMessage())];
    }
    echo '<h1>Successfully charged $50.00!</h1>';

    ?>

For getting test credentials see this document 要获取测试凭证,请参阅此文档

暂无
暂无

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

相关问题 Stripe invalid_request_error:令牌不能作为 Source 传入 - Stripe invalid_request_error: A token may not be passed in as a Source IntegrationError:stripe.confirmPayment() 的无效值:React Stripe 中出现错误 - IntegrationError: Invalid value for stripe.confirmPayment(): Getting error in React Stripe 在 Stripe 中创建客户时出现无效请求错误 - Invalid request error while creating customer in Stripe 条纹异常错误(必须提供来源或客户) - Stripe Exception error (Must provide source or customer) Axios条纹问题。 “ invalid_request_error” - Axios stripe issues. “invalid_request_error” 条纹错误 - 您的卡的到期年份无效 - Stripe Error - Your card's expiration year is invalid 编码器手册,Stripe引发错误:该客户没有附加的付款来源 - Coder Manual, Stripe raises an error: This customer has no attached payment source 未捕获(承诺)错误:提供给 `Elements` 的道具 `stripe` 无效。 我们建议使用来自 `@stripe/stripe-js` 的 `loadStripe` 实用程序 - Uncaught (in promise) Error: Invalid prop `stripe` supplied to `Elements`. We recommend using the `loadStripe` utility from `@stripe/stripe-js` 使用Stripe.js的Stripe with Rails 4错误“客户没有付款来源” - Stripe with Rails 4 Error “The Customer has no payment source” using stripe.js 为什么它给我这个错误 # Fatal error in, line 0 # Fatal JavaScript invalid size error 169220804? - Why its giving me this error # Fatal error in , line 0 # Fatal JavaScript invalid size error 169220804?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM